]> git.sesse.net Git - stockfish/blobdiff - src/thread.h
Polymorphic Thread hierarchy
[stockfish] / src / thread.h
index 92be3a22985572303fae5c669386daf25a6919a8..9d04e5bddc92d183ec7dc64c6bce2e22233064b8 100644 (file)
@@ -93,19 +93,15 @@ struct SplitPoint {
 
 class Thread {
 
-  typedef void (Thread::* Fn) (); // Pointer to member function
-
 public:
-  Thread(Fn fn);
- ~Thread();
+  Thread();
 virtual ~Thread();
 
-  void wake_up();
+  virtual void idle_loop();
+  void notify_one();
   bool cutoff_occurred() const;
   bool is_available_to(Thread* master) const;
-  void idle_loop();
-  void main_loop();
-  void timer_loop();
-  void wait_for_stop();
+  void wait_for(volatile const bool& b);
 
   SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
   Material::Table materialTable;
@@ -116,14 +112,24 @@ public:
   Mutex mutex;
   ConditionVariable sleepCondition;
   NativeHandle handle;
-  Fn start_fn;
   SplitPoint* volatile curSplitPoint;
   volatile int splitPointsCnt;
   volatile bool is_searching;
-  volatile bool do_sleep;
   volatile bool do_exit;
 };
 
+struct TimerThread : public Thread {
+  TimerThread() : msec(0) {}
+  virtual void idle_loop();
+  int msec;
+};
+
+struct MainThread : public Thread {
+  MainThread() : is_finished(false) {} // Avoid a race with start_searching()
+  virtual void idle_loop();
+  volatile bool is_finished;
+};
+
 
 /// ThreadPool class handles all the threads related stuff like init, starting,
 /// parking and, the most important, launching a slave thread at a split point.
@@ -136,16 +142,13 @@ public:
   void exit(); // be initialized and valid during the whole thread lifetime.
 
   Thread& operator[](size_t id) { return *threads[id]; }
-  bool use_sleeping_threads() const { return useSleepingThreads; }
   int min_split_depth() const { return minimumSplitDepth; }
   size_t size() const { return threads.size(); }
-  Thread* main_thread() { return threads[0]; }
+  MainThread* main_thread() { return static_cast<MainThread*>(threads[0]); }
+  TimerThread* timer_thread() { return timer; }
 
-  void wake_up() const;
-  void sleep() const;
   void read_uci_options();
   bool available_slave_exists(Thread* master) const;
-  void set_timer(int msec);
   void wait_for_search_finished();
   void start_searching(const Position&, const Search::LimitsType&,
                        const std::vector<Move>&, Search::StateStackPtr&);
@@ -155,15 +158,17 @@ public:
               Depth depth, Move threatMove, int moveCount, MovePicker& mp, int nodeType);
 private:
   friend class Thread;
+  friend struct MainThread;
   friend void check_time();
 
   std::vector<Thread*> threads;
-  Thread* timer;
+  TimerThread* timer;
   Mutex mutex;
   ConditionVariable sleepCondition;
   Depth minimumSplitDepth;
   int maxThreadsPerSplitPoint;
-  bool useSleepingThreads;
+public:
+  bool sleepWhileIdle;
 };
 
 extern ThreadPool Threads;