thread.h 9.5 KB
Newer Older
1
/* Thread definitions
Paul Eggert's avatar
Paul Eggert committed
2
Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

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 20 21

#ifndef THREAD_H
#define THREAD_H

22
#include "regex-emacs.h"
23

Eli Zaretskii's avatar
Eli Zaretskii committed
24 25 26 27
#ifdef WINDOWSNT
#include <sys/socket.h>
#endif

Eli Zaretskii's avatar
Eli Zaretskii committed
28 29 30 31
#ifdef MSDOS
#include <signal.h>		/* sigset_t */
#endif

Tom Tromey's avatar
Tom Tromey committed
32
#include "sysselect.h"		/* FIXME */
33
#include "systhread.h"
Tom Tromey's avatar
Tom Tromey committed
34

35 36
struct thread_state
{
37
  union vectorlike_header header;
38

39 40 41 42 43 44 45 46 47
  /* The buffer in which the last search was performed, or
     Qt if the last search was done in a string;
     Qnil if no searching has been done yet.  */
  Lisp_Object m_last_thing_searched;
#define last_thing_searched (current_thread->m_last_thing_searched)

  Lisp_Object m_saved_last_thing_searched;
#define saved_last_thing_searched (current_thread->m_saved_last_thing_searched)

48 49 50 51 52 53
  /* The thread's name.  */
  Lisp_Object name;

  /* The thread's function.  */
  Lisp_Object function;

54 55 56
  /* The thread's result, if function has finished.  */
  Lisp_Object result;

Paul Eggert's avatar
Paul Eggert committed
57
  /* If non-nil, this thread has been signaled.  */
58 59 60
  Lisp_Object error_symbol;
  Lisp_Object error_data;

61 62 63
  /* If we are waiting for some event, this holds the object we are
     waiting on.  */
  Lisp_Object event_object;
64
  /* event_object must be the last Lisp field.  */
65

66 67
  /* An address near the bottom of the stack.
     Tells GC how to save a copy of the stack.  */
68
  char const *m_stack_bottom;
69 70
#define stack_bottom (current_thread->m_stack_bottom)

71 72 73 74 75 76 77
  /* The address of an object near the C stack top, used to determine
     which words need to be scanned by the garbage collector.  This is
     also used to detect heuristically whether segmentation violation
     address indicates stack overflow, as opposed to some internal
     error in Emacs.  If the C function F calls G which calls H which
     calls ... F, then at least one of the functions in the chain
     should set this to the address of a local variable.  */
78
  void const *stack_top;
79 80 81 82 83 84 85

  struct catchtag *m_catchlist;
#define catchlist (current_thread->m_catchlist)

  /* Chain of condition handlers currently in effect.
     The elements of this chain are contained in the stack frames
     of Fcondition_case and internal_condition_case.
Eli Zaretskii's avatar
Eli Zaretskii committed
86
     When an error is signaled (by calling Fsignal),
87 88 89 90
     this chain is searched for an element that applies.  */
  struct handler *m_handlerlist;
#define handlerlist (current_thread->m_handlerlist)

Ken Raeburn's avatar
Ken Raeburn committed
91 92
  struct handler *m_handlerlist_sentinel;
#define handlerlist_sentinel (current_thread->m_handlerlist_sentinel)
93 94 95 96 97 98

  /* Current number of specbindings allocated in specpdl.  */
  ptrdiff_t m_specpdl_size;
#define specpdl_size (current_thread->m_specpdl_size)

  /* Pointer to beginning of specpdl.  */
Tom Tromey's avatar
Tom Tromey committed
99
  union specbinding *m_specpdl;
100 101 102
#define specpdl (current_thread->m_specpdl)

  /* Pointer to first unused element in specpdl.  */
Tom Tromey's avatar
Tom Tromey committed
103
  union specbinding *m_specpdl_ptr;
104 105 106
#define specpdl_ptr (current_thread->m_specpdl_ptr)

  /* Depth in Lisp evaluations and function calls.  */
107
  intmax_t m_lisp_eval_depth;
108 109 110 111 112 113
#define lisp_eval_depth (current_thread->m_lisp_eval_depth)

  /* This points to the current buffer.  */
  struct buffer *m_current_buffer;
#define current_buffer (current_thread->m_current_buffer)

114 115
  /* Every call to re_search, etc., must pass &search_regs as the regs
     argument unless you can show it is unnecessary (i.e., if re_search
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
     is certainly going to be called again before region-around-match
     can be called).

     Since the registers are now dynamically allocated, we need to make
     sure not to refer to the Nth register before checking that it has
     been allocated by checking search_regs.num_regs.

     The regex code keeps track of whether it has allocated the search
     buffer using bits in the re_pattern_buffer.  This means that whenever
     you compile a new pattern, it completely forgets whether it has
     allocated any registers, and will allocate new registers the next
     time you call a searching or matching function.  Therefore, we need
     to call re_set_registers after compiling a new pattern or after
     setting the match registers, so that the regex functions will be
     able to free or re-allocate it properly.  */
  struct re_registers m_search_regs;
#define search_regs (current_thread->m_search_regs)

  struct re_registers m_saved_search_regs;
#define saved_search_regs (current_thread->m_saved_search_regs)

Eli Zaretskii's avatar
Eli Zaretskii committed
137
  /* This member is different from waiting_for_input.
Tom Tromey's avatar
Tom Tromey committed
138 139 140 141 142 143 144 145 146 147 148
     It is used to communicate to a lisp process-filter/sentinel (via the
     function Fwaiting_for_user_input_p) whether Emacs was waiting
     for user-input when that process-filter was called.
     waiting_for_input cannot be used as that is by definition 0 when
     lisp code is being evalled.
     This is also used in record_asynch_buffer_change.
     For that purpose, this must be 0
     when not inside wait_reading_process_output.  */
  int m_waiting_for_user_input_p;
#define waiting_for_user_input_p (current_thread->m_waiting_for_user_input_p)

149 150 151 152
  /* True while doing kbd input.  */
  bool m_waiting_for_input;
#define waiting_for_input (current_thread->m_waiting_for_input)

153 154 155 156 157 158 159
  /* For longjmp to where kbd input is being done.  This is per-thread
     so that if more than one thread calls read_char, they don't
     clobber each other's getcjmp, which will cause
     quit_throw_to_read_char crash due to using a wrong stack.  */
  sys_jmp_buf m_getcjmp;
#define getcjmp (current_thread->m_getcjmp)

160 161 162 163 164 165 166 167 168 169 170 171
  /* The OS identifier for this thread.  */
  sys_thread_t thread_id;

  /* The condition variable for this thread.  This is associated with
     the global lock.  This thread broadcasts to it when it exits.  */
  sys_cond_t thread_condvar;

  /* This thread might be waiting for some condition.  If so, this
     points to the condition.  If the thread is interrupted, the
     interrupter should broadcast to this condition.  */
  sys_cond_t *wait_condvar;

172
  /* Thread's name in the locale encoding.  */
173 174
  char *thread_name;

175 176 177 178 179 180 181
  /* This thread might have released the global lock.  If so, this is
     non-zero.  When a thread runs outside thread_select with this
     flag non-zero, it means it has been interrupted by SIGINT while
     in thread_select, and didn't have a chance of acquiring the lock.
     It must do so ASAP.  */
  int not_holding_lock;

182 183
  /* Threads are kept on a linked list.  */
  struct thread_state *next_thread;
Paul Eggert's avatar
Paul Eggert committed
184
} GCALIGNED_STRUCT;
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
INLINE bool
THREADP (Lisp_Object a)
{
  return PSEUDOVECTORP (a, PVEC_THREAD);
}

INLINE void
CHECK_THREAD (Lisp_Object x)
{
  CHECK_TYPE (THREADP (x), Qthreadp, x);
}

INLINE struct thread_state *
XTHREAD (Lisp_Object a)
{
  eassert (THREADP (a));
202
  return XUNTAG (a, Lisp_Vectorlike, struct thread_state);
203 204
}

Tom Tromey's avatar
Tom Tromey committed
205
/* A mutex in lisp is represented by a system condition variable.
Tom Tromey's avatar
Tom Tromey committed
206 207 208 209 210 211 212
   The system mutex associated with this condition variable is the
   global lock.

   Using a condition variable lets us implement interruptibility for
   lisp mutexes.  */
typedef struct
{
Tom Tromey's avatar
Tom Tromey committed
213
  /* The owning thread, or NULL if unlocked.  */
Tom Tromey's avatar
Tom Tromey committed
214
  struct thread_state *owner;
Tom Tromey's avatar
Tom Tromey committed
215
  /* The lock count.  */
Tom Tromey's avatar
Tom Tromey committed
216
  unsigned int count;
Tom Tromey's avatar
Tom Tromey committed
217
  /* The underlying system condition variable.  */
Tom Tromey's avatar
Tom Tromey committed
218 219 220
  sys_cond_t condition;
} lisp_mutex_t;

Tom Tromey's avatar
Tom Tromey committed
221
/* A mutex as a lisp object.  */
222 223
struct Lisp_Mutex
{
224
  union vectorlike_header header;
225

Tom Tromey's avatar
Tom Tromey committed
226
  /* The name of the mutex, or nil.  */
227 228
  Lisp_Object name;

Tom Tromey's avatar
Tom Tromey committed
229
  /* The lower-level mutex object.  */
230
  lisp_mutex_t mutex;
Paul Eggert's avatar
Paul Eggert committed
231
} GCALIGNED_STRUCT;
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
INLINE bool
MUTEXP (Lisp_Object a)
{
  return PSEUDOVECTORP (a, PVEC_MUTEX);
}

INLINE void
CHECK_MUTEX (Lisp_Object x)
{
  CHECK_TYPE (MUTEXP (x), Qmutexp, x);
}

INLINE struct Lisp_Mutex *
XMUTEX (Lisp_Object a)
{
  eassert (MUTEXP (a));
249
  return XUNTAG (a, Lisp_Vectorlike, struct Lisp_Mutex);
250 251
}

Tom Tromey's avatar
Tom Tromey committed
252 253 254
/* A condition variable as a lisp object.  */
struct Lisp_CondVar
{
255
  union vectorlike_header header;
Tom Tromey's avatar
Tom Tromey committed
256 257 258 259 260 261 262 263 264

  /* The associated mutex.  */
  Lisp_Object mutex;

  /* The name of the condition variable, or nil.  */
  Lisp_Object name;

  /* The lower-level condition variable object.  */
  sys_cond_t cond;
Paul Eggert's avatar
Paul Eggert committed
265
} GCALIGNED_STRUCT;
Tom Tromey's avatar
Tom Tromey committed
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
INLINE bool
CONDVARP (Lisp_Object a)
{
  return PSEUDOVECTORP (a, PVEC_CONDVAR);
}

INLINE void
CHECK_CONDVAR (Lisp_Object x)
{
  CHECK_TYPE (CONDVARP (x), Qcondition_variable_p, x);
}

INLINE struct Lisp_CondVar *
XCONDVAR (Lisp_Object a)
{
  eassert (CONDVARP (a));
283
  return XUNTAG (a, Lisp_Vectorlike, struct Lisp_CondVar);
284 285
}

286 287
extern struct thread_state *current_thread;

288
extern void finalize_one_thread (struct thread_state *state);
289
extern void finalize_one_mutex (struct Lisp_Mutex *);
Tom Tromey's avatar
Tom Tromey committed
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);
Daniel Colascione's avatar
Daniel Colascione committed
295
extern bool main_thread_p (const void *);
296
extern bool in_current_thread (void);
297

Ken Raeburn's avatar
Ken Raeburn committed
298
typedef int select_func (int, fd_set *, fd_set *, fd_set *,
299
			 const struct timespec *, const sigset_t *);
Tom Tromey's avatar
Tom Tromey committed
300

Ken Raeburn's avatar
Ken Raeburn committed
301 302
int thread_select  (select_func *func, int max_fds, fd_set *rfds,
		    fd_set *wfds, fd_set *efds, struct timespec *timeout,
Tom Tromey's avatar
Tom Tromey committed
303 304
		    sigset_t *sigmask);

305
bool thread_check_current_buffer (struct buffer *);
306

307
#endif /* THREAD_H */