X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fthread.h;h=9d04e5bddc92d183ec7dc64c6bce2e22233064b8;hb=d1143794a01cd0540cf47e3415815cd60cde9422;hp=12c64e80c7af58f8f33ef9b388b1ef7248494d70;hpb=dda7de17e74d7e80c58b5f86be5c78393ae65541;p=stockfish diff --git a/src/thread.h b/src/thread.h index 12c64e80..9d04e5bd 100644 --- a/src/thread.h +++ b/src/thread.h @@ -93,18 +93,14 @@ struct SplitPoint { class Thread { - typedef void (Thread::* Fn) (); // Pointer to member function - public: - Thread(Fn fn); - ~Thread(); + Thread(); + virtual ~Thread(); + 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(volatile const bool& b); SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD]; @@ -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,11 +142,10 @@ 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]; } - Thread* timer_thread() { return timer; } + MainThread* main_thread() { return static_cast(threads[0]); } + TimerThread* timer_thread() { return timer; } void read_uci_options(); bool available_slave_exists(Thread* master) const; @@ -153,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 threads; - Thread* timer; + TimerThread* timer; Mutex mutex; ConditionVariable sleepCondition; Depth minimumSplitDepth; int maxThreadsPerSplitPoint; - bool useSleepingThreads; +public: + bool sleepWhileIdle; }; extern ThreadPool Threads;