1 //===-- Implementation of atexit ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdlib/atexit.h"
10 #include "src/__support/CPP/blockstore.h"
11 #include "src/__support/common.h"
12 #include "src/__support/threads/mutex.h"
13 
14 namespace __llvm_libc {
15 
16 namespace {
17 
18 Mutex handler_list_mtx(false, false, false);
19 
20 using AtExitCallback = void(void);
21 using ExitCallbackList = cpp::ReverseOrderBlockStore<AtExitCallback *, 32>;
22 constinit ExitCallbackList exit_callbacks;
23 
24 } // namespace
25 
26 namespace internal {
27 
call_exit_callbacks()28 void call_exit_callbacks() {
29   handler_list_mtx.lock();
30   while (!exit_callbacks.empty()) {
31     auto *callback = exit_callbacks.back();
32     exit_callbacks.pop_back();
33     handler_list_mtx.unlock();
34     callback();
35     handler_list_mtx.lock();
36   }
37   ExitCallbackList::destroy(&exit_callbacks);
38 }
39 
40 } // namespace internal
41 
42 LLVM_LIBC_FUNCTION(int, atexit, (AtExitCallback * callback)) {
43   handler_list_mtx.lock();
44   exit_callbacks.push_back(callback);
45   handler_list_mtx.unlock();
46   return 0;
47 }
48 
49 } // namespace __llvm_libc
50