-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Often atexit is implemented using __cxa_atexit. I have not implemented __cxa_atexit here because it potentially requires more discussion. It is unique for llvm-libc (I think) that it is an exported symbol that wouldn’t be defined in any spec file because it doesn’t have a header. Implementing it will be trivial given what is here already, but I figured it would be more contentious so it can be implemented later. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D119512
- Loading branch information
Showing
15 changed files
with
307 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===-- Definition of type __atexithandler_t ------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __LLVM_LIBC_TYPES_ATEXITHANDLER_T_H__ | ||
#define __LLVM_LIBC_TYPES_ATEXITHANDLER_T_H__ | ||
|
||
typedef void (*__atexithandler_t)(void); | ||
|
||
#endif // __LLVM_LIBC_TYPES_ATEXITHANDLER_T_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===-- Implementation of atexit ------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/stdlib/atexit.h" | ||
#include "src/__support/CPP/vector.h" | ||
#include "src/__support/common.h" | ||
#include "src/threads/mtx_init.h" | ||
#include "src/threads/mtx_lock.h" | ||
#include "src/threads/mtx_unlock.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
namespace { | ||
|
||
mtx_t lock; | ||
// TODO need an easier way to use mtx_t internally, or use pthread_mutex_t | ||
// with PTHREAD_MUTEX_INITIALIZER when it lands. | ||
struct Init { | ||
Init() { __llvm_libc::mtx_init(&lock, mtx_plain); } | ||
} init; | ||
|
||
// TOOD should we make cpp::vector like llvm::SmallVector<T, N> where it will | ||
// allocate at least N before needing dynamic allocation? | ||
static cpp::vector<void (*)(void)> handlers; | ||
|
||
} // namespace | ||
|
||
namespace internal { | ||
|
||
void call_exit_handlers() { | ||
__llvm_libc::mtx_lock(&lock); | ||
// TODO: implement rbegin() + rend() for cpp::vector | ||
for (int i = handlers.size() - 1; i >= 0; i--) { | ||
__llvm_libc::mtx_unlock(&lock); | ||
handlers[i](); | ||
__llvm_libc::mtx_lock(&lock); | ||
} | ||
} | ||
|
||
} // namespace internal | ||
|
||
LLVM_LIBC_FUNCTION(int, atexit, (void (*function)())) { | ||
__llvm_libc::mtx_lock(&lock); | ||
handlers.push_back(function); | ||
__llvm_libc::mtx_unlock(&lock); | ||
return 0; | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===-- Implementation header for atexit ------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_STDLIB_ATEXIT_H | ||
#define LLVM_LIBC_SRC_STDLIB_ATEXIT_H | ||
|
||
namespace __llvm_libc { | ||
|
||
int atexit(void (*function)()); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_STDLIB_ATEXIT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===-- Implementation of exit --------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/stdlib/exit.h" | ||
#include "src/__support/common.h" | ||
#include "src/stdlib/_Exit.h" | ||
|
||
namespace __llvm_libc { | ||
|
||
namespace internal { | ||
void call_exit_handlers(); | ||
} | ||
|
||
LLVM_LIBC_FUNCTION(void, exit, (int status)) { | ||
internal::call_exit_handlers(); | ||
_Exit(status); | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for exit --------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <stdlib.h> | ||
|
||
#ifndef LLVM_LIBC_SRC_STDLIB_EXIT_H | ||
#define LLVM_LIBC_SRC_STDLIB_EXIT_H | ||
|
||
namespace __llvm_libc { | ||
|
||
void exit(int status); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_STDLIB_EXIT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.