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