merge from trunk
[emacs.git] / src / thread.h
blobe77d1144ecf6df59dd898b19015638a664019719
1 /* Thread definitions
2 Copyright (C) 2012, 2013 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 <http://www.gnu.org/licenses/>. */
19 #ifndef THREAD_H
20 #define THREAD_H
22 #include "regex.h"
24 #include "sysselect.h" /* FIXME */
25 #include "systime.h" /* FIXME */
27 struct thread_state
29 struct vectorlike_header header;
31 /* The buffer in which the last search was performed, or
32 Qt if the last search was done in a string;
33 Qnil if no searching has been done yet. */
34 Lisp_Object m_last_thing_searched;
35 #define last_thing_searched (current_thread->m_last_thing_searched)
37 Lisp_Object m_saved_last_thing_searched;
38 #define saved_last_thing_searched (current_thread->m_saved_last_thing_searched)
40 /* The thread's name. */
41 Lisp_Object name;
43 /* The thread's function. */
44 Lisp_Object function;
46 /* If non-nil, this thread has been signalled. */
47 Lisp_Object error_symbol;
48 Lisp_Object error_data;
50 /* If we are waiting for some event, this holds the object we are
51 waiting on. */
52 Lisp_Object event_object;
54 /* m_gcprolist must be the first non-lisp field. */
55 /* Recording what needs to be marked for gc. */
56 struct gcpro *m_gcprolist;
57 #define gcprolist (current_thread->m_gcprolist)
59 /* A list of currently active byte-code execution value stacks.
60 Fbyte_code adds an entry to the head of this list before it starts
61 processing byte-code, and it removed the entry again when it is
62 done. Signalling an error truncates the list analoguous to
63 gcprolist. */
64 struct byte_stack *m_byte_stack_list;
65 #define byte_stack_list (current_thread->m_byte_stack_list)
67 /* An address near the bottom of the stack.
68 Tells GC how to save a copy of the stack. */
69 char *m_stack_bottom;
70 #define stack_bottom (current_thread->m_stack_bottom)
72 /* An address near the top of the stack. */
73 char *stack_top;
75 struct catchtag *m_catchlist;
76 #define catchlist (current_thread->m_catchlist)
78 /* Chain of condition handlers currently in effect.
79 The elements of this chain are contained in the stack frames
80 of Fcondition_case and internal_condition_case.
81 When an error is signaled (by calling Fsignal, below),
82 this chain is searched for an element that applies. */
83 struct handler *m_handlerlist;
84 #define handlerlist (current_thread->m_handlerlist)
86 /* Count levels of GCPRO to detect failure to UNGCPRO. */
87 int m_gcpro_level;
88 #define gcpro_level (current_thread->m_gcpro_level)
90 /* Current number of specbindings allocated in specpdl. */
91 ptrdiff_t m_specpdl_size;
92 #define specpdl_size (current_thread->m_specpdl_size)
94 /* Pointer to beginning of specpdl. */
95 union specbinding *m_specpdl;
96 #define specpdl (current_thread->m_specpdl)
98 /* Pointer to first unused element in specpdl. */
99 union specbinding *m_specpdl_ptr;
100 #define specpdl_ptr (current_thread->m_specpdl_ptr)
102 /* Depth in Lisp evaluations and function calls. */
103 EMACS_INT m_lisp_eval_depth;
104 #define lisp_eval_depth (current_thread->m_lisp_eval_depth)
106 /* This points to the current buffer. */
107 struct buffer *m_current_buffer;
108 #define current_buffer (current_thread->m_current_buffer)
110 /* Every call to re_match, etc., must pass &search_regs as the regs
111 argument unless you can show it is unnecessary (i.e., if re_match
112 is certainly going to be called again before region-around-match
113 can be called).
115 Since the registers are now dynamically allocated, we need to make
116 sure not to refer to the Nth register before checking that it has
117 been allocated by checking search_regs.num_regs.
119 The regex code keeps track of whether it has allocated the search
120 buffer using bits in the re_pattern_buffer. This means that whenever
121 you compile a new pattern, it completely forgets whether it has
122 allocated any registers, and will allocate new registers the next
123 time you call a searching or matching function. Therefore, we need
124 to call re_set_registers after compiling a new pattern or after
125 setting the match registers, so that the regex functions will be
126 able to free or re-allocate it properly. */
127 struct re_registers m_search_regs;
128 #define search_regs (current_thread->m_search_regs)
130 /* If non-zero the match data have been saved in saved_search_regs
131 during the execution of a sentinel or filter. */
132 bool m_search_regs_saved;
133 #define search_regs_saved (current_thread->m_search_regs_saved)
135 struct re_registers m_saved_search_regs;
136 #define saved_search_regs (current_thread->m_saved_search_regs)
138 /* This is the string or buffer in which we
139 are matching. It is used for looking up syntax properties. */
140 Lisp_Object m_re_match_object;
141 #define re_match_object (current_thread->m_re_match_object)
143 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
144 also be assigned to arbitrarily: each pattern buffer stores its own
145 syntax, so it can be changed between regex compilations. */
146 reg_syntax_t m_re_syntax_options;
147 #define re_syntax_options (current_thread->m_re_syntax_options)
149 /* Regexp to use to replace spaces, or NULL meaning don't. */
150 /*re_char*/ unsigned char *m_whitespace_regexp;
151 #define whitespace_regexp (current_thread->m_whitespace_regexp)
153 /* This variable is different from waiting_for_input in keyboard.c.
154 It is used to communicate to a lisp process-filter/sentinel (via the
155 function Fwaiting_for_user_input_p) whether Emacs was waiting
156 for user-input when that process-filter was called.
157 waiting_for_input cannot be used as that is by definition 0 when
158 lisp code is being evalled.
159 This is also used in record_asynch_buffer_change.
160 For that purpose, this must be 0
161 when not inside wait_reading_process_output. */
162 int m_waiting_for_user_input_p;
163 #define waiting_for_user_input_p (current_thread->m_waiting_for_user_input_p)
165 /* The OS identifier for this thread. */
166 sys_thread_t thread_id;
168 /* The condition variable for this thread. This is associated with
169 the global lock. This thread broadcasts to it when it exits. */
170 sys_cond_t thread_condvar;
172 /* This thread might be waiting for some condition. If so, this
173 points to the condition. If the thread is interrupted, the
174 interrupter should broadcast to this condition. */
175 sys_cond_t *wait_condvar;
177 /* Threads are kept on a linked list. */
178 struct thread_state *next_thread;
181 /* A mutex in lisp is represented by a system condition variable.
182 The system mutex associated with this condition variable is the
183 global lock.
185 Using a condition variable lets us implement interruptibility for
186 lisp mutexes. */
187 typedef struct
189 /* The owning thread, or NULL if unlocked. */
190 struct thread_state *owner;
191 /* The lock count. */
192 unsigned int count;
193 /* The underlying system condition variable. */
194 sys_cond_t condition;
195 } lisp_mutex_t;
197 /* A mutex as a lisp object. */
198 struct Lisp_Mutex
200 struct vectorlike_header header;
202 /* The name of the mutex, or nil. */
203 Lisp_Object name;
205 /* The lower-level mutex object. */
206 lisp_mutex_t mutex;
209 /* A condition variable as a lisp object. */
210 struct Lisp_CondVar
212 struct vectorlike_header header;
214 /* The associated mutex. */
215 Lisp_Object mutex;
217 /* The name of the condition variable, or nil. */
218 Lisp_Object name;
220 /* The lower-level condition variable object. */
221 sys_cond_t cond;
224 extern struct thread_state *current_thread;
226 extern void unmark_threads (void);
227 extern void finalize_one_thread (struct thread_state *state);
228 extern void finalize_one_mutex (struct Lisp_Mutex *);
229 extern void finalize_one_condvar (struct Lisp_CondVar *);
231 extern void init_threads_once (void);
232 extern void init_threads (void);
233 extern void syms_of_threads (void);
235 typedef int select_func (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
236 EMACS_TIME *, sigset_t *);
238 int thread_select (select_func *func, int max_fds, SELECT_TYPE *rfds,
239 SELECT_TYPE *wfds, SELECT_TYPE *efds, EMACS_TIME *timeout,
240 sigset_t *sigmask);
242 int thread_check_current_buffer (struct buffer *);
244 #endif /* THREAD_H */