threads.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef SCM_THREADS_H
  2. #define SCM_THREADS_H
  3. /* Copyright 1996-1998,2000-2004,2006-2009,2011-2014,2018-2019
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/procs.h"
  18. #include "libguile/throw.h"
  19. #include "libguile/dynstack.h"
  20. #include "libguile/iselect.h"
  21. #include "libguile/smob.h"
  22. #include "libguile/vm.h"
  23. #if SCM_USE_PTHREAD_THREADS
  24. #include "libguile/pthread-threads.h"
  25. #endif
  26. #if SCM_USE_NULL_THREADS
  27. #include "libguile/null-threads.h"
  28. #endif
  29. #define SCM_INLINE_GC_GRANULE_WORDS 2
  30. #define SCM_INLINE_GC_GRANULE_BYTES \
  31. (sizeof(void *) * SCM_INLINE_GC_GRANULE_WORDS)
  32. /* A freelist set contains SCM_INLINE_GC_FREELIST_COUNT pointers to
  33. singly linked lists of objects of different sizes, the ith one
  34. containing objects i + 1 granules in size. This setting of
  35. SCM_INLINE_GC_FREELIST_COUNT will hold freelists for allocations of
  36. up to 256 bytes. */
  37. #define SCM_INLINE_GC_FREELIST_COUNT (256U / SCM_INLINE_GC_GRANULE_BYTES)
  38. /* smob tags for the thread datatypes */
  39. SCM_API scm_t_bits scm_tc16_thread;
  40. SCM_API scm_t_bits scm_tc16_mutex;
  41. SCM_API scm_t_bits scm_tc16_condvar;
  42. struct scm_thread_wake_data;
  43. struct scm_thread {
  44. struct scm_thread *next_thread;
  45. /* VM state for this thread. */
  46. struct scm_vm vm;
  47. /* For system asyncs.
  48. */
  49. SCM pending_asyncs; /* The thunks to be run at the next
  50. safe point. Accessed atomically. */
  51. unsigned int block_asyncs; /* Non-zero means that asyncs should
  52. not be run. */
  53. /* Thread-local freelists; see gc-inline.h. */
  54. void *freelists[SCM_INLINE_GC_FREELIST_COUNT];
  55. void *pointerless_freelists[SCM_INLINE_GC_FREELIST_COUNT];
  56. SCM handle;
  57. scm_i_pthread_t pthread;
  58. SCM result;
  59. int exited;
  60. /* Boolean indicating whether the thread is in guile mode. */
  61. int guile_mode;
  62. /* Boolean indicating whether to call GC_unregister_my_thread () when
  63. this thread exits. */
  64. int needs_unregister;
  65. struct scm_thread_wake_data *wake;
  66. scm_i_pthread_cond_t sleep_cond;
  67. int sleep_pipe[2];
  68. /* Other thread local things.
  69. */
  70. scm_t_dynamic_state *dynamic_state;
  71. /* The dynamic stack. */
  72. scm_t_dynstack dynstack;
  73. /* The current continuation root and the stack base for it.
  74. The continuation root is an arbitrary but unique object that
  75. identifies a dynamic extent. Continuations created during that
  76. extent can also only be invoked during it.
  77. We use pairs where the car is the thread handle and the cdr links
  78. to the previous pair. This might be used for better error
  79. messages but is not essential for identifying continuation roots.
  80. The continuation base is the far end of the stack upto which it
  81. needs to be copied.
  82. */
  83. SCM continuation_root;
  84. SCM_STACKITEM *continuation_base;
  85. /* Stack base. Used when checking for C stack overflow. */
  86. SCM_STACKITEM *base;
  87. #if SCM_HAVE_AUXILIARY_STACK
  88. /* Auxiliary stack base. */
  89. SCM_STACKITEM *auxiliary_stack_base;
  90. #endif
  91. /* JIT state; NULL until this thread needs to JIT-compile something. */
  92. struct scm_jit_state *jit_state;
  93. };
  94. #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
  95. #define SCM_I_THREAD_DATA(x) ((scm_thread *) SCM_SMOB_DATA (x))
  96. #define SCM_VALIDATE_THREAD(pos, a) \
  97. scm_assert_smob_type (scm_tc16_thread, (a))
  98. #define SCM_VALIDATE_MUTEX(pos, a) \
  99. scm_assert_smob_type (scm_tc16_mutex, (a))
  100. #define SCM_VALIDATE_CONDVAR(pos, a) \
  101. scm_assert_smob_type (scm_tc16_condvar, (a))
  102. SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
  103. scm_t_catch_handler handler, void *handler_data);
  104. SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
  105. SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
  106. SCM_INTERNAL void scm_threads_prehistory (void *);
  107. SCM_INTERNAL void scm_init_threads (void);
  108. SCM_INTERNAL void scm_init_threads_default_dynamic_state (void);
  109. SCM_INTERNAL void scm_i_dynwind_pthread_mutex_lock_block_asyncs (scm_i_pthread_mutex_t *mutex);
  110. SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
  111. SCM_API SCM scm_yield (void);
  112. SCM_API SCM scm_cancel_thread (SCM t);
  113. SCM_API SCM scm_join_thread (SCM t);
  114. SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
  115. SCM_API SCM scm_thread_p (SCM t);
  116. SCM_API SCM scm_make_mutex (void);
  117. SCM_API SCM scm_make_recursive_mutex (void);
  118. SCM_API SCM scm_make_mutex_with_kind (SCM kind);
  119. SCM_API SCM scm_lock_mutex (SCM m);
  120. SCM_API SCM scm_timed_lock_mutex (SCM m, SCM timeout);
  121. SCM_API void scm_dynwind_lock_mutex (SCM mutex);
  122. SCM_API SCM scm_try_mutex (SCM m);
  123. SCM_API SCM scm_unlock_mutex (SCM m);
  124. SCM_API SCM scm_mutex_p (SCM o);
  125. SCM_API SCM scm_mutex_locked_p (SCM m);
  126. SCM_API SCM scm_mutex_owner (SCM m);
  127. SCM_API SCM scm_mutex_level (SCM m);
  128. SCM_API SCM scm_make_condition_variable (void);
  129. SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
  130. SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
  131. SCM abstime);
  132. SCM_API SCM scm_signal_condition_variable (SCM cond);
  133. SCM_API SCM scm_broadcast_condition_variable (SCM cond);
  134. SCM_API SCM scm_condition_variable_p (SCM o);
  135. SCM_API SCM scm_current_thread (void);
  136. SCM_API SCM scm_all_threads (void);
  137. SCM_API int scm_c_thread_exited_p (SCM thread);
  138. SCM_API SCM scm_thread_exited_p (SCM thread);
  139. #ifdef BUILDING_LIBGUILE
  140. /* Though we don't need the key for SCM_I_CURRENT_THREAD if we have TLS,
  141. we do use it for cleanup purposes. */
  142. SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key;
  143. # ifdef SCM_HAVE_THREAD_STORAGE_CLASS
  144. SCM_INTERNAL SCM_THREAD_LOCAL scm_thread *scm_i_current_thread;
  145. # define SCM_I_CURRENT_THREAD (scm_i_current_thread)
  146. # else /* !SCM_HAVE_THREAD_STORAGE_CLASS */
  147. # define SCM_I_CURRENT_THREAD \
  148. ((scm_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
  149. # endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */
  150. #endif /* BUILDING_LIBGUILE */
  151. SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
  152. /* Convenience functions for working with the pthread API in guile
  153. mode.
  154. */
  155. #if SCM_USE_PTHREAD_THREADS
  156. SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
  157. SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
  158. SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
  159. pthread_mutex_t *mutex);
  160. SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
  161. pthread_mutex_t *mutex,
  162. const scm_t_timespec *abstime);
  163. #endif
  164. /* More convenience functions.
  165. */
  166. SCM_API unsigned int scm_std_sleep (unsigned int);
  167. SCM_API unsigned long scm_std_usleep (unsigned long);
  168. SCM_API SCM scm_total_processor_count (void);
  169. SCM_API SCM scm_current_processor_count (void);
  170. #endif /* SCM_THREADS_H */