µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
atexit.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2015-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#if defined(__clang__)
14#pragma clang diagnostic ignored "-Wempty-translation-unit"
15#endif
16
17// ----------------------------------------------------------------------------
18
19#if defined(__ARM_EABI__)
20
21// ----------------------------------------------------------------------------
22
23#if defined(OS_USE_OS_APP_CONFIG_H)
24#include <cmsis-plus/os-app-config.h>
25#endif
26
27#include <cmsis-plus/rtos/os.h>
29
30#include <stdlib.h>
31#include <assert.h>
32
33#include "atexit.h"
34
35// ----------------------------------------------------------------------------
36
37#if defined(OS_INCLUDE_ATEXIT_STATIC) && !defined(__EXCEPTIONS)
38
67int
68atexit (exit_func_t fn)
69{
70#if defined(OS_TRACE_LIBC_ATEXIT)
71 trace_printf ("%s(%p)\n", __func__, fn);
72#endif
73
74 return __register_exitproc (__et_atexit, fn, NULL, NULL);
75}
76
77// ----------------------------------------------------------------------------
78
79#if !defined(OS_INTEGER_ATEXIT_ARRAY_SIZE)
80// Due to an odd behaviour, destructors for main and idle are
81// called via atexit().
82#define OS_INTEGER_ATEXIT_ARRAY_SIZE (3)
83#endif
84
88size_t __atexit_count;
89
98
112int
113__register_exitproc (int type, exit_func_t fn,
114 void *arg __attribute__((unused)),
115 void *d __attribute__((unused)))
116{
117 assert(type == __et_atexit);
118 assert(__atexit_count < OS_INTEGER_ATEXIT_ARRAY_SIZE);
119
120#if defined(NDEBUG)
121 if ((type != __et_atexit) || (__atexit_count >= OS_INTEGER_ATEXIT_ARRAY_SIZE))
122 {
123 return -1;
124 }
125#endif
126
127 // Use scheduler lock to synchronise access to the array.
129
130 __atexit_functions[__atexit_count++] = fn;
131 return 0;
132}
133
134// ----------------------------------------------------------------------------
135
136void
137__call_exitprocs (int code __attribute__((unused)),
138 void* d __attribute__((unused)))
139{
140 trace_printf("%s()\n", __func__);
141
142 // Call registered functions in reverse order.
143 for (size_t i = __atexit_count; i > 0;)
144 {
145 __atexit_functions[--i] ();
146 }
147}
148
149#endif /* !defined(__EXCEPTIONS) */
150
151// ----------------------------------------------------------------------------
152
153#endif /* defined(__ARM_EABI__) */
void(* exit_func_t)(void)
Definition atexit.h:41
void __call_exitprocs(int, void *)
@ __et_atexit
Definition atexit.h:32
int __register_exitproc(int, exit_func_t fn, void *, void *)
Scheduler critical section RAII helper.
Definition os-sched.h:170
#define OS_INTEGER_ATEXIT_ARRAY_SIZE
Define the size of the atexit() array.
Single file µOS++ RTOS definitions.
int trace_printf(const char *format,...)