1 /***************************************************************************
2 * Copyright (C) 2008 by Michael Andreen *
3 * andreen@student.chalmers.se *
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. *
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. *
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 ***************************************************************************/
21 #include "threadgenesorter.h"
22 #include "sortaction.h"
27 * Handles locking and unlocking of mutexes.
29 * RAII style class for mutex acquisition.
35 Mutex(pthread_mutex_t* m){
37 pthread_mutex_lock(_m);
41 pthread_mutex_unlock(_m);
50 struct ExecutionStuff {
51 ExecutionStuff(ThreadGeneSorter* so, const GeneOrder& go): _sorter(so)
53 ThreadGeneSorter* _sorter;
58 ThreadGeneSorter::ThreadGeneSorter(const Model& m, int threads) : _model(m){
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);
67 void ThreadGeneSorter::join(){
68 pthread_join(_tid,NULL);
71 void ThreadGeneSorter::stop(){
75 size_t ThreadGeneSorter::size(){
76 //TODO: thread safety..
77 return _solutions.size();
80 double progress(int time, int solutions = 1){
83 ThreadGeneSorter::SolutionsQueue ThreadGeneSorter::solutions(){
84 //TODO: thread safety..
88 void ThreadGeneSorter::start(const GeneOrder& go){
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);
96 void ThreadGeneSorter::sorter(const GeneOrder& go){
97 _queue.push(SortUnit(0.0,go,ActionList()));
99 pthread_attr_init(&tattr);
100 pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
102 _queue = SortQueue();
103 _solutions = SolutionsQueue();
105 queue<pthread_t> threads;
107 _queue.push(SortUnit(0.0,go,ActionList()));
109 int workers = _workers;
114 pthread_create(&tid, &tattr, t_worker, this);
117 Mutex m(&_queuelock);
118 while(_workers != 0 && !_done){
119 pthread_cond_wait(&_waiting,&_queuelock);
125 pthread_cond_broadcast(&_addedTask);
126 while(threads.size() > 0){
127 pthread_join(threads.front(),NULL);
130 pthread_exit((void*)0);
133 void ThreadGeneSorter::worker(){
135 Mutex m(&_queuelock);
137 while (_queue.size() == 0 && !_done){
139 pthread_cond_signal(&_waiting);
140 pthread_cond_wait(&_addedTask,&_queuelock);
146 SortUnit su = _queue.top();
152 void* t_sorter(void* arg){
153 ExecutionStuff* es = static_cast<ExecutionStuff*>(arg);
154 es->_sorter->sorter(es->_go);
156 pthread_exit((void*)0);
159 void* t_worker(void* arg){
160 ThreadGeneSorter* sorter = static_cast<ThreadGeneSorter*>(arg);
162 pthread_exit((void*)0);