merge from trunk
[emacs.git] / src / thread.h
blob97bdb2c805cdad795ed8100c3b3ae2601e8aff6a
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 backtrace *m_backtrace_list;
76 #define backtrace_list (current_thread->m_backtrace_list)
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, below),
85 this chain is searched for an element that applies. */
86 struct handler *m_handlerlist;
87 #define handlerlist (current_thread->m_handlerlist)
89 /* Count levels of GCPRO to detect failure to UNGCPRO. */
90 int m_gcpro_level;
91 #define gcpro_level (current_thread->m_gcpro_level)
93 /* Current number of specbindings allocated in specpdl. */
94 ptrdiff_t m_specpdl_size;
95 #define specpdl_size (current_thread->m_specpdl_size)
97 /* Pointer to beginning of specpdl. */
98 struct specbinding *m_specpdl;
99 #define specpdl (current_thread->m_specpdl)
101 /* Pointer to first unused element in specpdl. */
102 struct specbinding *m_specpdl_ptr;
103 #define specpdl_ptr (current_thread->m_specpdl_ptr)
105 /* Pointer to the first "saved" element in specpdl. When this
106 thread is swapped out, the current values of all specpdl bindings
107 are pushed onto the specpdl; then these are popped again when
108 switching back to this thread. */
109 struct specbinding *m_saved_specpdl_ptr;
111 /* Depth in Lisp evaluations and function calls. */
112 EMACS_INT m_lisp_eval_depth;
113 #define lisp_eval_depth (current_thread->m_lisp_eval_depth)
115 /* This points to the current buffer. */
116 struct buffer *m_current_buffer;
117 #define current_buffer (current_thread->m_current_buffer)
119 /* Every call to re_match, etc., must pass &search_regs as the regs
120 argument unless you can show it is unnecessary (i.e., if re_match
121 is certainly going to be called again before region-around-match
122 can be called).
124 Since the registers are now dynamically allocated, we need to make
125 sure not to refer to the Nth register before checking that it has
126 been allocated by checking search_regs.num_regs.
128 The regex code keeps track of whether it has allocated the search
129 buffer using bits in the re_pattern_buffer. This means that whenever
130 you compile a new pattern, it completely forgets whether it has
131 allocated any registers, and will allocate new registers the next
132 time you call a searching or matching function. Therefore, we need
133 to call re_set_registers after compiling a new pattern or after
134 setting the match registers, so that the regex functions will be
135 able to free or re-allocate it properly. */
136 struct re_registers m_search_regs;
137 #define search_regs (current_thread->m_search_regs)
139 /* If non-zero the match data have been saved in saved_search_regs
140 during the execution of a sentinel or filter. */
141 bool m_search_regs_saved;
142 #define search_regs_saved (current_thread->m_search_regs_saved)
144 struct re_registers m_saved_search_regs;
145 #define saved_search_regs (current_thread->m_saved_search_regs)
147 /* This is the string or buffer in which we
148 are matching. It is used for looking up syntax properties. */
149 Lisp_Object m_re_match_object;
150 #define re_match_object (current_thread->m_re_match_object)
152 /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
153 also be assigned to arbitrarily: each pattern buffer stores its own
154 syntax, so it can be changed between regex compilations. */
155 reg_syntax_t m_re_syntax_options;
156 #define re_syntax_options (current_thread->m_re_syntax_options)
158 /* Regexp to use to replace spaces, or NULL meaning don't. */
159 /*re_char*/ unsigned char *m_whitespace_regexp;
160 #define whitespace_regexp (current_thread->m_whitespace_regexp)
162 /* This variable is different from waiting_for_input in keyboard.c.
163 It is used to communicate to a lisp process-filter/sentinel (via the
164 function Fwaiting_for_user_input_p) whether Emacs was waiting
165 for user-input when that process-filter was called.
166 waiting_for_input cannot be used as that is by definition 0 when
167 lisp code is being evalled.
168 This is also used in record_asynch_buffer_change.
169 For that purpose, this must be 0
170 when not inside wait_reading_process_output. */
171 int m_waiting_for_user_input_p;
172 #define waiting_for_user_input_p (current_thread->m_waiting_for_user_input_p)
174 /* The OS identifier for this thread. */
175 sys_thread_t thread_id;
177 /* The condition variable for this thread. This is associated with
178 the global lock. This thread broadcasts to it when it exits. */
179 sys_cond_t thread_condvar;
181 /* This thread might be waiting for some condition. If so, this
182 points to the condition. If the thread is interrupted, the
183 interrupter should broadcast to this condition. */
184 sys_cond_t *wait_condvar;
186 /* Threads are kept on a linked list. */
187 struct thread_state *next_thread;
190 /* A mutex in lisp is represented by a system condition variable.
191 The system mutex associated with this condition variable is the
192 global lock.
194 Using a condition variable lets us implement interruptibility for
195 lisp mutexes. */
196 typedef struct
198 /* The owning thread, or NULL if unlocked. */
199 struct thread_state *owner;
200 /* The lock count. */
201 unsigned int count;
202 /* The underlying system condition variable. */
203 sys_cond_t condition;
204 } lisp_mutex_t;
206 /* A mutex as a lisp object. */
207 struct Lisp_Mutex
209 struct vectorlike_header header;
211 /* The name of the mutex, or nil. */
212 Lisp_Object name;
214 /* The lower-level mutex object. */
215 lisp_mutex_t mutex;
218 /* A condition variable as a lisp object. */
219 struct Lisp_CondVar
221 struct vectorlike_header header;
223 /* The associated mutex. */
224 Lisp_Object mutex;
226 /* The name of the condition variable, or nil. */
227 Lisp_Object name;
229 /* The lower-level condition variable object. */
230 sys_cond_t cond;
233 extern struct thread_state *current_thread;
235 extern void unmark_threads (void);
236 extern void finalize_one_thread (struct thread_state *state);
237 extern void finalize_one_mutex (struct Lisp_Mutex *);
238 extern void finalize_one_condvar (struct Lisp_CondVar *);
240 extern void init_threads_once (void);
241 extern void init_threads (void);
242 extern void syms_of_threads (void);
244 typedef int select_func (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
245 EMACS_TIME *, sigset_t *);
247 int thread_select (select_func *func, int max_fds, SELECT_TYPE *rfds,
248 SELECT_TYPE *wfds, SELECT_TYPE *efds, EMACS_TIME *timeout,
249 sigset_t *sigmask);
251 #endif /* THREAD_H */