Updated DEPRECATED.pod, bumped PBC_COMPAT
[parrot.git] / include / parrot / thr_windows.h
blobc7745b723bd4c0b768c83b6ab51d99baf3e3c5ed
1 /* thread.h
2 * Copyright (C) 2001-2003, Parrot Foundation.
3 * SVN Info
4 * $Id$
5 * Overview:
6 * This is the api header for the windows thread primitives
7 * Data Structure and Algorithms:
8 * History:
9 * Notes:
10 * References:
13 #ifndef PARROT_THR_WINDOWS_H_GUARD
14 #define PARROT_THR_WINDOWS_H_GUARD
16 # undef FASTCALL
17 # include <process.h>
18 # include <limits.h>
20 typedef CRITICAL_SECTION Parrot_mutex;
21 typedef struct Windows_cond
23 HANDLE m_hSema;
24 LONG m_lWaiters;
25 } Parrot_cond;
26 typedef HANDLE Parrot_thread;
28 # define MUTEX_INIT(m) InitializeCriticalSection((PCRITICAL_SECTION)&(m))
29 # define MUTEX_DESTROY(m) DeleteCriticalSection((PCRITICAL_SECTION)&(m))
31 # define COND_INIT(c) \
32 do { \
33 (c).m_hSema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); \
34 (c).m_lWaiters = 0; \
35 } while (0)
37 # define COND_DESTROY(c) CloseHandle((c).m_hSema)
39 # define LOCK(m) EnterCriticalSection((PCRITICAL_SECTION)&(m))
40 # define UNLOCK(m) LeaveCriticalSection((PCRITICAL_SECTION)&(m))
42 # define COND_WAIT(c, m) \
43 do { \
44 ++(c).m_lWaiters; \
45 UNLOCK(m); \
46 WaitForSingleObject((c).m_hSema, INFINITE); \
47 LOCK(m); \
48 --(c).m_lWaiters; \
49 } while (0)
51 # define COND_TIMED_WAIT(c, m, t) \
52 do { \
53 FLOATVAL now; \
54 time_t sec; \
55 LONG nsec; \
56 DWORD diff; \
57 now = Parrot_floatval_time(); \
58 sec = (time_t)now; \
59 nsec = (LONG)((now - sec)*1000.0f)*1000000L; \
60 if ((t)->tv_sec > sec || ((t)->tv_sec == sec && (t)->tv_nsec > nsec)) \
61 { \
62 ++(c).m_lWaiters; \
63 UNLOCK(m); \
64 diff = (DWORD)(((t)->tv_sec - sec)*1000L + ((t)->tv_nsec - nsec)/1000000L); \
65 WaitForSingleObject((c).m_hSema, diff); \
66 LOCK(m); \
67 --(c).m_lWaiters; \
68 } \
69 } while (0)
71 # define COND_SIGNAL(c) \
72 do { \
73 if ((c).m_lWaiters > 0) \
74 ReleaseSemaphore((c).m_hSema, 1, NULL); \
75 } while (0)
77 # define COND_BROADCAST(c) \
78 do { \
79 if ((c).m_lWaiters > 0) \
80 ReleaseSemaphore((c).m_hSema, (c).m_lWaiters, NULL); \
81 } while (0)
83 # define JOIN(t, ret) \
84 do { \
85 WaitForSingleObject((t), INFINITE); \
86 GetExitCodeThread((t), (LPDWORD)&(ret)); \
87 CloseHandle(t); \
88 } while (0)
90 # define DETACH(t) CloseHandle(t)
92 /* If the compiler CRT library has a good _beginthreadXX() routine, use it instead of
93 the Win32 API CreateThread(). _beginthreadXX guards call to the thread start routine
94 with SEH to implement runtime errors and signal support. Also it frees calloc-ed
95 per-thread data block at exit */
96 #ifdef _MCS_VER1
97 # define THREAD_CREATE_JOINABLE(t, func, arg) \
98 do { \
99 unsigned tid; \
100 (t) = (HANDLE)_beginthreadex(NULL, 0, unsigned (__stdcall * (func)) (void*), \
101 (void*)(arg), 0, &tid); \
102 } while (0)
103 #else
104 # define THREAD_CREATE_JOINABLE(t, func, arg) \
105 do { \
106 DWORD tid; \
107 (t) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(func), (PVOID)(arg), 0, &tid); \
108 } while (0)
109 #endif
111 # define THREAD_CREATE_DETACHED(t, func, arg) \
112 do { \
113 THREAD_CREATE_JOINABLE((t), (func), (arg)); \
114 DETACH(t); \
115 } while (0)
117 # define CLEANUP_PUSH(f, a)
118 # define CLEANUP_POP(a)
120 typedef void (*Cleanup_Handler)(void *);
122 #ifndef _TIMESPEC_DEFINED
123 # define _TIMESPEC_DEFINED
124 struct timespec {
125 time_t tv_sec;
126 long tv_nsec;
128 #endif /* _TIMESPEC_DEFINED */
130 #endif /* PARROT_THR_WINDOWS_H_GUARD */
133 * Local variables:
134 * c-file-style: "parrot"
135 * End:
136 * vim: expandtab shiftwidth=4: