root/src/thread.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. THREADP
  2. CHECK_THREAD
  3. XTHREAD
  4. MUTEXP
  5. CHECK_MUTEX
  6. XMUTEX
  7. CONDVARP
  8. CHECK_CONDVAR
  9. XCONDVAR

     1 /* Thread definitions
     2 Copyright (C) 2012-2023 Free Software Foundation, Inc.
     3 
     4 This file is part of GNU Emacs.
     5 
     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.
    10 
    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.
    15 
    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/>.  */
    18 
    19 #ifndef THREAD_H
    20 #define THREAD_H
    21 
    22 #include "regex-emacs.h"
    23 
    24 #ifdef WINDOWSNT
    25 #include <sys/socket.h>
    26 #endif
    27 
    28 #ifdef MSDOS
    29 #include <time.h>               /* struct rpl_timespec */
    30 #include <signal.h>             /* sigset_t */
    31 #endif
    32 
    33 #include "sysselect.h"          /* FIXME */
    34 #include "systhread.h"
    35 
    36 /* Yield an address close enough to the top of the stack that the
    37    garbage collector need not scan above it.  Callers should be
    38    declared NO_INLINE.  */
    39 #ifdef HAVE___BUILTIN_FRAME_ADDRESS
    40 # define NEAR_STACK_TOP(addr) ((void) (addr), __builtin_frame_address (0))
    41 #else
    42 # define NEAR_STACK_TOP(addr) (addr)
    43 #endif
    44 
    45 INLINE_HEADER_BEGIN
    46 
    47 /* Byte-code interpreter thread state.  */
    48 struct bc_thread_state {
    49   struct bc_frame *fp;   /* current frame pointer */
    50 
    51   /* start and end of allocated bytecode stack */
    52   char *stack;
    53   char *stack_end;
    54 };
    55 
    56 struct thread_state
    57 {
    58   union vectorlike_header header;
    59 
    60   /* The buffer in which the last search was performed, or
    61      Qt if the last search was done in a string;
    62      Qnil if no searching has been done yet.  */
    63   Lisp_Object m_last_thing_searched;
    64 #define last_thing_searched (current_thread->m_last_thing_searched)
    65 
    66   Lisp_Object m_saved_last_thing_searched;
    67 #define saved_last_thing_searched (current_thread->m_saved_last_thing_searched)
    68 
    69   /* The thread's name.  */
    70   Lisp_Object name;
    71 
    72   /* The thread's function.  */
    73   Lisp_Object function;
    74 
    75   /* The thread's result, if function has finished.  */
    76   Lisp_Object result;
    77 
    78   /* If non-nil, this thread has been signaled.  */
    79   Lisp_Object error_symbol;
    80   Lisp_Object error_data;
    81 
    82   /* If we are waiting for some event, this holds the object we are
    83      waiting on.  */
    84   Lisp_Object event_object;
    85   /* event_object must be the last Lisp field.  */
    86 
    87   /* An address near the bottom of the stack.
    88      Tells GC how to save a copy of the stack.  */
    89   char const *m_stack_bottom;
    90 #define stack_bottom (current_thread->m_stack_bottom)
    91 
    92   /* The address of an object near the C stack top, used to determine
    93      which words need to be scanned by the garbage collector.  This is
    94      also used to detect heuristically whether segmentation violation
    95      address indicates stack overflow, as opposed to some internal
    96      error in Emacs.  If the C function F calls G which calls H which
    97      calls ... F, then at least one of the functions in the chain
    98      should set this to the address of a local variable.  */
    99   void const *stack_top;
   100 
   101   struct catchtag *m_catchlist;
   102 #define catchlist (current_thread->m_catchlist)
   103 
   104   /* Chain of condition handlers currently in effect.
   105      The elements of this chain are contained in the stack frames
   106      of Fcondition_case and internal_condition_case.
   107      When an error is signaled (by calling Fsignal),
   108      this chain is searched for an element that applies.  */
   109   struct handler *m_handlerlist;
   110 #define handlerlist (current_thread->m_handlerlist)
   111 
   112   struct handler *m_handlerlist_sentinel;
   113 #define handlerlist_sentinel (current_thread->m_handlerlist_sentinel)
   114 
   115   /* Pointer to beginning of specpdl.  */
   116   union specbinding *m_specpdl;
   117 #define specpdl (current_thread->m_specpdl)
   118 
   119   /* End of specpld (just beyond the last element).  */
   120   union specbinding *m_specpdl_end;
   121 #define specpdl_end (current_thread->m_specpdl_end)
   122 
   123   /* Pointer to first unused element in specpdl.  */
   124   union specbinding *m_specpdl_ptr;
   125 #define specpdl_ptr (current_thread->m_specpdl_ptr)
   126 
   127   /* Depth in Lisp evaluations and function calls.  */
   128   intmax_t m_lisp_eval_depth;
   129 #define lisp_eval_depth (current_thread->m_lisp_eval_depth)
   130 
   131   /* This points to the current buffer.  */
   132   struct buffer *m_current_buffer;
   133 #define current_buffer (current_thread->m_current_buffer)
   134 
   135   /* Every call to re_search, etc., must pass &search_regs as the regs
   136      argument unless you can show it is unnecessary (i.e., if re_search
   137      is certainly going to be called again before region-around-match
   138      can be called).
   139 
   140      Since the registers are now dynamically allocated, we need to make
   141      sure not to refer to the Nth register before checking that it has
   142      been allocated by checking search_regs.num_regs.
   143 
   144      The regex code keeps track of whether it has allocated the search
   145      buffer using bits in the re_pattern_buffer.  This means that whenever
   146      you compile a new pattern, it completely forgets whether it has
   147      allocated any registers, and will allocate new registers the next
   148      time you call a searching or matching function.  Therefore, we need
   149      to call re_set_registers after compiling a new pattern or after
   150      setting the match registers, so that the regex functions will be
   151      able to free or re-allocate it properly.  */
   152   struct re_registers m_search_regs;
   153 #define search_regs (current_thread->m_search_regs)
   154 
   155   struct re_registers m_saved_search_regs;
   156 #define saved_search_regs (current_thread->m_saved_search_regs)
   157 
   158   /* This member is different from waiting_for_input.
   159      It is used to communicate to a lisp process-filter/sentinel (via the
   160      function Fwaiting_for_user_input_p) whether Emacs was waiting
   161      for user-input when that process-filter was called.
   162      waiting_for_input cannot be used as that is by definition 0 when
   163      lisp code is being evalled.
   164      For that purpose, this must be 0
   165      when not inside wait_reading_process_output.  */
   166   int m_waiting_for_user_input_p;
   167 #define waiting_for_user_input_p (current_thread->m_waiting_for_user_input_p)
   168 
   169   /* True while doing kbd input.  */
   170   bool m_waiting_for_input;
   171 #define waiting_for_input (current_thread->m_waiting_for_input)
   172 
   173   /* For longjmp to where kbd input is being done.  This is per-thread
   174      so that if more than one thread calls read_char, they don't
   175      clobber each other's getcjmp, which will cause
   176      quit_throw_to_read_char crash due to using a wrong stack.  */
   177   sys_jmp_buf m_getcjmp;
   178 #define getcjmp (current_thread->m_getcjmp)
   179 
   180   /* The OS identifier for this thread.  */
   181   sys_thread_t thread_id;
   182 
   183   /* The condition variable for this thread.  This is associated with
   184      the global lock.  This thread broadcasts to it when it exits.  */
   185   sys_cond_t thread_condvar;
   186 
   187   /* This thread might be waiting for some condition.  If so, this
   188      points to the condition.  If the thread is interrupted, the
   189      interrupter should broadcast to this condition.  */
   190   sys_cond_t *wait_condvar;
   191 
   192   /* Thread's name in the locale encoding.  */
   193   char *thread_name;
   194 
   195   /* This thread might have released the global lock.  If so, this is
   196      non-zero.  When a thread runs outside thread_select with this
   197      flag non-zero, it means it has been interrupted by SIGINT while
   198      in thread_select, and didn't have a chance of acquiring the lock.
   199      It must do so ASAP.  */
   200   int not_holding_lock;
   201 
   202   /* Threads are kept on a linked list.  */
   203   struct thread_state *next_thread;
   204 
   205   struct bc_thread_state bc;
   206 } GCALIGNED_STRUCT;
   207 
   208 INLINE bool
   209 THREADP (Lisp_Object a)
   210 {
   211   return PSEUDOVECTORP (a, PVEC_THREAD);
   212 }
   213 
   214 INLINE void
   215 CHECK_THREAD (Lisp_Object x)
   216 {
   217   CHECK_TYPE (THREADP (x), Qthreadp, x);
   218 }
   219 
   220 INLINE struct thread_state *
   221 XTHREAD (Lisp_Object a)
   222 {
   223   eassert (THREADP (a));
   224   return XUNTAG (a, Lisp_Vectorlike, struct thread_state);
   225 }
   226 
   227 /* A mutex in lisp is represented by a system condition variable.
   228    The system mutex associated with this condition variable is the
   229    global lock.
   230 
   231    Using a condition variable lets us implement interruptibility for
   232    lisp mutexes.  */
   233 typedef struct
   234 {
   235   /* The owning thread, or NULL if unlocked.  */
   236   struct thread_state *owner;
   237   /* The lock count.  */
   238   unsigned int count;
   239   /* The underlying system condition variable.  */
   240   sys_cond_t condition;
   241 } lisp_mutex_t;
   242 
   243 /* A mutex as a lisp object.  */
   244 struct Lisp_Mutex
   245 {
   246   union vectorlike_header header;
   247 
   248   /* The name of the mutex, or nil.  */
   249   Lisp_Object name;
   250 
   251   /* The lower-level mutex object.  */
   252   lisp_mutex_t mutex;
   253 } GCALIGNED_STRUCT;
   254 
   255 INLINE bool
   256 MUTEXP (Lisp_Object a)
   257 {
   258   return PSEUDOVECTORP (a, PVEC_MUTEX);
   259 }
   260 
   261 INLINE void
   262 CHECK_MUTEX (Lisp_Object x)
   263 {
   264   CHECK_TYPE (MUTEXP (x), Qmutexp, x);
   265 }
   266 
   267 INLINE struct Lisp_Mutex *
   268 XMUTEX (Lisp_Object a)
   269 {
   270   eassert (MUTEXP (a));
   271   return XUNTAG (a, Lisp_Vectorlike, struct Lisp_Mutex);
   272 }
   273 
   274 /* A condition variable as a lisp object.  */
   275 struct Lisp_CondVar
   276 {
   277   union vectorlike_header header;
   278 
   279   /* The associated mutex.  */
   280   Lisp_Object mutex;
   281 
   282   /* The name of the condition variable, or nil.  */
   283   Lisp_Object name;
   284 
   285   /* The lower-level condition variable object.  */
   286   sys_cond_t cond;
   287 } GCALIGNED_STRUCT;
   288 
   289 INLINE bool
   290 CONDVARP (Lisp_Object a)
   291 {
   292   return PSEUDOVECTORP (a, PVEC_CONDVAR);
   293 }
   294 
   295 INLINE void
   296 CHECK_CONDVAR (Lisp_Object x)
   297 {
   298   CHECK_TYPE (CONDVARP (x), Qcondition_variable_p, x);
   299 }
   300 
   301 INLINE struct Lisp_CondVar *
   302 XCONDVAR (Lisp_Object a)
   303 {
   304   eassert (CONDVARP (a));
   305   return XUNTAG (a, Lisp_Vectorlike, struct Lisp_CondVar);
   306 }
   307 
   308 extern struct thread_state *current_thread;
   309 
   310 extern void finalize_one_thread (struct thread_state *state);
   311 extern void finalize_one_mutex (struct Lisp_Mutex *);
   312 extern void finalize_one_condvar (struct Lisp_CondVar *);
   313 extern void maybe_reacquire_global_lock (void);
   314 
   315 extern void init_threads (void);
   316 extern void syms_of_threads (void);
   317 extern bool main_thread_p (const void *);
   318 extern bool in_current_thread (void);
   319 
   320 typedef int select_func (int, fd_set *, fd_set *, fd_set *,
   321                          const struct timespec *, const sigset_t *);
   322 
   323 int thread_select  (select_func *func, int max_fds, fd_set *rfds,
   324                     fd_set *wfds, fd_set *efds, struct timespec *timeout,
   325                     sigset_t *sigmask);
   326 
   327 bool thread_check_current_buffer (struct buffer *);
   328 
   329 INLINE_HEADER_END
   330 
   331 #endif /* THREAD_H */

/* [<][>][^][v][top][bottom][index][help] */