File: pthread_internal.h

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (547 lines) | stat: -rw-r--r-- 16,075 bytes parent folder | download
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * Copyright (c) 1996, 1998, 1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * Internal definitions for threads package.
 */
#ifndef	_PTHREAD_INT_H_
#define _PTHREAD_INT_H_

#ifndef ASSEMBLER
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>

/*
 * Turn on deadlock detection in spinlocks and interrupt mask checks.
 * Also turn on stackguards and deadlock detection.
 */
#ifdef  DEBUG
#undef  THREADS_DEBUG
#define THREADS_DEBUG
#endif

#if (defined(SMP) || defined(THREADS_DEBUG)) && !defined(OSKIT_ARM32)
#undef  THREADS_SPINLOCKS
#define THREADS_SPINLOCKS
#endif

#ifdef	USERMODE
#define OSKIT_INLINE	static inline
#endif

#include <oskit/compiler.h>
#include <oskit/base_critical.h>

#include "machine/pcb.h"

#include <oskit/queue.h>
#include <oskit/debug.h>
#include <oskit/com/listener.h>
#include <oskit/dev/dev.h>
#include <oskit/dev/clock.h>
#include <oskit/dev/timer.h>
#include <oskit/threads/pthread.h>
#include <threads/pthread_locking.h>
#ifdef  CPU_INHERIT
#include <threads/cpuinherit/pthread_cpuinherit.h>
#endif

#ifdef	SMP
#include <oskit/smp.h>
#endif

/*
 * Thread structure. The PCB is stored in the thread structure, but
 * accessed indirectly to leave room for future expansion.
 */
typedef struct pthread_thread {
	queue_chain_t		runq;		/* Runq/free link */
	pcb_t			*ppcb;		/* Pointer to PCB */
	oskit_u32_t		*pstk;		/* Pointer to stack alloc */
	void			*(*func)(void *);/* Start function */
	void			*cookie;	/* Argument to start func */
	size_t			ssize;		/* Allocated stack size */
	size_t			guardsize;	/* Stack Guard size */
	pthread_t		tid;		/* Thread ID */
	oskit_u32_t		flags;		/* Thread state flags */
	pthread_lock_t		lock;		/* Lock */
	queue_chain_t		chain;		/* Queuing element */
	int			preempt;	/* Preempt this thread */

	/*
	 * The exit code is painful. Use a mutex/cond pair and special dead
	 * flag. This avoids specialized handling in the cancel and signal
	 * code (looks just like a condwait). 
	 */
	pthread_mutex_t		mutex;		/* Protect dead flag */
	pthread_cond_t		cond;		/* Wait for dead flag=1 */
	oskit_u32_t		exitval;	/* Exit value for joiner */
	int			dead;		/* Thread is finally dead */

	/*
	 * Resource accounting stuff. Added for the demo!
	 */
	int			cputime;	/* Total ticks consumed */
	int			cpticks;	/* Ticks in last second */
	oskit_u32_t		pctcpu;		/* Percent of CPU usage */
	int			childtime;	/* Accumulation of child CPU */

	/*
	 * To implement cancelation and signals, must know the object
	 * the thread is blocked on. This set of fields is protected
	 * by its own lock to avoid having to take the pthread lock
	 * everywhere.
	 */
	pthread_lock_t		waitlock;	/* Lock */
	oskit_u32_t		waitflags;	/* Wait state flags */
	pthread_cond_t		*waitcond;	/* Condition variable */
	struct osenv_sleeprec	*sleeprec;	/* pthread in an osenv_sleep */

	/*
	 * IPC stuff.
	 */
	struct {
		void		        *msg;		/* Message pointer */
		oskit_size_t		msg_size;
		pthread_t		tid;
		void			*reply;
		oskit_size_t		reply_size;
		queue_head_t		senders;
		queue_chain_t		senders_chain;
	} ipc_state;

	/*
	 * Timed condition wait is implemented using a clock timer.
	 */
	struct oskit_timer	*condtimer;

	/*
	 * pthread sleep/wakeup is implemented using a separate clock timer.
	 */
	struct oskit_timer	*sleeptimer;

	/*
	 * Signal stuff.
	 */
	pthread_lock_t		siglock;	/* Protect signal state */
	sigset_t		sigmask;	/* Blocked signals */
	sigset_t		sigpending;	/* Pending signals */
	sigset_t		sigwaiting;	/* Waiting for signals */
	oskit_u32_t		eip;		/* For page fault handling */

	/*
	 * Keys values are currently a fixed array.
	 */
	void			*keyvalues[PTHREAD_KEYS_MAX]; /* Key/Value */

	/*
	 * Cancelation state and cleanup handlers.
	 */
	pthread_cleanup_t	*cleanups;	/* Cleanup handlers chain */
	char			cancelstate;	/* Cancelation State */
	char			canceltype;	/* Cancelation Type */

	/*
	 * The scheduler uses a separate lock.
	 */
	pthread_lock_t		schedlock;

#ifdef  DEFAULT_SCHEDULER
	int			policy;		/* Scheduling Policy */
	int			priority;	/* Current Priority */
	int			base_priority;  /* Original priority */
	int			ticks;		/* Scheduling ticks left */
#endif
#ifdef  PRI_INHERIT
	/*
	 * Priority inheritance support.
	 */
	queue_head_t		waiters;	/* Threads waiting on me */
	queue_chain_t		waiters_chain;	/* Waiters queueing element */
	struct pthread_thread	*waiting_for;	/* Thread being waited for */
	struct pthread_thread	*inherits_from;	/* Thread inheriting from */
#endif
#ifdef  CPU_INHERIT
	/*
	 * CPU inheritance support.
	 */
	struct pthread_thread   *scheduler;	/* The threads scheduler */
	schedmsg_t		unblockmsg;
	schedmsg_queue_t	*msgqueue;
	sched_wakecond_t	wakeup_cond;	/* Wakeup condition */
	schedflags_t		schedflags;	/* Flags */
	int			donate_rc;	/* Return code from donation */
	int			timeout;	/* In milliseconds */
	queue_head_t		donors;		/* Threads donating to me */
	queue_chain_t		donors_chain;	/* Donors queueing element */
	struct pthread_thread	*donating_to;	/* Thread being donated to */
	struct pthread_thread	*inherits_from;	/* Thread inheriting from */
	struct pthread_thread	*nextup;	/* Next thread to run. Chain */
#endif
	pcb_t			pcb;		/* Actual PCB */
} pthread_thread_t;
#define NULL_THREADPTR		((pthread_thread_t *) 0)

/*
 * These are generic flags. They can be modified once preemptions are
 * disabled and the pthread lock is held.
 */
#define THREAD_EXITING		0x00001
#define THREAD_DETACHED		0x00004
#define THREAD_CANCELED		0x00008
#define THREAD_KILLED		0x00010
#define THREAD_USERSTACK	0x00020

/*
 * These are the waitstate flags. They are modified from interrupt handlers,
 * so interrupts must be disabled and the pthread waitlock held.
 */
#define THREAD_WS_SLEEPING	0x00001		/* These must be disjoint */
#define THREAD_WS_TIMEDOUT	0x00002
#define THREAD_WS_TIMERSET	0x00004
#define THREAD_WS_SLEEPFLAGS    0x00007

#define THREAD_WS_CONDWAIT	0x00010
#define THREAD_WS_OSENVSLEEP	0x00020
#define THREAD_WS_CPUIRECV_WAIT	0x00040
#define THREAD_WS_SIGWAIT	0x00080
#define THREAD_WS_IPCSEND_WAIT	0x01000
#define THREAD_WS_IPCRECV_WAIT	0x02000
#define THREAD_WS_IPCREPL_WAIT	0x04000
#define THREAD_WS_IPCWAIT_FLAG	0x07000

/*
 * When a thread gives up the CPU, tell the reschedule code why. This allows
 * for more control with regards to scheduling policy.
 */
typedef enum {
	RESCHED_PREEMPT		= 1,	/* Time based preemption yield */
	RESCHED_YIELD,			/* Involuntary yield */
	RESCHED_USERYIELD,		/* User directed yield */
	RESCHED_BLOCK,			/* Waiting for a resource (mutex) */
	RESCHED_INTERNAL,		/* Internal rescheduling call */
} resched_flags_t;

/* Default scheduling interval for SCHED_RR (in ticks) */
#define SCHED_RR_INTERVAL	1

/*
 * Currently executing thread and idlethreads.
 */
extern pthread_thread_t		*threads_curthreads[];
extern pthread_thread_t		*threads_idlethreads[];

#ifdef SMP
#define CURPTHREAD()		threads_curthreads[smp_find_cur_cpu()]
#define SETCURPTHREAD(p)	threads_curthreads[smp_find_cur_cpu()] = (p)
#define IDLETHREAD		threads_idlethreads[smp_find_cur_cpu()]
#else
#define CURPTHREAD()		threads_curthreads[0]
#define SETCURPTHREAD(p)	threads_curthreads[0] = (p)
#define IDLETHREAD		threads_idlethreads[0]
#endif

/*
 * Interrupts.  Change the '1' below to a '0' and you'll
 * get inlinable versions of these functions.  Breaks
 * UNIX mode emulation, though.
 */
#if 1 || defined(GPROF)
#define machine_intr_enable()	(osenv_intr_enable())
#define machine_intr_disable()	(osenv_intr_disable())
#define machine_intr_enabled()  (osenv_intr_enabled())
#else
#include "machine/interrupt.h"
#endif

#define disable_interrupts()		machine_intr_disable()
#define enable_interrupts()		machine_intr_enable()
#define save_interrupt_enable(f)	(f) = machine_intr_enabled()
#define restore_interrupt_enable(f)	if (f) enable_interrupts()
#define assert_interrupts_enabled()	assert(machine_intr_enabled())
#define assert_interrupts_disabled()	assert(!machine_intr_enabled())

/*
 * Preemption.
 */
extern int			threads_preempt_enable[];
extern int			threads_preempt_needed[];
extern int			threads_preempt_ready;

#ifdef SMP
#define PREEMPT_ENABLE		threads_preempt_enable[smp_find_cur_cpu()]
#define PREEMPT_NEEDED		threads_preempt_needed[smp_find_cur_cpu()]
#else
#define PREEMPT_ENABLE		threads_preempt_enable[0]
#define PREEMPT_NEEDED		threads_preempt_needed[0]
#endif

#define disable_preemption()						\
	(PREEMPT_ENABLE = 0)
#define enable_preemption()						\
	(PREEMPT_ENABLE = 1,						\
	 (PREEMPT_NEEDED && machine_intr_enabled()) ?			\
				pthread_yield() : (void)0)
#define save_preemption_enable(flag)					\
	((flag) = PREEMPT_ENABLE)
#define restore_preemption_enable(flag)					\
	((flag) ? enable_preemption() : (void)0)
#define check_preemption()						\
	 (PREEMPT_ENABLE && PREEMPT_NEEDED && machine_intr_enabled()) ?	\
				pthread_yield() : (void)0)

#define assert_preemption_disabled() \
	assert(!threads_preempt_ready || !PREEMPT_ENABLE)
#define assert_preemption_enabled() \
	assert(!threads_preempt_ready || PREEMPT_ENABLE)

/*
 * Scheduler definitions. The scheduler module needs to define these
 * symbols.
 */
void			pthread_init_scheduler(void);
int			pthread_sched_reschedule(resched_flags_t reason,
				pthread_lock_t *lock);
int			pthread_sched_setrunnable(pthread_thread_t *pthread);
void			pthread_sched_deschedule(pthread_thread_t *pthread);
int			pthread_sched_change_state(pthread_thread_t *pthread,
				int newprio, int policy);
void			pthread_sched_handoff(int waitstate,
				pthread_thread_t *pnext);
int			pthread_sched_thread_wakeup(int waitstate,
				pthread_thread_t *pthread);

/*
 * The maximum number of allowed threads.
 */
#define THREADS_MAX_THREAD	512

/*
 * The array of thread structure pointers, indexed by TID.
 */
extern pthread_thread_t		*threads_tidtothread[];

OSKIT_INLINE pthread_thread_t *
tidtothread(pthread_t tid)
{
	int		 realid = (int) tid;
	pthread_thread_t *pthread;

	if (realid < 0 || realid >= THREADS_MAX_THREAD ||
	    ((pthread = threads_tidtothread[realid]) == NULL_THREADPTR))
		return NULL;

	return pthread;
}

/*
 * Callouts.
 */
extern void		       *(*threads_allocator)(size_t);
extern void			(*threads_deallocator)(void *);

/*
 * Key table stuff.
 */
struct keytable {
	int		inuse;
	void		(*destructor)(void *);
};

extern pthread_lock_t		threads_key_lock;
extern struct keytable		threads_key_table[];

OSKIT_INLINE int
validkey(int key)
{
	return (key >= 0 && key < PTHREAD_KEYS_MAX &&
		threads_key_table[key].inuse);
}

/*
 * The switch routine is dependent on user/real mode operation, so its a
 * function pointer.
 */
extern void	  (*thread_switch)(pthread_thread_t *pnext,
				   pthread_lock_t *l, pthread_thread_t *cur);

/*
 * Internal Prototypes.
 */
pthread_thread_t *pthread_init_mainthread(pthread_thread_t *pthread);
void		  thread_setup(pthread_thread_t *pthread);
void		  thread_destroy(pthread_thread_t *pthread);
void	         *pthread_alloc_memory(size_t size);
int		  pthread_mprotect(oskit_addr_t addr, size_t length,
			int writeable);
void		  pthread_dealloc_memory(void *pmem);
void		  *pthread_idle_function(void *argument);
pthread_thread_t *pthread_create_internal(void *(*function)(void *),
			void *argument, const pthread_attr_t *pattr);
void		  pthread_destroy_internal(pthread_thread_t *pthread);
void		  pthread_block_onQ(queue_head_t *q, pthread_lock_t *plock);
pthread_thread_t *pthread_dequeue_fromQ(queue_head_t *q);
pthread_thread_t *pthread_remove_fromQ(queue_head_t *q, pthread_thread_t *pth);
void		  pthread_resume_allQ(queue_head_t *q);
void		  pthread_blockints(void);
void		  pthread_unblockints(void);
int		  pthread_wakeup_locked(pthread_thread_t *pthread);
int		  pthread_wakeup_unlocked(pthread_thread_t *pthread);
pthread_thread_t *pthread_current(void);
int		  pthread_setprio_internal(pthread_thread_t *pthread, int pri);
void		  pthread_preempt(void);
void		  pthread_yield(void);
int		  threads_stack_back_trace(int tid, int max_st_levels);
void		  thread_getstate(pthread_thread_t *pth, pthread_state_t *pst);
void		  pthread_call_key_destructors(void);
void		  pthread_call_cleanup_handlers(void);
void		  osenv_process_release(void);
int		  osenv_process_locked(void);
void		  dump_all_threads(void);
void		  pthread_delay(void);

int		  pthread_cond_wait_safe(pthread_cond_t *c,
			pthread_mutex_t *m);

void		  pthread_exit_locked(void *status);
void		  pthread_reap_threads(void);

int		  pthread_init_comlock(void);
void		  pthread_init_attributes(void);
void		  pthread_init_guard(void);
void		  pthread_init_libc_locks(void);
void		  pthread_init_osenv_sleep(void);
void		  pthread_init_osenv_intr(void);
void		  pthread_init_keytable(void);
void		  pthread_init_process_lock(void);
void		  pthread_init_fs_sleep(void);
void		  pthread_init_exit(void);
void		  pthread_init_signals(void);
void		  pthread_ipc_cancel(pthread_thread_t *pthread);
void		  oskit_init_libc(void);
void		  thread_machdep_init(void);
oskit_error_t	  pthread_register_interface(void);
int		  oskit_pthread_sleep_withflags(int waitflags,
			const oskit_timespec_t *timeout);

/*
 * Internal mutex prototypes.
 */
void		  pthread_mutex_panic(pthread_mutex_t *m,
			char *t, char *msg);

#ifdef PRI_INHERIT
void		  pthread_priority_inherit(pthread_thread_t *pthread);
void		  pthread_priority_uninherit(pthread_thread_t *pthread);
void		  pthread_priority_decreasing_recompute(pthread_thread_t *pth);
void		  pthread_priority_increasing_recompute(pthread_thread_t *pth);
void		  pthread_priority_kill(pthread_thread_t *pthread);
#endif

#ifndef PAGE_SIZE
#define PAGE_SIZE	0x1000
#endif

#ifdef SMP
#define MAXCPUS		8
#define THISCPU		(smp_find_cur_cpu())
#else
#define MAXCPUS		1
#define THISCPU		0
#endif

#ifdef THREADS_DEBUG
#undef  STACKGUARD
#define STACKGUARD

/*
 * Deadlock detection.
 */
extern int		threads_sleepers;
extern pthread_lock_t	threads_sleepers_lock;
#endif

#ifdef	STACKGUARD
#define DEFAULT_STACKGUARD	PAGE_SIZE
#else
#define DEFAULT_STACKGUARD	0
#endif

extern int		threads_debug;
extern pthread_thread_t threads_mainthread;

/*
 * Soft interrupt support.
 *
 * There are two situations in which a softint handler will be requested.
 * The first is from the time-based preemption code. The second is
 * when a wakeup operation (resume or timed condtion timeout) occurs,
 * and the priorities have changed, requiring a reschedule operation.
 */
extern int			threads_switch_mode;

#define SOFTINT_TIMEOUT		0x01
#define SOFTINT_ASYNCREQ	0x02

/*
 * Seems kinda non-portable, don't ya think?
 */
#include <oskit/machine/base_irq.h>

/* Ack */
#define IN_AN_INTERRUPT()	(base_irq_nest & ~0x80)

OSKIT_INLINE void
softint_request(int type)
{
	threads_switch_mode |= type;
	base_irq_softint_request();
}

OSKIT_INLINE void
queue_check(queue_head_t *queue, pthread_thread_t *pthread)
{
	pthread_thread_t	*pnext;

	queue_iterate(queue, pnext, pthread_thread_t *, chain) {
		if (pnext == pthread)
			panic("queue_check");
	}
}

#ifdef THREADS_DEBUG
#define DPRINTF(fmt, args... ) \
	{ \
		if (threads_debug) \
			printf(__FUNCTION__  ":" fmt , ## args); \
	}
#else
#define DPRINTF(fmt, args... )
#endif

#endif /* ASSEMBLER */

/*
 * Define assembly language constants.
 */
#define THREAD_NEXT		0x0
#define THREAD_PREV		0x4
#define THREAD_PPCB		0x8

#endif /* _THREADS_INT_H_ */