Document where we want a uint16_t instead of a uint64_t
[glaurung_clone.git] / src / lock.h
blob2efb2076fbda03f72b74d895ab230cb22ebaa7fb
1 /*
2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
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.
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.
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/>.
20 #if !defined(LOCK_H_INCLUDED)
21 #define LOCK_H_INCLUDED
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.
28 //#define ASM_LOCK
29 //#define OS_SPIN_LOCK
32 #if defined(ASM_LOCK)
35 typedef volatile int Lock;
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");
49 static inline void UnlockX86(Lock *lock) {
50 int dummy;
51 asm __volatile__("movl $0, (%1)":"=&q"(dummy)
52 :"q"(lock));
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)
61 #elif defined(OS_SPIN_LOCK)
64 # include <libkern/OSAtomic.h>
66 typedef OSSpinLock Lock;
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)
74 #elif !defined(_MSC_VER)
76 # include <pthread.h>
78 typedef pthread_mutex_t Lock;
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)
86 #else
89 # include <windows.h>
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)
98 #endif
101 #endif // !defined(LOCK_H_INCLUDED)