]> ruin.nu Git - germs.git/blob - src/threadgenesorter.cpp
Initial thread infrastructure and test
[germs.git] / src / threadgenesorter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Michael Andreen                                 *
3  *   andreen@student.chalmers.se                                           *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
19  ***************************************************************************/
20
21 #include "threadgenesorter.h"
22 #include "sortaction.h"
23
24 using namespace std;
25
26 /**
27  * Handles locking and unlocking of mutexes.
28  *
29  * RAII style class for mutex acquisition.
30  */
31 class Mutex {
32         pthread_mutex_t* _m;
33         bool _locked;
34         public:
35                 Mutex(pthread_mutex_t* m){
36                         _m = m;
37                         pthread_mutex_lock(_m);
38                         _locked = true;
39                 }
40                 void unlock(){
41                         pthread_mutex_unlock(_m);
42                         _locked = false;
43                 }
44                 ~Mutex(){
45                         if (_locked)
46                                 unlock();
47                 }
48 };
49
50 struct ExecutionStuff {
51         ExecutionStuff(ThreadGeneSorter* so, const GeneOrder& go): _sorter(so)
52                         ,_go(go){};
53         ThreadGeneSorter* _sorter;
54         GeneOrder _go;
55 };
56
57
58 ThreadGeneSorter::ThreadGeneSorter(const Model& m, int threads) : _model(m){
59         _workers = threads;
60         pthread_mutex_init(&_queuelock, NULL);
61         pthread_mutex_init(&_solutionslock, NULL);
62         pthread_cond_init(&_addedSolution,NULL);
63         pthread_cond_init(&_addedTask,NULL);
64         pthread_cond_init(&_waiting,NULL);
65 }
66
67 void ThreadGeneSorter::join(){
68         pthread_join(_tid,NULL);
69 }
70
71 void ThreadGeneSorter::stop(){
72         _done = true;
73 }
74
75 size_t ThreadGeneSorter::size(){
76         //TODO: thread safety..
77         return _solutions.size();
78 }
79
80 double progress(int time, int solutions = 1){
81 }
82
83 ThreadGeneSorter::SolutionsQueue ThreadGeneSorter::solutions(){
84         //TODO: thread safety..
85         return _solutions;
86 }
87
88 void ThreadGeneSorter::start(const GeneOrder& go){
89         pthread_attr_t tattr;
90         pthread_attr_init(&tattr);
91         pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
92         ExecutionStuff* es = new ExecutionStuff(this,go);
93         pthread_create(&_tid, &tattr, t_sorter, es);
94 }
95
96 void ThreadGeneSorter::sorter(const GeneOrder& go){
97         _queue.push(SortUnit(0.0,go,ActionList()));
98         pthread_attr_t tattr;
99         pthread_attr_init(&tattr);
100         pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
101         _done = false;
102         _queue = SortQueue();
103         _solutions = SolutionsQueue();
104
105         queue<pthread_t> threads;
106
107         _queue.push(SortUnit(0.0,go,ActionList()));
108
109         int workers = _workers;
110         while (!_done){
111                 if (workers){
112                         --workers;
113                         pthread_t tid;
114                         pthread_create(&tid, &tattr, t_worker, this);
115                         threads.push(tid);
116                 }else{
117                         Mutex m(&_queuelock);
118                         while(_workers != 0 && !_done){
119                                 pthread_cond_wait(&_waiting,&_queuelock);
120                         }
121                         if (_workers == 0)
122                                 _done = true;
123                 }
124         }
125         pthread_cond_broadcast(&_addedTask);
126         while(threads.size() > 0){
127                 pthread_join(threads.front(),NULL);
128                 threads.pop();
129         }
130         pthread_exit((void*)0);
131 }
132
133 void ThreadGeneSorter::worker(){
134         while(!_done){
135                 Mutex m(&_queuelock);
136
137                 while (_queue.size() == 0 && !_done){
138                         --_workers;
139                         pthread_cond_signal(&_waiting);
140                         pthread_cond_wait(&_addedTask,&_queuelock);
141                         ++_workers;
142                 }
143                 if (_done)
144                         break;
145
146                 SortUnit su = _queue.top();
147                 _queue.pop();
148                 m.unlock();
149         }
150 }
151
152 void* t_sorter(void* arg){
153         ExecutionStuff* es = static_cast<ExecutionStuff*>(arg);
154         es->_sorter->sorter(es->_go);
155         delete es;
156         pthread_exit((void*)0);
157 }
158
159 void* t_worker(void* arg){
160         ThreadGeneSorter* sorter = static_cast<ThreadGeneSorter*>(arg);
161         sorter->worker();
162         pthread_exit((void*)0);
163 }