threads.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* classes: h_files */
  2. #ifndef SCM_THREADS_H
  3. #define SCM_THREADS_H
  4. /* Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2006,
  5. * 2007, 2008, 2009, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include "libguile/__scm.h"
  23. #include "libguile/procs.h"
  24. #include "libguile/throw.h"
  25. #include "libguile/root.h"
  26. #include "libguile/dynstack.h"
  27. #include "libguile/iselect.h"
  28. #include "libguile/continuations.h"
  29. #if SCM_USE_PTHREAD_THREADS
  30. #include "libguile/pthread-threads.h"
  31. #endif
  32. #if SCM_USE_NULL_THREADS
  33. #include "libguile/null-threads.h"
  34. #endif
  35. /* smob tags for the thread datatypes */
  36. SCM_API scm_t_bits scm_tc16_thread;
  37. SCM_API scm_t_bits scm_tc16_mutex;
  38. SCM_API scm_t_bits scm_tc16_condvar;
  39. typedef struct scm_i_thread {
  40. struct scm_i_thread *next_thread;
  41. SCM handle;
  42. scm_i_pthread_t pthread;
  43. SCM cleanup_handler;
  44. SCM join_queue;
  45. scm_i_pthread_mutex_t admin_mutex;
  46. SCM mutexes;
  47. scm_i_pthread_mutex_t *held_mutex;
  48. SCM result;
  49. int canceled;
  50. int exited;
  51. /* Boolean indicating whether the thread is in guile mode. */
  52. int guile_mode;
  53. SCM sleep_object;
  54. scm_i_pthread_mutex_t *sleep_mutex;
  55. scm_i_pthread_cond_t sleep_cond;
  56. int sleep_fd, sleep_pipe[2];
  57. /* Thread-local freelists; see gc-inline.h. */
  58. void **freelists;
  59. void **pointerless_freelists;
  60. /* Other thread local things.
  61. */
  62. SCM dynamic_state;
  63. /* The dynamic stack. */
  64. scm_t_dynstack dynstack;
  65. /* For system asyncs.
  66. */
  67. SCM active_asyncs; /* The thunks to be run at the next
  68. safe point */
  69. unsigned int block_asyncs; /* Non-zero means that asyncs should
  70. not be run. */
  71. unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending.
  72. */
  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. /* For keeping track of the stack and registers. */
  86. struct scm_vm *vp;
  87. SCM_STACKITEM *base;
  88. scm_i_jmp_buf regs;
  89. #ifdef __ia64__
  90. void *register_backing_store_base;
  91. scm_t_contregs *pending_rbs_continuation;
  92. #endif
  93. /* Whether this thread is in a critical section. */
  94. int critical_section_level;
  95. } scm_i_thread;
  96. #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
  97. #define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
  98. #define SCM_VALIDATE_THREAD(pos, a) \
  99. scm_assert_smob_type (scm_tc16_thread, (a))
  100. #define SCM_VALIDATE_MUTEX(pos, a) \
  101. scm_assert_smob_type (scm_tc16_mutex, (a))
  102. #define SCM_VALIDATE_CONDVAR(pos, a) \
  103. scm_assert_smob_type (scm_tc16_condvar, (a))
  104. SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
  105. scm_t_catch_handler handler, void *handler_data);
  106. SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
  107. SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
  108. SCM_INTERNAL void scm_i_reset_fluid (size_t);
  109. SCM_INTERNAL void scm_threads_prehistory (void *);
  110. SCM_INTERNAL void scm_init_threads (void);
  111. SCM_INTERNAL void scm_init_thread_procs (void);
  112. SCM_INTERNAL void scm_init_threads_default_dynamic_state (void);
  113. SCM_INTERNAL void scm_i_dynwind_pthread_mutex_lock_block_asyncs (scm_i_pthread_mutex_t *mutex);
  114. SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
  115. SCM_API SCM scm_yield (void);
  116. SCM_API SCM scm_cancel_thread (SCM t);
  117. SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc);
  118. SCM_API SCM scm_thread_cleanup (SCM thread);
  119. SCM_API SCM scm_join_thread (SCM t);
  120. SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
  121. SCM_API SCM scm_thread_p (SCM t);
  122. SCM_API SCM scm_make_mutex (void);
  123. SCM_API SCM scm_make_recursive_mutex (void);
  124. SCM_API SCM scm_make_mutex_with_flags (SCM flags);
  125. SCM_API SCM scm_lock_mutex (SCM m);
  126. SCM_API SCM scm_lock_mutex_timed (SCM m, SCM timeout, SCM owner);
  127. SCM_API void scm_dynwind_lock_mutex (SCM mutex);
  128. SCM_API SCM scm_try_mutex (SCM m);
  129. SCM_API SCM scm_unlock_mutex (SCM m);
  130. SCM_API SCM scm_unlock_mutex_timed (SCM m, SCM cond, SCM timeout);
  131. SCM_API SCM scm_mutex_p (SCM o);
  132. SCM_API SCM scm_mutex_locked_p (SCM m);
  133. SCM_API SCM scm_mutex_owner (SCM m);
  134. SCM_API SCM scm_mutex_level (SCM m);
  135. SCM_API SCM scm_make_condition_variable (void);
  136. SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
  137. SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
  138. SCM abstime);
  139. SCM_API SCM scm_signal_condition_variable (SCM cond);
  140. SCM_API SCM scm_broadcast_condition_variable (SCM cond);
  141. SCM_API SCM scm_condition_variable_p (SCM o);
  142. SCM_API SCM scm_current_thread (void);
  143. SCM_API SCM scm_all_threads (void);
  144. SCM_API int scm_c_thread_exited_p (SCM thread);
  145. SCM_API SCM scm_thread_exited_p (SCM thread);
  146. SCM_API void scm_dynwind_critical_section (SCM mutex);
  147. #ifdef BUILDING_LIBGUILE
  148. /* Though we don't need the key for SCM_I_CURRENT_THREAD if we have TLS,
  149. we do use it for cleanup purposes. */
  150. SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key;
  151. # ifdef SCM_HAVE_THREAD_STORAGE_CLASS
  152. SCM_INTERNAL SCM_THREAD_LOCAL scm_i_thread *scm_i_current_thread;
  153. # define SCM_I_CURRENT_THREAD (scm_i_current_thread)
  154. # else /* !SCM_HAVE_THREAD_STORAGE_CLASS */
  155. # define SCM_I_CURRENT_THREAD \
  156. ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
  157. # endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */
  158. #endif /* BUILDING_LIBGUILE */
  159. SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
  160. /* Convenience functions for working with the pthread API in guile
  161. mode.
  162. */
  163. #if SCM_USE_PTHREAD_THREADS
  164. SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
  165. SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
  166. SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
  167. pthread_mutex_t *mutex);
  168. SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
  169. pthread_mutex_t *mutex,
  170. const scm_t_timespec *abstime);
  171. #endif
  172. /* More convenience functions.
  173. */
  174. SCM_API unsigned int scm_std_sleep (unsigned int);
  175. SCM_API unsigned long scm_std_usleep (unsigned long);
  176. SCM_API SCM scm_total_processor_count (void);
  177. SCM_API SCM scm_current_processor_count (void);
  178. #endif /* SCM_THREADS_H */
  179. /*
  180. Local Variables:
  181. c-file-style: "gnu"
  182. End:
  183. */