* lex.c (java_read_char): Check for EOF from getc first.
[official-gcc.git] / libjava / include / posix-threads.h
blob75285ca6f50acea2b14303e2e5777272e319b10e
1 // -*- c++ -*-
2 // posix-threads.h - Defines for using POSIX threads.
4 /* Copyright (C) 1998, 1999 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
12 #ifndef __JV_POSIX_THREADS__
13 #define __JV_POSIX_THREADS__
15 // NOTE: This file may only reference those pthread functions which
16 // are known not to be overridden by the Boehm GC. If in doubt, scan
17 // boehm-gc/gc.h. This is yucky but lets us avoid including gc.h
18 // everywhere (which would be truly yucky).
20 #include <pthread.h>
21 #include <sched.h>
24 // Typedefs.
27 typedef struct _Jv_Thread_t
29 // Flag values are defined in implementation.
30 int flags;
32 // Actual thread id.
33 pthread_t thread;
35 // Java Thread object.
36 java::lang::Thread *thread_obj;
38 // Condition variable and corresponding mutex, used to implement the
39 // interruptable wait/notify mechanism.
40 pthread_cond_t wait_cond;
41 pthread_mutex_t wait_mutex;
43 // Next thread for Condition Variable wait-list chain.
44 _Jv_Thread_t *next;
46 } _Jv_Thread_t;
48 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
51 // Condition Variables used to implement wait/notify/sleep/interrupt.
52 typedef struct
54 // Linked list of Threads that are waiting to be notified.
55 _Jv_Thread_t *first;
57 } _Jv_ConditionVariable_t;
59 typedef struct
61 // For compatibility, simplicity, and correctness, we do not use the native
62 // pthreads recursive mutex implementation, but simulate them instead.
64 // Mutex the thread holds the entire time this mutex is held.
65 pthread_mutex_t mutex;
67 // Thread holding this mutex.
68 pthread_t owner;
70 // Number of times mutex is held (lock depth). If 0, the lock is not held.
71 int count;
72 } _Jv_Mutex_t;
74 // This is a convenience function used only by the pthreads thread
75 // implementation. This is slow, but that's too bad -- we need to do
76 // the checks for correctness. It might be nice to be able to compile
77 // this out. Returns 0 if the lock is held by the current thread, and
78 // 1 otherwise.
79 inline int
80 _Jv_PthreadCheckMonitor (_Jv_Mutex_t *mu)
82 pthread_t self = pthread_self();
83 if (mu->owner == self)
84 return 0;
85 else return 1;
89 // Condition variables.
92 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
93 jlong millis, jint nanos);
95 int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu);
97 int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu);
99 inline void
100 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
102 cv->first = NULL;
106 // Mutexes.
109 inline void
110 _Jv_MutexInit (_Jv_Mutex_t *mu)
112 pthread_mutex_init (&mu->mutex, NULL);
114 mu->count = 0;
115 mu->owner = 0;
118 inline int
119 _Jv_MutexLock (_Jv_Mutex_t *mu)
121 pthread_t self = pthread_self ();
122 if (mu->owner == self)
124 mu->count++;
126 else
128 pthread_mutex_lock (&mu->mutex);
129 mu->count = 1;
130 mu->owner = self;
132 return 0;
135 inline int
136 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
138 if (_Jv_PthreadCheckMonitor (mu))
139 return 1;
141 mu->count--;
143 if (mu->count == 0)
145 mu->owner = 0;
146 pthread_mutex_unlock (&mu->mutex);
148 return 0;
151 #ifndef LINUX_THREADS
153 // pthread_mutex_destroy does nothing on Linux and it is a win to avoid
154 // defining this macro.
156 #define _Jv_HaveMutexDestroy
158 inline void
159 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
161 pthread_mutex_destroy (&mu->mutex);
164 #endif /* LINUX_THREADS */
167 // Thread creation and manipulation.
170 void _Jv_InitThreads (void);
172 _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
173 void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
175 inline java::lang::Thread *
176 _Jv_ThreadCurrent (void)
178 extern pthread_key_t _Jv_ThreadKey;
179 return (java::lang::Thread *) pthread_getspecific (_Jv_ThreadKey);
182 inline _Jv_Thread_t *
183 _Jv_ThreadCurrentData (void)
185 extern pthread_key_t _Jv_ThreadDataKey;
186 return (_Jv_Thread_t *) pthread_getspecific (_Jv_ThreadDataKey);
189 inline void
190 _Jv_ThreadYield (void)
192 #ifdef HAVE_SCHED_YIELD
193 sched_yield ();
194 #endif /* HAVE_SCHED_YIELD */
197 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
199 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
200 _Jv_ThreadStartFunc *meth);
202 void _Jv_ThreadWait (void);
204 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
206 #endif /* __JV_POSIX_THREADS__ */