; doc/emacs/misc.texi (Network Security): Fix typo.
[emacs.git] / src / systhread.h
blob3805cb261f1fd0feabac4927bc70f26fe5aa3de5
1 /* System thread definitions
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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/>. */
19 #ifndef SYSTHREAD_H
20 #define SYSTHREAD_H
22 #include <stdbool.h>
24 #ifndef __has_attribute
25 # define __has_attribute(a) false
26 #endif
28 #if __has_attribute (__warn_unused_result__)
29 # define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
30 #else
31 # define ATTRIBUTE_WARN_UNUSED_RESULT
32 #endif
34 #ifdef THREADS_ENABLED
36 #ifdef HAVE_PTHREAD
38 #include <pthread.h>
40 /* A system mutex is just a pthread mutex. This is only used for the
41 GIL. */
42 typedef pthread_mutex_t sys_mutex_t;
44 typedef pthread_cond_t sys_cond_t;
46 /* A system thread. */
47 typedef pthread_t sys_thread_t;
49 #else /* HAVE_PTHREAD */
51 #ifdef WINDOWSNT
53 /* This header is indirectly included in every source file. We don't
54 want to include windows.h in every source file, so we repeat
55 declarations of the few necessary data types here (under different
56 names, to avoid conflicts with files that do include
57 windows.h). */
59 typedef struct {
60 struct _CRITICAL_SECTION_DEBUG *DebugInfo;
61 long LockCount;
62 long RecursionCount;
63 void *OwningThread;
64 void *LockSemaphore;
65 unsigned long SpinCount;
66 } w32thread_critsect;
68 enum { CONDV_SIGNAL = 0, CONDV_BROADCAST = 1, CONDV_MAX = 2 };
70 typedef struct {
71 /* Count of threads that are waiting for this condition variable. */
72 unsigned wait_count;
73 /* Critical section to protect changes to the count above. */
74 w32thread_critsect wait_count_lock;
75 /* Handles of events used for signal and broadcast. */
76 void *events[CONDV_MAX];
77 bool initialized;
78 } w32thread_cond_t;
80 typedef w32thread_critsect sys_mutex_t;
82 typedef w32thread_cond_t sys_cond_t;
84 typedef unsigned long sys_thread_t;
86 #else /* !WINDOWSNT */
88 #error port me
90 #endif /* WINDOWSNT */
91 #endif /* HAVE_PTHREAD */
93 #else /* THREADS_ENABLED */
95 /* For the no-threads case we can simply use dummy definitions. */
96 typedef int sys_mutex_t;
97 typedef int sys_cond_t;
98 typedef int sys_thread_t;
100 #endif /* THREADS_ENABLED */
102 typedef void *(thread_creation_function) (void *);
104 extern void sys_mutex_init (sys_mutex_t *);
105 extern void sys_mutex_lock (sys_mutex_t *);
106 extern void sys_mutex_unlock (sys_mutex_t *);
108 extern void sys_cond_init (sys_cond_t *);
109 extern void sys_cond_wait (sys_cond_t *, sys_mutex_t *);
110 extern void sys_cond_signal (sys_cond_t *);
111 extern void sys_cond_broadcast (sys_cond_t *);
112 extern void sys_cond_destroy (sys_cond_t *);
114 extern sys_thread_t sys_thread_self (void)
115 ATTRIBUTE_WARN_UNUSED_RESULT;
116 extern bool sys_thread_equal (sys_thread_t, sys_thread_t)
117 ATTRIBUTE_WARN_UNUSED_RESULT;
119 extern bool sys_thread_create (sys_thread_t *, const char *,
120 thread_creation_function *, void *)
121 ATTRIBUTE_WARN_UNUSED_RESULT;
123 extern void sys_thread_yield (void);
125 #endif /* SYSTHREAD_H */