blob: b22fcbbd86e2a39387dcf0184f039cb6c0631ac6 [file] [log] [blame]
Howard Hinnant4a889712011-05-24 22:01:16 +00001//===---------------------------- cxa_guard.cpp ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Nick Kledzik49cbb022011-08-02 01:18:14 +000010#include "abort_message.h"
Jonathan Roelofs40e98422014-05-06 21:30:56 +000011#include "config.h"
Howard Hinnant4a889712011-05-24 22:01:16 +000012
Jonathan Roelofs40e98422014-05-06 21:30:56 +000013#if !LIBCXXABI_SINGLE_THREADED
14# include <pthread.h>
15#endif
Howard Hinnant4a889712011-05-24 22:01:16 +000016#include <stdint.h>
Howard Hinnant5d6b9d22012-01-25 19:02:40 +000017
18/*
19 This implementation must be careful to not call code external to this file
20 which will turn around and try to call __cxa_guard_acquire reentrantly.
21 For this reason, the headers of this file are as restricted as possible.
22 Previous implementations of this code for __APPLE__ have used
23 pthread_mutex_lock and the abort_message utility without problem. This
24 implementation also uses pthread_cond_wait which has tested to not be a
25 problem.
26*/
Howard Hinnant4a889712011-05-24 22:01:16 +000027
28namespace __cxxabiv1
29{
30
31namespace
32{
33
Howard Hinnantbaae2be2012-03-14 19:30:00 +000034#if __arm__
Nick Lewycky69e35a72011-06-07 18:46:10 +000035
36// A 32-bit, 4-byte-aligned static data value. The least significant 2 bits must
37// be statically initialized to 0.
38typedef uint32_t guard_type;
39
40// Test the lowest bit.
41inline bool is_initialized(guard_type* guard_object) {
42 return (*guard_object) & 1;
43}
44
45inline void set_initialized(guard_type* guard_object) {
46 *guard_object |= 1;
47}
48
49#else
50
51typedef uint64_t guard_type;
52
53bool is_initialized(guard_type* guard_object) {
54 char* initialized = (char*)guard_object;
55 return *initialized;
56}
57
58void set_initialized(guard_type* guard_object) {
59 char* initialized = (char*)guard_object;
60 *initialized = 1;
61}
62
63#endif
64
Jonathan Roelofs40e98422014-05-06 21:30:56 +000065#if !LIBCXXABI_SINGLE_THREADED
Howard Hinnant4a889712011-05-24 22:01:16 +000066pthread_mutex_t guard_mut = PTHREAD_MUTEX_INITIALIZER;
67pthread_cond_t guard_cv = PTHREAD_COND_INITIALIZER;
Jonathan Roelofs40e98422014-05-06 21:30:56 +000068#endif
Howard Hinnant4a889712011-05-24 22:01:16 +000069
Howard Hinnantffa26662012-03-14 19:39:50 +000070#if defined(__APPLE__) && !defined(__arm__)
Howard Hinnant4a889712011-05-24 22:01:16 +000071
72typedef uint32_t lock_type;
73
74#if __LITTLE_ENDIAN__
75
76inline
77lock_type
78get_lock(uint64_t x)
79{
80 return static_cast<lock_type>(x >> 32);
81}
82
83inline
84void
85set_lock(uint64_t& x, lock_type y)
86{
87 x = static_cast<uint64_t>(y) << 32;
88}
89
90#else // __LITTLE_ENDIAN__
91
92inline
93lock_type
94get_lock(uint64_t x)
95{
96 return static_cast<lock_type>(x);
97}
98
99inline
100void
101set_lock(uint64_t& x, lock_type y)
102{
103 x = y;
104}
105
106#endif // __LITTLE_ENDIAN__
107
Howard Hinnantbaae2be2012-03-14 19:30:00 +0000108#else // !__APPLE__ || __arm__
Howard Hinnant4a889712011-05-24 22:01:16 +0000109
110typedef bool lock_type;
111
112inline
113lock_type
114get_lock(uint64_t x)
115{
116 union
117 {
118 uint64_t guard;
119 uint8_t lock[2];
120 } f = {x};
121 return f.lock[1] != 0;
122}
123
124inline
125void
126set_lock(uint64_t& x, lock_type y)
127{
128 union
129 {
130 uint64_t guard;
131 uint8_t lock[2];
132 } f = {0};
133 f.lock[1] = y;
134 x = f.guard;
135}
136
Nick Lewycky69e35a72011-06-07 18:46:10 +0000137inline
138lock_type
139get_lock(uint32_t x)
140{
141 union
142 {
143 uint32_t guard;
144 uint8_t lock[2];
145 } f = {x};
146 return f.lock[1] != 0;
147}
148
149inline
150void
151set_lock(uint32_t& x, lock_type y)
152{
153 union
154 {
155 uint32_t guard;
156 uint8_t lock[2];
157 } f = {0};
158 f.lock[1] = y;
159 x = f.guard;
160}
161
Howard Hinnant4a889712011-05-24 22:01:16 +0000162#endif // __APPLE__
163
164} // unnamed namespace
165
166extern "C"
167{
168
Jonathan Roelofs40e98422014-05-06 21:30:56 +0000169#if LIBCXXABI_SINGLE_THREADED
Nick Lewycky69e35a72011-06-07 18:46:10 +0000170int __cxa_guard_acquire(guard_type* guard_object)
Howard Hinnant4a889712011-05-24 22:01:16 +0000171{
Jonathan Roelofs40e98422014-05-06 21:30:56 +0000172 return !is_initialized(guard_object);
173}
174
175void __cxa_guard_release(guard_type* guard_object)
176{
177 *guard_object = 0;
178 set_initialized(guard_object);
179}
180
181void __cxa_guard_abort(guard_type* guard_object)
182{
183 *guard_object = 0;
184}
185
186#else // !LIBCXXABI_SINGLE_THREADED
187
188int __cxa_guard_acquire(guard_type* guard_object)
189{
Howard Hinnant4a889712011-05-24 22:01:16 +0000190 if (pthread_mutex_lock(&guard_mut))
191 abort_message("__cxa_guard_acquire failed to acquire mutex");
192 int result = *initialized == 0;
193 if (result)
194 {
Howard Hinnantffa26662012-03-14 19:39:50 +0000195#if defined(__APPLE__) && !defined(__arm__)
Howard Hinnant4a889712011-05-24 22:01:16 +0000196 const lock_type id = pthread_mach_thread_np(pthread_self());
197 lock_type lock = get_lock(*guard_object);
198 if (lock)
199 {
200 // if this thread set lock for this same guard_object, abort
201 if (lock == id)
202 abort_message("__cxa_guard_acquire detected deadlock");
203 do
204 {
205 if (pthread_cond_wait(&guard_cv, &guard_mut))
206 abort_message("__cxa_guard_acquire condition variable wait failed");
207 lock = get_lock(*guard_object);
208 } while (lock);
Nick Lewycky69e35a72011-06-07 18:46:10 +0000209 result = !is_initialized(guard_object);
Howard Hinnant4a889712011-05-24 22:01:16 +0000210 if (result)
211 set_lock(*guard_object, id);
212 }
213 else
214 set_lock(*guard_object, id);
Howard Hinnantbaae2be2012-03-14 19:30:00 +0000215#else // !__APPLE__ || __arm__
Howard Hinnant4a889712011-05-24 22:01:16 +0000216 while (get_lock(*guard_object))
217 if (pthread_cond_wait(&guard_cv, &guard_mut))
218 abort_message("__cxa_guard_acquire condition variable wait failed");
219 result = *initialized == 0;
220 if (result)
221 set_lock(*guard_object, true);
Howard Hinnantbaae2be2012-03-14 19:30:00 +0000222#endif // !__APPLE__ || __arm__
Howard Hinnant4a889712011-05-24 22:01:16 +0000223 }
224 if (pthread_mutex_unlock(&guard_mut))
225 abort_message("__cxa_guard_acquire failed to release mutex");
226 return result;
227}
228
Nick Lewycky69e35a72011-06-07 18:46:10 +0000229void __cxa_guard_release(guard_type* guard_object)
Howard Hinnant4a889712011-05-24 22:01:16 +0000230{
Howard Hinnant4a889712011-05-24 22:01:16 +0000231 if (pthread_mutex_lock(&guard_mut))
232 abort_message("__cxa_guard_release failed to acquire mutex");
233 *guard_object = 0;
Nick Lewycky69e35a72011-06-07 18:46:10 +0000234 set_initialized(guard_object);
Howard Hinnant4a889712011-05-24 22:01:16 +0000235 if (pthread_mutex_unlock(&guard_mut))
236 abort_message("__cxa_guard_release failed to release mutex");
237 if (pthread_cond_broadcast(&guard_cv))
238 abort_message("__cxa_guard_release failed to broadcast condition variable");
239}
240
Nick Lewycky69e35a72011-06-07 18:46:10 +0000241void __cxa_guard_abort(guard_type* guard_object)
Howard Hinnant4a889712011-05-24 22:01:16 +0000242{
243 if (pthread_mutex_lock(&guard_mut))
244 abort_message("__cxa_guard_abort failed to acquire mutex");
245 *guard_object = 0;
246 if (pthread_mutex_unlock(&guard_mut))
247 abort_message("__cxa_guard_abort failed to release mutex");
248 if (pthread_cond_broadcast(&guard_cv))
249 abort_message("__cxa_guard_abort failed to broadcast condition variable");
250}
251
Jonathan Roelofs40e98422014-05-06 21:30:56 +0000252#endif // !LIBCXXABI_SINGLE_THREADED
253
Howard Hinnant4a889712011-05-24 22:01:16 +0000254} // extern "C"
255
256} // __cxxabiv1