63char*
parse_word(
const char*,
char*,
unsigned int);
64char*
parse_field(
const char*,
char*,
unsigned int,
char);
65char*
parse_line(
const char*,
char*,
unsigned int);
72char*
parse_command(
const char*,
char*,
char*,
const unsigned int);
98#define MAX(a, b) ((a) > (b) ? (a) : (b))
101#define MIN(a, b) ((a) < (b) ? (a) : (b))
104#define BOUND(var, min, max, name) do {\
105 if (var < min && min <= max) {\
106 if (name) fprintf(stderr, "\nWARNING: %s = %lld is out of range. Set to %lld\n", name, (long long)var, (long long)min);\
108 } else if (var > max) {\
109 if (name) fprintf(stderr, "\nWARNING: %s = %lld is out of range. Set to %lld\n", name, (long long)var, (long long)max);\
118#if defined(__unix__) || (defined(_WIN32) && defined(USE_PTHREAD)) || defined(__APPLE__)
123typedef pthread_t Thread;
126typedef pthread_mutex_t Lock;
129typedef pthread_cond_t Condition;
132#define thread_detach(thread) pthread_detach(thread)
134#if DEBUG && defined(__unix__) && !defined(__APPLE__)
136#define lock(c) if (pthread_mutex_lock(&(c)->lock)) { \
141#define unlock(c) if (pthread_mutex_unlock(&(c)->lock)) { \
146#define lock_init(c) do {\
147 pthread_mutexattr_t attr;\
148 pthread_mutexattr_init(&attr);\
149 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);\
150 pthread_mutex_init(&(c)->lock, &attr);\
154#define lock_free(c) if (pthread_mutex_destroy(&(c)->lock)) { \
155 error("lock_free"); \
161#define lock(c) pthread_mutex_lock(&(c)->lock)
164#define unlock(c) pthread_mutex_unlock(&(c)->lock)
167#define lock_init(c) pthread_mutex_init(&(c)->lock, NULL)
170#define lock_free(c) pthread_mutex_destroy(&(c)->lock)
176#include <libkern/OSAtomic.h>
179typedef OSSpinLock SpinLock;
182#define spin_lock(c) OSSpinLockLock(&(c)->spin)
185#define spin_unlock(c) OSSpinLockUnlock(&(c)->spin)
188#define spin_init(c) do {(c)->spin = OS_SPINLOCK_INIT;} while (0)
194#elif defined(__USE_XOPEN2K)
197typedef pthread_spinlock_t SpinLock;
200#define spin_lock(c) pthread_spin_lock(&(c)->spin)
203#define spin_unlock(c) pthread_spin_unlock(&(c)->spin)
206#define spin_init(c) pthread_spin_init(&(c)->spin, PTHREAD_PROCESS_PRIVATE)
209#define spin_free(c) pthread_spin_destroy(&(c)->spin)
214typedef pthread_mutex_t SpinLock;
217#define spin_lock(c) pthread_mutex_lock(&(c)->spin)
220#define spin_unlock(c) pthread_mutex_unlock(&(c)->spin)
223#define spin_init(c) pthread_mutex_init(&(c)->spin, NULL)
226#define spin_free(c) pthread_mutex_destroy(&(c)->spin)
231#define condition_init(c) pthread_cond_init(&(c)->cond, NULL)
234#define condition_wait(c) pthread_cond_wait(&(c)->cond, &(c)->lock)
237#define condition_signal(c) pthread_cond_signal(&(c)->cond)
240#define condition_broadcast(c) pthread_cond_broadcast(&(c)->cond)
243#define condition_free(c) pthread_cond_destroy(&(c)->cond)
250typedef HANDLE Thread;
253typedef CRITICAL_SECTION Lock;
256typedef CRITICAL_SECTION SpinLock;
264void InitializeConditionVariable(CONDITION_VARIABLE*);
265void WakeConditionVariable(CONDITION_VARIABLE*);
266void WakeAllConditionVariable(CONDITION_VARIABLE*);
267BOOL SleepConditionVariableCS(CONDITION_VARIABLE*, CRITICAL_SECTION*, DWORD);
272typedef CONDITION_VARIABLE Condition;
275#define condition_init(c) InitializeConditionVariable(&(c)->cond)
278#define condition_wait(c) SleepConditionVariableCS(&(c)->cond, &(c)->lock, INFINITE)
281#define condition_signal(c) WakeConditionVariable(&(c)->cond)
284#define condition_broadcast(c) WakeAllConditionVariable(&(c)->cond)
287#define condition_free(c)
292#define thread_detach(thread) CloseHandle(thread)
295#define lock(c) EnterCriticalSection(&(c)->lock)
298#define unlock(c) LeaveCriticalSection(&(c)->lock)
301#define lock_init(c) InitializeCriticalSection(&(c)->lock)
304#define lock_free(c) DeleteCriticalSection(&(c)->lock)
307#define spin_lock(c) EnterCriticalSection(&(c)->spin)
310#define spin_unlock(c) LeaveCriticalSection(&(c)->spin)
313#define spin_init(c) InitializeCriticalSection(&(c)->spin)
316#define spin_free(c) DeleteCriticalSection(&(c)->spin)
327static inline void atomic_add(
volatile unsigned long long *value,
long long i)
329#if defined(USE_GAS_X64)
330 __asm__ __volatile__(
"lock xaddq %1, %0":
"=m"(*value) :
"r"(i),
"m" (*value));
331#elif defined(USE_MSVC_X64)
332 _InterlockedAdd64(value, i);
349#define fatal_error(...) \
351 fprintf(stderr, "\nFATAL ERROR: %s : %s : %d : ", __FILE__, __func__, __LINE__); \
352 if (errno) fprintf(stderr, "\terror #%d : %s", errno, strerror(errno)); \
353 fputc('\n', stderr); \
354 fprintf(stderr, __VA_ARGS__); \
363 fprintf(stderr, "\nERROR: %s : %s : %d :", __FILE__, __func__, __LINE__); \
364 if (errno) fprintf(stderr, " error #%d : %s", errno, strerror(errno)); \
365 fputc('\n', stderr); \
366 fprintf(stderr, __VA_ARGS__); \
375 fprintf(stderr, "\nWARNING: "); \
376 fprintf(stderr, __VA_ARGS__); \
382#define info(...) if (options.info) { \
383 fprintf(stderr, __VA_ARGS__); \
389#define cassio_debug(...) if (options.debug_cassio) { \
391 printf(__VA_ARGS__); \
392 log_print(engine_log, "DEBUG: "); \
393 log_print(engine_log, __VA_ARGS__);\
401#define trace(...) do {\
402 fprintf(stderr, "trace %s : %s : %d : ", __FILE__, __func__, __LINE__); \
403 fprintf(stderr, __VA_ARGS__); \
411 fprintf(stderr, "\nDEBUG : "); \
412 fprintf(stderr, __VA_ARGS__); \
429#define log_open(l, file) if ((l)->f == NULL && file != NULL) { \
431 (l)->f = fopen(file, "w"); \
435#define log_close(l) if ((l)->f) { \
442#define log_print(l, ...) if ((l)->f) { \
443 fprintf((l)->f, __VA_ARGS__); \
448#define log_is_open(l) ((l)->f != NULL)
451#define log_receive(l, title, ...) if ((l)->f) { \
452 fprintf((l)->f, "%s", title); \
453 time_stamp((l)->f); \
454 fprintf((l)->f, " <== "); \
455 fprintf((l)->f, __VA_ARGS__); \
460#define log_send(l, title, ...) if ((l)->f) { \
461 fprintf((l)->f, "%s", title); \
462 time_stamp((l)->f); \
463 fprintf((l)->f, " ==> "); \
464 fprintf((l)->f, __VA_ARGS__); \
LogFile.
Definition: util.h:423
Lock lock
Definition: util.h:425
FILE * f
Definition: util.h:424
unsigned long long x
Definition: util.h:88
char * parse_boolean(const char *, bool *)
Parse a boolean.
Definition: util.c:741
char * file_add_ext(const char *, const char *, char *)
Add an extension to a string.
Definition: util.c:907
char * parse_move(const char *, const struct Board *, struct Move *)
char * parse_int(const char *, int *)
Parse an integer.
Definition: util.c:761
char * parse_command(const char *, char *, char *, const unsigned int)
Parse a command.
Definition: util.c:867
char * string_copy_line(FILE *)
void time_print(long long, bool, FILE *)
Print time as "D:HH:MM:SS.CC".
Definition: util.c:131
long long real_clock(void)
char * parse_skip_spaces(const char *)
Skip spaces.
Definition: util.c:514
long long time_read(FILE *)
read time as "D:HH:MM:SS.C".
Definition: util.c:156
long long cpu_clock(void)
int string_to_int(const char *, const int)
Convert a string into an integer.
Definition: util.c:457
void time_stamp(FILE *)
Print local time.
Definition: util.c:189
long long(* time_clock)(void)
Time clock.
Definition: util.c:122
void random_seed(Random *, const unsigned long long)
Pseudo-random number seed.
Definition: util.c:1062
char * parse_real(const char *, double *)
Parse a real number (as a double floating point).
Definition: util.c:796
int string_to_coordinate(const char *)
Convert the two first chars of a string into a coordinate.
Definition: util.c:384
void print_scientific(double, const char *, FILE *)
Print a value with a unit.
Definition: util.c:250
char * parse_line(const char *, char *, unsigned int)
Parse a line.
Definition: util.c:604
bool is_stdin_keyboard(void)
void relax(int)
sleep for t ms.
Definition: util.c:203
char * parse_skip_word(const char *)
Skip word.
Definition: util.c:528
char * parse_game(const char *, const struct Board *, struct Line *)
char * string_read_line(FILE *)
Read a line.
Definition: util.c:265
void thread_create2(Thread *, void *(*f)(void *), void *)
Create a thread.
Definition: util.c:922
static void atomic_add(volatile unsigned long long *value, long long i)
Definition: util.h:327
char * parse_board(const char *, struct Board *, int *)
Parse a board.
Definition: util.c:682
int get_cpu_number(void)
Get the number of cpus or cores on the machine.
Definition: util.c:987
void path_get_dir(const char *, char *)
Extract the directory of a file path.
Definition: util.c:888
void thread_join(Thread)
Join a thread.
Definition: util.c:940
void string_to_uppercase(char *)
Change all char of a string to uppercase.
Definition: util.c:369
char * string_to_word(char *)
remove spaces from a string.
Definition: util.c:411
char * parse_field(const char *, char *, unsigned int, char)
Parse a field.
Definition: util.c:582
double string_to_real(const char *, const double)
Convert a string into a real number.
Definition: util.c:488
unsigned long long random_get(Random *)
Pseudo-random number generator.
Definition: util.c:1043
char * string_duplicate(const char *)
Duplicate a string.
Definition: util.c:299
char * parse_find(const char *, const int)
Find a char.
Definition: util.c:545
long long string_to_time(const char *)
Read time as "D:HH:MM:SS.C".
Definition: util.c:320
char * format_scientific(double, const char *, char *)
Format a value with a unit.
Definition: util.c:222
char * parse_word(const char *, char *, unsigned int)
Parse a word.
Definition: util.c:562
bool string_to_boolean(const char *)
Convert a string into a boolean.
Definition: util.c:432
void string_to_lowercase(char *)
Change all char of a string to lowercase.
Definition: util.c:355
void thread_set_cpu(Thread, int)
Choose a single core or cpu to run on, under linux systems, to avoid context changes.
Definition: util.c:967
Thread thread_self(void)
Current thread.
Definition: util.c:954