code cleanup
parent
9943ecd6a5
commit
6751caf057
32
src/main.cpp
32
src/main.cpp
|
@ -23,7 +23,6 @@
|
|||
#include "position.h"
|
||||
#include "psqt.h"
|
||||
#include "search.h"
|
||||
#include "syzygy/tbprobe.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "uci.h"
|
||||
|
@ -34,8 +33,7 @@ Outstream outstream, errstream;
|
|||
std::atomic<int> done = 0;
|
||||
|
||||
int sf_init() {
|
||||
std::cout << engine_info() << std::endl;
|
||||
|
||||
sync_cout << engine_info() << sync_endl;
|
||||
UCI::init(Options);
|
||||
Tune::init();
|
||||
PSQT::init();
|
||||
|
@ -67,32 +65,17 @@ void input_reader() {
|
|||
if (cmd == "quit")
|
||||
break;
|
||||
}
|
||||
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
// sync_cout << "done1" << sync_endl; // need to unblock reader
|
||||
// sync_cerr << "done" << sync_endl; // need to unblock reader
|
||||
}
|
||||
|
||||
void print_lines(std::string str, std::ostream& os) {
|
||||
std::stringstream ss(str);
|
||||
std::string line;
|
||||
|
||||
while(std::getline(ss, line, '\n')) {
|
||||
os << line.c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
static std::mutex mutex_;
|
||||
|
||||
void output_reader() {
|
||||
while (!done) {
|
||||
// while (!done || !outstream.empty()) {
|
||||
// std::string res = outstream.read();
|
||||
std::string res;
|
||||
int len = outstream.read(res);
|
||||
if (len < 0) {
|
||||
break;
|
||||
}
|
||||
// print_lines(res, std::cout);
|
||||
mutex_.lock();
|
||||
std::cout << res;
|
||||
mutex_.unlock();
|
||||
|
@ -100,19 +83,12 @@ void output_reader() {
|
|||
}
|
||||
|
||||
void error_reader() {
|
||||
// while (!done) {
|
||||
//// while (!done || !errstream.empty()) {
|
||||
// print_lines(errstream.read(), std::cerr);
|
||||
// }
|
||||
while (!done) {
|
||||
// while (!done || !outstream.empty()) {
|
||||
// std::string res = outstream.read();
|
||||
std::string res;
|
||||
int len = errstream.read(res);
|
||||
if (len < 0) {
|
||||
break;
|
||||
}
|
||||
// print_lines(res, std::cerr);
|
||||
mutex_.lock();
|
||||
std::cerr << res;
|
||||
mutex_.unlock();
|
||||
|
@ -122,7 +98,6 @@ void error_reader() {
|
|||
/// When SF is called with some command line arguments, e.g. to
|
||||
/// run 'bench', once the command is executed the program stops.
|
||||
int main(int argc, char* argv[]) {
|
||||
std::thread* input_reader_thread = NULL;
|
||||
std::thread output_reader_thread(output_reader);
|
||||
std::thread error_reader_thread(error_reader);
|
||||
|
||||
|
@ -134,9 +109,8 @@ int main(int argc, char* argv[]) {
|
|||
cmd += std::string(argv[i]) + " ";
|
||||
UCI::execute(cmd);
|
||||
} else {
|
||||
input_reader_thread = new std::thread(input_reader);
|
||||
input_reader_thread->join();
|
||||
delete input_reader_thread;
|
||||
std::thread input_reader_thread(input_reader);
|
||||
input_reader_thread.join();
|
||||
}
|
||||
unblock_readers();
|
||||
output_reader_thread.join();
|
||||
|
|
23
src/misc.h
23
src/misc.h
|
@ -31,13 +31,6 @@
|
|||
|
||||
#include "types.h"
|
||||
|
||||
//#define DEBUG
|
||||
//#ifdef DEBUG
|
||||
//#define DEBUG_PRINT(x) printf x
|
||||
//#else
|
||||
//#define DEBUG_PRINT(x)
|
||||
//#endif
|
||||
|
||||
enum SyncCout { IO_LOCK, IO_UNLOCK };
|
||||
#define sync_endl IO_UNLOCK
|
||||
/*
|
||||
|
@ -54,10 +47,7 @@ public:
|
|||
return -1;
|
||||
} else {
|
||||
std::unique_lock<std::mutex> mlock(mutex_);
|
||||
_DEBUG_PRINT("read() cond_.wait\n");
|
||||
cond_.wait(mlock);
|
||||
// cond_.wait(mlock, [this]{return !buf_.empty();});
|
||||
_DEBUG_PRINT("read() unblocked\n");
|
||||
if (done) {
|
||||
mlock.unlock();
|
||||
return -1;
|
||||
|
@ -69,12 +59,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
// bool empty() {
|
||||
// return buf_.empty();
|
||||
// }
|
||||
|
||||
void finish() {
|
||||
std::cout << "finish!!!" << std::endl;
|
||||
done = true;
|
||||
cond_.notify_one();
|
||||
}
|
||||
|
@ -83,9 +68,7 @@ public:
|
|||
if (sc == IO_LOCK) {
|
||||
std::thread::id current_thread_id = std::this_thread::get_id();
|
||||
if (current_thread_id != owner_thread_id) {
|
||||
_DEBUG_PRINT("locking mutex_\n");
|
||||
mutex_.lock();
|
||||
_DEBUG_PRINT("mutex_ locked\n");
|
||||
owner_thread_id = current_thread_id;
|
||||
}
|
||||
}
|
||||
|
@ -115,12 +98,6 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
// std::string get_buf() {
|
||||
// std::string res(buf_);
|
||||
// buf_.clear();
|
||||
// return res;
|
||||
// }
|
||||
|
||||
const std::thread::id no_thread_id;
|
||||
std::mutex mutex_;
|
||||
std::thread::id owner_thread_id;
|
||||
|
|
|
@ -78,11 +78,9 @@ void Thread::clear() {
|
|||
|
||||
void Thread::start_searching() {
|
||||
|
||||
std::cout << "start_searching lock_guard " << std::this_thread::get_id() << std::endl;
|
||||
std::lock_guard<std::mutex> lk(mutex);
|
||||
searching = true;
|
||||
cv.notify_one(); // Wake up the thread in idle_loop()
|
||||
std::cout << "start_searching lock_guard freed " << std::this_thread::get_id() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
@ -91,12 +89,8 @@ std::cout << "start_searching lock_guard freed " << std::this_thread::get_id() <
|
|||
|
||||
void Thread::wait_for_search_finished() {
|
||||
|
||||
std::cout << "wait_for_search_finished unique_lock " << std::this_thread::get_id() << std::endl;
|
||||
std::unique_lock<std::mutex> lk(mutex);
|
||||
std::cout << "wait_for_search_finished cv.wait 1 " << std::this_thread::get_id() << std::endl;
|
||||
cv.wait(lk, [&]{ return !searching; });
|
||||
std::cout << "wait_for_search_finished cv.wait 1 done " << std::this_thread::get_id() << std::endl;
|
||||
lk.unlock();
|
||||
}
|
||||
|
||||
|
||||
|
@ -112,21 +106,18 @@ void Thread::idle_loop() {
|
|||
// NUMA machinery is not needed.
|
||||
if (Options["Threads"] > 8)
|
||||
WinProcGroup::bindThisThread(idx);
|
||||
std::cout << "idle_loop start " << std::this_thread::get_id() << std::endl;
|
||||
|
||||
while (true)
|
||||
{
|
||||
std::cout << "idle_loop unique_lock " << std::this_thread::get_id() << std::endl;
|
||||
std::unique_lock<std::mutex> lk(mutex);
|
||||
searching = false;
|
||||
cv.notify_one(); // Wake up anyone waiting for search finished
|
||||
std::cout << "idle_loop cv.wait " << std::this_thread::get_id() << std::endl;
|
||||
cv.wait(lk, [&]{ return searching; });
|
||||
std::cout << "idle_loop cv.wait done " << std::this_thread::get_id() << std::endl;
|
||||
|
||||
if (exit)
|
||||
break;
|
||||
|
||||
lk.unlock();
|
||||
if (exit) {
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
search();
|
||||
|
@ -134,7 +125,6 @@ std::cout << "idle_loop cv.wait done " << std::this_thread::get_id() << std::end
|
|||
sync_cerr << e.what() << sync_endl; // report error and continue
|
||||
}
|
||||
}
|
||||
std::cout << "idle_loop ended " << std::this_thread::get_id() << std::endl;
|
||||
}
|
||||
|
||||
/// ThreadPool::set() creates/destroys threads to match the requested number.
|
||||
|
|
|
@ -65,14 +65,6 @@
|
|||
|
||||
int throw_exception(const char* func, const char* file, int line, const char* msg);
|
||||
|
||||
//#define DEBUG
|
||||
//#ifdef DEBUG
|
||||
//#define DEBUG_PRINT(x) printf x
|
||||
//#else
|
||||
//#define DEBUG_PRINT(x)
|
||||
//#endif
|
||||
#define _DEBUG_PRINT(x)
|
||||
|
||||
#undef assert
|
||||
#define assert(e) \
|
||||
(__builtin_expect(!(e), 0) ? (void)throw_exception(__func__, __FILE__, __LINE__, #e) : (void)0)
|
||||
|
|
Loading…
Reference in New Issue