CACAO
mutex-posix.hpp
Go to the documentation of this file.
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  Copyright (C) 2008 Theobroma Systems Ltd.
6 
7  This file is part of CACAO.
8 
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.
13 
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.
18 
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
22  02110-1301, USA.
23 
24 */
25 
26 
27 #ifndef _MUTEX_POSIX_HPP
28 #define _MUTEX_POSIX_HPP
29 
30 #include "config.h"
31 
32 #include <pthread.h>
33 
34 /**
35  * POSIX implementation of a mutex.
36  */
37 class Mutex {
38 private:
39  // POSIX mutex structure.
40  pthread_mutex_t _mutex;
41  pthread_mutexattr_t _attr;
42 
43  // Condition class needs to access _mutex for wait() and
44  // timedwait().
45  friend class Condition;
46 
47 public:
48  inline Mutex();
49  inline ~Mutex();
50 
51  inline void lock();
52  inline void unlock();
53 };
54 
55 // Includes.
56 #include "vm/os.hpp"
57 
58 /**
59  * Initializes the given mutex object and checks for errors.
60  */
61 inline Mutex::Mutex()
62 {
63  int result = pthread_mutexattr_init(&_attr);
64 
65  if (result != 0) {
66  os::abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_init failed");
67  }
68 
69  result = pthread_mutexattr_settype(&_attr, PTHREAD_MUTEX_RECURSIVE);
70 
71  if (result != 0) {
72  os::abort_errnum(result, "Mutex::Mutex(): pthread_mutexattr_settype failed");
73  }
74 
75  result = pthread_mutex_init(&_mutex, &_attr);
76 
77  if (result != 0) {
78  os::abort_errnum(result, "Mutex::Mutex(): pthread_mutex_init failed");
79  }
80 }
81 
82 
83 /**
84  * Destroys the given mutex object and checks for errors.
85  */
86 inline Mutex::~Mutex()
87 {
88  int result = pthread_mutexattr_destroy(&_attr);
89 
90  if (result != 0) {
91  os::abort_errnum(result, "Mutex::~Mutex(): pthread_mutexattr_destroy failed");
92  }
93 
94  result = pthread_mutex_destroy(&_mutex);
95 
96  if (result != 0) {
97  os::abort_errnum(result, "Mutex::~Mutex(): pthread_mutex_destroy failed");
98  }
99 }
100 
101 
102 /**
103  * Locks the given mutex object and checks for errors. If the mutex is
104  * already locked by another thread, the calling thread is suspended until
105  * the mutex is unlocked.
106  *
107  * If the mutex is already locked by the calling thread, the same applies,
108  * thus effectively causing the calling thread to deadlock. (This is because
109  * we use "fast" pthread mutexes without error checking.)
110  */
111 inline void Mutex::lock()
112 {
113  int result = pthread_mutex_lock(&_mutex);
114 
115  if (result != 0) {
116  os::abort_errnum(result, "Mutex::lock(): pthread_mutex_lock failed");
117  }
118 }
119 
120 
121 /**
122  * Unlocks the given mutex object and checks for errors. The mutex is
123  * assumed to be locked and owned by the calling thread.
124  */
125 inline void Mutex::unlock()
126 {
127  int result = pthread_mutex_unlock(&_mutex);
128 
129  if (result != 0) {
130  os::abort_errnum(result, "Mutex::unlock: pthread_mutex_unlock failed");
131  }
132 }
133 
134 #endif /* _MUTEX_POSIX_HPP */
135 
136 
137 /*
138  * These are local overrides for various environment variables in Emacs.
139  * Please do not remove this and leave it at the end of the file, where
140  * Emacs will automagically detect them.
141  * ---------------------------------------------------------------------
142  * Local variables:
143  * mode: c++
144  * indent-tabs-mode: t
145  * c-basic-offset: 4
146  * tab-width: 4
147  * End:
148  * vim:noexpandtab:sw=4:ts=4:
149  */
Mutex()
Initializes the given mutex object and checks for errors.
Definition: mutex-posix.hpp:61
Dummy implementation of a mutex.
Definition: mutex-none.hpp:33
pthread_mutexattr_t _attr
Definition: mutex-posix.hpp:41
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
~Mutex()
Destroys the given mutex object and checks for errors.
Definition: mutex-posix.hpp:86
Dummy condition variable.
pthread_mutex_t _mutex
Definition: mutex-posix.hpp:40
void unlock()
Unlocks the given mutex object and checks for errors.
Definition: mutex-none.hpp:36
void lock()
Locks the given mutex object and checks for errors.
Definition: mutex-none.hpp:35