WG14 threadsafe signals
Loading...
Searching...
No Matches
current_thread_id.h
Go to the documentation of this file.
1/* Proposed WG14 improved signals support
2(C) 2024 Niall Douglas <http://www.nedproductions.biz/>
3File Created: Nov 2024
4
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License in the accompanying file
9Licence.txt or at
10
11http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18*/
19
20#ifndef WG14_SIGNALS_GET_TID_H
21#define WG14_SIGNALS_GET_TID_H
22
23#include "config.h"
24
25#include <stdint.h>
26
27#ifdef __cplusplus
28extern "C"
29{
30#endif
31
33 typedef uintptr_t WG14_SIGNALS_PREFIX(thread_id_t);
34
35 static const WG14_SIGNALS_PREFIX(thread_id_t)
36 WG14_SIGNALS_PREFIX(thread_id_t_tombstone) = 0;
37
38#if WG14_SIGNALS_HAVE_ASYNC_SAFE_THREAD_LOCAL
39// If this platform has async signal safe thread locals, we can cache the thread
40// id in an inlineable variable.
41#ifdef _WIN32
42 // On Windows thread local vars can't be dllexported, so cache per DLL
44#elif WG14_SIGNALS_ENABLE_HEADER_ONLY
47#else
49#endif
50 WG14_SIGNALS_ASYNC_SAFE_THREAD_LOCAL WG14_SIGNALS_PREFIX(thread_id_t)
51 WG14_SIGNALS_PREFIX(current_thread_id_cached);
52#endif
53
54 WG14_SIGNALS_EXTERN WG14_SIGNALS_PREFIX(thread_id_t)
55 WG14_SIGNALS_PREFIX(internal_current_thread_id_cached_set)(void);
56
58 static WG14_SIGNALS_INLINE WG14_SIGNALS_PREFIX(thread_id_t)
59 WG14_SIGNALS_PREFIX(current_thread_id)(void)
60 {
61#if WG14_SIGNALS_HAVE_ASYNC_SAFE_THREAD_LOCAL
62 if(WG14_SIGNALS_PREFIX(current_thread_id_cached) ==
63 WG14_SIGNALS_PREFIX(thread_id_t_tombstone))
64 {
65 WG14_SIGNALS_PREFIX(current_thread_id_cached) =
66 WG14_SIGNALS_PREFIX(internal_current_thread_id_cached_set)();
67 }
68 return WG14_SIGNALS_PREFIX(current_thread_id_cached);
69#else
70 return WG14_SIGNALS_PREFIX(internal_current_thread_id_cached_set)();
71#endif
72 }
73
74#ifdef __cplusplus
75}
76#endif
77
78#if WG14_SIGNALS_ENABLE_HEADER_ONLY
79#include "detail/impl/current_thread_id.c.ipp"
80#endif
81
82#endif
#define WG14_SIGNALS_INLINE
Definition config.h:30
#define WG14_SIGNALS_EXTERN
Definition config.h:113
#define WG14_SIGNALS_EXTERN_IMPL
Definition config.h:105
#define WG14_SIGNALS_IGNORE_MULTIPLE_DEFINITIONS
Definition config.h:81
uintptr_t thread_id_t
The type of a thread id.
thread_id_t internal_current_thread_id_cached_set(void)