OSDN Git Service

* win32.cc: (_Jv_pipe) Implemented.
[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 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
54   // Protects access to the thread's interrupt_flag and
55   // interrupt_event variables within this module.
56   CRITICAL_SECTION interrupt_mutex;
57   
58   // A Win32 auto-reset event for thread interruption
59   HANDLE interrupt_event;
60
61   java::lang::Thread *thread_obj;
62 } _Jv_Thread_t;
63
64 typedef DWORD _Jv_ThreadId_t;
65
66 inline _Jv_ThreadId_t
67 _Jv_ThreadSelf (void)
68 {
69   return GetCurrentThreadId();
70 }
71
72 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
73
74 //
75 // Condition variables.
76 //
77
78 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu, jlong millis, jint nanos);
79 void _Jv_CondInit (_Jv_ConditionVariable_t *cv);
80 void _Jv_CondDestroy (_Jv_ConditionVariable_t *cv);
81 int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
82 int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *);
83
84 //
85 // Mutexes.
86 // We use CRITICAL_SECTIONs instead of CreateMutex() for better performance
87 //
88
89 inline void _Jv_MutexInit (_Jv_Mutex_t *mu)
90 {
91   mu->owner = 0UL;
92   mu->refcount = 0;
93   InitializeCriticalSection (&(mu->cs));
94 }
95
96 #define _Jv_HaveMutexDestroy
97 inline void _Jv_MutexDestroy (_Jv_Mutex_t *mu)
98 {
99   mu->owner = 0UL;
100   mu->refcount = 0;
101   DeleteCriticalSection (&(mu->cs));
102   mu = NULL;
103 }
104
105 inline int _Jv_MutexUnlock (_Jv_Mutex_t *mu)
106 {
107   if (mu->owner == GetCurrentThreadId ( ))
108     {
109       mu->refcount--;
110       if (mu->refcount == 0)
111         mu->owner = 0UL;
112       LeaveCriticalSection (&(mu->cs));
113       return 0;
114     }
115   else
116     return 1;
117 }
118
119 inline int _Jv_MutexLock (_Jv_Mutex_t *mu)
120 {
121   EnterCriticalSection (&(mu->cs));
122   mu->owner = GetCurrentThreadId ( );
123   mu->refcount++;
124   return 0;
125 }
126
127 //
128 // Thread creation and manipulation.
129 //
130
131 void _Jv_InitThreads (void);
132 _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
133 void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
134
135 inline java::lang::Thread* _Jv_ThreadCurrent (void)
136 {
137   extern DWORD _Jv_ThreadKey;
138   return (java::lang::Thread *) TlsGetValue(_Jv_ThreadKey);
139 }
140
141 inline _Jv_Thread_t *_Jv_ThreadCurrentData(void)
142 {
143   extern DWORD _Jv_ThreadDataKey;
144   return (_Jv_Thread_t *) TlsGetValue(_Jv_ThreadDataKey);
145 }
146
147 inline void _Jv_ThreadYield (void)
148 {
149   Sleep (0);
150 }
151
152 void _Jv_ThreadRegister (_Jv_Thread_t *data);
153 void _Jv_ThreadUnRegister ();
154
155 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
156 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
157                       _Jv_ThreadStartFunc *meth);
158 void _Jv_ThreadWait (void);
159 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
160
161 //
162 // Thread interruption support
163 //
164
165 // Gets the auto-reset event for the current thread which is
166 // signalled by _Jv_ThreadInterrupt. The caller can wait on this
167 // event in addition to other waitable objects.
168 //
169 // NOTE: After waiting on this event with WaitForMultipleObjects,
170 // you should ALWAYS use the return value of WaitForMultipleObjects
171 // to test whether this event was signalled and whether thread
172 // interruption has occurred. You should do this instead of checking
173 // the thread's interrupted_flag, because someone could have reset
174 // this flag in the interval of time between the return of
175 // WaitForMultipleObjects and the time you query interrupted_flag.
176 // See java/lang/natWin32Process.cc (waitFor) for an example.
177 HANDLE _Jv_Win32GetInterruptEvent (void);
178
179 // Remove defines from <windows.h> that conflict with various things in libgcj code
180
181 #undef TRUE
182 #undef FALSE
183 #undef MAX_PRIORITY
184 #undef MIN_PRIORITY
185 #undef min
186 #undef max
187 #undef interface
188 #undef STRICT
189 #undef VOID
190
191 #endif /* __JV_WIN32_THREADS__ */