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
40 // thread id in an inlineable variable. current_thread_id_cached is
41 // deliberately a single symbol shared across ALL translation units of the
42 // program, not a per-TU copy: it holds the one cached current thread id,
43 // written by internal_current_thread_id_cached_set() and read by
44 // current_thread_id(). The declaration below is extern first and the
45 // definition lives separately in current_thread_id.c.ipp; in header-only mode
46 // every TU provides a weak/selectany definition that the linker merges into
47 // one. (Variables cannot be dllexported, so the symbol is not exported from
48 // Windows DLLs; WG14_SIGNALS_DEFAULT_VISIBILITY only affects non-Windows
49 // shared libraries, keeping the symbol visible under -fvisibility=hidden.)
51#if defined(_WIN32) || WG14_SIGNALS_ENABLE_HEADER_ONLY
52 // Weak/selectany: on Windows thread local vars can't be dllexported, so
53 // cache per DLL; in header-only mode every TU provides a definition that the
54 // linker merges into one.
56#endif
57 WG14_SIGNALS_ASYNC_SAFE_THREAD_LOCAL WG14_SIGNALS_PREFIX(thread_id_t)
58 WG14_SIGNALS_PREFIX(current_thread_id_cached);
59#endif
60
61 WG14_SIGNALS_EXTERN WG14_SIGNALS_PREFIX(thread_id_t)
62 WG14_SIGNALS_PREFIX(internal_current_thread_id_cached_set)(void);
63
65 static WG14_SIGNALS_INLINE WG14_SIGNALS_PREFIX(thread_id_t)
66 WG14_SIGNALS_PREFIX(current_thread_id)(void)
67 {
68#if WG14_SIGNALS_HAVE_ASYNC_SAFE_THREAD_LOCAL
69 if(WG14_SIGNALS_PREFIX(current_thread_id_cached) ==
70 WG14_SIGNALS_PREFIX(thread_id_t_tombstone))
71 {
72 WG14_SIGNALS_PREFIX(current_thread_id_cached) =
73 WG14_SIGNALS_PREFIX(internal_current_thread_id_cached_set)();
74 }
75 return WG14_SIGNALS_PREFIX(current_thread_id_cached);
76#else
77 return WG14_SIGNALS_PREFIX(internal_current_thread_id_cached_set)();
78#endif
79 }
80
81#ifdef __cplusplus
82}
83#endif
84
85#if WG14_SIGNALS_ENABLE_HEADER_ONLY
86#include "detail/impl/current_thread_id.c.ipp"
87#endif
88
89#endif
#define WG14_SIGNALS_INLINE
Definition config.h:30
#define WG14_SIGNALS_DEFAULT_VISIBILITY
Definition config.h:99
#define WG14_SIGNALS_EXTERN
Definition config.h:128
#define WG14_SIGNALS_IGNORE_MULTIPLE_DEFINITIONS
Definition config.h:91
uintptr_t thread_id_t
The type of a thread id.
thread_id_t internal_current_thread_id_cached_set(void)