summaryrefslogtreecommitdiff
path: root/libthreads/cthread_internals.h
blob: 81e3b91c218b39f97c32b7c66717d72b09b682b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Mach Operating System
 * Copyright (c) 1992,1991,1990,1989 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 * HISTORY
 * 26-Oct-94  Johannes Helander (jvh) Helsinki University of Technology
 *	Defined WAIT_DEBUG and initialized wait_enum
 *
 * $Log: cthread_internals.h,v $
 * Revision 1.6  2002/05/27 02:50:10  roland
 * 2002-05-26  Roland McGrath  <roland@frob.com>
 *
 * 	Changes merged from CMU MK83a version:
 * 	* cthreads.h, options.h: Various cleanups.
 * 	* call.c, cthread_data.c, sync.c, mig_support.c: Likewise.
 * 	* i386/cthreads.h, i386/thread.c, i386/lock.s: Likewise.
 * 	* cthread_internals.h: Add decls for internal functions.
 * 	(struct cproc): Use vm_offset_t for stack_base and stack_size members.
 * 	Use natural_t for context member.
 * 	* cprocs.c: Use prototypes for all defns.
 * 	* cthreads.c: Likewise.
 * 	(cthread_exit): Cast any_t to integer_t before int.
 *
 * Revision 2.17  93/05/10  21:33:36  rvb
 * 	Context is a natural_t.  Assumming, that is, that on
 * 	some future architecture one word might be enough.
 * 	[93/05/06  09:19:35  af]
 *
 * Revision 2.16  93/05/10  17:51:23  rvb
 * 	Flush stdlib
 * 	[93/05/05  09:12:29  rvb]
 *
 * Revision 2.15  93/01/14  18:04:56  danner
 * 	Added declarations for library-internal routines.
 * 	[92/12/18            pds]
 *
 * 	Replaced malloc and mach_error declarations with includes of
 * 	mach_error.h and stdlib.h.
 * 	[92/06/13            pds]
 * 	64bit cleanup.
 * 	[92/12/01            af]
 *
 * Revision 2.14  92/08/03  18:03:56  jfriedl
 * 	Made state element of struct cproc volatile.
 * 	[92/08/02            jfriedl]
 *
 * Revision 2.13  92/03/06  14:09:24  rpd
 * 	Added yield, defined using thread_switch.
 * 	[92/03/06            rpd]
 *
 * Revision 2.12  92/03/01  00:40:23  rpd
 * 	Removed exit declaration.  It conflicted with the real thing.
 * 	[92/02/29            rpd]
 *
 * Revision 2.11  91/08/28  11:19:23  jsb
 * 	Fixed MACH_CALL to allow multi-line expressions.
 * 	[91/08/23            rpd]
 *
 * Revision 2.10  91/07/31  18:33:33  dbg
 * 	Protect against redefinition of ASSERT.
 * 	[91/07/30  17:33:21  dbg]
 *
 * Revision 2.9  91/05/14  17:56:24  mrt
 * 	Correcting copyright
 *
 * Revision 2.8  91/02/14  14:19:42  mrt
 * 	Added new Mach copyright
 * 	[91/02/13  12:41:02  mrt]
 *
 * Revision 2.7  90/11/05  14:36:55  rpd
 * 	Added spin_lock_t.
 * 	[90/10/31            rwd]
 *
 * Revision 2.6  90/09/09  14:34:51  rpd
 * 	Remove special field.
 * 	[90/08/24            rwd]
 *
 * Revision 2.5  90/06/02  15:13:44  rpd
 * 	Converted to new IPC.
 * 	[90/03/20  20:52:47  rpd]
 *
 * Revision 2.4  90/03/14  21:12:11  rwd
 * 	Added waiting_for field for debugging deadlocks.
 * 	[90/03/01            rwd]
 * 	Added list field to keep a master list of all cprocs.
 * 	[90/03/01            rwd]
 *
 * Revision 2.3  90/01/19  14:37:08  rwd
 * 	Keep track of real thread for use in thread_* substitutes.
 * 	Add CPROC_ARUN for about to run and CPROC_HOLD to avoid holding
 * 	spin_locks over system calls.
 * 	[90/01/03            rwd]
 * 	Add busy field to be used by cthread_msg calls to make sure we
 * 	have the right number of blocked kernel threads.
 * 	[89/12/21            rwd]
 *
 * Revision 2.2  89/12/08  19:53:28  rwd
 * 	Added CPROC_CONDWAIT state
 * 	[89/11/28            rwd]
 * 	Added on_special field.
 * 	[89/11/26            rwd]
 * 	Removed MSGOPT conditionals
 * 	[89/11/25            rwd]
 * 	Removed old debugging code.  Add wired port/flag.  Add state
 * 	for small state machine.
 * 	[89/10/30            rwd]
 * 	Added CPDEBUG code
 * 	[89/10/26            rwd]
 * 	Change TRACE to {x;} else.
 * 	[89/10/24            rwd]
 * 	Rewrote to work for limited number of kernel threads.  This is
 * 	basically a merge of coroutine and thread.  Added
 * 	cthread_receivce call for use by servers.
 * 	[89/10/23            rwd]
 *
 */
/*
 * cthread_internals.h
 *
 *
 * Private definitions for the C Threads implementation.
 *
 * The cproc structure is used for different implementations
 * of the basic schedulable units that execute cthreads.
 *
 */


#include "options.h"
#include <mach/port.h>
#include <mach/message.h>
#include <mach/thread_switch.h>

#if !defined(__STDC__) && !defined(volatile)
# ifdef __GNUC__
#   define volatile __volatile__
# else
#   define volatile /* you lose */
# endif
#endif

/* Type of the TCB.  */
typedef struct
{
	void *tcb;			/* Points to this structure.  */
	void *dtv;			/* Vector of pointers to TLS data.  */
	thread_t self;			/* This thread's control port.  */
} tcbhead_t;

/*
 * Low-level thread implementation.
 * This structure must agree with struct ur_cthread in cthreads.h
 */
typedef struct cproc {
	struct cproc *next;		/* for lock, condition, and ready queues */
	cthread_t incarnation;		/* for cthread_self() */

	struct cproc *list;		/* for master cproc list */
#ifdef	WAIT_DEBUG
	volatile char *waiting_for;	/* address of mutex/cond waiting for */
#endif	 /* WAIT_DEBUG */

#if 0
	/* This is not needed in GNU; libc handles it.  */
	mach_port_t reply_port;		/* for mig_get_reply_port() */
#endif

	natural_t context;
	spin_lock_t lock;
	volatile int state;			/* current state */
#define CPROC_RUNNING	0
#define CPROC_SWITCHING 1
#define CPROC_BLOCKED	2
#define CPROC_CONDWAIT	4

	mach_port_t wired;		/* is cthread wired to kernel thread */
	void *busy;			/* used with cthread_msg calls */

	mach_msg_header_t msg;

	vm_offset_t stack_base;
	vm_offset_t stack_size;
} *cproc_t;

#define	NO_CPROC		((cproc_t) 0)
#define	cproc_self()		((cproc_t) ur_cthread_self())

#if 0
/* This declaration conflicts with <stdlib.h> in GNU.  */
/*
 * C Threads imports:
 */
extern char *malloc();
#endif

/*
 * Mach imports:
 */
extern void mach_error();

/*
 * Macro for MACH kernel calls.
 */
#ifdef CHECK_STATUS
#define	MACH_CALL(expr, ret)	\
	if (((ret) = (expr)) != KERN_SUCCESS) { \
	quit(1, "error in %s at %d: %s\n", __FILE__, __LINE__, \
	     mach_error_string(ret)); \
	} else
#else  /* CHECK_STATUS */
#define MACH_CALL(expr, ret) (ret) = (expr)
#endif  /* CHECK_STATUS */

#define private static
#ifndef	ASSERT
#define ASSERT(x)
#endif
#define TRACE(x)

/*
 * What we do to yield the processor:
 * (This depresses the thread's priority for up to 10ms.)
 */

#define yield()		\
	(void) thread_switch(MACH_PORT_NULL, SWITCH_OPTION_DEPRESS, 10)

/*
 * Functions implemented in malloc.c.
 */

#if	defined(DEBUG)
extern void	print_malloc_free_list(void);
#endif	/* defined(DEBUG) */

extern void		malloc_fork_prepare(void);

extern void		malloc_fork_parent(void);

extern void		malloc_fork_child(void);


/*
 * Functions implemented in stack.c.
 */

extern vm_offset_t	stack_init(cproc_t _cproc);

extern void		alloc_stack(cproc_t _cproc);

extern vm_offset_t	cproc_stack_base(cproc_t _cproc, int _offset);

extern void		stack_fork_child(void);

/*
 * Functions implemented in cprocs.c.
 */

extern vm_offset_t	cproc_init(void);

extern void		cproc_waiting(cproc_t _waiter);

extern void		cproc_block(void);

extern cproc_t		cproc_create(void);

extern void		cproc_fork_prepare(void);

extern void		cproc_fork_parent(void);

extern void		cproc_fork_child(void);

/*
 * Function implemented in cthreads.c.
 */

extern void		cthread_body(cproc_t _self);

/*
 * Functions from machine dependent files.
 */

extern void		cproc_switch(natural_t *_cur, const natural_t *_new,
				     spin_lock_t *_lock);

extern void		cproc_start_wait(natural_t *_parent, cproc_t _child,
					 vm_offset_t _stackp,
					 spin_lock_t *_lock);

extern void		cproc_prepare(cproc_t _child,
				      natural_t *_child_context,
				      vm_offset_t _stackp,
				      void (*cthread_body_pc)());

extern void		cproc_setup(cproc_t _child, thread_t _mach_thread,
				    tcbhead_t *tcb, void (*_routine)(cproc_t));


/* From glibc.  */

/* Dynamic linker TLS allocation.  */
extern void *_dl_allocate_tls(void *);