9
9
#include " src/stdlib/atexit.h"
10
10
#include " src/__support/CPP/vector.h"
11
11
#include " src/__support/common.h"
12
- #include " src/threads/mtx_init.h"
13
- #include " src/threads/mtx_lock.h"
14
- #include " src/threads/mtx_unlock.h"
12
+ #include " src/__support/threads/mutex.h"
15
13
16
14
namespace __llvm_libc {
17
15
18
16
namespace {
19
17
20
- mtx_t lock;
21
- // TODO need an easier way to use mtx_t internally, or use pthread_mutex_t
22
- // with PTHREAD_MUTEX_INITIALIZER when it lands.
23
- struct Init {
24
- Init () { __llvm_libc::mtx_init (&lock, mtx_plain); }
25
- } init;
18
+ Mutex handler_list_mtx (false , false , false );
26
19
27
20
// TOOD should we make cpp::vector like llvm::SmallVector<T, N> where it will
28
21
// allocate at least N before needing dynamic allocation?
@@ -33,21 +26,21 @@ static cpp::vector<void (*)(void)> handlers;
33
26
namespace internal {
34
27
35
28
void call_exit_handlers () {
36
- __llvm_libc::mtx_lock (& lock);
29
+ handler_list_mtx. lock ( );
37
30
// TODO: implement rbegin() + rend() for cpp::vector
38
31
for (int i = handlers.size () - 1 ; i >= 0 ; i--) {
39
- __llvm_libc::mtx_unlock (&lock );
32
+ handler_list_mtx. unlock ( );
40
33
handlers[i]();
41
- __llvm_libc::mtx_lock (& lock);
34
+ handler_list_mtx. lock ( );
42
35
}
43
36
}
44
37
45
38
} // namespace internal
46
39
47
40
LLVM_LIBC_FUNCTION (int , atexit, (void (*function)())) {
48
- __llvm_libc::mtx_lock (& lock);
41
+ handler_list_mtx. lock ( );
49
42
handlers.push_back (function);
50
- __llvm_libc::mtx_unlock (&lock );
43
+ handler_list_mtx. unlock ( );
51
44
return 0 ;
52
45
}
53
46
0 commit comments