]> git.sesse.net Git - stockfish/blob - src/lock.h
movegen: Introduce generate_pawn_captures()
[stockfish] / src / lock.h
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
5   Glaurung is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9   
10   Glaurung is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14   
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 #if !defined(LOCK_H_INCLUDED)
21 #define LOCK_H_INCLUDED
22
23
24 // x86 assembly language locks or OS spin locks may perform faster than
25 // mutex locks on some platforms.  On my machine, mutexes seem to be the
26 // best.
27
28 //#define ASM_LOCK
29 //#define OS_SPIN_LOCK
30
31
32 #if defined(ASM_LOCK)
33
34
35 typedef volatile int Lock;
36
37 static inline void LockX86(Lock *lock) {
38   int dummy;
39   asm __volatile__("1:          movl    $1, %0" "\n\t"
40       "            xchgl   (%1), %0" "\n\t" "            testl   %0, %0" "\n\t"
41       "            jz      3f" "\n\t" "2:          pause" "\n\t"
42       "            movl    (%1), %0" "\n\t" "            testl   %0, %0" "\n\t"
43       "            jnz     2b" "\n\t" "            jmp     1b" "\n\t" "3:"
44       "\n\t":"=&q"(dummy)
45       :"q"(lock)
46       :"cc");
47 }
48
49 static inline void UnlockX86(Lock *lock) {
50   int dummy;
51   asm __volatile__("movl    $0, (%1)":"=&q"(dummy)
52       :"q"(lock));
53 }
54
55 #  define lock_init(x, y) (*(x) = 0)
56 #  define lock_grab(x) LockX86(x)
57 #  define lock_release(x) UnlockX86(x)
58 #  define lock_destroy(x)
59
60
61 #elif defined(OS_SPIN_LOCK)
62
63
64 #  include <libkern/OSAtomic.h>
65
66 typedef OSSpinLock Lock;
67
68 #  define lock_init(x, y) (*(x) = 0)
69 #  define lock_grab(x) OSSpinLockLock(x)
70 #  define lock_release(x) OSSpinLockUnlock(x)
71 #  define lock_destroy(x)
72
73
74 #elif !defined(_MSC_VER)
75
76 #  include <pthread.h>
77
78 typedef pthread_mutex_t Lock;
79
80 #  define lock_init(x, y) pthread_mutex_init(x, y)
81 #  define lock_grab(x) pthread_mutex_lock(x)
82 #  define lock_release(x) pthread_mutex_unlock(x)
83 #  define lock_destroy(x) pthread_mutex_destroy(x)
84
85
86 #else
87
88
89 #  include <windows.h>
90
91 typedef CRITICAL_SECTION Lock;
92 #  define lock_init(x, y) InitializeCriticalSection(x)
93 #  define lock_grab(x) EnterCriticalSection(x)
94 #  define lock_release(x) LeaveCriticalSection(x)
95 #  define lock_destroy(x) DeleteCriticalSection(x)
96
97
98 #endif
99
100
101 #endif // !defined(LOCK_H_INCLUDED)