* src/vm/vm.hpp (VM): Added member_vm, added functions get_current,
[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::get_current()->abort_errnum(result, "Condition::Condition(): pthread_cond_init failed");
68         }
69 }
70
71
72 /**
73  * Destroys a POSIX condition variable.
74  */
75 inline Condition::~Condition()
76 {
77         int result;
78
79         result = pthread_cond_destroy(&_cond);
80
81         if (result != 0) {
82                 VM::get_current()->abort_errnum(result, "Condition::~Condition(): pthread_cond_destroy failed");
83         }
84 }
85
86
87 /**
88  * Restarts all the threads that are waiting on the condition
89  * variable.
90  */
91 inline void Condition::broadcast()
92 {
93         int result;
94
95         result = pthread_cond_broadcast(&_cond);
96
97         if (result != 0) {
98                 VM::get_current()->abort_errnum(result, "Condition::broadcast(): pthread_cond_broadcast failed");
99         }
100 }
101
102
103 /**
104  * Restarts one of the threads that are waiting on this condition
105  * variable.
106  */
107 inline void Condition::signal()
108 {
109         int result;
110
111         result = pthread_cond_signal(&_cond);
112
113         if (result != 0) {
114                 VM::get_current()->abort_errnum(result, "Condition::signal(): pthread_cond_signal failed");
115         }
116 }
117
118
119 /**
120  * Waits on the condition variable, as wait() does, but it also bounds
121  * the duration of the wait.
122  */
123 inline void Condition::timedwait(Mutex* mutex, const struct timespec* abstime)
124 {
125         // This function can return return values which are valid.
126         (void) pthread_cond_timedwait(&_cond, &(mutex->_mutex), abstime);
127 }
128
129
130 /**
131  * Waits for the condition variable.
132  */
133 inline void Condition::wait(Mutex* mutex)
134 {
135         int result;
136
137         result = pthread_cond_wait(&_cond, &(mutex->_mutex));
138
139         if (result != 0) {
140                 VM::get_current()->abort_errnum(result, "Condition::wait(): pthread_cond_wait failed");
141         }
142 }
143
144 #else
145
146 // This structure must have the same layout as the class above.
147 typedef struct Condition {
148         pthread_mutex_t _mutex;
149         pthread_cond_t _cond;
150 } Condition;
151
152 Condition* Condition_new();
153 void       Condition_delete(Condition* cond);
154 void       Condition_lock(Condition* cond);
155 void       Condition_unlock(Condition* cond);
156 void       Condition_broadcast(Condition* cond);
157 void       Condition_signal(Condition* cond);
158 void       Condition_timedwait(Condition* cond, Mutex *mutex, const struct timespec* abstime);
159 void       Condition_wait(Condition* cond, Mutex* mutex);
160
161 #endif
162
163 #endif /* _CONDITION_POSIX_HPP */
164
165
166 /*
167  * These are local overrides for various environment variables in Emacs.
168  * Please do not remove this and leave it at the end of the file, where
169  * Emacs will automagically detect them.
170  * ---------------------------------------------------------------------
171  * Local variables:
172  * mode: c++
173  * indent-tabs-mode: t
174  * c-basic-offset: 4
175  * tab-width: 4
176  * End:
177  * vim:noexpandtab:sw=4:ts=4:
178  */