1 /* classes: h_files */
2 
3 #ifndef SCM_NULL_THREADS_H
4 #define SCM_NULL_THREADS_H
5 
6 /* Copyright (C) 2005, 2006, 2010 Free Software Foundation, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 3 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 
24 
25 
26 /* The null-threads implementation.  We provide the subset of the
27    standard pthread API that is used by Guile, but no new threads can
28    be created.
29 
30    This file merely exits so that Guile can be compiled and run
31    without using pthreads.  Improving performance via optimizations
32    that are possible in a single-threaded program is not a primary
33    goal.
34 */
35 
36 #include <stdlib.h>
37 #include <signal.h>
38 #include <errno.h>
39 
40 /* Threads
41 */
42 typedef int scm_i_pthread_t;
43 typedef void scm_i_pthread_attr_t;
44 
45 static inline scm_i_pthread_t
scm_i_pthread_self(void)46 scm_i_pthread_self (void)
47 {
48   return 0;
49 }
50 
51 static inline int
scm_i_pthread_create(scm_i_pthread_t * t,const scm_i_pthread_attr_t * attr,void * (* f)(void *),void * arg)52 scm_i_pthread_create (scm_i_pthread_t *t, const scm_i_pthread_attr_t *attr,
53                       void* (*f) (void*), void *arg)
54 {
55   return ENOSYS;
56 }
57 
58 static inline int
scm_i_pthread_detach(scm_i_pthread_t t)59 scm_i_pthread_detach (scm_i_pthread_t t)
60 {
61   return 0;
62 }
63 
64 static inline void
scm_i_pthread_exit(void * retval)65 scm_i_pthread_exit (void *retval)
66 {
67   exit (EXIT_SUCCESS);
68 }
69 
70 static inline int
scm_i_pthread_cancel(scm_i_pthread_t t)71 scm_i_pthread_cancel (scm_i_pthread_t t)
72 {
73   return 0;
74 }
75 
76 static inline int
scm_i_sched_yield(void)77 scm_i_sched_yield (void)
78 {
79   return 0;
80 }
81 
82 
83 /* Signals
84  */
85 static inline int
scm_i_pthread_sigmask(int how,const sigset_t * set,sigset_t * oldset)86 scm_i_pthread_sigmask (int how, const sigset_t *set, sigset_t *oldset)
87 {
88   return sigprocmask (how, set, oldset);
89 }
90 
91 /* Mutexes
92  */
93 typedef enum {
94   SCM_I_PTHREAD_MUTEX_INITIALIZER = 0,
95   SCM_I_PTHREAD_MUTEX_LOCKED = 1
96 } scm_i_pthread_mutex_t;
97 typedef int scm_i_pthread_mutexattr_t;
98 
99 static inline int
scm_i_pthread_mutex_init(scm_i_pthread_mutex_t * m,scm_i_pthread_mutexattr_t * attr)100 scm_i_pthread_mutex_init (scm_i_pthread_mutex_t *m,
101                           scm_i_pthread_mutexattr_t *attr)
102 {
103   *m = SCM_I_PTHREAD_MUTEX_INITIALIZER;
104   return 0;
105 }
106 
107 static inline int
scm_i_pthread_mutex_destroy(scm_i_pthread_mutex_t * m)108 scm_i_pthread_mutex_destroy (scm_i_pthread_mutex_t *m)
109 {
110   return 0;
111 }
112 
113 static inline int
scm_i_pthread_mutex_trylock(scm_i_pthread_mutex_t * m)114 scm_i_pthread_mutex_trylock(scm_i_pthread_mutex_t *m)
115 {
116   if (*m == SCM_I_PTHREAD_MUTEX_LOCKED)
117     return EDEADLK;
118   *m = SCM_I_PTHREAD_MUTEX_LOCKED;
119   return 0;
120 }
121 
122 static inline int
scm_i_pthread_mutex_lock(scm_i_pthread_mutex_t * m)123 scm_i_pthread_mutex_lock (scm_i_pthread_mutex_t *m)
124 {
125   *m = SCM_I_PTHREAD_MUTEX_LOCKED;
126   return 0;
127 }
128 
129 static inline int
scm_i_pthread_mutex_unlock(scm_i_pthread_mutex_t * m)130 scm_i_pthread_mutex_unlock (scm_i_pthread_mutex_t *m)
131 {
132   *m = SCM_I_PTHREAD_MUTEX_INITIALIZER;
133   return 0;
134 }
135 
136 #define scm_i_pthread_mutexattr_recursive   0
137 
138 /* Condition variables
139  */
140 typedef enum {
141   SCM_I_PTHREAD_COND_INITIALIZER = 0
142 } scm_i_pthread_cond_t;
143 typedef int scm_i_pthread_condattr_t;
144 
145 static inline int
scm_i_pthread_cond_init(scm_i_pthread_cond_t * c,scm_i_pthread_condattr_t * attr)146 scm_i_pthread_cond_init (scm_i_pthread_cond_t *c,
147                          scm_i_pthread_condattr_t *attr)
148 {
149   *c = SCM_I_PTHREAD_COND_INITIALIZER;
150   return 0;
151 }
152 
153 static inline int
scm_i_pthread_cond_destroy(scm_i_pthread_cond_t * c)154 scm_i_pthread_cond_destroy (scm_i_pthread_cond_t *c)
155 {
156   return 0;
157 }
158 
159 static inline int
scm_i_pthread_cond_signal(scm_i_pthread_cond_t * c)160 scm_i_pthread_cond_signal (scm_i_pthread_cond_t *c)
161 {
162   return 0;
163 }
164 
165 static inline int
scm_i_pthread_cond_broadcast(scm_i_pthread_cond_t * c)166 scm_i_pthread_cond_broadcast (scm_i_pthread_cond_t *c)
167 {
168   return 0;
169 }
170 
171 static inline int
scm_i_pthread_cond_wait(scm_i_pthread_cond_t * c,scm_i_pthread_mutex_t * m)172 scm_i_pthread_cond_wait (scm_i_pthread_cond_t *c, scm_i_pthread_mutex_t *m)
173 {
174   abort ();
175   return 0;
176 }
177 
178 static inline int
scm_i_pthread_cond_timedwait(scm_i_pthread_cond_t * c,scm_i_pthread_mutex_t * m,const scm_t_timespec * t)179 scm_i_pthread_cond_timedwait (scm_i_pthread_cond_t *c, scm_i_pthread_mutex_t *m,
180                               const scm_t_timespec *t)
181 {
182   abort();
183   return 0;
184 }
185 
186 /* Onces
187  */
188 typedef enum {
189   SCM_I_PTHREAD_ONCE_INIT = 0,
190   SCM_I_PTHREAD_ONCE_ALREADY = 1
191 } scm_i_pthread_once_t;
192 
193 static inline int
scm_i_pthread_once(scm_i_pthread_once_t * o,void (* init)(void))194 scm_i_pthread_once (scm_i_pthread_once_t *o, void(*init)(void))
195 {
196   if (*o == SCM_I_PTHREAD_ONCE_INIT)
197     {
198       *o = SCM_I_PTHREAD_ONCE_ALREADY;
199       init ();
200     }
201   return 0;
202 }
203 
204 /* Thread specific storage
205  */
206 typedef struct scm_i_pthread_key_t {
207   struct scm_i_pthread_key_t *next;
208   void *value;
209   void (*destr_func) (void *);
210 } scm_i_pthread_key_t;
211 
212 SCM_API int scm_i_pthread_key_create (scm_i_pthread_key_t *key,
213 				      void (*destr_func) (void *));
214 #define scm_i_pthread_setspecific(k,p)      ((k).value = (p))
215 #define scm_i_pthread_getspecific(k)        ((k).value)
216 
217 /* Convenience functions
218  */
219 #define scm_i_scm_pthread_mutex_lock        scm_i_pthread_mutex_lock
220 #define scm_i_dynwind_pthread_mutex_lock    scm_i_pthread_mutex_lock
221 #define scm_i_scm_pthread_cond_wait         scm_i_pthread_cond_wait
222 #define scm_i_scm_pthread_cond_timedwait    scm_i_pthread_cond_timedwait
223 
224 
225 #endif  /* SCM_NULL_THREADS_H */
226 
227 /*
228   Local Variables:
229   c-file-style: "gnu"
230   End:
231 */
232