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