Update codebase to remove clang warnings (and a couple of legit errors
[typhoon.git] / src / lock.h
1 #if defined(SMP)
2
3 #  undef Pause
4 #  define Pause()
5
6 /*
7  *******************************************************************************
8  *                                                                             *
9  *  this is a Microsoft windows-based operating system.                        *
10  *                                                                             *
11  *******************************************************************************
12  */
13 #  if defined(_WIN32) || defined(_WIN64)
14
15 #    define pthread_attr_t  HANDLE
16 #    define pthread_t       HANDLE
17 #    define thread_t        HANDLE
18
19 extern pthread_t NumaStartThread(void *func, void *args);
20
21 #    if ((defined (_M_IA64) || defined (_M_AMD64)) && !defined(NT_INTEREX))
22
23 #      include <windows.h>
24
25 #      pragma intrinsic (_InterlockedExchange)
26
27 typedef volatile LONG lock_t[1];
28
29 #      define LockInit(v)      ((v)[0] = 0)
30 #      define LockFree(v)      ((v)[0] = 0)
31 #      define Unlock(v)        ((v)[0] = 0)
32
33 __forceinline void Lock(volatile LONG * hPtr)
34 {
35   int iValue;
36
37   for (;;) {
38     iValue = _InterlockedExchange((LPLONG) hPtr, 1);
39     if (0 == iValue)
40       return;
41     while (*hPtr);
42   }
43 }
44
45 #    else                       /* NT non-Alpha/Intel, without assembler Lock() */
46
47 #      define lock_t           volatile int
48 #      define LockInit(v)      ((v) = 0)
49 #      define LockFree(v)      ((v) = 0)
50 #      define Lock(v)          do {                                         \
51                              while(InterlockedExchange((LPLONG)&(v),1) != 0);  \
52                            } while (0)
53 #      define Unlock(v)        ((v) = 0)
54
55 #    endif                      /* architecture check */
56
57 #  else
58 /*
59  *******************************************************************************
60  *                                                                             *
61  *  this is a Unix-based operating system.  define the spinlock code as needed *
62  *  for SMP synchronization.                                                   *
63  *                                                                             *
64  *******************************************************************************
65  */
66
67 #    if defined(ALPHA)
68
69 #      include <machine/builtins.h>
70
71 #      define lock_t           volatile long
72 #      define LockInit(v)      ((v) = 0)
73 #      define LockFree(v)      ((v) = 0)
74 #      define Lock(v)          __LOCK_LONG(&(v))
75 #      define Unlock(v)        __UNLOCK_LONG(&(v))
76
77 #    elif defined(POWERPC)
78                         /* OS X */
79
80 #      include <libkern/OSAtomic.h>
81
82 #      define lock_t                            OSSpinLock
83 #      define LockInit(v)               ((v) = 0)
84 #      define LockFree(v)               ((v) = 0)
85 #      define Lock(v)                   OSSpinLockLock(&(v))
86 #      define Unlock(v)                 OSSpinLockUnlock(&(v))
87
88 #    else                       /* X86 */
89
90 #      undef Pause
91 #      define Pause() ({asm volatile ("pause");})
92
93 static void __inline__ LockX86(volatile int *lock)
94 {
95   int dummy;
96   asm __volatile__("1:          movl    $1, %0" "\n\t"
97       "            xchgl   (%1), %0" "\n\t" "            testl   %0, %0" "\n\t"
98       "            jz      3f" "\n\t" "2:          pause" "\n\t"
99       "            movl    (%1), %0" "\n\t" "            testl   %0, %0" "\n\t"
100       "            jnz     2b" "\n\t" "            jmp     1b" "\n\t" "3:"
101       "\n\t":"=&q"(dummy)
102       :"q"(lock)
103       :"cc");
104 }
105
106 static void __inline__ UnlockX86(volatile int *lock)
107 {
108   int dummy;
109   asm __volatile__("movl    $0, (%1)":"=&q"(dummy)
110       :"q"(lock));
111 }
112
113 #      define LockInit(p)           (p=0)
114 #      define LockFree(p)           (p=0)
115 #      define Unlock(p)             (UnlockX86(&p))
116 #      define Lock(p)               (LockX86(&p))
117 #      define lock_t                volatile int
118
119 #    endif
120
121 #  endif                        /* windows or unix */
122
123 #else
124 #  define LockInit(p)
125 #  define LockFree(p)
126 #  define Lock(p)
127 #  define Unlock(p)
128 #endif                          /*  SMP code */