]> ruin.nu Git - germs.git/blobdiff - src/threadgenesorter.h
Initial thread infrastructure and test
[germs.git] / src / threadgenesorter.h
diff --git a/src/threadgenesorter.h b/src/threadgenesorter.h
new file mode 100644 (file)
index 0000000..57d1052
--- /dev/null
@@ -0,0 +1,136 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by Michael Andreen                                 *
+ *   andreen@student.chalmers.se                                           *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
+ ***************************************************************************/
+
+#ifndef __THREADGENESORTER_H__
+#define __THREADGENESORTER_H__
+
+#include "genesorter.h"
+#include "geneorder.h"
+#include "model.h"
+
+#include <pthread.h>
+
+#include <queue>
+#include <vector>
+#include <utility>
+
+extern "C" void* t_sorter(void* arg);
+extern "C" void* t_worker(void* arg);
+
+/**
+ * Finding the set of all possible sorting sequences.
+ *
+ * \author Michael Andreen
+ */
+class ThreadGeneSorter : public GeneSorter{
+       friend void* t_sorter(void* arg);
+       friend void* t_worker(void* arg);
+       public:
+
+               struct ScoreCmp {
+                       template<typename T>
+                               bool operator()(T s1, T s2){
+                                       return s1.first < s2.first;
+                               }
+               };
+               typedef std::priority_queue<std::pair<double,ActionList>,std::vector<std::pair<double,ActionList> >, ScoreCmp > SolutionsQueue;
+
+               struct SortUnit{
+                       double _score;
+                       GeneOrder _go;
+                       ActionList _al;
+                       SortUnit(double score,const GeneOrder& go,const ActionList& al) : _score(score),_go(go),_al(al){}
+                       bool operator<(const SortUnit& su) const{
+                               return _score < su._score;
+                       }
+               };
+
+               /**
+                * Creates a new threaded GeneSorter.
+                *
+                * \param threads The number of worker threads that you want to spawn.
+                */
+               ThreadGeneSorter(const Model& m, int threads = 2);
+
+               /**
+                * Takes a GeneOrder, and Model.
+                *
+                * Starts the work to find all possible sorting sequences.
+                */
+               void start(const GeneOrder& go);
+
+               /**
+                * Wait for sorter to end.
+                */
+               void join();
+
+               /**
+                * Tell the sorter to stop execution.
+                */
+               void stop();
+
+               /**
+                * Number of possible solutions.
+                */
+               size_t size();
+
+               /**
+                * Waits for progress.
+                *
+                * \param time Max time to wait for.
+                * \param solutions The minimum number of solutions we'll wait for.
+                */
+               double progress(int time, int solutions = 1);
+
+               /**
+                * Get the full list of solutions.
+                */
+               SolutionsQueue solutions();
+
+               ~ThreadGeneSorter(){};
+
+       private:
+
+               void sorter(const GeneOrder& go);
+               void worker();
+
+               typedef std::priority_queue<SortUnit> SortQueue;
+               //! Queue for possible sort alternatives.
+               SortQueue _queue;
+               //! List of full sort sequences.
+               SolutionsQueue _solutions;
+
+               bool _done;
+
+               int _workers;
+
+               Model _model;
+
+               pthread_mutex_t _queuelock;
+               pthread_mutex_t _solutionslock;
+
+               pthread_cond_t _addedSolution;
+               pthread_cond_t _waiting;
+               pthread_cond_t _addedTask;
+
+               pthread_t _tid;
+};
+
+#endif