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