Removed sprintf() which generated a warning, because of security reasons.
Replace NULL with nullptr
Replace typedef with using
Do not inherit from std::vector. Use composition instead.
optimize mutex-unlocking
closes https://github.com/official-stockfish/Stockfish/pull/4327
No functional change
Siad Daboul (Topologist)
Stefan Geschwentner (locutus2)
Stefano Cardanobile (Stefano80)
+Stefano Di Martino (StefanoD)
Steinar Gunderson (sesse)
Stéphane Nicolet (snicolet)
Syine Mineta (MinetaS)
/// Material::Entry contains various information about a material configuration.
/// It contains a material imbalance evaluation, a function pointer to a special
-/// endgame evaluation function (which in most cases is NULL, meaning that the
+/// endgame evaluation function (which in most cases is nullptr, meaning that the
/// standard evaluation function will be used), and scale factors.
///
/// The scale factors are used to scale the evaluation score up or down. For
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hProcessToken))
return nullptr;
- if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &luid))
+ if (LookupPrivilegeValue(nullptr, SE_LOCK_MEMORY_NAME, &luid))
{
TOKEN_PRIVILEGES tp { };
TOKEN_PRIVILEGES prevTp { };
// Round up size to full pages and allocate
allocSize = (allocSize + largePageSize - 1) & ~size_t(largePageSize - 1);
mem = VirtualAlloc(
- NULL, allocSize, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
+ nullptr, allocSize, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
// Privilege no longer needed, restore previous state
- AdjustTokenPrivileges(hProcessToken, FALSE, &prevTp, 0, NULL, NULL);
+ AdjustTokenPrivileges(hProcessToken, FALSE, &prevTp, 0, nullptr, nullptr);
}
}
// Fall back to regular, page aligned, allocation if necessary
if (!mem)
- mem = VirtualAlloc(NULL, allocSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
+ mem = VirtualAlloc(nullptr, allocSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
return mem;
}
#include "../evaluate.h"
#include "../position.h"
-#include "../misc.h"
#include "../uci.h"
#include "../types.h"
}
- // format_cp_aligned_dot() converts a Value into (centi)pawns and writes it in a buffer,
- // always keeping two decimals. The buffer must have capacity for at least 7 chars.
- static void format_cp_aligned_dot(Value v, char* buffer) {
+ // format_cp_aligned_dot() converts a Value into (centi)pawns, always keeping two decimals.
+ static void format_cp_aligned_dot(Value v, std::stringstream &stream) {
+ const double cp = 1.0 * std::abs(int(v)) / UCI::NormalizeToPawnValue;
- buffer[0] = (v < 0 ? '-' : v > 0 ? '+' : ' ');
-
- double cp = 1.0 * std::abs(int(v)) / UCI::NormalizeToPawnValue;
- sprintf(&buffer[1], "%6.2f", cp);
+ stream << (v < 0 ? '-' : v > 0 ? '+' : ' ')
+ << std::setiosflags(std::ios::fixed)
+ << std::setw(6)
+ << std::setprecision(2)
+ << cp;
}
for (std::size_t bucket = 0; bucket < LayerStacks; ++bucket)
{
- char buffer[3][8];
- std::memset(buffer, '\0', sizeof(buffer));
-
- format_cp_aligned_dot(t.psqt[bucket], buffer[0]);
- format_cp_aligned_dot(t.positional[bucket], buffer[1]);
- format_cp_aligned_dot(t.psqt[bucket] + t.positional[bucket], buffer[2]);
-
- ss << "| " << bucket << " "
- << " | " << buffer[0] << " "
- << " | " << buffer[1] << " "
- << " | " << buffer[2] << " "
+ ss << "| " << bucket << " ";
+ ss << " | "; format_cp_aligned_dot(t.psqt[bucket], ss); ss << " "
+ << " | "; format_cp_aligned_dot(t.positional[bucket], ss); ss << " "
+ << " | "; format_cp_aligned_dot(t.psqt[bucket] + t.positional[bucket], ss); ss << " "
<< " |";
if (bucket == t.correctBucket)
ss << " <-- this bucket is used";
/// Thread::start_searching() wakes up the thread that will start the search
void Thread::start_searching() {
-
- std::lock_guard<std::mutex> lk(mutex);
+ mutex.lock();
searching = true;
+ mutex.unlock(); // Unlock before notifying saves a few CPU-cycles
cv.notify_one(); // Wake up the thread in idle_loop()
}
void ThreadPool::set(size_t requested) {
- if (size() > 0) // destroy any existing thread(s)
+ if (threads.size() > 0) // destroy any existing thread(s)
{
main()->wait_for_search_finished();
- while (size() > 0)
- delete back(), pop_back();
+ while (threads.size() > 0)
+ delete threads.back(), threads.pop_back();
}
if (requested > 0) // create new thread(s)
{
- push_back(new MainThread(0));
+ threads.push_back(new MainThread(0));
- while (size() < requested)
- push_back(new Thread(size()));
+ while (threads.size() < requested)
+ threads.push_back(new Thread(threads.size()));
clear();
// Reallocate the hash with the new threadpool size
void ThreadPool::clear() {
- for (Thread* th : *this)
+ for (Thread* th : threads)
th->clear();
main()->callsCnt = 0;
Tablebases::rank_root_moves(pos, rootMoves);
// After ownership transfer 'states' becomes empty, so if we stop the search
- // and call 'go' again without setting a new position states.get() == NULL.
+ // and call 'go' again without setting a new position states.get() == nullptr.
assert(states.get() || setupStates.get());
if (states.get())
// be deduced from a fen string, so set() clears them and they are set from
// setupStates->back() later. The rootState is per thread, earlier states are shared
// since they are read-only.
- for (Thread* th : *this)
+ for (Thread* th : threads)
{
th->nodes = th->tbHits = th->nmpMinPly = th->bestMoveChanges = 0;
th->rootDepth = th->completedDepth = 0;
Thread* ThreadPool::get_best_thread() const {
- Thread* bestThread = front();
+ Thread* bestThread = threads.front();
std::map<Move, int64_t> votes;
Value minScore = VALUE_NONE;
// Find minimum score of all threads
- for (Thread* th: *this)
+ for (Thread* th: threads)
minScore = std::min(minScore, th->rootMoves[0].score);
// Vote according to score and depth, and select the best thread
return (th->rootMoves[0].score - minScore + 14) * int(th->completedDepth);
};
- for (Thread* th : *this)
+ for (Thread* th : threads)
votes[th->rootMoves[0].pv[0]] += thread_value(th);
- for (Thread* th : *this)
+ for (Thread* th : threads)
if (abs(bestThread->rootMoves[0].score) >= VALUE_TB_WIN_IN_MAX_PLY)
{
// Make sure we pick the shortest mate / TB conversion or stave off mate the longest
void ThreadPool::start_searching() {
- for (Thread* th : *this)
- if (th != front())
+ for (Thread* th : threads)
+ if (th != threads.front())
th->start_searching();
}
void ThreadPool::wait_for_search_finished() const {
- for (Thread* th : *this)
- if (th != front())
+ for (Thread* th : threads)
+ if (th != threads.front())
th->wait_for_search_finished();
}
/// parking and, most importantly, launching a thread. All the access to threads
/// is done through this class.
-struct ThreadPool : public std::vector<Thread*> {
+struct ThreadPool {
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
void clear();
void set(size_t);
- MainThread* main() const { return static_cast<MainThread*>(front()); }
+ MainThread* main() const { return static_cast<MainThread*>(threads.front()); }
uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
uint64_t tb_hits() const { return accumulate(&Thread::tbHits); }
Thread* get_best_thread() const;
std::atomic_bool stop, increaseDepth;
+ auto cbegin() const noexcept { return threads.cbegin(); }
+ auto begin() noexcept { return threads.begin(); }
+ auto end() noexcept { return threads.end(); }
+ auto cend() const noexcept { return threads.cend(); }
+ auto size() const noexcept { return threads.size(); }
+ auto empty() const noexcept { return threads.empty(); }
+
private:
StateListPtr setupStates;
+ std::vector<Thread*> threads;
uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
uint64_t sum = 0;
- for (Thread* th : *this)
+ for (Thread* th : threads)
sum += (th->*member).load(std::memory_order_relaxed);
return sum;
}
P* p = reinterpret_cast<P*>(ptr);
(p->first->*(p->second))(); // Call member function pointer
delete p;
- return NULL;
+ return nullptr;
}
class NativeThread {
pthread_attr_setstacksize(attr, TH_STACK_SIZE);
pthread_create(&thread, attr, start_routine<T>, new P(obj, fun));
}
- void join() { pthread_join(thread, NULL); }
+ void join() { pthread_join(thread, nullptr); }
};
} // namespace Stockfish
namespace Stockfish {
-typedef std::thread NativeThread;
+using NativeThread = std::thread;
} // namespace Stockfish