-// init() is called at startup to create and launch requested threads, that will
-// go immediately to sleep due to 'sleepWhileIdle' set to true. We cannot use
-// a c'tor because Threads is a static object and we need a fully initialized
-// engine at this point due to allocation of Endgames in Thread c'tor.
-
-void ThreadPool::init() {
-
- sleepWhileIdle = true;
- timer = new_thread<TimerThread>();
- push_back(new_thread<MainThread>());
- read_uci_options();
-}
-
-
-// exit() cleanly terminates the threads before the program exits
-
-void ThreadPool::exit() {
-
- delete_thread(timer); // As first because check_time() accesses threads data
-
- for (iterator it = begin(); it != end(); ++it)
- delete_thread(*it);
-}
-
-
-// read_uci_options() updates internal threads parameters from the corresponding
-// UCI options and creates/destroys threads to match the requested number. Thread
-// objects are dynamically allocated to avoid creating all possible threads
-// in advance (which include pawns and material tables), even if only a few
-// are to be used.
-
-void ThreadPool::read_uci_options() {
-
- maxThreadsPerSplitPoint = Options["Max Threads per Split Point"];
- minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
- size_t requested = Options["Threads"];
-
- assert(requested > 0);
-
- // Value 0 has a special meaning: We determine the optimal minimum split depth
- // automatically. Anyhow the minimumSplitDepth should never be under 4 plies.
- if (!minimumSplitDepth)
- minimumSplitDepth = (requested < 8 ? 4 : 7) * ONE_PLY;
- else
- minimumSplitDepth = std::max(4 * ONE_PLY, minimumSplitDepth);
-
- while (size() < requested)
- push_back(new_thread<Thread>());
-
- while (size() > requested)
- {
- delete_thread(back());
- pop_back();
- }
-}
-
-
-// slave_available() tries to find an idle thread which is available as a slave
-// for the thread 'master'.
-
-Thread* ThreadPool::available_slave(const Thread* master) const {
-
- for (const_iterator it = begin(); it != end(); ++it)
- if ((*it)->available_to(master))
- return *it;
-
- return NULL;
-}
-
-
-// split() does the actual work of distributing the work at a node between