ecd21cdd90dc1c87ddc8fc705063a8e7e453f815
[bpt/guile.git] / libguile / mit-pthreads.h
1 /* classes: h_files */
2
3 #ifndef MIT_PTHREADSH
4 #define MIT_PTHREADSH
5
6 /* Copyright (C) 1996 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 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
48 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
49 \f
50
51 #include "libguile/__scm.h"
52
53 #define PTHREAD_KERNEL
54 #include <pthread.h>
55
56 /* Identify where the stack pointer can be found in a jmpbuf.
57 */
58
59 /* Solaris 2.4 */
60 #if defined(__sparc_setjmp_h)
61 # define THREAD_SP machdep_data.machdep_state[2]
62 #endif
63
64 /* Solaris 2.5 */
65 #if defined(__sparc)
66 #ifndef THREAD_SP
67 # define THREAD_SP machdep_data.machdep_state[2]
68 #endif
69 #endif
70
71 #if defined(linux)
72 # define THREAD_SP machdep_data.machdep_state[0].__sp
73 #endif
74
75 #if defined(sgi)
76 # define THREAD_SP machdep_data.machdep_state[JB_SP]
77 #endif
78
79 /* ...define THREAD_SP for your architecture here...
80 */
81
82 #if !defined(THREAD_SP)
83 --> where is your stack pointer?
84 #endif
85
86 \f
87
88 /* Boost the priority of this thread so that it is the only
89 one running. PTHREAD_MAX_PRIORITY is reserved for this
90 purpose */
91
92 #define SCM_THREAD_CRITICAL_SECTION_START \
93 struct sched_param param; \
94 int previous_prio; \
95 int policy; \
96 pthread_getschedparam(pthread_self(), &policy, &param); \
97 previous_prio = param.prio; \
98 param.prio = PTHREAD_MAX_PRIORITY; \
99 pthread_setschedparam(pthread_self(), policy, &param)
100
101 #define SCM_THREAD_CRITICAL_SECTION_END \
102 param.prio = previous_prio; \
103 pthread_setschedparam(pthread_self(), policy, &param)
104
105 \f
106
107 #if 1
108
109 #define SCM_NO_CRITICAL_SECTION_OWNER 0
110
111 #define SCM_THREAD_DEFER pthread_kernel_lock++
112 #define SCM_THREAD_ALLOW pthread_kernel_lock--
113
114 #define SCM_THREAD_REDEFER pthread_kernel_lock++
115 #define SCM_THREAD_REALLOW_1 pthread_kernel_lock--
116 #define SCM_THREAD_REALLOW_2 \
117 do { \
118 scm_critical_section_owner = SCM_NO_CRITICAL_SECTION_OWNER; \
119 pthread_mutex_unlock(&scm_critical_section_mutex); \
120 } while (0)
121
122 #else
123
124 #define SCM_NO_CRITICAL_SECTION_OWNER 0
125
126 #define SCM_THREAD_DEFER \
127 do { \
128 pthread_mutex_lock (&scm_critical_section_mutex); \
129 scm_critical_section_owner = pthread_self(); \
130 } while (0)
131
132 #define SCM_THREAD_ALLOW \
133 do { \
134 scm_critical_section_owner = SCM_NO_CRITICAL_SECTION_OWNER; \
135 pthread_mutex_unlock (&scm_critical_section_mutex); \
136 } while (0)
137
138 #define SCM_THREAD_REDEFER \
139 do { \
140 if ((scm_critical_section_owner != pthread_self()) || \
141 (scm_critical_section_owner == SCM_NO_CRITICAL_SECTION_OWNER)) \
142 { \
143 pthread_mutex_lock(&scm_critical_section_mutex); \
144 scm_critical_section_owner = pthread_self(); \
145 } \
146 } while (0)
147
148 #define SCM_THREAD_REALLOW_1
149 #define SCM_THREAD_REALLOW_2 \
150 do { \
151 scm_critical_section_owner = SCM_NO_CRITICAL_SECTION_OWNER; \
152 pthread_mutex_unlock (&scm_critical_section_mutex); \
153 } while (0)
154
155 #endif
156
157 #define SCM_THREAD_SWITCHING_CODE
158
159 #define SCM_THREAD_LOCAL_DATA (pthread_self () -> attr.arg_attr)
160 #define SCM_SET_THREAD_LOCAL_DATA(new_root) \
161 do { \
162 pthread_t t = pthread_self (); \
163 void *r = (new_root); \
164 pthread_attr_setcleanup (&t -> attr, NULL, r); \
165 pthreads_find_info (t) -> root = r; \
166 } while (0)
167
168
169 \f
170
171 void scm_threads_init_mit_pthreads ();
172
173 typedef struct QUEUE {
174 struct QUEUE *flink, *blink;
175 } queue;
176
177 extern pthread_mutex_t scm_critical_section_mutex;
178 extern pthread_t scm_critical_section_owner;
179
180 /* Key to thread specific data */
181 extern pthread_key_t info_key;
182
183 struct scm_pthread_create_info_type
184 {
185 SCM thunk;
186 SCM error;
187 SCM *prots;
188 } scm_pthread_create_info;
189
190 #endif /* MIT_PTHREADSH */