* src/vm/vm.c, src/vm/vm.h: Moved to .cpp.
[cacao.git] / src / threads / posix / condition-posix.hpp
1 /* src/threads/posix/condition-posix.hpp - POSIX condition variable
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _CONDITION_POSIX_HPP
27 #define _CONDITION_POSIX_HPP
28
29 #include "config.h"
30
31 #include <pthread.h>
32 #include <time.h>
33
34 #include "vm/vm.hpp"
35
36 #ifdef __cplusplus
37
38 /**
39  * POSIX condition variable.
40  */
41 class Condition {
42 private:
43         // POSIX condition structure.
44         pthread_cond_t _cond;
45
46 public:
47         Condition();
48         ~Condition();
49
50         void broadcast();
51         void signal();
52         void timedwait(Mutex* mutex, const struct timespec* abstime);
53         void wait(Mutex* mutex);
54 };
55
56
57 /**
58  * Initialize a POSIX condition variable.
59  */
60 inline Condition::Condition()
61 {
62         int result;
63
64         result = pthread_cond_init(&_cond, NULL);
65
66         if (result != 0)
67                 vm_abort_errnum(result, "Condition::Condition(): pthread_cond_init failed");
68 }
69
70
71 /**
72  * Destroys a POSIX condition variable.
73  */
74 inline Condition::~Condition()
75 {
76         int result;
77
78         result = pthread_cond_destroy(&_cond);
79
80         if (result != 0)
81                 vm_abort_errnum(result, "Condition::~Condition(): pthread_cond_destroy failed");
82 }
83
84
85 /**
86  * Restarts all the threads that are waiting on the condition
87  * variable.
88  */
89 inline void Condition::broadcast()
90 {
91         int result;
92
93         result = pthread_cond_broadcast(&_cond);
94
95         if (result != 0)
96                 vm_abort_errnum(result, "Condition::broadcast(): pthread_cond_broadcast failed");
97 }
98
99
100 /**
101  * Restarts one of the threads that are waiting on this condition
102  * variable.
103  */
104 inline void Condition::signal()
105 {
106         int result;
107
108         result = pthread_cond_signal(&_cond);
109
110         if (result != 0)
111                 vm_abort_errnum(result, "Condition::signal(): pthread_cond_signal failed");
112 }
113
114
115 /**
116  * Waits on the condition variable, as wait() does, but it also bounds
117  * the duration of the wait.
118  */
119 inline void Condition::timedwait(Mutex* mutex, const struct timespec* abstime)
120 {
121         // This function can return return values which are valid.
122         (void) pthread_cond_timedwait(&_cond, &(mutex->_mutex), abstime);
123 }
124
125
126 /**
127  * Waits for the condition variable.
128  */
129 inline void Condition::wait(Mutex* mutex)
130 {
131         int result;
132
133         result = pthread_cond_wait(&_cond, &(mutex->_mutex));
134
135         if (result != 0)
136                 vm_abort_errnum(result, "Condition::wait(): pthread_cond_wait failed");
137 }
138
139 #else
140
141 // This structure must have the same layout as the class above.
142 typedef struct Condition {
143         pthread_mutex_t _mutex;
144         pthread_cond_t _cond;
145 } Condition;
146
147 Condition* Condition_new();
148 void       Condition_delete(Condition* cond);
149 void       Condition_lock(Condition* cond);
150 void       Condition_unlock(Condition* cond);
151 void       Condition_broadcast(Condition* cond);
152 void       Condition_signal(Condition* cond);
153 void       Condition_timedwait(Condition* cond, Mutex *mutex, const struct timespec* abstime);
154 void       Condition_wait(Condition* cond, Mutex* mutex);
155
156 #endif
157
158 #endif /* _CONDITION_POSIX_HPP */
159
160
161 /*
162  * These are local overrides for various environment variables in Emacs.
163  * Please do not remove this and leave it at the end of the file, where
164  * Emacs will automagically detect them.
165  * ---------------------------------------------------------------------
166  * Local variables:
167  * mode: c++
168  * indent-tabs-mode: t
169  * c-basic-offset: 4
170  * tab-width: 4
171  * End:
172  * vim:noexpandtab:sw=4:ts=4:
173  */