* This commit adds C++ wrapper classes for OpenJDK. Actually I'm done
[cacao.git] / src / threads / posix / mutex-posix.hpp
1 /* src/threads/posix/mutex-posix.hpp - POSIX mutual exclusion functions
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 _MUTEX_POSIX_HPP
27 #define _MUTEX_POSIX_HPP
28
29 #include "config.h"
30
31 #include <pthread.h>
32
33 #include "vm/vm.hpp"
34
35 #ifdef __cplusplus
36
37 /**
38  * POSIX implementation of a mutex.
39  */
40 class Mutex {
41 private:
42         // POSIX mutex structure.
43         pthread_mutex_t _mutex;
44
45         // Condition class needs to access _mutex for wait() and
46         // timedwait().
47         friend class Condition;
48         
49 public:
50         Mutex();
51         ~Mutex();
52
53         void lock();
54         void unlock();
55 };
56
57
58 /* static mutex initializer ***************************************************/
59
60 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
61
62
63 /**
64  * Initializes the given mutex object and checks for errors.
65  */
66 inline Mutex::Mutex()
67 {
68         int result;
69
70         result = pthread_mutex_init(&_mutex, NULL);
71
72         if (result != 0)
73                 vm_abort_errnum(result, "Mutex::Mutex(): pthread_mutex_init failed");
74 }
75
76
77 /**
78  * Destroys the given mutex object and checks for errors.
79  */
80 inline Mutex::~Mutex()
81 {
82         int result;
83
84         result = pthread_mutex_destroy(&_mutex);
85
86         if (result != 0)
87                 vm_abort_errnum(result, "Mutex::~Mutex(): pthread_mutex_destroy failed");
88 }
89
90
91 /**
92  * Locks the given mutex object and checks for errors. If the mutex is
93  * already locked by another thread, the calling thread is suspended until
94  * the mutex is unlocked.
95  *
96  * If the mutex is already locked by the calling thread, the same applies,
97  * thus effectively causing the calling thread to deadlock. (This is because
98  * we use "fast" pthread mutexes without error checking.)
99  */
100 inline void Mutex::lock()
101 {
102         int result;
103
104         result = pthread_mutex_lock(&_mutex);
105
106         if (result != 0)
107                 vm_abort_errnum(result, "Mutex::lock(): pthread_mutex_lock failed");
108 }
109
110
111 /**
112  * Unlocks the given mutex object and checks for errors. The mutex is
113  * assumed to be locked and owned by the calling thread.
114  */
115 inline void Mutex::unlock()
116 {
117         int result;
118
119         result = pthread_mutex_unlock(&_mutex);
120
121         if (result != 0)
122                 vm_abort_errnum(result, "Mutex::unlock: pthread_mutex_unlock failed");
123 }
124
125 #else
126
127 // This structure must have the same layout as the class above.
128 typedef struct Mutex {
129         pthread_mutex_t _mutex;
130 } Mutex;
131
132 Mutex* Mutex_new();
133 void   Mutex_delete(Mutex* mutex);
134 void   Mutex_lock(Mutex* mutex);
135 void   Mutex_unlock(Mutex* mutex);
136
137 #endif
138
139 #endif /* _MUTEX_POSIX_HPP */
140
141
142 /*
143  * These are local overrides for various environment variables in Emacs.
144  * Please do not remove this and leave it at the end of the file, where
145  * Emacs will automagically detect them.
146  * ---------------------------------------------------------------------
147  * Local variables:
148  * mode: c++
149  * indent-tabs-mode: t
150  * c-basic-offset: 4
151  * tab-width: 4
152  * End:
153  * vim:noexpandtab:sw=4:ts=4:
154  */