1 /* System thread definitions
2 Copyright (C) 2012-2023 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs 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 GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
18
19 #ifndef SYSTHREAD_H
20 #define SYSTHREAD_H
21
22 #include <attribute.h>
23
24 #ifdef THREADS_ENABLED
25
26 #ifdef HAVE_PTHREAD
27
28 #include <pthread.h>
29
30 /* A system mutex is just a pthread mutex. This is only used for the
31 GIL. */
32 typedef pthread_mutex_t sys_mutex_t;
33
34 typedef pthread_cond_t sys_cond_t;
35
36 /* A system thread. */
37 typedef pthread_t sys_thread_t;
38
39 #else /* HAVE_PTHREAD */
40
41 #ifdef WINDOWSNT
42
43 /* This header is indirectly included in every source file. We don't
44 want to include windows.h in every source file, so we repeat
45 declarations of the few necessary data types here (under different
46 names, to avoid conflicts with files that do include
47 windows.h). */
48
49 typedef struct {
50 struct _CRITICAL_SECTION_DEBUG *DebugInfo;
51 long LockCount;
52 long RecursionCount;
53 void *OwningThread;
54 void *LockSemaphore;
55 unsigned long SpinCount;
56 } w32thread_critsect;
57
58 enum { CONDV_SIGNAL = 0, CONDV_BROADCAST = 1, CONDV_MAX = 2 };
59
60 typedef struct {
61 /* Count of threads that are waiting for this condition variable. */
62 unsigned wait_count;
63 /* Critical section to protect changes to the count above. */
64 w32thread_critsect wait_count_lock;
65 /* Handles of events used for signal and broadcast. */
66 void *events[CONDV_MAX];
67 bool initialized;
68 } w32thread_cond_t;
69
70 typedef w32thread_critsect sys_mutex_t;
71
72 typedef w32thread_cond_t sys_cond_t;
73
74 typedef unsigned long sys_thread_t;
75
76 #else /* !WINDOWSNT */
77
78 #error port me
79
80 #endif /* WINDOWSNT */
81 #endif /* HAVE_PTHREAD */
82
83 #else /* THREADS_ENABLED */
84
85 /* For the no-threads case we can simply use dummy definitions. */
86 typedef int sys_mutex_t;
87 typedef int sys_cond_t;
88 typedef int sys_thread_t;
89
90 #endif /* THREADS_ENABLED */
91
92 typedef void *(thread_creation_function) (void *);
93
94 extern void sys_mutex_init (sys_mutex_t *);
95 extern void sys_mutex_lock (sys_mutex_t *);
96 extern void sys_mutex_unlock (sys_mutex_t *);
97
98 extern void sys_cond_init (sys_cond_t *);
99 extern void sys_cond_wait (sys_cond_t *, sys_mutex_t *);
100 extern void sys_cond_signal (sys_cond_t *);
101 extern void sys_cond_broadcast (sys_cond_t *);
102 extern void sys_cond_destroy (sys_cond_t *);
103
104 NODISCARD extern sys_thread_t sys_thread_self (void);
105 NODISCARD extern bool sys_thread_equal (sys_thread_t, sys_thread_t);
106
107 NODISCARD extern bool sys_thread_create (sys_thread_t *,
108 thread_creation_function *, void *);
109
110 extern void sys_thread_yield (void);
111 extern void sys_thread_set_name (const char *);
112
113 #endif /* SYSTHREAD_H */