Ajla 0.1.0
[ajla.git] / tick.h
blob2b2b9d82bd12ea9ff9c12a49946f1842314050a7
1 /*
2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
9 * version.
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
19 #ifndef AJLA_TICK_H
20 #define AJLA_TICK_H
22 #if defined(HAVE_TIMER_CREATE) && defined(HAVE_STRUCT_SIGEVENT)
23 #define USE_TIMER_CREATE
24 #endif
26 /* Cygwin has bugs in signal-vs-thread interactions */
27 #if defined(HAVE_SIGNAL_H) && defined(HAVE_SETITIMER) && defined(HAVE_SIGACTION) && defined(HAVE_SIGSET_T) && !defined(__CYGWIN__)
28 #define USEABLE_SIGNAL
29 #endif
32 * If we don't have timer_create, we use setitimer. Unfortunatelly, there is no
33 * way to stop setitimer (because it is sometimes implemented incorrectly
34 * per-thread, not per-process).
36 * So we'd better fallback to a timer thread (that is stoppable), to reduce CPU
37 * consumption when we are idle.
39 #if defined(USEABLE_SIGNAL) && !defined(THREAD_NONE) && !defined(USE_TIMER_CREATE)
40 #undef USEABLE_SIGNAL
41 #endif
43 extern uint32_t tick_us;
44 extern uint32_t thread_tick;
46 typedef unsigned tick_stamp_t;
47 extern atomic_type tick_stamp_t *tick_stamp_ptr;
48 #define tick_start(state) (*(state) = load_relaxed(&tick_stamp))
49 #define tick_elapsed(state) (unlikely(*(state) != load_relaxed(&tick_stamp)))
51 void tick_suspend(void);
52 void tick_resume(void);
54 void tick_init(void);
55 void tick_done(void);
57 #endif