Skip to content

Commit 83b8878

Browse files
author
Siva Chandra Reddy
committed
[libc] Use the constexpr constructor to initialize exit handlers mutex.
Reviewed By: abrachet Differential Revision: https://reviews.llvm.org/D121334
1 parent 3cb9af1 commit 83b8878

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

libc/src/stdlib/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ add_entrypoint_object(
278278
atexit.h
279279
DEPENDS
280280
libc.src.__support.CPP.vector
281-
libc.src.threads.mtx_init
282-
libc.src.threads.mtx_lock
283-
libc.src.threads.mtx_unlock
281+
libc.src.__support.threads.thread
284282
)
285283

286284
add_entrypoint_object(

libc/src/stdlib/atexit.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@
99
#include "src/stdlib/atexit.h"
1010
#include "src/__support/CPP/vector.h"
1111
#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"
1513

1614
namespace __llvm_libc {
1715

1816
namespace {
1917

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);
2619

2720
// TOOD should we make cpp::vector like llvm::SmallVector<T, N> where it will
2821
// allocate at least N before needing dynamic allocation?
@@ -33,21 +26,21 @@ static cpp::vector<void (*)(void)> handlers;
3326
namespace internal {
3427

3528
void call_exit_handlers() {
36-
__llvm_libc::mtx_lock(&lock);
29+
handler_list_mtx.lock();
3730
// TODO: implement rbegin() + rend() for cpp::vector
3831
for (int i = handlers.size() - 1; i >= 0; i--) {
39-
__llvm_libc::mtx_unlock(&lock);
32+
handler_list_mtx.unlock();
4033
handlers[i]();
41-
__llvm_libc::mtx_lock(&lock);
34+
handler_list_mtx.lock();
4235
}
4336
}
4437

4538
} // namespace internal
4639

4740
LLVM_LIBC_FUNCTION(int, atexit, (void (*function)())) {
48-
__llvm_libc::mtx_lock(&lock);
41+
handler_list_mtx.lock();
4942
handlers.push_back(function);
50-
__llvm_libc::mtx_unlock(&lock);
43+
handler_list_mtx.unlock();
5144
return 0;
5245
}
5346

0 commit comments

Comments
 (0)