null-threads.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifndef SCM_NULL_THREADS_H
  2. #define SCM_NULL_THREADS_H
  3. /* Copyright 2005-2006,2010,2018,2020
  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. /* The null-threads implementation. We provide the subset of the
  18. standard pthread API that is used by Guile, but no new threads can
  19. be created.
  20. This file merely exits so that Guile can be compiled and run
  21. without using pthreads. Improving performance via optimizations
  22. that are possible in a single-threaded program is not a primary
  23. goal.
  24. */
  25. #include <stdlib.h>
  26. #include <signal.h>
  27. #include <errno.h>
  28. #include "libguile/scm.h"
  29. /* Threads
  30. */
  31. typedef int scm_i_pthread_t;
  32. typedef void scm_i_pthread_attr_t;
  33. static inline scm_i_pthread_t
  34. scm_i_pthread_self (void)
  35. {
  36. return 0;
  37. }
  38. static inline int
  39. scm_i_pthread_create (scm_i_pthread_t *t, const scm_i_pthread_attr_t *attr,
  40. void* (*f) (void*), void *arg)
  41. {
  42. return ENOSYS;
  43. }
  44. static inline int
  45. scm_i_pthread_detach (scm_i_pthread_t t)
  46. {
  47. return 0;
  48. }
  49. static inline void
  50. scm_i_pthread_exit (void *retval)
  51. {
  52. exit (EXIT_SUCCESS);
  53. }
  54. static inline int
  55. scm_i_pthread_cancel (scm_i_pthread_t t)
  56. {
  57. return 0;
  58. }
  59. static inline int
  60. scm_i_sched_yield (void)
  61. {
  62. return 0;
  63. }
  64. /* Signals
  65. */
  66. #if SCM_HAVE_PTHREAD_SIGMASK == 1
  67. static inline int
  68. scm_i_pthread_sigmask (int how, const sigset_t *set, sigset_t *oldset)
  69. {
  70. return sigprocmask (how, set, oldset);
  71. }
  72. #endif
  73. /* Mutexes
  74. */
  75. typedef enum {
  76. SCM_I_PTHREAD_MUTEX_INITIALIZER = 0,
  77. SCM_I_PTHREAD_MUTEX_LOCKED = 1
  78. } scm_i_pthread_mutex_t;
  79. typedef int scm_i_pthread_mutexattr_t;
  80. static inline int
  81. scm_i_pthread_mutex_init (scm_i_pthread_mutex_t *m,
  82. scm_i_pthread_mutexattr_t *attr)
  83. {
  84. *m = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  85. return 0;
  86. }
  87. static inline int
  88. scm_i_pthread_mutex_destroy (scm_i_pthread_mutex_t *m)
  89. {
  90. return 0;
  91. }
  92. static inline int
  93. scm_i_pthread_mutex_trylock(scm_i_pthread_mutex_t *m)
  94. {
  95. if (*m == SCM_I_PTHREAD_MUTEX_LOCKED)
  96. return EDEADLK;
  97. *m = SCM_I_PTHREAD_MUTEX_LOCKED;
  98. return 0;
  99. }
  100. static inline int
  101. scm_i_pthread_mutex_lock (scm_i_pthread_mutex_t *m)
  102. {
  103. *m = SCM_I_PTHREAD_MUTEX_LOCKED;
  104. return 0;
  105. }
  106. static inline int
  107. scm_i_pthread_mutex_unlock (scm_i_pthread_mutex_t *m)
  108. {
  109. *m = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  110. return 0;
  111. }
  112. #define scm_i_pthread_mutexattr_recursive 0
  113. /* Condition variables
  114. */
  115. typedef enum {
  116. SCM_I_PTHREAD_COND_INITIALIZER = 0
  117. } scm_i_pthread_cond_t;
  118. typedef int scm_i_pthread_condattr_t;
  119. static inline int
  120. scm_i_pthread_cond_init (scm_i_pthread_cond_t *c,
  121. scm_i_pthread_condattr_t *attr)
  122. {
  123. *c = SCM_I_PTHREAD_COND_INITIALIZER;
  124. return 0;
  125. }
  126. static inline int
  127. scm_i_pthread_cond_destroy (scm_i_pthread_cond_t *c)
  128. {
  129. return 0;
  130. }
  131. static inline int
  132. scm_i_pthread_cond_signal (scm_i_pthread_cond_t *c)
  133. {
  134. return 0;
  135. }
  136. static inline int
  137. scm_i_pthread_cond_broadcast (scm_i_pthread_cond_t *c)
  138. {
  139. return 0;
  140. }
  141. static inline int
  142. scm_i_pthread_cond_wait (scm_i_pthread_cond_t *c, scm_i_pthread_mutex_t *m)
  143. {
  144. abort ();
  145. return 0;
  146. }
  147. static inline int
  148. scm_i_pthread_cond_timedwait (scm_i_pthread_cond_t *c, scm_i_pthread_mutex_t *m,
  149. const scm_t_timespec *t)
  150. {
  151. abort();
  152. return 0;
  153. }
  154. /* Onces
  155. */
  156. typedef enum {
  157. SCM_I_PTHREAD_ONCE_INIT = 0,
  158. SCM_I_PTHREAD_ONCE_ALREADY = 1
  159. } scm_i_pthread_once_t;
  160. static inline int
  161. scm_i_pthread_once (scm_i_pthread_once_t *o, void(*init)(void))
  162. {
  163. if (*o == SCM_I_PTHREAD_ONCE_INIT)
  164. {
  165. *o = SCM_I_PTHREAD_ONCE_ALREADY;
  166. init ();
  167. }
  168. return 0;
  169. }
  170. /* Thread specific storage
  171. */
  172. typedef struct scm_i_pthread_key_t {
  173. struct scm_i_pthread_key_t *next;
  174. void *value;
  175. void (*destr_func) (void *);
  176. } scm_i_pthread_key_t;
  177. SCM_API int scm_i_pthread_key_create (scm_i_pthread_key_t *key,
  178. void (*destr_func) (void *));
  179. #define scm_i_pthread_setspecific(k,p) ((k).value = (p))
  180. #define scm_i_pthread_getspecific(k) ((k).value)
  181. /* Convenience functions
  182. */
  183. #define scm_i_scm_pthread_mutex_lock scm_i_pthread_mutex_lock
  184. #define scm_i_dynwind_pthread_mutex_lock scm_i_pthread_mutex_lock
  185. #define scm_i_scm_pthread_cond_wait scm_i_pthread_cond_wait
  186. #define scm_i_scm_pthread_cond_timedwait scm_i_pthread_cond_timedwait
  187. #endif /* SCM_NULL_THREADS_H */