]> git.draconx.ca Git - cdecl99.git/blob - src/thread-w32.h
fb3bdc40870e4ee005185215a429bfdafc3d856e
[cdecl99.git] / src / thread-w32.h
1 /*
2  * Copyright © 2024 Nick Bowler
3  *
4  * Helpers for hosts using native Windows threading API.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #define WIN32_LEAN_AND_MEAN
21 #include <windows.h>
22 #include "windows-once.h"
23
24 static DWORD tls_key;
25 #define tls_key_valid (tls_key != TLS_OUT_OF_INDEXES)
26
27 static void init_once_cb(void);
28
29 static void *tls_get(void)
30 {
31         return TlsGetValue(tls_key);
32 }
33
34 static BOOL tls_set(void *p)
35 {
36         return TlsSetValue(tls_key, p);
37 }
38
39 static void init_once_with_tls(void)
40 {
41         tls_key = TlsAlloc();
42         init_once_cb();
43 }
44
45 static int init_once(void)
46 {
47         static glwthread_once_t ctrl = GLWTHREAD_ONCE_INIT;
48
49         glwthread_once(&ctrl, init_once_with_tls);
50         return 1;
51 }