File: pthread_create.c

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 (290 lines) | stat: -rw-r--r-- 7,895 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
/*
 * 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.
 */

/*
 * Create a new thread and return a thread ID.
 */
#include <threads/pthread_internal.h>
#include <threads/pthread_ipc.h>
#include <oskit/machine/base_stack.h>
#include <oskit/machine/base_paging.h>
#include <oskit/c/sys/mman.h>
#include <strings.h>

/*
 * Hardwired.
 */
#define DEFAULT_STACK_SIZE		(3 * PAGE_SIZE)

/*
 * Need a lock to protect the tidtothread array.
 */
pthread_lock_t	pthread_create_lock = PTHREAD_LOCK_INITIALIZER;

/*
 * Strictly for GDB macros.
 */
int		threads_maxtid = THREADS_MAX_THREAD;

/*
 * Forward decl.
 */
void		pthread_prepare_timer(pthread_thread_t *pthread);

extern int	threads_alive;

/*
 * Create a thread at the given priority. The thread is started as
 * function(arg). The caller can specify a stacksize, or if 0 supply
 * a reasonable default.
 */
int
pthread_create(pthread_t *tid, const pthread_attr_t *attr,
	       void *(*function)(void *), void *argument)
{
	pthread_thread_t	*pthread;
	int			i;
#ifdef  CPU_INHERIT
	pthread_thread_t	*pscheduler;
#endif

	if (! attr)
		attr = &pthread_attr_default;

	pthread = pthread_create_internal(function, argument, attr);

	if (attr->detachstate == PTHREAD_CREATE_DETACHED)
		pthread->flags |= THREAD_DETACHED;

	/*
	 * Signal mask is inherited from creating thread.
	 */
	pthread->sigmask = CURPTHREAD()->sigmask;

#ifdef  CPU_INHERIT
	pscheduler = CURPTHREAD()->scheduler;

	if (attr->scheduler) {
		if (tidtothread(attr->scheduler) == NULL_THREADPTR)
			printf("pthread_create: Bad scheduler: tid(%p)\n",
			       attr->scheduler);
		else
			pscheduler = tidtothread(attr->scheduler);
	}

	pthread->scheduler  = pscheduler;
#endif
	/*
	 * Find a free slot in tidtothread.
	 */
	assert_preemption_enabled();
	disable_preemption();
	pthread_lock(&pthread_create_lock);
	for (i = 1; i < THREADS_MAX_THREAD; i++)
		if (threads_tidtothread[i] == 0)
			break;

	if (i == THREADS_MAX_THREAD)
		panic("threads_create: Too many threads!\n");

	threads_tidtothread[i] = pthread;
	pthread->tid = (pthread_t) i;
	pthread_unlock(&pthread_create_lock);

#ifdef  CPU_INHERIT
	/*
	 * Send a new thread message to the scheduler.
	 */
	if (pthread->scheduler) {
		schedmsg_t	msg;

		msg.type    = MSG_SCHED_NEWTHREAD;
		msg.tid     = pthread->tid;
		msg.opaque  = attr->priority;
		msg.opaque2 = attr->policy;
		pthread_sched_message_send(pthread->scheduler, &msg);
	}
#else
	/*
	 * Go ahead and schedule the thread.
	 */
	pthread_sched_setrunnable(pthread);
#endif

	*tid = (pthread_t *) pthread->tid;
	threads_alive++;

	enable_preemption();
	return 0;
}

pthread_thread_t *
pthread_create_internal(void *(*function)(void *), void *argument,
			const pthread_attr_t *attr)
{
	pthread_thread_t	*pthread;

	if (! attr)
		attr = &pthread_attr_default;

	if ((pthread =
	     (pthread_thread_t *) threads_allocator(PAGE_SIZE)) == NULL)
		panic("pthread_create_internal: Not enough memory");

	memset((void *) pthread, 0, PAGE_SIZE);

	if ((pthread->ssize = attr->stacksize) < PTHREAD_STACK_MIN)
		return NULL_THREADPTR;

	/*
	 * Look for user provided stack. If one is provided, the stack
	 * size better be right!
	 */
	if (attr->stackaddr) {
		pthread->pstk  = (oskit_u32_t *) attr->stackaddr;
		pthread->flags = THREAD_USERSTACK;
	}
	else {
		if (attr->guardsize) {
			pthread->guardsize = round_page(attr->guardsize);
			pthread->ssize    += pthread->guardsize;
		}

		if ((pthread->pstk = (oskit_u32_t *)
		     threads_allocator(pthread->ssize)) == NULL)
			panic("pthread_create_internal: Not enough memory");
	}
	memset((void *) pthread->pstk, 0, pthread->ssize);

	/*
	 * Set up the thread.
	 */
#ifdef  DEFAULT_SCHEDULER
	pthread->priority = pthread->base_priority = attr->priority;
	pthread->policy   = attr->policy;
	pthread->ticks    = SCHED_RR_INTERVAL;
#endif
	pthread->ppcb     = &pthread->pcb;
	pthread->cookie   = argument;
	pthread->func     = function;
	pthread->cancelstate = PTHREAD_CANCEL_ENABLE;
	pthread->canceltype  = PTHREAD_CANCEL_DEFERRED;
#ifdef	PRI_INHERIT
	queue_init(&pthread->waiters);
#endif
#ifdef	CPU_INHERIT
	queue_init(&pthread->donors);
	pthread->schedflags = SCHED_READY;
#endif
	queue_init(&pthread->ipc_state.senders);
	pthread_lock_init(&pthread->lock);
	pthread_lock_init(&pthread->schedlock);
	pthread_lock_init(&pthread->waitlock);
	pthread_lock_init(&pthread->siglock);
	pthread_mutex_init(&pthread->mutex, NULL);
	pthread_cond_init(&pthread->cond, NULL);
	pthread_prepare_timer(pthread);

	/*
	 * All "internal" threads block all signals. pthread_create above
	 * will reset the signal mask to the correct value.
	 */
	sigfillset(&pthread->sigmask);

	/*
	 * Most of the work is done in the machine dependent code.
	 */
	thread_setup(pthread);

	return pthread;
}

/*
 * Create a stub thread for the main process.
 */
pthread_thread_t *
pthread_init_mainthread(pthread_thread_t *pthread)
{
#ifdef  DEFAULT_SCHEDULER
	pthread->base_priority = pthread->priority = PRIORITY_NORMAL;
	pthread->policy   = SCHED_RR;
	pthread->ticks    = SCHED_RR_INTERVAL;
#endif
	pthread->flags    = 0;
	pthread->ppcb     = &pthread->pcb;
	pthread->pstk     = (oskit_u32_t *) base_stack_start;
	pthread->ssize    = BASE_STACK_SIZE;
	pthread->cookie   = 0;
	pthread->func     = 0;
	pthread->cancelstate = PTHREAD_CANCEL_ENABLE;
	pthread->canceltype  = PTHREAD_CANCEL_DEFERRED;
#ifdef	PRI_INHERIT
	queue_init(&pthread->waiters);
#endif
#ifdef	CPU_INHERIT
	queue_init(&pthread->donors);
	pthread->schedflags = SCHED_READY;
#endif
	queue_init(&pthread->ipc_state.senders);
	pthread_lock_init(&pthread->lock);
	pthread_lock_init(&pthread->schedlock);
	pthread_lock_init(&pthread->waitlock);
	pthread_lock_init(&pthread->siglock);
	pthread_mutex_init(&pthread->mutex, NULL);
	pthread_cond_init(&pthread->cond, NULL);
	pthread_prepare_timer(pthread);
#ifdef	STACKGUARD
	pthread_mprotect((oskit_addr_t) pthread->pstk, PAGE_SIZE, PROT_READ);
#endif
	/*
	 * Reserve slot 0 for root scheduler when CPU_INHERIT defined.
	 */
	threads_tidtothread[1] = pthread;
	pthread->tid = (pthread_t) 1;

	return pthread;
}

/*
 * Timer stuff. Create the timers for sleep and cond_timedwait.
 * Do this at init time to avoid calls to malloc later.
 *
 * I could probably combine these and dispatch on wait state.
 */
oskit_error_t	pthread_condwait_timeout(struct oskit_iunknown *l, void *arg);
oskit_error_t	pthread_sleep_timeout(struct oskit_iunknown *l, void *arg);

void
pthread_prepare_timer(pthread_thread_t *pthread)
{
	extern oskit_clock_t    *oskit_system_clock;    /* XXX */
	oskit_listener_t	*listener;

	/* XXX invokes "malloc" */
	listener = oskit_create_listener(pthread_condwait_timeout, pthread);

	oskit_clock_createtimer(oskit_system_clock, &pthread->condtimer);
	oskit_timer_setlistener(pthread->condtimer, listener);
	oskit_listener_release(listener);

	/* XXX invokes "malloc" */
	listener = oskit_create_listener(pthread_sleep_timeout, pthread);

	oskit_clock_createtimer(oskit_system_clock, &pthread->sleeptimer);
	oskit_timer_setlistener(pthread->sleeptimer, listener);
	oskit_listener_release(listener);
}