OSDN Git Service

4089b223ddbe05f5cb8e119acca7f5fb70176aad
[pf3gnuchains/gcc-fork.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, 2006 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 #define WIN32_LEAN_AND_MEAN
17 #include <windows.h>
18
19 //
20 // Typedefs.
21 //
22
23 typedef struct
24 {
25   // ev[0] (signal) is a Win32 auto-reset event for _Jv_CondNotify
26   // ev[1] (broadcast) is a Win32 manual-reset event for _Jv_CondNotifyAll
27   HANDLE ev[2];
28
29   // Number of threads waiting on this condition variable
30   int blocked_count;
31
32   // Protects access to the blocked_count variable
33   CRITICAL_SECTION count_mutex;
34
35 } _Jv_ConditionVariable_t;
36
37 typedef struct
38 {
39   // The thread-id of the owner thread if any, 0 otherwise
40   DWORD owner;
41
42   // Track nested mutex acquisitions by the same thread
43   int refcount;
44
45   // The actual Windows construct used to implement this mutex
46   CRITICAL_SECTION cs;
47
48 } _Jv_Mutex_t;
49
50 typedef struct
51 {
52   int flags;            // Flags are defined in implementation.
53   HANDLE handle;        // Actual handle to the thread
54
55   // Protects access to the thread's interrupt_flag and
56   // interrupt_event variables within this module.
57   CRITICAL_SECTION interrupt_mutex;
58   
59   // A Win32 auto-reset event for thread interruption
60   HANDLE interrupt_event;
61
62   java::lang::Thread *thread_obj;
63 } _Jv_Thread_t;
64
65 typedef DWORD _Jv_ThreadId_t;
66
67 inline _Jv_ThreadId_t
68 _Jv_ThreadSelf (void)
69 {
70   return GetCurrentThreadId();
71 }
72
73 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
74
75 //
76 // Condition variables.
77 //
78
79 #define _Jv_HaveCondDestroy
80 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos);
81 void _Jv_CondInit (_Jv_ConditionVariable_t *cv);
82 void _Jv_CondDestroy (_Jv_ConditionVariable_t *cv);
83 int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
84 int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
85
86 //
87 // Mutexes.
88 // We use CRITICAL_SECTIONs instead of CreateMutex() for better performance
89 //
90
91 // Returns 0 if the mutex lock is held by the current thread, and 1 otherwise.
92 inline int _Jv_MutexCheckMonitor (_Jv_Mutex_t *mu)
93 {
94   return (mu->owner != GetCurrentThreadId ( ));
95 }
96
97 inline void _Jv_MutexInit (_Jv_Mutex_t *mu)
98 {
99   mu->owner = 0UL;
100   mu->refcount = 0;
101   InitializeCriticalSection (&(mu->cs));
102 }
103
104 #define _Jv_HaveMutexDestroy
105 inline void _Jv_MutexDestroy (_Jv_Mutex_t *mu)
106 {
107   mu->owner = 0UL;
108   mu->refcount = 0;
109   DeleteCriticalSection (&(mu->cs));
110   mu = NULL;
111 }
112
113 inline int _Jv_MutexUnlock (_Jv_Mutex_t *mu)
114 {
115   if (mu->owner == GetCurrentThreadId ( ))
116     {
117       mu->refcount--;
118       if (mu->refcount == 0)
119         mu->owner = 0UL;
120       LeaveCriticalSection (&(mu->cs));
121       return 0;
122     }
123   else
124     return 1;
125 }
126
127 inline int _Jv_MutexLock (_Jv_Mutex_t *mu)
128 {
129   EnterCriticalSection (&(mu->cs));
130   mu->owner = GetCurrentThreadId ( );
131   mu->refcount++;
132   return 0;
133 }
134
135 //
136 // Thread creation and manipulation.
137 //
138
139 void _Jv_InitThreads (void);
140 _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
141 void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
142
143 inline java::lang::Thread* _Jv_ThreadCurrent (void)
144 {
145   extern DWORD _Jv_ThreadKey;
146   return (java::lang::Thread *) TlsGetValue(_Jv_ThreadKey);
147 }
148
149 inline _Jv_Thread_t *_Jv_ThreadCurrentData(void)
150 {
151   extern DWORD _Jv_ThreadDataKey;
152   return (_Jv_Thread_t *) TlsGetValue(_Jv_ThreadDataKey);
153 }
154
155 inline void _Jv_ThreadYield (void)
156 {
157   Sleep (0);
158 }
159
160 void _Jv_ThreadRegister (_Jv_Thread_t *data);
161 void _Jv_ThreadUnRegister ();
162
163 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
164 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
165                       _Jv_ThreadStartFunc *meth);
166 void _Jv_ThreadWait (void);
167 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
168
169 //
170 // Thread interruption support
171 //
172
173 // Gets the auto-reset event for the current thread which is
174 // signalled by _Jv_ThreadInterrupt. The caller can wait on this
175 // event in addition to other waitable objects.
176 //
177 // NOTE: After waiting on this event with WaitForMultipleObjects,
178 // you should ALWAYS use the return value of WaitForMultipleObjects
179 // to test whether this event was signalled and whether thread
180 // interruption has occurred. You should do this instead of checking
181 // the thread's interrupted_flag, because someone could have reset
182 // this flag in the interval of time between the return of
183 // WaitForMultipleObjects and the time you query interrupted_flag.
184 // See java/lang/natWin32Process.cc (waitFor) for an example.
185 HANDLE _Jv_Win32GetInterruptEvent (void);
186
187 // Increases a thread's suspend count. If the thread's previous
188 // suspend count was zero, i.e., it is not suspended, this function
189 // will suspend the thread. This function may be used to suspend
190 // any thread from any other thread (or suspend itself).
191 void _Jv_ThreadDebugSuspend (_Jv_Thread_t* data);
192
193 // Decreases a thread's suspend count. If the thread's new thread
194 // count is zero, the thread is resumed. This function may be used
195 // by any thread to resume any other thread.
196 void _Jv_ThreadDebugResume (_Jv_Thread_t* data);
197
198 // Get the suspend count for a thread
199 jint _Jv_ThreadDebugSuspendCount (_Jv_Thread_t* data);
200
201 // Remove defines from <windows.h> that conflict with various things in libgcj code
202
203 #undef TRUE
204 #undef FALSE
205 #undef MAX_PRIORITY
206 #undef MIN_PRIORITY
207 #undef min
208 #undef max
209 #undef interface
210 #undef STRICT
211 #undef VOID
212
213 #endif /* __JV_WIN32_THREADS__ */