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