null-threads.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #ifndef SCM_NULL_THREADS_H
  2. #define SCM_NULL_THREADS_H
  3. /* Copyright 2005-2006,2010,2018
  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. static inline int
  67. scm_i_pthread_sigmask (int how, const sigset_t *set, sigset_t *oldset)
  68. {
  69. return sigprocmask (how, set, oldset);
  70. }
  71. /* Mutexes
  72. */
  73. typedef enum {
  74. SCM_I_PTHREAD_MUTEX_INITIALIZER = 0,
  75. SCM_I_PTHREAD_MUTEX_LOCKED = 1
  76. } scm_i_pthread_mutex_t;
  77. typedef int scm_i_pthread_mutexattr_t;
  78. static inline int
  79. scm_i_pthread_mutex_init (scm_i_pthread_mutex_t *m,
  80. scm_i_pthread_mutexattr_t *attr)
  81. {
  82. *m = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  83. return 0;
  84. }
  85. static inline int
  86. scm_i_pthread_mutex_destroy (scm_i_pthread_mutex_t *m)
  87. {
  88. return 0;
  89. }
  90. static inline int
  91. scm_i_pthread_mutex_trylock(scm_i_pthread_mutex_t *m)
  92. {
  93. if (*m == SCM_I_PTHREAD_MUTEX_LOCKED)
  94. return EDEADLK;
  95. *m = SCM_I_PTHREAD_MUTEX_LOCKED;
  96. return 0;
  97. }
  98. static inline int
  99. scm_i_pthread_mutex_lock (scm_i_pthread_mutex_t *m)
  100. {
  101. *m = SCM_I_PTHREAD_MUTEX_LOCKED;
  102. return 0;
  103. }
  104. static inline int
  105. scm_i_pthread_mutex_unlock (scm_i_pthread_mutex_t *m)
  106. {
  107. *m = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  108. return 0;
  109. }
  110. #define scm_i_pthread_mutexattr_recursive 0
  111. /* Condition variables
  112. */
  113. typedef enum {
  114. SCM_I_PTHREAD_COND_INITIALIZER = 0
  115. } scm_i_pthread_cond_t;
  116. typedef int scm_i_pthread_condattr_t;
  117. static inline int
  118. scm_i_pthread_cond_init (scm_i_pthread_cond_t *c,
  119. scm_i_pthread_condattr_t *attr)
  120. {
  121. *c = SCM_I_PTHREAD_COND_INITIALIZER;
  122. return 0;
  123. }
  124. static inline int
  125. scm_i_pthread_cond_destroy (scm_i_pthread_cond_t *c)
  126. {
  127. return 0;
  128. }
  129. static inline int
  130. scm_i_pthread_cond_signal (scm_i_pthread_cond_t *c)
  131. {
  132. return 0;
  133. }
  134. static inline int
  135. scm_i_pthread_cond_broadcast (scm_i_pthread_cond_t *c)
  136. {
  137. return 0;
  138. }
  139. static inline int
  140. scm_i_pthread_cond_wait (scm_i_pthread_cond_t *c, scm_i_pthread_mutex_t *m)
  141. {
  142. abort ();
  143. return 0;
  144. }
  145. static inline int
  146. scm_i_pthread_cond_timedwait (scm_i_pthread_cond_t *c, scm_i_pthread_mutex_t *m,
  147. const scm_t_timespec *t)
  148. {
  149. abort();
  150. return 0;
  151. }
  152. /* Onces
  153. */
  154. typedef enum {
  155. SCM_I_PTHREAD_ONCE_INIT = 0,
  156. SCM_I_PTHREAD_ONCE_ALREADY = 1
  157. } scm_i_pthread_once_t;
  158. static inline int
  159. scm_i_pthread_once (scm_i_pthread_once_t *o, void(*init)(void))
  160. {
  161. if (*o == SCM_I_PTHREAD_ONCE_INIT)
  162. {
  163. *o = SCM_I_PTHREAD_ONCE_ALREADY;
  164. init ();
  165. }
  166. return 0;
  167. }
  168. /* Thread specific storage
  169. */
  170. typedef struct scm_i_pthread_key_t {
  171. struct scm_i_pthread_key_t *next;
  172. void *value;
  173. void (*destr_func) (void *);
  174. } scm_i_pthread_key_t;
  175. SCM_API int scm_i_pthread_key_create (scm_i_pthread_key_t *key,
  176. void (*destr_func) (void *));
  177. #define scm_i_pthread_setspecific(k,p) ((k).value = (p))
  178. #define scm_i_pthread_getspecific(k) ((k).value)
  179. /* Convenience functions
  180. */
  181. #define scm_i_scm_pthread_mutex_lock scm_i_pthread_mutex_lock
  182. #define scm_i_dynwind_pthread_mutex_lock scm_i_pthread_mutex_lock
  183. #define scm_i_scm_pthread_cond_wait scm_i_pthread_cond_wait
  184. #define scm_i_scm_pthread_cond_timedwait scm_i_pthread_cond_timedwait
  185. #endif /* SCM_NULL_THREADS_H */