1 /* src/threads/posix/mutex-posix.hpp - POSIX mutual exclusion functions
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
26 #ifndef _MUTEX_POSIX_HPP
27 #define _MUTEX_POSIX_HPP
38 * POSIX implementation of a mutex.
42 // POSIX mutex structure.
43 pthread_mutex_t _mutex;
45 // Condition class needs to access _mutex for wait() and
47 friend class Condition;
58 /* static mutex initializer ***************************************************/
60 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
64 * Initializes the given mutex object and checks for errors.
70 result = pthread_mutex_init(&_mutex, NULL);
73 vm_abort_errnum(result, "Mutex::Mutex(): pthread_mutex_init failed");
78 * Destroys the given mutex object and checks for errors.
80 inline Mutex::~Mutex()
84 result = pthread_mutex_destroy(&_mutex);
87 vm_abort_errnum(result, "Mutex::~Mutex(): pthread_mutex_destroy failed");
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.
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.)
100 inline void Mutex::lock()
104 result = pthread_mutex_lock(&_mutex);
107 vm_abort_errnum(result, "Mutex::lock(): pthread_mutex_lock failed");
112 * Unlocks the given mutex object and checks for errors. The mutex is
113 * assumed to be locked and owned by the calling thread.
115 inline void Mutex::unlock()
119 result = pthread_mutex_unlock(&_mutex);
122 vm_abort_errnum(result, "Mutex::unlock: pthread_mutex_unlock failed");
127 // This structure must have the same layout as the class above.
128 typedef struct Mutex {
129 pthread_mutex_t _mutex;
133 void Mutex_delete(Mutex* mutex);
134 void Mutex_lock(Mutex* mutex);
135 void Mutex_unlock(Mutex* mutex);
139 #endif /* _MUTEX_POSIX_HPP */
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 * ---------------------------------------------------------------------
149 * indent-tabs-mode: t
153 * vim:noexpandtab:sw=4:ts=4: