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 * Returns true while the sorter is running.
90 * Tell the sorter to stop execution.
95 * Number of possible solutions.
100 * Waits for progress.
102 * \param time Max time to wait for, in seconds.
103 * \param solutions The minimum number of solutions we'll wait for.
104 * \return The best score so far.
106 double wait(time_t time, size_t solutions = 1);
109 * Get the full list of solutions.
111 SolutionsQueue solutions();
113 ~ThreadGeneSorter(){};
117 void sorter(const GeneOrder& go);
120 typedef std::priority_queue<SortUnit> SortQueue;
121 //! Queue for possible sort alternatives.
123 //! List of full sort sequences.
124 SolutionsQueue _solutions;
132 pthread_mutex_t _queuelock;
133 pthread_mutex_t _solutionslock;
135 pthread_cond_t _addedSolution;
136 pthread_cond_t _waiting;
137 pthread_cond_t _addedTask;