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 
28 void call_exit_callbacks() {
29   handler_list_mtx.lock();
30   for (auto callback : exit_callbacks) {
31     handler_list_mtx.unlock();
32     callback();
33     handler_list_mtx.lock();
34   }
35   ExitCallbackList::destroy(&exit_callbacks);
36 }
37 
38 } // namespace internal
39 
40 LLVM_LIBC_FUNCTION(int, atexit, (AtExitCallback * callback)) {
41   handler_list_mtx.lock();
42   exit_callbacks.push_back(callback);
43   handler_list_mtx.unlock();
44   return 0;
45 }
46 
47 } // namespace __llvm_libc
48