Implement invocation interface; don't create new thread for main.
[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 Free Software Foundation
5
6 This file is part of libgcj.
7
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
11
12 #ifndef __JV_WIN32_THREADS__
13 #define __JV_WIN32_THREADS__
14
15 #include <windows.h>
16
17 //
18 // Typedefs.
19 //
20
21 typedef HANDLE _Jv_ConditionVariable_t;
22 typedef HANDLE _Jv_Mutex_t;
23
24 typedef struct
25 {
26 int flags; // Flags are defined in implementation.
27 HANDLE handle; // Actual handle to the thread
28 java::lang::Thread *thread_obj;
29 } _Jv_Thread_t;
30
31 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
32
33 //
34 // Condition variables.
35 //
36
37 inline void
38 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
39 {
40 *cv = CreateEvent (NULL, 0, 0, NULL);
41 }
42
43 #define _Jv_HaveCondDestroy
44
45 inline void
46 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
47 {
48 CloseHandle (*cv);
49 cv = NULL;
50 }
51
52 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
53 jlong millis, jint nanos);
54
55 inline int
56 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
57 {
58 // FIXME: check for mutex ownership?
59 return PulseEvent (*cv) ? 0 : _JV_NOT_OWNER; // FIXME?
60 }
61
62 inline int
63 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
64 {
65 // FIXME: check for mutex ownership?
66 return PulseEvent (*cv) ? 0 : _JV_NOT_OWNER; // FIXME?
67 }
68
69 //
70 // Mutexes.
71 //
72
73 inline void
74 _Jv_MutexInit (_Jv_Mutex_t *mu)
75 {
76 *mu = CreateMutex (NULL, 0, NULL);
77 }
78
79 #define _Jv_HaveMutexDestroy
80
81 inline void
82 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
83 {
84 CloseHandle (*mu);
85 mu = NULL;
86 }
87
88 int _Jv_MutexLock (_Jv_Mutex_t *mu);
89
90 inline int
91 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
92 {
93 return ReleaseMutex(*mu) ? 0 : GetLastError(); // FIXME: Map error code?
94 }
95
96 //
97 // Thread creation and manipulation.
98 //
99
100 void _Jv_InitThreads (void);
101 _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
102 void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
103
104 inline java::lang::Thread *
105 _Jv_ThreadCurrent (void)
106 {
107 extern DWORD _Jv_ThreadKey;
108 return (java::lang::Thread *) TlsGetValue(_Jv_ThreadKey);
109 }
110
111 inline _Jv_Thread_t *
112 _Jv_ThreadCurrentData (void)
113 {
114 extern DWORD _Jv_ThreadDataKey;
115 return (_Jv_Thread_t *) TlsGetValue(_Jv_ThreadDataKey);
116 }
117
118 inline void
119 _Jv_ThreadYield (void)
120 {
121 Sleep (0);
122 }
123
124 void _Jv_ThreadRegister (_Jv_Thread_t *data);
125 void _Jv_ThreadUnRegister ();
126
127 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
128 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
129 _Jv_ThreadStartFunc *meth);
130 void _Jv_ThreadWait (void);
131 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
132
133 // Remove defines from <windows.h> that conflict with various things in libgcj code
134
135 #undef TRUE
136 #undef FALSE
137 #undef MAX_PRIORITY
138 #undef MIN_PRIORITY
139 #undef min
140 #undef max
141 #undef interface
142 #undef STRICT
143 #undef VOID
144
145 #endif /* __JV_WIN32_THREADS__ */