re PR java/9254 (java::lang::Object::wait(), threads-win32.cc returns wrong return...
[gcc.git] / libjava / include / win32-threads.h
1 // -*- c++ -*-
2 // win32-threads.h - Defines for using Win32 threads.
3
4 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software
5 Foundation
6
7 This file is part of libgcj.
8
9 This software is copyrighted work licensed under the terms of the
10 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 details. */
12
13 #ifndef __JV_WIN32_THREADS__
14 #define __JV_WIN32_THREADS__
15
16 #include <windows.h>
17
18 //
19 // Typedefs.
20 //
21
22 typedef struct
23 {
24 // ev[0] (signal) is a Win32 auto-reset event for _Jv_CondNotify
25 // ev[1] (broadcast) is a Win32 manual-reset event for _Jv_CondNotifyAll
26 HANDLE ev[2];
27
28 // Number of threads waiting on this condition variable
29 int blocked_count;
30
31 // Protects access to the blocked_count variable
32 CRITICAL_SECTION count_mutex;
33
34 } _Jv_ConditionVariable_t;
35
36 typedef struct
37 {
38 // The thread-id of the owner thread if any, 0 otherwise
39 DWORD owner;
40
41 // Track nested mutex acquisitions by the same thread
42 int refcount;
43
44 // The actual Windows construct used to implement this mutex
45 CRITICAL_SECTION cs;
46
47 } _Jv_Mutex_t;
48
49 typedef struct
50 {
51 int flags; // Flags are defined in implementation.
52 HANDLE handle; // Actual handle to the thread
53 java::lang::Thread *thread_obj;
54 } _Jv_Thread_t;
55
56 typedef DWORD _Jv_ThreadId_t;
57
58 inline _Jv_ThreadId_t
59 _Jv_ThreadSelf (void)
60 {
61 return GetCurrentThreadId();
62 }
63
64 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
65
66 //
67 // Condition variables.
68 //
69
70 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos);
71 void _Jv_CondInit (_Jv_ConditionVariable_t *cv);
72 void _Jv_CondDestroy (_Jv_ConditionVariable_t *cv);
73 int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
74 int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
75
76 //
77 // Mutexes.
78 // We use CRITICAL_SECTIONs instead of CreateMutex() for better performance
79 //
80
81 inline void _Jv_MutexInit (_Jv_Mutex_t *mu)
82 {
83 mu->owner = 0UL;
84 mu->refcount = 0;
85 InitializeCriticalSection (&(mu->cs));
86 }
87
88 #define _Jv_HaveMutexDestroy
89 inline void _Jv_MutexDestroy (_Jv_Mutex_t *mu)
90 {
91 mu->owner = 0UL;
92 mu->refcount = 0;
93 DeleteCriticalSection (&(mu->cs));
94 mu = NULL;
95 }
96
97 inline int _Jv_MutexUnlock (_Jv_Mutex_t *mu)
98 {
99 if (mu->owner == GetCurrentThreadId ( ))
100 {
101 mu->refcount--;
102 if (mu->refcount == 0)
103 mu->owner = 0UL;
104 LeaveCriticalSection (&(mu->cs));
105 return 0;
106 }
107 else
108 return 1;
109 }
110
111 inline int _Jv_MutexLock (_Jv_Mutex_t *mu)
112 {
113 EnterCriticalSection (&(mu->cs));
114 mu->owner = GetCurrentThreadId ( );
115 mu->refcount++;
116 return 0;
117 }
118
119 //
120 // Thread creation and manipulation.
121 //
122
123 void _Jv_InitThreads (void);
124 _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
125 void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
126
127 inline java::lang::Thread* _Jv_ThreadCurrent (void)
128 {
129 extern DWORD _Jv_ThreadKey;
130 return (java::lang::Thread *) TlsGetValue(_Jv_ThreadKey);
131 }
132
133 inline _Jv_Thread_t *_Jv_ThreadCurrentData(void)
134 {
135 extern DWORD _Jv_ThreadDataKey;
136 return (_Jv_Thread_t *) TlsGetValue(_Jv_ThreadDataKey);
137 }
138
139 inline void _Jv_ThreadYield (void)
140 {
141 Sleep (0);
142 }
143
144 void _Jv_ThreadRegister (_Jv_Thread_t *data);
145 void _Jv_ThreadUnRegister ();
146
147 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
148 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
149 _Jv_ThreadStartFunc *meth);
150 void _Jv_ThreadWait (void);
151 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
152
153 // Remove defines from <windows.h> that conflict with various things in libgcj code
154
155 #undef TRUE
156 #undef FALSE
157 #undef MAX_PRIORITY
158 #undef MIN_PRIORITY
159 #undef min
160 #undef max
161 #undef interface
162 #undef STRICT
163 #undef VOID
164
165 #endif /* __JV_WIN32_THREADS__ */