1 /* Thread definitions
2 Copyright (C) 2012-2021 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 THREAD_H
20 #define THREAD_H
21
22 #include "regex-emacs.h"
23
24 #ifdef WINDOWSNT
25 #include <sys/socket.h>
26 #endif
27
28 #ifdef MSDOS
29 #include <signal.h> /* sigset_t */
30 #endif
31
32 #include "sysselect.h" /* FIXME */
33 #include "systhread.h"
34
35 struct thread_state
36 {
37 union vectorlike_header header;
38
39 /* The buffer in which the last search was performed, or
40 Qt if the last search was done in a string;
41 Qnil if no searching has been done yet. */
42 Lisp_Object m_last_thing_searched;
43 #define last_thing_searched (current_thread->m_last_thing_searched)
44
45 Lisp_Object m_saved_last_thing_searched;
46 #define saved_last_thing_searched (current_thread->m_saved_last_thing_searched)
47
48 /* The thread's name. */
49 Lisp_Object name;
50
51 /* The thread's function. */
52 Lisp_Object function;
53
54 /* The thread's result, if function has finished. */
55 Lisp_Object result;
56
57 /* If non-nil, this thread has been signaled. */
58 Lisp_Object error_symbol;
59 Lisp_Object error_data;
60
61 /* If we are waiting for some event, this holds the object we are
62 waiting on. */
63 Lisp_Object event_object;
64 /* event_object must be the last Lisp field. */
65
66 /* An address near the bottom of the stack.
67 Tells GC how to save a copy of the stack. */
68 char const *m_stack_bottom;
69 #define stack_bottom (current_thread->m_stack_bottom)
70
71 /* The address of an object near the C stack top, used to determine
72 which words need to be scanned by the garbage collector. This is
73 also used to detect heuristically whether segmentation violation
74 address indicates stack overflow, as opposed to some internal
75 error in Emacs. If the C function F calls G which calls H which
76 calls ... F, then at least one of the functions in the chain
77 should set this to the address of a local variable. */
78 void const *stack_top;
79
80 struct catchtag *m_catchlist;
81 #define catchlist (current_thread->m_catchlist)
82
83 /* Chain of condition handlers currently in effect.
84 The elements of this chain are contained in the stack frames
85 of Fcondition_case and internal_condition_case.
86 When an error is signaled (by calling Fsignal),
87 this chain is searched for an element that applies. */
88 struct handler *m_handlerlist;
89 #define handlerlist (current_thread->m_handlerlist)
90
91 struct handler *m_handlerlist_sentinel;
92 #define handlerlist_sentinel (current_thread->m_handlerlist_sentinel)
93
94 /* Current number of specbindings allocated in specpdl. */
95 ptrdiff_t m_specpdl_size;
96 #define specpdl_size (current_thread->m_specpdl_size)
97
98 /* Pointer to beginning of specpdl. */
99 union specbinding *m_specpdl;
100 #define specpdl (current_thread->m_specpdl)
101
102 /* Pointer to first unused element in specpdl. */
103 union specbinding *m_specpdl_ptr;
104 #define specpdl_ptr (current_thread->m_specpdl_ptr)
105
106 /* Depth in Lisp evaluations and function calls. */
107 intmax_t m_lisp_eval_depth;
108 #define lisp_eval_depth (current_thread->m_lisp_eval_depth)
109
110 /* This points to the current buffer. */
111 struct buffer *m_current_buffer;
112 #define current_buffer (current_thread->m_current_buffer)
113
114 /* Every call to re_search, etc., must pass &search_regs as the regs
115 argument unless you can show it is unnecessary (i.e., if re_search
116 is certainly going to be called again before region-around-match
117 can be called).
118
119 Since the registers are now dynamically allocated, we need to make
120 sure not to refer to the Nth register before checking that it has
121 been allocated by checking search_regs.num_regs.
122
123 The regex code keeps track of whether it has allocated the search
124 buffer using bits in the re_pattern_buffer. This means that whenever
125 you compile a new pattern, it completely forgets whether it has
126 allocated any registers, and will allocate new registers the next
127 time you call a searching or matching function. Therefore, we need
128 to call re_set_registers after compiling a new pattern or after
129 setting the match registers, so that the regex functions will be
130 able to free or re-allocate it properly. */
131 struct re_registers m_search_regs;
132 #define search_regs (current_thread->m_search_regs)
133
134 struct re_registers m_saved_search_regs;
135 #define saved_search_regs (current_thread->m_saved_search_regs)
136
137 /* This member is different from waiting_for_input.
138 It is used to communicate to a lisp process-filter/sentinel (via the
139 function Fwaiting_for_user_input_p) whether Emacs was waiting
140 for user-input when that process-filter was called.
141 waiting_for_input cannot be used as that is by definition 0 when
142 lisp code is being evalled.
143 This is also used in record_asynch_buffer_change.
144 For that purpose, this must be 0
145 when not inside wait_reading_process_output. */
146 int m_waiting_for_user_input_p;
147 #define waiting_for_user_input_p (current_thread->m_waiting_for_user_input_p)
148
149 /* True while doing kbd input. */
150 bool m_waiting_for_input;
151 #define waiting_for_input (current_thread->m_waiting_for_input)
152
153 /* For longjmp to where kbd input is being done. This is per-thread
154 so that if more than one thread calls read_char, they don't
155 clobber each other's getcjmp, which will cause
156 quit_throw_to_read_char crash due to using a wrong stack. */
157 sys_jmp_buf m_getcjmp;
158 #define getcjmp (current_thread->m_getcjmp)
159
160 /* The OS identifier for this thread. */
161 sys_thread_t thread_id;
162
163 /* The condition variable for this thread. This is associated with
164 the global lock. This thread broadcasts to it when it exits. */
165 sys_cond_t thread_condvar;
166
167 /* This thread might be waiting for some condition. If so, this
168 points to the condition. If the thread is interrupted, the
169 interrupter should broadcast to this condition. */
170 sys_cond_t *wait_condvar;
171
172 /* Thread's name in the locale encoding. */
173 char *thread_name;
174
175 /* This thread might have released the global lock. If so, this is
176 non-zero. When a thread runs outside thread_select with this
177 flag non-zero, it means it has been interrupted by SIGINT while
178 in thread_select, and didn't have a chance of acquiring the lock.
179 It must do so ASAP. */
180 int not_holding_lock;
181
182 /* Threads are kept on a linked list. */
183 struct thread_state *next_thread;
184 } GCALIGNED_STRUCT;
185
186 INLINE bool
THREADP(Lisp_Object a)187 THREADP (Lisp_Object a)
188 {
189 return PSEUDOVECTORP (a, PVEC_THREAD);
190 }
191
192 INLINE void
CHECK_THREAD(Lisp_Object x)193 CHECK_THREAD (Lisp_Object x)
194 {
195 CHECK_TYPE (THREADP (x), Qthreadp, x);
196 }
197
198 INLINE struct thread_state *
XTHREAD(Lisp_Object a)199 XTHREAD (Lisp_Object a)
200 {
201 eassert (THREADP (a));
202 return XUNTAG (a, Lisp_Vectorlike, struct thread_state);
203 }
204
205 /* A mutex in lisp is represented by a system condition variable.
206 The system mutex associated with this condition variable is the
207 global lock.
208
209 Using a condition variable lets us implement interruptibility for
210 lisp mutexes. */
211 typedef struct
212 {
213 /* The owning thread, or NULL if unlocked. */
214 struct thread_state *owner;
215 /* The lock count. */
216 unsigned int count;
217 /* The underlying system condition variable. */
218 sys_cond_t condition;
219 } lisp_mutex_t;
220
221 /* A mutex as a lisp object. */
222 struct Lisp_Mutex
223 {
224 union vectorlike_header header;
225
226 /* The name of the mutex, or nil. */
227 Lisp_Object name;
228
229 /* The lower-level mutex object. */
230 lisp_mutex_t mutex;
231 } GCALIGNED_STRUCT;
232
233 INLINE bool
MUTEXP(Lisp_Object a)234 MUTEXP (Lisp_Object a)
235 {
236 return PSEUDOVECTORP (a, PVEC_MUTEX);
237 }
238
239 INLINE void
CHECK_MUTEX(Lisp_Object x)240 CHECK_MUTEX (Lisp_Object x)
241 {
242 CHECK_TYPE (MUTEXP (x), Qmutexp, x);
243 }
244
245 INLINE struct Lisp_Mutex *
XMUTEX(Lisp_Object a)246 XMUTEX (Lisp_Object a)
247 {
248 eassert (MUTEXP (a));
249 return XUNTAG (a, Lisp_Vectorlike, struct Lisp_Mutex);
250 }
251
252 /* A condition variable as a lisp object. */
253 struct Lisp_CondVar
254 {
255 union vectorlike_header header;
256
257 /* The associated mutex. */
258 Lisp_Object mutex;
259
260 /* The name of the condition variable, or nil. */
261 Lisp_Object name;
262
263 /* The lower-level condition variable object. */
264 sys_cond_t cond;
265 } GCALIGNED_STRUCT;
266
267 INLINE bool
CONDVARP(Lisp_Object a)268 CONDVARP (Lisp_Object a)
269 {
270 return PSEUDOVECTORP (a, PVEC_CONDVAR);
271 }
272
273 INLINE void
CHECK_CONDVAR(Lisp_Object x)274 CHECK_CONDVAR (Lisp_Object x)
275 {
276 CHECK_TYPE (CONDVARP (x), Qcondition_variable_p, x);
277 }
278
279 INLINE struct Lisp_CondVar *
XCONDVAR(Lisp_Object a)280 XCONDVAR (Lisp_Object a)
281 {
282 eassert (CONDVARP (a));
283 return XUNTAG (a, Lisp_Vectorlike, struct Lisp_CondVar);
284 }
285
286 extern struct thread_state *current_thread;
287
288 extern void finalize_one_thread (struct thread_state *state);
289 extern void finalize_one_mutex (struct Lisp_Mutex *);
290 extern void finalize_one_condvar (struct Lisp_CondVar *);
291 extern void maybe_reacquire_global_lock (void);
292
293 extern void init_threads (void);
294 extern void syms_of_threads (void);
295 extern bool main_thread_p (const void *);
296 extern bool in_current_thread (void);
297
298 typedef int select_func (int, fd_set *, fd_set *, fd_set *,
299 const struct timespec *, const sigset_t *);
300
301 int thread_select (select_func *func, int max_fds, fd_set *rfds,
302 fd_set *wfds, fd_set *efds, struct timespec *timeout,
303 sigset_t *sigmask);
304
305 bool thread_check_current_buffer (struct buffer *);
306
307 #endif /* THREAD_H */
308