File: pthread_message.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 (315 lines) | stat: -rw-r--r-- 7,809 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
/*
 * 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.
 */

#ifdef	CPU_INHERIT
/*
 * Full Blown CPU Inheritance scheduler.
 */
#include <threads/pthread_internal.h>
#include <oskit/c/malloc.h>

/*
 * Allocate and initialize a scheduler message queue.
 */
schedmsg_queue_t *
schedmsg_queue_allocate(int size)
{
	int		  logsize;
	schedmsg_queue_t *queue;

	/*
	 * Must be a power of two, so round to next power of two greater
	 * than the given size.
	 */
	for (logsize = 1; logsize < size; logsize *= 2)
		;

	if ((queue = (schedmsg_queue_t *)
	     calloc(1, sizeof(schedmsg_queue_t) +
		    ((logsize - 1) * sizeof(schedmsg_t)))) == NULL)
		panic("schedmsg_queue_allocate: No more memory");

	queue->mask = logsize - 1;
	queue->size = logsize;
	pthread_lock_init(&queue->lock);

	return queue;
}

/*
 * Deallocate a scheduler message queue.
 */
void
schedmsg_queue_deallocate(schedmsg_queue_t *queue)
{
	free(queue);
}

/*
 * Check queue status.
 */
static inline int
schedmsg_queue_empty(schedmsg_queue_t *queue)
{
	int		empty = 0;

	if (queue->head == queue->tail)
		empty = 1;

	return empty;
}

static inline int
schedmsg_queue_full(schedmsg_queue_t *queue)
{
	int		full = 0;

	if ((queue->head - queue->tail) == queue->size)
		full = 1;

	return full;
}

/*
 * Enqueue a message for delivery.
 */
static inline void
schedmsg_enqueue(schedmsg_queue_t *queue, schedmsg_types_t type,
		 pthread_t tid, oskit_u32_t opaque, oskit_u32_t opaque2)
{
	int		x;

	x = (queue->head++ & queue->mask);
	queue->entries[x].type    = type;
	queue->entries[x].tid     = tid;
	queue->entries[x].opaque  = opaque;
	queue->entries[x].opaque2 = opaque2;
}

/*
 * Return the next available message. The values are placed in the provided
 * message buffer since the actual queue entry cannot be used.
 *
 * Return 0 if none available, 1 otherwise.
 */
static inline void
schedmsg_dequeue(schedmsg_queue_t *queue, schedmsg_t *msg)
{
	int		x;

	x = (queue->tail++ & queue->mask);
	msg->type    = queue->entries[x].type;
	msg->tid     = queue->entries[x].tid;
	msg->opaque  = queue->entries[x].opaque;
	msg->opaque2 = queue->entries[x].opaque2;
}

/*
 * Blocking receive. The receiver waits for a message to arrive. Timeout
 * determines how long the wait, but for now its never or forever.
 */
oskit_error_t
pthread_sched_message_recv(schedmsg_t *msg, oskit_s32_t timeout)
{
	pthread_thread_t	*pthread = CURPTHREAD();
	int			enabled;

	save_interrupt_enable(enabled);
	disable_interrupts();

	pthread_lock(&pthread->msgqueue->lock);
	if (!schedmsg_queue_empty(pthread->msgqueue)) {
		schedmsg_dequeue(pthread->msgqueue, msg);
		pthread_unlock(&pthread->msgqueue->lock);
		restore_interrupt_enable(enabled);
		return 0;
	}

	/*
	 * Only two timeout values right now, 0 and forever ...
	 */
	if (timeout == 0) {
		pthread_unlock(&pthread->msgqueue->lock);
		restore_interrupt_enable(enabled);
		return OSKIT_EAGAIN;
	}
	pthread_unlock(&pthread->msgqueue->lock);

	pthread_lock(&pthread->waitlock);
	pthread->waitflags |= THREAD_WS_CPUIRECV_WAIT;
	pthread_sched_reschedule(RESCHED_BLOCK, &pthread->waitlock);

	/*
	 * Back from receive. The sender woke us up, or we got canceled.
	 */
	pthread_lock(&pthread->lock);
	if (pthread->flags & THREAD_CANCELED) {
		pthread_unlock(&pthread->lock);
		restore_interrupt_enable(enabled);
		return OSKIT_ECANCELED;
	}
	pthread_unlock(&pthread->lock);

	/*
	 * Extract the message.
	 */
	pthread_lock(&pthread->msgqueue->lock);
	if (schedmsg_queue_empty(pthread->msgqueue))
		panic("pthread_sched_message_recv: No message!");
	schedmsg_dequeue(pthread->msgqueue, msg);
	pthread_unlock(&pthread->msgqueue->lock);

	restore_interrupt_enable(enabled);
	return 0;
}

/*
 * Non blocking scheduler message send.
 */
oskit_error_t
pthread_sched_message_send(pthread_thread_t *pthread, schedmsg_t *msg)
{
	int			enabled;

	save_interrupt_enable(enabled);
	disable_interrupts();

	/*
	 * Insert message into receiver queue.
	 */
	pthread_lock(&pthread->msgqueue->lock);
	if (schedmsg_queue_full(pthread->msgqueue))
		panic("pthread_sched_message_send: Message Queue Full!");
		
	schedmsg_enqueue(pthread->msgqueue,
			 msg->type, msg->tid, msg->opaque, msg->opaque2);
	
	pthread_unlock(&pthread->msgqueue->lock);

	/*
	 * If the target is in the wait, get it going again.
	 */
	pthread_lock(&pthread->waitlock);
	if (pthread->waitflags & THREAD_WS_CPUIRECV_WAIT) {
		pthread->waitflags &= ~THREAD_WS_CPUIRECV_WAIT;
		pthread_unlock(&pthread->waitlock);
		restore_interrupt_enable(enabled);
		pthread_sched_setrunnable(pthread);
		return 0;
	}
	pthread_unlock(&pthread->waitlock);
	
	restore_interrupt_enable(enabled);
	return 0;
}

/*
 * Special version for sending a scheduler message out of the wakeup code.
 *
 * The thread is locked. Interrupts are blocked.
 */
oskit_error_t
pthread_sched_special_send(pthread_thread_t *pthread, schedmsg_t *msg)
{
	assert_interrupts_disabled();
	/*
	 * Insert message into receiver queue.
	 */
	pthread_lock(&pthread->msgqueue->lock);

	if (schedmsg_queue_full(pthread->msgqueue))
		panic("pthread_sched_special_send: Message Queue Full!");
		
	schedmsg_enqueue(pthread->msgqueue,
			 msg->type, msg->tid, msg->opaque, msg->opaque2);

	pthread_unlock(&pthread->msgqueue->lock);

	/*
	 * If the target is in the wait, get it going again.
	 */
	if (pthread->waitflags & THREAD_WS_CPUIRECV_WAIT) {
		pthread->waitflags &= ~THREAD_WS_CPUIRECV_WAIT;
		pthread_unlock(&pthread->waitlock);
		pthread_sched_setrunnable(pthread);
		return 0;
	}
	
	return 0;
}

/*
 * This call is intended to make the given thread appear as if it is
 * in a scheduler message wait during a donation.
 */
int
pthread_sched_recv_wait(pthread_thread_t *pthread, schedmsg_t *msg)
{
	/*
	 * First, check to see if there is a message pending. Have the
	 * donator process that message first.
	 */
	pthread_lock(&pthread->msgqueue->lock);
	if (!schedmsg_queue_empty(pthread->msgqueue)) {
		schedmsg_dequeue(pthread->msgqueue, msg);
		pthread_unlock(&pthread->msgqueue->lock);
		return 1;
	}
	pthread_unlock(&pthread->msgqueue->lock);

	/*
	 * Nothing waiting ...
	 */
	pthread->waitflags |= THREAD_WS_CPUIRECV_WAIT;
	return 0;
}	

/*
 * Undo the effects of the previous function.  Return the pid of the
 * thread that sent it a message (if there was one).
 *
 * The thread should be locked.
 */
int
pthread_sched_recv_unwait(pthread_thread_t *pthread, schedmsg_t *msg)
{
	pthread->waitflags &= ~THREAD_WS_CPUIRECV_WAIT;
	
	pthread_lock(&pthread->msgqueue->lock);
	if (!schedmsg_queue_empty(pthread->msgqueue)) {
		schedmsg_dequeue(pthread->msgqueue, msg);
		pthread_unlock(&pthread->msgqueue->lock);
		return 1;
	}
	pthread_unlock(&pthread->msgqueue->lock);

	return 0;
}

/*
 * Cancelation support. Terminate a CPUI recv wait. 
 *
 * The thread is locked.
 */
void
pthread_sched_recv_cancel(pthread_thread_t *pthread)
{
	pthread->waitflags &= ~THREAD_WS_CPUIRECV_WAIT;
	pthread_unlock(&pthread->waitlock);
	pthread_sched_setrunnable(pthread);
}
#endif /* CPU_INHERIT */