coop-defs.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* classes: h_files */
  2. #ifndef COOP_DEFSH
  3. #define COOP_DEFSH
  4. /* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
  5. *
  6. * This program 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 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program 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 this software; see the file COPYING. If not, write to
  18. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307 USA
  20. *
  21. * As a special exception, the Free Software Foundation gives permission
  22. * for additional uses of the text contained in its release of GUILE.
  23. *
  24. * The exception is that, if you link the GUILE library with other files
  25. * to produce an executable, this does not by itself cause the
  26. * resulting executable to be covered by the GNU General Public License.
  27. * Your use of that executable is in no way restricted on account of
  28. * linking the GUILE library code into it.
  29. *
  30. * This exception does not however invalidate any other reasons why
  31. * the executable file might be covered by the GNU General Public License.
  32. *
  33. * This exception applies only to the code released by the
  34. * Free Software Foundation under the name GUILE. If you copy
  35. * code from other Free Software Foundation releases into a copy of
  36. * GUILE, as the General Public License permits, the exception does
  37. * not apply to the code that you add in this way. To avoid misleading
  38. * anyone as to the status of such modified files, you must delete
  39. * this exception notice from them.
  40. *
  41. * If you write modifications of your own for GUILE, it is your choice
  42. * whether to permit this exception to apply to your modifications.
  43. * If you do not wish that, delete this exception notice. */
  44. # ifdef TIME_WITH_SYS_TIME
  45. # include <sys/time.h>
  46. # include <time.h>
  47. # else
  48. # ifdef HAVE_SYS_TIME_H
  49. # include <sys/time.h>
  50. # else
  51. # ifdef HAVE_TIME_H
  52. # include <time.h>
  53. # endif
  54. # endif
  55. # endif
  56. #ifdef GUILE_ISELECT
  57. #include "libguile/iselect.h"
  58. #endif
  59. #ifdef GUILE_PTHREAD_COMPAT
  60. #include <pthread.h>
  61. #endif
  62. /* This file is included by threads.h, which, in turn, is included by
  63. libguile.h while coop-threads.h only is included by
  64. coop-threads.c. */
  65. /* The coop_t struct must be declared here, since macros in this file
  66. refer to the data member. */
  67. /* The notion of a thread is merged with the notion of a queue.
  68. Thread stuff: thread status (sp) and stuff to use during
  69. (re)initialization. Queue stuff: next thread in the queue
  70. (next). */
  71. struct qt_t;
  72. typedef struct coop_t {
  73. struct qt_t *sp; /* QuickThreads handle. */
  74. void *sto; /* `malloc'-allocated stack. */
  75. struct coop_t *next; /* Next thread in the queue. */
  76. struct coop_t *all_next;
  77. struct coop_t *all_prev;
  78. void *data; /* Thread local data */
  79. void **specific; /* Data associated with keys */
  80. int n_keys; /* Upper limit for keys on this thread */
  81. void *base; /* Base of stack */
  82. void *top; /* Top of stack */
  83. void *joining; /* A queue of threads waiting to join this
  84. thread */
  85. #ifdef GUILE_ISELECT
  86. int nfds;
  87. SELECT_TYPE *readfds;
  88. SELECT_TYPE *writefds;
  89. SELECT_TYPE *exceptfds;
  90. int timeoutp;
  91. struct timeval wakeup_time; /* Time to stop sleeping */
  92. int _errno;
  93. int retval;
  94. #else
  95. time_t wakeup_time; /* Time to stop sleeping */
  96. #endif
  97. #ifdef GUILE_PTHREAD_COMPAT
  98. pthread_t dummy_thread;
  99. pthread_mutex_t dummy_mutex;
  100. #endif
  101. } coop_t;
  102. /* A queue is a circular list of threads. The queue head is a
  103. designated list element. If this is a uniprocessor-only
  104. implementation we can store the `main' thread in this, but in a
  105. multiprocessor there are several `heavy' threads but only one run
  106. queue. A fancier implementation might have private run queues,
  107. which would lead to a simpler (trivial) implementation */
  108. typedef struct coop_q_t {
  109. coop_t t;
  110. coop_t *tail;
  111. } coop_q_t;
  112. /* A Mutex variable is made up of a owner thread, and a queue of threads
  113. waiting on the mutex */
  114. typedef struct coop_m {
  115. coop_t *owner; /* Mutex owner */
  116. coop_q_t waiting; /* Queue of waiting threads */
  117. } coop_m;
  118. typedef int coop_mattr;
  119. typedef coop_m scm_mutex_t;
  120. extern int coop_mutex_init (coop_m*);
  121. extern int coop_new_mutex_init (coop_m*, coop_mattr*);
  122. extern int coop_mutex_lock (coop_m*);
  123. extern int coop_mutex_trylock (coop_m*);
  124. extern int coop_mutex_unlock (coop_m*);
  125. extern int coop_mutex_destroy (coop_m*);
  126. #define scm_mutex_init coop_mutex_init
  127. #define scm_mutex_lock coop_mutex_lock
  128. #define scm_mutex_trylock coop_mutex_lock
  129. #define scm_mutex_unlock coop_mutex_unlock
  130. #define scm_mutex_destroy coop_mutex_destroy
  131. /* A Condition variable is made up of a list of threads waiting on the
  132. condition. */
  133. typedef struct coop_c {
  134. coop_q_t waiting; /* Queue of waiting threads */
  135. } coop_c;
  136. typedef int coop_cattr;
  137. typedef coop_c scm_cond_t;
  138. #ifndef HAVE_STRUCT_TIMESPEC
  139. /* POSIX.4 structure for a time value. This is like a `struct timeval' but
  140. has nanoseconds instead of microseconds. */
  141. struct timespec
  142. {
  143. long int tv_sec; /* Seconds. */
  144. long int tv_nsec; /* Nanoseconds. */
  145. };
  146. #endif
  147. extern int coop_condition_variable_init (coop_c*);
  148. extern int coop_new_condition_variable_init (coop_c*, coop_cattr*);
  149. extern int coop_condition_variable_wait_mutex (coop_c*, coop_m*);
  150. extern int coop_condition_variable_timed_wait_mutex (coop_c*,
  151. coop_m*,
  152. const struct timespec *abstime);
  153. extern int coop_condition_variable_signal (coop_c*);
  154. extern int coop_condition_variable_destroy (coop_c*);
  155. #define scm_cond_init coop_new_condition_variable_init
  156. #define scm_cond_wait coop_condition_variable_wait_mutex
  157. #define scm_cond_timedwait coop_condition_variable_timed_wait_mutex
  158. #define scm_cond_signal coop_condition_variable_signal
  159. #define scm_cond_broadcast coop_condition_variable_signal /* yes */
  160. #define scm_cond_destroy coop_condition_variable_destroy
  161. typedef int coop_k;
  162. typedef coop_k scm_key_t;
  163. extern int coop_key_create (coop_k *keyp, void (*destructor) (void *value));
  164. extern int coop_setspecific (coop_k key, const void *value);
  165. extern void *coop_getspecific (coop_k key);
  166. extern int coop_key_delete (coop_k);
  167. #define scm_key_create coop_key_create
  168. #define scm_setspecific coop_setspecific
  169. #define scm_getspecific coop_getspecific
  170. #define scm_key_delete coop_key_delete
  171. extern coop_t *coop_global_curr; /* Currently-executing thread. */
  172. extern void coop_join (coop_t *t);
  173. extern void coop_yield (void);
  174. extern size_t scm_switch_counter;
  175. extern size_t scm_thread_count;
  176. /* Some iselect functions. */
  177. /* I'm not sure whether these three declarations should be here.
  178. They're really defined in iselect.c, so you'd think they'd go in
  179. iselect.h, but they use coop_t, defined above, which uses things
  180. defined in iselect.h. Basically, we're making at best a flailing
  181. (and failing) attempt at modularity here, and I don't have time to
  182. rethink this at the moment. This code awaits a Hero. --JimB */
  183. #ifdef GUILE_ISELECT
  184. void coop_timeout_qinsert (coop_q_t *, coop_t *);
  185. #endif
  186. extern coop_t *coop_next_runnable_thread (void);
  187. extern coop_t *coop_wait_for_runnable_thread_now (struct timeval *);
  188. extern coop_t *coop_wait_for_runnable_thread (void);
  189. /* Cooperative threads don't need to have these defined */
  190. #define SCM_THREAD_CRITICAL_SECTION_START
  191. #define SCM_THREAD_CRITICAL_SECTION_END
  192. #define SCM_NO_CRITICAL_SECTION_OWNER 0
  193. #define SCM_THREAD_SWITCH_COUNT 50 /* was 10 /mdj */
  194. #define SCM_THREAD_DEFER
  195. #define SCM_THREAD_ALLOW
  196. #define SCM_THREAD_REDEFER
  197. #define SCM_THREAD_REALLOW_1
  198. #define SCM_THREAD_REALLOW_2
  199. #if 0
  200. #define SCM_THREAD_SWITCHING_CODE \
  201. do { \
  202. if (scm_thread_count > 1) \
  203. coop_yield(); \
  204. } while (0)
  205. #else
  206. #define SCM_THREAD_SWITCHING_CODE \
  207. do { \
  208. if (scm_thread_count > 1) \
  209. { \
  210. scm_switch_counter--; \
  211. if (scm_switch_counter == 0) \
  212. { \
  213. scm_switch_counter = SCM_THREAD_SWITCH_COUNT; \
  214. coop_yield(); \
  215. } \
  216. } \
  217. } while (0)
  218. #endif
  219. /* For pthreads, this is a value associated with a specific key.
  220. * For coop, we use a special field for increased efficiency.
  221. */
  222. #define SCM_THREAD_LOCAL_DATA (coop_global_curr->data)
  223. #define SCM_SET_THREAD_LOCAL_DATA(ptr) (coop_global_curr->data = (ptr))
  224. #endif /* COOP_DEFSH */
  225. /*
  226. Local Variables:
  227. c-file-style: "gnu"
  228. End:
  229. */