comparison Python/thread_nt.h @ 38579:19430cdff2bd legacy-trunk

clean up function declarations to conform to PEP-7 style.
author Andrew MacIntyre <andymac@bullseye.apana.org.au>
date Sun, 04 Jun 2006 12:59:59 +0000
parents 0b2b076e5908
children c4587a5f5e94
comparison
equal deleted inserted replaced
38578:0b2b076e5908 38579:19430cdff2bd
14 } NRMUTEX, *PNRMUTEX ; 14 } NRMUTEX, *PNRMUTEX ;
15 15
16 typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ; 16 typedef PVOID WINAPI interlocked_cmp_xchg_t(PVOID *dest, PVOID exc, PVOID comperand) ;
17 17
18 /* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */ 18 /* Sorry mate, but we haven't got InterlockedCompareExchange in Win95! */
19 static PVOID WINAPI interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand) 19 static PVOID WINAPI
20 interlocked_cmp_xchg(PVOID *dest, PVOID exc, PVOID comperand)
20 { 21 {
21 static LONG spinlock = 0 ; 22 static LONG spinlock = 0 ;
22 PVOID result ; 23 PVOID result ;
23 DWORD dwSleep = 0; 24 DWORD dwSleep = 0;
24 25
52 /* Release spinlock */ 53 /* Release spinlock */
53 spinlock = 0 ; 54 spinlock = 0 ;
54 return result ; 55 return result ;
55 } ; 56 } ;
56 57
57 static interlocked_cmp_xchg_t *ixchg ; 58 static interlocked_cmp_xchg_t *ixchg;
58 BOOL InitializeNonRecursiveMutex(PNRMUTEX mutex) 59
60 BOOL
61 InitializeNonRecursiveMutex(PNRMUTEX mutex)
59 { 62 {
60 if (!ixchg) 63 if (!ixchg)
61 { 64 {
62 /* Sorely, Win95 has no InterlockedCompareExchange API (Win98 has), so we have to use emulation */ 65 /* Sorely, Win95 has no InterlockedCompareExchange API (Win98 has), so we have to use emulation */
63 HANDLE kernel = GetModuleHandle("kernel32.dll") ; 66 HANDLE kernel = GetModuleHandle("kernel32.dll") ;
74 #ifdef InterlockedCompareExchange 77 #ifdef InterlockedCompareExchange
75 #undef InterlockedCompareExchange 78 #undef InterlockedCompareExchange
76 #endif 79 #endif
77 #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand))) 80 #define InterlockedCompareExchange(dest,exchange,comperand) (ixchg((dest), (exchange), (comperand)))
78 81
79 VOID DeleteNonRecursiveMutex(PNRMUTEX mutex) 82 VOID
83 DeleteNonRecursiveMutex(PNRMUTEX mutex)
80 { 84 {
81 /* No in-use check */ 85 /* No in-use check */
82 CloseHandle(mutex->hevent) ; 86 CloseHandle(mutex->hevent) ;
83 mutex->hevent = NULL ; /* Just in case */ 87 mutex->hevent = NULL ; /* Just in case */
84 } 88 }
85 89
86 DWORD EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait) 90 DWORD
91 EnterNonRecursiveMutex(PNRMUTEX mutex, BOOL wait)
87 { 92 {
88 /* Assume that the thread waits successfully */ 93 /* Assume that the thread waits successfully */
89 DWORD ret ; 94 DWORD ret ;
90 95
91 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */ 96 /* InterlockedIncrement(&mutex->owned) == 0 means that no thread currently owns the mutex */
102 107
103 mutex->thread_id = GetCurrentThreadId() ; /* We own it */ 108 mutex->thread_id = GetCurrentThreadId() ; /* We own it */
104 return ret ; 109 return ret ;
105 } 110 }
106 111
107 BOOL LeaveNonRecursiveMutex(PNRMUTEX mutex) 112 BOOL
113 LeaveNonRecursiveMutex(PNRMUTEX mutex)
108 { 114 {
109 /* We don't own the mutex */ 115 /* We don't own the mutex */
110 mutex->thread_id = 0 ; 116 mutex->thread_id = 0 ;
111 return 117 return
112 InterlockedDecrement(&mutex->owned) < 0 || 118 InterlockedDecrement(&mutex->owned) < 0 ||
113 SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */ 119 SetEvent(mutex->hevent) ; /* Other threads are waiting, wake one on them up */
114 } 120 }
115 121
116 PNRMUTEX AllocNonRecursiveMutex(void) 122 PNRMUTEX
123 AllocNonRecursiveMutex(void)
117 { 124 {
118 PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ; 125 PNRMUTEX mutex = (PNRMUTEX)malloc(sizeof(NRMUTEX)) ;
119 if (mutex && !InitializeNonRecursiveMutex(mutex)) 126 if (mutex && !InitializeNonRecursiveMutex(mutex))
120 { 127 {
121 free(mutex) ; 128 free(mutex) ;
122 mutex = NULL ; 129 mutex = NULL ;
123 } 130 }
124 return mutex ; 131 return mutex ;
125 } 132 }
126 133
127 void FreeNonRecursiveMutex(PNRMUTEX mutex) 134 void
135 FreeNonRecursiveMutex(PNRMUTEX mutex)
128 { 136 {
129 if (mutex) 137 if (mutex)
130 { 138 {
131 DeleteNonRecursiveMutex(mutex) ; 139 DeleteNonRecursiveMutex(mutex) ;
132 free(mutex) ; 140 free(mutex) ;
136 long PyThread_get_thread_ident(void); 144 long PyThread_get_thread_ident(void);
137 145
138 /* 146 /*
139 * Initialization of the C package, should not be needed. 147 * Initialization of the C package, should not be needed.
140 */ 148 */
141 static void PyThread__init_thread(void) 149 static void
150 PyThread__init_thread(void)
142 { 151 {
143 } 152 }
144 153
145 /* 154 /*
146 * Thread support. 155 * Thread support.
207 216
208 /* 217 /*
209 * Return the thread Id instead of an handle. The Id is said to uniquely identify the 218 * Return the thread Id instead of an handle. The Id is said to uniquely identify the
210 * thread in the system 219 * thread in the system
211 */ 220 */
212 long PyThread_get_thread_ident(void) 221 long
222 PyThread_get_thread_ident(void)
213 { 223 {
214 if (!initialized) 224 if (!initialized)
215 PyThread_init_thread(); 225 PyThread_init_thread();
216 226
217 return GetCurrentThreadId(); 227 return GetCurrentThreadId();
218 } 228 }
219 229
220 static void do_PyThread_exit_thread(int no_cleanup) 230 static void
231 do_PyThread_exit_thread(int no_cleanup)
221 { 232 {
222 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident())); 233 dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
223 if (!initialized) 234 if (!initialized)
224 if (no_cleanup) 235 if (no_cleanup)
225 _exit(0); 236 _exit(0);
226 else 237 else
227 exit(0); 238 exit(0);
228 _endthread(); 239 _endthread();
229 } 240 }
230 241
231 void PyThread_exit_thread(void) 242 void
243 PyThread_exit_thread(void)
232 { 244 {
233 do_PyThread_exit_thread(0); 245 do_PyThread_exit_thread(0);
234 } 246 }
235 247
236 void PyThread__exit_thread(void) 248 void
249 PyThread__exit_thread(void)
237 { 250 {
238 do_PyThread_exit_thread(1); 251 do_PyThread_exit_thread(1);
239 } 252 }
240 253
241 #ifndef NO_EXIT_PROG 254 #ifndef NO_EXIT_PROG
242 static void do_PyThread_exit_prog(int status, int no_cleanup) 255 static void
256 do_PyThread_exit_prog(int status, int no_cleanup)
243 { 257 {
244 dprintf(("PyThread_exit_prog(%d) called\n", status)); 258 dprintf(("PyThread_exit_prog(%d) called\n", status));
245 if (!initialized) 259 if (!initialized)
246 if (no_cleanup) 260 if (no_cleanup)
247 _exit(status); 261 _exit(status);
248 else 262 else
249 exit(status); 263 exit(status);
250 } 264 }
251 265
252 void PyThread_exit_prog(int status) 266 void
267 PyThread_exit_prog(int status)
253 { 268 {
254 do_PyThread_exit_prog(status, 0); 269 do_PyThread_exit_prog(status, 0);
255 } 270 }
256 271
257 void PyThread__exit_prog(int status) 272 void
273 PyThread__exit_prog(int status)
258 { 274 {
259 do_PyThread_exit_prog(status, 1); 275 do_PyThread_exit_prog(status, 1);
260 } 276 }
261 #endif /* NO_EXIT_PROG */ 277 #endif /* NO_EXIT_PROG */
262 278
263 /* 279 /*
264 * Lock support. It has too be implemented as semaphores. 280 * Lock support. It has too be implemented as semaphores.
265 * I [Dag] tried to implement it with mutex but I could find a way to 281 * I [Dag] tried to implement it with mutex but I could find a way to
266 * tell whether a thread already own the lock or not. 282 * tell whether a thread already own the lock or not.
267 */ 283 */
268 PyThread_type_lock PyThread_allocate_lock(void) 284 PyThread_type_lock
285 PyThread_allocate_lock(void)
269 { 286 {
270 PNRMUTEX aLock; 287 PNRMUTEX aLock;
271 288
272 dprintf(("PyThread_allocate_lock called\n")); 289 dprintf(("PyThread_allocate_lock called\n"));
273 if (!initialized) 290 if (!initialized)
278 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock)); 295 dprintf(("%ld: PyThread_allocate_lock() -> %p\n", PyThread_get_thread_ident(), aLock));
279 296
280 return (PyThread_type_lock) aLock; 297 return (PyThread_type_lock) aLock;
281 } 298 }
282 299
283 void PyThread_free_lock(PyThread_type_lock aLock) 300 void
301 PyThread_free_lock(PyThread_type_lock aLock)
284 { 302 {
285 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); 303 dprintf(("%ld: PyThread_free_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
286 304
287 FreeNonRecursiveMutex(aLock) ; 305 FreeNonRecursiveMutex(aLock) ;
288 } 306 }
291 * Return 1 on success if the lock was acquired 309 * Return 1 on success if the lock was acquired
292 * 310 *
293 * and 0 if the lock was not acquired. This means a 0 is returned 311 * and 0 if the lock was not acquired. This means a 0 is returned
294 * if the lock has already been acquired by this thread! 312 * if the lock has already been acquired by this thread!
295 */ 313 */
296 int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag) 314 int
315 PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
297 { 316 {
298 int success ; 317 int success ;
299 318
300 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag)); 319 dprintf(("%ld: PyThread_acquire_lock(%p, %d) called\n", PyThread_get_thread_ident(),aLock, waitflag));
301 320
304 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success)); 323 dprintf(("%ld: PyThread_acquire_lock(%p, %d) -> %d\n", PyThread_get_thread_ident(),aLock, waitflag, success));
305 324
306 return success; 325 return success;
307 } 326 }
308 327
309 void PyThread_release_lock(PyThread_type_lock aLock) 328 void
329 PyThread_release_lock(PyThread_type_lock aLock)
310 { 330 {
311 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock)); 331 dprintf(("%ld: PyThread_release_lock(%p) called\n", PyThread_get_thread_ident(),aLock));
312 332
313 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock))) 333 if (!(aLock && LeaveNonRecursiveMutex((PNRMUTEX) aLock)))
314 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError())); 334 dprintf(("%ld: Could not PyThread_release_lock(%p) error: %l\n", PyThread_get_thread_ident(), aLock, GetLastError()));