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/vector.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 // TOOD should we make cpp::vector like llvm::SmallVector<T, N> where it will 21 // allocate at least N before needing dynamic allocation? 22 static cpp::vector<void (*)(void)> handlers; 23 24 } // namespace 25 26 namespace internal { 27 28 void call_exit_handlers() { 29 handler_list_mtx.lock(); 30 // TODO: implement rbegin() + rend() for cpp::vector 31 for (int i = handlers.size() - 1; i >= 0; i--) { 32 handler_list_mtx.unlock(); 33 handlers[i](); 34 handler_list_mtx.lock(); 35 } 36 } 37 38 } // namespace internal 39 40 LLVM_LIBC_FUNCTION(int, atexit, (void (*function)())) { 41 handler_list_mtx.lock(); 42 handlers.push_back(function); 43 handler_list_mtx.unlock(); 44 return 0; 45 } 46 47 } // namespace __llvm_libc 48