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 #ifndef __THREADGENESORTER_H__
22 #define __THREADGENESORTER_H__
24 #include "genesorter.h"
25 #include "geneorder.h"
34 extern "C" void* t_sorter(void* arg);
35 extern "C" void* t_worker(void* arg);
38 * Finding the set of all possible sorting sequences.
40 * \author Michael Andreen
42 class ThreadGeneSorter : public GeneSorter{
43 friend void* t_sorter(void* arg);
44 friend void* t_worker(void* arg);
49 bool operator()(T s1, T s2){
50 return s1.first < s2.first;
53 typedef std::priority_queue<std::pair<double,ActionList>,std::vector<std::pair<double,ActionList> >, ScoreCmp > SolutionsQueue;
59 SortUnit(double score,const GeneOrder& go,const ActionList& al) : _score(score),_go(go),_al(al){}
60 bool operator<(const SortUnit& su) const{
61 return _score < su._score;
66 * Creates a new threaded GeneSorter.
68 * \param threads The number of worker threads that you want to spawn.
70 ThreadGeneSorter(const Model& m, int threads = 2);
73 * Takes a GeneOrder, and Model.
75 * Starts the work to find all possible sorting sequences.
77 void start(const GeneOrder& go);
80 * Wait for sorter to end.
85 * Tell the sorter to stop execution.
90 * Number of possible solutions.
97 * \param time Max time to wait for.
98 * \param solutions The minimum number of solutions we'll wait for.
100 double progress(int time, int solutions = 1);
103 * Get the full list of solutions.
105 SolutionsQueue solutions();
107 ~ThreadGeneSorter(){};
111 void sorter(const GeneOrder& go);
114 typedef std::priority_queue<SortUnit> SortQueue;
115 //! Queue for possible sort alternatives.
117 //! List of full sort sequences.
118 SolutionsQueue _solutions;
126 pthread_mutex_t _queuelock;
127 pthread_mutex_t _solutionslock;
129 pthread_cond_t _addedSolution;
130 pthread_cond_t _waiting;
131 pthread_cond_t _addedTask;