1 /* src/threads/posix/mutex-posix.hpp - POSIX mutual exclusion functions
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #ifndef _MUTEX_POSIX_HPP
28 #define _MUTEX_POSIX_HPP
38 * POSIX implementation of a mutex.
42 // POSIX mutex structure.
43 pthread_mutex_t _mutex;
44 pthread_mutexattr_t _attr;
46 // Condition class needs to access _mutex for wait() and
48 friend class Condition;
61 typedef struct Mutex Mutex;
73 * Initializes the given mutex object and checks for errors.
77 int result = pthread_mutexattr_init(&_attr);
80 os::abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_init failed");
83 result = pthread_mutexattr_settype(&_attr, PTHREAD_MUTEX_RECURSIVE);
86 os::abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_settype failed");
89 result = pthread_mutex_init(&_mutex, &_attr);
92 os::abort_errnum(result, "Mutex::Mutex(): pthread_mutex_init failed");
98 * Destroys the given mutex object and checks for errors.
100 inline Mutex::~Mutex()
102 int result = pthread_mutexattr_destroy(&_attr);
105 os::abort_errnum(result, "Mutex::~Mutex(): pthread_mutexattr_destroy failed");
108 result = pthread_mutex_destroy(&_mutex);
111 os::abort_errnum(result, "Mutex::~Mutex(): pthread_mutex_destroy failed");
117 * Locks the given mutex object and checks for errors. If the mutex is
118 * already locked by another thread, the calling thread is suspended until
119 * the mutex is unlocked.
121 * If the mutex is already locked by the calling thread, the same applies,
122 * thus effectively causing the calling thread to deadlock. (This is because
123 * we use "fast" pthread mutexes without error checking.)
125 inline void Mutex::lock()
127 int result = pthread_mutex_lock(&_mutex);
130 os::abort_errnum(result, "Mutex::lock(): pthread_mutex_lock failed");
136 * Unlocks the given mutex object and checks for errors. The mutex is
137 * assumed to be locked and owned by the calling thread.
139 inline void Mutex::unlock()
141 int result = pthread_mutex_unlock(&_mutex);
144 os::abort_errnum(result, "Mutex::unlock: pthread_mutex_unlock failed");
150 // This structure must have the same layout as the class above.
152 pthread_mutex_t _mutex;
153 pthread_mutexattr_t _attr;
157 void Mutex_delete(Mutex* mutex);
158 void Mutex_lock(Mutex* mutex);
159 void Mutex_unlock(Mutex* mutex);
163 #endif /* _MUTEX_POSIX_HPP */
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 * ---------------------------------------------------------------------
173 * indent-tabs-mode: t
177 * vim:noexpandtab:sw=4:ts=4: