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