CACAO
condition-posix.hpp
Go to the documentation of this file.
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 #include "threads/mutex.hpp"
34 
35 
36 /**
37  * POSIX condition variable.
38  */
39 class Condition {
40 private:
41  // POSIX condition structure.
42  pthread_cond_t _cond;
43 
44 public:
45  Condition();
46  ~Condition();
47 
48  void broadcast();
49  void signal();
50  void timedwait(Mutex* mutex, const struct timespec* abstime);
51  void wait(Mutex* mutex);
52  void wait(Mutex& mutex);
53 };
54 
55 
56 // Includes.
57 #include "vm/vm.hpp"
58 
59 
60 // Includes.
61 #include "vm/os.hpp"
62 
63 
64 /**
65  * Initialize a POSIX condition variable.
66  */
68 {
69  int result = pthread_cond_init(&_cond, NULL);
70 
71  if (result != 0) {
72  os::abort_errnum(result, "Condition::Condition(): pthread_cond_init failed");
73  }
74 }
75 
76 
77 /**
78  * Destroys a POSIX condition variable.
79  */
81 {
82  // Restart all threads waiting on this condition.
83  broadcast();
84 
85  int result = pthread_cond_destroy(&_cond);
86 
87  if (result != 0) {
88  os::abort_errnum(result, "Condition::~Condition(): pthread_cond_destroy failed");
89  }
90 }
91 
92 
93 /**
94  * Restarts all the threads that are waiting on the condition
95  * variable.
96  */
97 inline void Condition::broadcast()
98 {
99  int result = pthread_cond_broadcast(&_cond);
100 
101  if (result != 0) {
102  os::abort_errnum(result, "Condition::broadcast(): pthread_cond_broadcast failed");
103  }
104 }
105 
106 
107 /**
108  * Restarts one of the threads that are waiting on this condition
109  * variable.
110  */
111 inline void Condition::signal()
112 {
113  int result = pthread_cond_signal(&_cond);
114 
115  if (result != 0) {
116  os::abort_errnum(result, "Condition::signal(): pthread_cond_signal failed");
117  }
118 }
119 
120 
121 /**
122  * Waits on the condition variable, as wait() does, but it also bounds
123  * the duration of the wait.
124  */
125 inline void Condition::timedwait(Mutex* mutex, const struct timespec* abstime)
126 {
127  // This function can return return values which are valid.
128  (void) pthread_cond_timedwait(&_cond, &(mutex->_mutex), abstime);
129 }
130 
131 
132 /**
133  * Waits for the condition variable.
134  */
135 inline void Condition::wait(Mutex* mutex)
136 {
137  wait(*mutex);
138 }
139 
140 
141 /**
142  * Waits for the condition variable.
143  */
144 inline void Condition::wait(Mutex& mutex)
145 {
146  int result = pthread_cond_wait(&_cond, &(mutex._mutex));
147 
148  if (result != 0) {
149  os::abort_errnum(result, "Condition::wait(): pthread_cond_wait failed");
150  }
151 }
152 
153 #endif /* _CONDITION_POSIX_HPP */
154 
155 
156 /*
157  * These are local overrides for various environment variables in Emacs.
158  * Please do not remove this and leave it at the end of the file, where
159  * Emacs will automagically detect them.
160  * ---------------------------------------------------------------------
161  * Local variables:
162  * mode: c++
163  * indent-tabs-mode: t
164  * c-basic-offset: 4
165  * tab-width: 4
166  * End:
167  * vim:noexpandtab:sw=4:ts=4:
168  */
Condition()
Initialize a POSIX condition variable.
void signal()
Restarts one of the threads that are waiting on this condition variable.
typedef void(JNICALL *jvmtiEventSingleStep)(jvmtiEnv *jvmti_env
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
static void abort_errnum(int errnum, const char *text,...)
Prints an error message, appends &quot;:&quot; plus the strerror-message of errnum and aborts the VM...
Definition: os.cpp:150
pthread_cond_t _cond
Dummy condition variable.
void broadcast()
Restarts all the threads that are waiting on the condition variable.
static Mutex mutex
Definition: show.cpp:73
pthread_mutex_t _mutex
Definition: mutex-posix.hpp:40
~Condition()
Destroys a POSIX condition variable.
void wait(Mutex *mutex)
Waits for the condition variable.
void timedwait(Mutex *mutex, const timespec *abstime)