Merge pull request #8 from glinscott/master

Optimize undefended minor check. Little editing by
me, no change even at assembly level.

No regression after 8K games at fast TC on a 64bit CPU.
Penalty for undefended rook as well
Use a local copy of tte->value()

This should avoid some aliasing issues
with TT table access.

After 3913 games at 10"+0.05
Mod vs Orig 662 - 651 - 2600  ELO +0 (+- 6.4)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Merge two loops in ThreadsManager::init()

In analogy with ThreadsManager::exit()

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Remove last platform specific code form thread.cpp

A somewhat tricky function pointer cast allows us
to move the platform specifics to lock.h, the cast
is tricky because return type is not the same of the
casted function in Linux (for Windows return type is
a DWORD that is a long) but this should not be a
problem as long as the size is the same;

From: http://stackoverflow.com/questions/188839/function-pointer-cast-to-different-signature

"OpenSSL was only casting functions pointers to
other function types taking and returning the same
number of values of the same exact sizes, and this
(assuming you're not dealing with floating-point)
happens to be safe across all the platforms and
calling conventions I know of. However, anything
else is potentially unsafe."

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Rename lock.h to platform.h

And move some more platform specific code there.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Fix a MSVC warning

Not correct warning about use of an uninitialized
variable. The warning is not correct becuase we can
never reach the warned code when in SpNode, anyhow
the fix is simple.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Don't ceil cpu_count()

It is already done at calling site where it is
more appropiate.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Revert "Don't sync with C library I/O buffers"

It seems is the cause of strange and rare hangs
reported by some users where Stockfish stops
responding to GUI. It is not clear why but for
the moment revert the patch.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Refactor ThreadsManager::set_size() functionality

Split the data allocation, now done (mostly once)
in read_uci_options(), from the wake up and sleeping
of the slave threads upon entering/exiting the search.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Refactor Thread class

Associate platform OS thread to the Thread class instead of
creating it from ThreadsManager.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Use std::vector<Thread*> to store threads

We store pointers instead of Thread objects because
Thread is not copy-constructible nor copy-assignable
and default ones are not suitable. So we cannot store
directly in a std::vector.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Try to mimic std::thread API

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Merge remote-tracking branch 'upstream/master'
Fix race in ThreadsManager::sleep()

We cannot set do_sleep flag of main thread before
"bestmove" is sent to GUI, otherwise GUI could send
immediately the next "go" command that triggers
start_thinking() and because do_sleep is set UI
thread resets the flag to launch a new search. But
when shortly after main thread returns to main_loop()
flag is incorrectly reset and main thread goes to sleep
hanging the engine.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Reset Thread::maxPly before a new search

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Fix compile error with gcc

We have a clash with start_fn defined both as a
Thread memeber and as a function pointer type in
pthread_create().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Move ThreadsManager::exit() to d'tor

And add final touches to this long patch series.

All the series has been verified against regression with
20K games at fast TC.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
Add more detailed pawn shelter/storm evaluation
Introduce and use wait_for_search_finished()

Helper function that allows us to simplify
the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>