Polymorphic Thread hierarchy

Subclass MainThread and TimerThread and declare
idle_loop() virtual. This allow us to cleanly
remove a good bunch of hacks, relying on C++
polymorphism to do the job.

No functional change.
This commit is contained in:
Marco Costalba
2013-01-14 00:32:30 +01:00
parent e70eae2c91
commit d1143794a0
3 changed files with 40 additions and 39 deletions

View File

@@ -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 is_finished;
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.
@@ -138,8 +144,8 @@ public:
Thread& operator[](size_t id) { return *threads[id]; }
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<MainThread*>(threads[0]); }
TimerThread* timer_thread() { return timer; }
void read_uci_options();
bool available_slave_exists(Thread* master) const;
@@ -152,10 +158,11 @@ 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;