Commit | Line | Data |
---|---|---|
eac85310 MV |
1 | /* classes: h_files */ |
2 | ||
3 | #ifndef SCM_THREADS_PTHREADS_H | |
4 | #define SCM_THREADS_PTHREADS_H | |
5 | ||
6 | /* Copyright (C) 2002 Free Software Foundation, Inc. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this software; see the file COPYING. If not, write to | |
20 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
21 | * Boston, MA 02111-1307 USA | |
22 | * | |
23 | * As a special exception, the Free Software Foundation gives permission | |
24 | * for additional uses of the text contained in its release of GUILE. | |
25 | * | |
26 | * The exception is that, if you link the GUILE library with other files | |
27 | * to produce an executable, this does not by itself cause the | |
28 | * resulting executable to be covered by the GNU General Public License. | |
29 | * Your use of that executable is in no way restricted on account of | |
30 | * linking the GUILE library code into it. | |
31 | * | |
32 | * This exception does not however invalidate any other reasons why | |
33 | * the executable file might be covered by the GNU General Public License. | |
34 | * | |
35 | * This exception applies only to the code released by the | |
36 | * Free Software Foundation under the name GUILE. If you copy | |
37 | * code from other Free Software Foundation releases into a copy of | |
38 | * GUILE, as the General Public License permits, the exception does | |
39 | * not apply to the code that you add in this way. To avoid misleading | |
40 | * anyone as to the status of such modified files, you must delete | |
41 | * this exception notice from them. | |
42 | * | |
43 | * If you write modifications of your own for GUILE, it is your choice | |
44 | * whether to permit this exception to apply to your modifications. | |
45 | * If you do not wish that, delete this exception notice. */ | |
46 | ||
47 | \f | |
48 | ||
9bc4701c | 49 | /* The pthreads-threads implementation. This is a direct mapping. |
eac85310 MV |
50 | */ |
51 | ||
9bc4701c MD |
52 | /* This is an interface between Guile and the pthreads thread package. */ |
53 | ||
eac85310 | 54 | #include <pthread.h> |
0b6843b1 | 55 | #include <sched.h> |
eac85310 | 56 | |
9bc4701c MD |
57 | /* MDJ 021209 <djurfeldt@nada.kth.se>: |
58 | The separation of the plugin interface and the low-level C API | |
59 | (currently in threads.h) needs to be completed in a sensible way. | |
60 | */ | |
61 | ||
62 | /* The scm_t_ types are temporarily used both in plugin and low-level API */ | |
63 | #define scm_t_thread pthread_t | |
eac85310 | 64 | |
9bc4701c | 65 | #define scm_i_plugin_thread_create pthread_create |
eac85310 | 66 | |
9bc4701c MD |
67 | #define scm_i_plugin_thread_join pthread_join |
68 | #define scm_i_plugin_thread_detach pthread_detach | |
0b6843b1 MD |
69 | #define scm_i_plugin_thread_self pthread_self |
70 | #define scm_i_plugin_thread_yield sched_yield | |
eac85310 | 71 | |
0b6843b1 MD |
72 | /* Size is checked in scm_init_pthread_threads */ |
73 | #ifdef SCM_DEBUG_THREADS | |
74 | #define SCM_MUTEX_MAXSIZE (9 * sizeof (long)) | |
75 | #else | |
76 | #define SCM_MUTEX_MAXSIZE (6 * sizeof (long)) | |
77 | #endif | |
78 | typedef struct { char _[SCM_MUTEX_MAXSIZE]; } scm_t_mutex; | |
28d52ebb MD |
79 | #define scm_t_mutexattr pthread_mutexattr_t |
80 | ||
81 | extern scm_t_mutexattr scm_i_plugin_mutex; /* The "fast" mutex. */ | |
eac85310 | 82 | |
6da2dfc4 MD |
83 | /* This debug stuff made things a bit messy. This needs some |
84 | reorganization. */ | |
0b6843b1 MD |
85 | #ifdef SCM_DEBUG_THREADS |
86 | int scm_i_plugin_mutex_init (scm_t_mutex *, const scm_t_mutexattr *); | |
87 | int scm_i_plugin_mutex_lock (scm_t_mutex *); | |
88 | int scm_i_plugin_mutex_unlock (scm_t_mutex *); | |
89 | #else | |
90 | #define scm_i_plugin_mutex_init(m,a) \ | |
91 | pthread_mutex_init ((pthread_mutex_t *) (m), (a)) | |
92 | #define scm_i_plugin_mutex_lock(m) \ | |
93 | pthread_mutex_lock ((pthread_mutex_t *) (m)) | |
94 | #define scm_i_plugin_mutex_unlock(m) \ | |
95 | pthread_mutex_unlock ((pthread_mutex_t *) (m)) | |
96 | #endif | |
97 | #define scm_i_plugin_mutex_destroy(m) \ | |
98 | pthread_mutex_destroy ((pthread_mutex_t *) (m)) | |
99 | #define scm_i_plugin_mutex_trylock(m) \ | |
100 | pthread_mutex_trylock ((pthread_mutex_t *) (m)) | |
eac85310 | 101 | |
93cd4dcd | 102 | /* Size is checked in scm_init_pthread_threads */ |
0b6843b1 MD |
103 | #ifdef SCM_DEBUG_THREADS |
104 | #define SCM_REC_MUTEX_MAXSIZE (SCM_MUTEX_MAXSIZE + 3 * sizeof (long)) | |
105 | #else | |
106 | #ifdef SCM_MUTEX_RECURSIVE | |
107 | #define SCM_REC_MUTEX_MAXSIZE SCM_MUTEX_MAXSIZE | |
108 | #else | |
109 | #define SCM_REC_MUTEX_MAXSIZE (SCM_MUTEX_MAXSIZE + 2 * sizeof (long)) | |
110 | #endif | |
111 | #endif | |
28d52ebb MD |
112 | typedef struct { char _[SCM_REC_MUTEX_MAXSIZE]; } scm_t_rec_mutex; |
113 | ||
114 | extern scm_t_mutexattr scm_i_plugin_rec_mutex; | |
115 | ||
0b6843b1 MD |
116 | #if defined (SCM_MUTEX_RECURSIVE) && !defined (SCM_DEBUG_THREADS) |
117 | /* pthreads has recursive mutexes! */ | |
93cd4dcd MD |
118 | #define scm_i_plugin_rec_mutex_init(m,a) \ |
119 | pthread_mutex_init ((pthread_mutex_t *) (m), (a)) | |
120 | #define scm_i_plugin_rec_mutex_destroy(m) \ | |
121 | pthread_mutex_destroy ((pthread_mutex_t *) (m)) | |
122 | #define scm_i_plugin_rec_mutex_lock(m) \ | |
123 | pthread_mutex_lock ((pthread_mutex_t *) (m)) | |
124 | #define scm_i_plugin_rec_mutex_trylock(m) \ | |
125 | pthread_mutex_trylock ((pthread_mutex_t *) (m)) | |
126 | #define scm_i_plugin_rec_mutex_unlock(m) \ | |
127 | pthread_mutex_unlock ((pthread_mutex_t *) (m)) | |
28d52ebb MD |
128 | #else |
129 | int scm_i_plugin_rec_mutex_init (scm_t_rec_mutex *, const scm_t_mutexattr *); | |
130 | #define scm_i_plugin_rec_mutex_destroy(mx) do { (void) (mx); } while (0) | |
131 | int scm_i_plugin_rec_mutex_lock (scm_t_rec_mutex *); | |
132 | int scm_i_plugin_rec_mutex_trylock (scm_t_rec_mutex *); | |
133 | int scm_i_plugin_rec_mutex_unlock (scm_t_rec_mutex *); | |
134 | #endif | |
135 | ||
9bc4701c | 136 | #define scm_t_cond pthread_cond_t |
eac85310 | 137 | |
9bc4701c | 138 | #define scm_i_plugin_cond_init pthread_cond_init |
6da2dfc4 MD |
139 | #define scm_i_plugin_cond_destroy pthread_cond_destroy |
140 | #ifdef SCM_DEBUG_THREADS | |
141 | int scm_i_plugin_cond_wait (scm_t_cond *, scm_t_mutex *); | |
142 | int scm_i_plugin_cond_timedwait (scm_t_cond *, | |
143 | scm_t_mutex *, | |
144 | const struct timespec *); | |
145 | #else | |
0b6843b1 MD |
146 | #define scm_i_plugin_cond_wait(c, m) \ |
147 | pthread_cond_wait ((c), (pthread_mutex_t *) (m)) | |
148 | #define scm_i_plugin_cond_timedwait(c, m, t) \ | |
149 | pthread_cond_timedwait ((c), (pthread_mutex_t *) (m), (t)) | |
6da2dfc4 | 150 | #endif |
9bc4701c MD |
151 | #define scm_i_plugin_cond_signal pthread_cond_signal |
152 | #define scm_i_plugin_cond_broadcast pthread_cond_broadcast | |
eac85310 | 153 | |
9bc4701c | 154 | #define scm_t_key pthread_key_t |
eac85310 | 155 | |
9bc4701c MD |
156 | #define scm_i_plugin_key_create pthread_key_create |
157 | #define scm_i_plugin_key_delete pthread_key_delete | |
158 | #define scm_i_plugin_setspecific pthread_setspecific | |
159 | #define scm_i_plugin_getspecific pthread_getspecific | |
eac85310 | 160 | |
9bc4701c | 161 | #define scm_i_plugin_select select |
eac85310 | 162 | |
28d52ebb MD |
163 | void scm_init_pthread_threads (void); |
164 | ||
93cd4dcd | 165 | #endif /* SCM_THREADS_PTHREADS_H */ |
eac85310 MV |
166 | |
167 | /* | |
168 | Local Variables: | |
169 | c-file-style: "gnu" | |
170 | End: | |
171 | */ |