summaryrefslogtreecommitdiff
path: root/kernel/microkernel/k_sema.c
blob: 17da96abd6cd75935ce8256f6a47401cd89562b1 (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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/* semaphore kernel services */

/*
 * Copyright (c) 1997-2010, 2012-2015 Wind River Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1) Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2) Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3) Neither the name of Wind River Systems nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/* includes */

#include <microkernel.h>
#include <toolchain.h>
#include <sections.h>

#include <minik.h>

/*******************************************************************************
*
* signal_semaphore - common code for signaling a semaphore
*
* RETURNS: N/A
*/

static void signal_semaphore(int n, struct sem_struct *S)
{
	struct k_args *A, *X, *Y;

#ifdef CONFIG_OBJECT_MONITOR
	S->Count += n;
#endif

	S->Level += n;
	A = S->Waiters;
	Y = NULL;
	while (A && S->Level) {
		X = A->Forw;

#ifdef CONFIG_SYS_CLOCK_EXISTS
		if (A->Comm == WAITSREQ || A->Comm == WAITSTMO)
#else
		if (A->Comm == WAITSREQ)
#endif
		{
			S->Level--;
			if (Y)
				Y->Forw = X;
			else
				S->Waiters = X;
#ifdef CONFIG_SYS_CLOCK_EXISTS
			if (A->Time.timer) {
				force_timeout(A);
				A->Comm = WAITSRPL;
			} else {
#endif
				A->Time.rcode = RC_OK;
					reset_state_bit(A->Ctxt.proc, TF_SEMA);
#ifdef CONFIG_SYS_CLOCK_EXISTS
			}
#endif
		}
		else if (A->Comm == WAITMREQ) {
			S->Level--;
			A->Comm = WAITMRDY;
			GETARGS(Y);
			*Y = *A;
			SENDARGS(Y);
			Y = A;
		}
		else
			Y = A;
		A = X;
	}
}

/*******************************************************************************
*
* _k_sem_group_wait - finish handling incomplete waits on semaphores
*
* RETURNS: N/A
*/

void _k_sem_group_wait(struct k_args *R)
{
	struct k_args *A = R->Ctxt.args;

	FREEARGS(R);
	if (--(A->Args.s1.nsem) == 0)
		reset_state_bit(A->Ctxt.proc, TF_LIST);
}

/*******************************************************************************
*
* _k_sem_group_wait_cancel - handle cancellation of a semaphore involved in a
*              semaphore group wait request
*
* This routine only applies to semaphore group wait requests.  It is invoked
* for each semaphore in the semaphore group that "lost" the semaphore group
* wait request.
*
* RETURNS: N/A
*/

void _k_sem_group_wait_cancel(struct k_args *A)
{
	struct sem_struct *S = _k_sem_list + OBJ_INDEX(A->Args.s1.sema);
	struct k_args *X = S->Waiters;
	struct k_args *Y = NULL;

	while (X && (X->Prio <= A->Prio)) {
		if (X->Ctxt.args == A->Ctxt.args) {
			if (Y)
				Y->Forw = X->Forw;
			else
				S->Waiters = X->Forw;
			if (X->Comm == WAITMREQ || X->Comm == WAITMRDY) {
				if (X->Comm == WAITMRDY) {
					/* obtain struct k_args of waiting task */

					struct k_args *waitTaskArgs = X->Ctxt.args;

/*
 * Determine if the wait cancellation request is being
 * processed after the state of the 'Waiters' packet state
 * has been updated to WAITMRDY, but before the WAITMRDY
 * packet has been processed. This will occur if a WAITMTMO
 * timer expiry occurs between the update of the packet state
 * and the processing of the WAITMRDY packet.
 */
					if (unlikely(waitTaskArgs->Args.s1
								  .sema ==
							  ENDLIST))
						waitTaskArgs->Args.s1.sema =
							A->Args.s1.sema;
					else
						signal_semaphore(1, S);
				}

				_k_sem_group_wait(X);
			} else
				FREEARGS(X); /* ERROR */
			FREEARGS(A);
			return;
		} else {
			Y = X;
			X = X->Forw;
		}
	}
	A->Forw = X;
	if (Y)
		Y->Forw = A;
	else
		S->Waiters = A;
}

/*******************************************************************************
*
* _k_sem_group_wait_accept - handle acceptance of the ready semaphore request
*
* This routine only applies to semaphore group wait requests.  It handles
* the request for the one semaphore in the group that "wins" the semaphore
* group wait request.
*
* RETURNS: N/A
*/

void _k_sem_group_wait_accept(struct k_args *A)
{
	struct sem_struct *S = _k_sem_list + OBJ_INDEX(A->Args.s1.sema);
	struct k_args *X = S->Waiters;
	struct k_args *Y = NULL;

	while (X && (X->Prio <= A->Prio)) {
		if (X->Ctxt.args == A->Ctxt.args) {
			if (Y)
				Y->Forw = X->Forw;
			else
				S->Waiters = X->Forw;
			if (X->Comm == WAITMRDY) {
					_k_sem_group_wait(X);
			} else
				FREEARGS(X); /* ERROR */
			FREEARGS(A);
			return;
		} else {
			Y = X;
			X = X->Forw;
		}
	}
	/* ERROR */
}

/*******************************************************************************
*
* _k_sem_group_wait_timeout - handle semaphore group timeout request
*
* RETURNS: N/A
*/

void _k_sem_group_wait_timeout(struct k_args *A)
{
	ksem_t *L;

#ifdef CONFIG_SYS_CLOCK_EXISTS
	if (A->Time.timer)
		FREETIMER(A->Time.timer);
#endif

	L = A->Args.s1.list;
	while (*L != ENDLIST) {
		struct k_args *R;

		GETARGS(R);
		R->Prio = A->Prio;
		R->Comm =
			(K_COMM)((*L == A->Args.s1.sema) ? WAITMACC : WAITMCAN);
		R->Ctxt.args = A;
		R->Args.s1.sema = *L++;
		SENDARGS(R);
	}
}

/*******************************************************************************
*
* _k_sem_group_ready - handle semaphore ready request
*
* This routine only applies to semaphore group wait requests.  It identifies
* the one semaphore in the group that "won" the semaphore group wait request
* before triggering the semaphore group timeout handler.
*
* RETURNS: N/A
*/

void _k_sem_group_ready(struct k_args *R)
{
	struct k_args *A = R->Ctxt.args;

	if (A->Args.s1.sema == ENDLIST) {
		A->Args.s1.sema = R->Args.s1.sema;
		A->Comm = WAITMTMO;
#ifdef CONFIG_SYS_CLOCK_EXISTS
		if (A->Time.timer)
			force_timeout(A);
		else
#endif
			_k_sem_group_wait_timeout(A);
	}
	FREEARGS(R);
}

/*******************************************************************************
*
* _k_sem_wait_reply - reply to a semaphore wait request
*
* RETURNS: N/A
*/

void _k_sem_wait_reply(struct k_args *A)
{
#ifdef CONFIG_SYS_CLOCK_EXISTS
	if (A->Time.timer)
		FREETIMER(A->Time.timer);
	if (A->Comm == WAITSTMO) {
		REMOVE_ELM(A);
		A->Time.rcode = RC_TIME;
	} else
#endif
		A->Time.rcode = RC_OK;
   	reset_state_bit(A->Ctxt.proc, TF_SEMA);
}

/*******************************************************************************
*
* _k_sem_group_wait_request - handle internal wait request on a semaphore involved in a
*              semaphore group wait request
*
* RETURNS: N/A
*/

void _k_sem_group_wait_request(struct k_args *A)
{
	struct sem_struct *S = _k_sem_list + OBJ_INDEX(A->Args.s1.sema);
	struct k_args *X = S->Waiters;
	struct k_args *Y = NULL;

	while (X && (X->Prio <= A->Prio)) {
		if (X->Ctxt.args == A->Ctxt.args) {
			if (Y)
				Y->Forw = X->Forw;
			else
				S->Waiters = X->Forw;
			if (X->Comm == WAITMCAN) {
				_k_sem_group_wait(X);
			} else
				FREEARGS(X); /* ERROR */
			FREEARGS(A);
			return;
		} else {
			Y = X;
			X = X->Forw;
		}
	}
	A->Forw = X;
	if (Y)
		Y->Forw = A;
	else
		S->Waiters = A;
	signal_semaphore(0, S);
}

/*******************************************************************************
*
* _k_sem_group_wait_any - handle semaphore group wait request
*
* This routine splits the single semaphore group wait request into several
* internal wait requests--one for each semaphore in the group.
*
* RETURNS: N/A
*/

void _k_sem_group_wait_any(struct k_args *A)
{
	ksem_t *L;

	L = A->Args.s1.list;
	A->Args.s1.sema = ENDLIST;
	A->Args.s1.nsem = 0;

	if (*L == ENDLIST)
		return;

	while (*L != ENDLIST) {
		struct k_args *R;

		GETARGS(R);
		R->Prio = _k_current_task->Prio;
		R->Comm = WAITMREQ;
		R->Ctxt.args = A;
		R->Args.s1.sema = *L++;
		SENDARGS(R);
		(A->Args.s1.nsem)++;
	}

	A->Ctxt.proc = _k_current_task;
	set_state_bit(_k_current_task, TF_LIST);

#ifdef CONFIG_SYS_CLOCK_EXISTS
	if (A->Time.ticks != TICKS_NONE) {
		if (A->Time.ticks == TICKS_UNLIMITED)
			A->Time.timer = NULL;
		else {
			A->Comm = WAITMTMO;
			enlist_timeout(A);
		}
	}
#endif
}

/*******************************************************************************
*
* _k_sem_wait_request - handle semaphore test and wait request
*
* RETURNS: N/A
*/

void _k_sem_wait_request(struct k_args *A)
{
	struct sem_struct *S;
	uint32_t Sid;

	Sid = A->Args.s1.sema;
	S = _k_sem_list + OBJ_INDEX(Sid);

	if (S->Level) {
		S->Level--;
		A->Time.rcode = RC_OK;
	} else if (A->Time.ticks != TICKS_NONE) {
		A->Ctxt.proc = _k_current_task;
		A->Prio = _k_current_task->Prio;
		set_state_bit(_k_current_task, TF_SEMA);
		INSERT_ELM(S->Waiters, A);
#ifdef CONFIG_SYS_CLOCK_EXISTS
		if (A->Time.ticks == TICKS_UNLIMITED)
			A->Time.timer = NULL;
		else {
			A->Comm = WAITSTMO;
			enlist_timeout(A);
		}
#endif
		return;
	} else
		A->Time.rcode = RC_FAIL;
}

/*******************************************************************************
*
* _task_sem_take - test a semaphore
*
* This routine tests a semaphore to see if it has been signaled.  If the signal
* count is greater than zero, it is decremented.
*
* RETURNS: RC_OK, RC_FAIL, RC_TIME on success, failure, timeout respectively
*/

int _task_sem_take(ksem_t sema, /* semaphore to test */
		 int32_t time /* maximum number of ticks to wait */
		 )
{
	struct k_args A;

	A.Comm = WAITSREQ;
	A.Time.ticks = time;
	A.Args.s1.sema = sema;
	KERNEL_ENTRY(&A);
	return A.Time.rcode;
}

/*******************************************************************************
*
* _task_sem_group_take - test multiple semaphores
*
* This routine tests a group of semaphores. A semaphore group is an array of
* semaphore names terminated by the predefined constant ENDLIST.
*
* It returns the ID of the first semaphore in the group whose signal count is
* greater than zero, and decrements the signal count.
*
* RETURNS: N/A
*/

ksem_t _task_sem_group_take(ksemg_t group, /* group of semaphores to test */
			 int32_t time   /* maximum number of ticks to wait */
			 )
{
	struct k_args A;

	A.Comm = WAITMANY;
	A.Prio = _k_current_task->Prio;
	A.Time.ticks = time;
	A.Args.s1.list = group;
	KERNEL_ENTRY(&A);
	return A.Args.s1.sema;
}

/*******************************************************************************
*
* _k_sem_signal - handle semaphore signal request
*
* RETURNS: N/A
*/

void _k_sem_signal(struct k_args *A)
{
	uint32_t Sid = A->Args.s1.sema;

	signal_semaphore(1, _k_sem_list + OBJ_INDEX(Sid));
}

/*******************************************************************************
*
* _k_sem_group_signal - handle signal semaphore group request
*
* RETURNS: N/A
*/

void _k_sem_group_signal(struct k_args *A)
{
	ksem_t *L = A->Args.s1.list;

	while ((A->Args.s1.sema = *L++) != ENDLIST)
		_k_sem_signal(A);
}

/*******************************************************************************
*
* task_sem_give - signal a semaphore
*
* This routine signals the specified semaphore.
*
* RETURNS: N/A
*/

void task_sem_give(ksem_t sema /* semaphore to signal */
		   )
{
	struct k_args A;

	A.Comm = SIGNALS;
	A.Args.s1.sema = sema;
	KERNEL_ENTRY(&A);
}

/*******************************************************************************
*
* task_sem_group_give - signal a group of semaphores
*
* This routine signals a group of semaphores. A semaphore group is an array of
* semaphore names terminated by the predefined constant ENDLIST.
*
* If the semaphore list of waiting tasks is empty, the signal count is
* incremented, otherwise the highest priority waiting task is released.
*
* Using task_sem_group_give() is faster than using multiple single signals,
* and ensures all signals take place before other tasks run.
*
* RETURNS: N/A
*/

void task_sem_group_give(ksemg_t group /* group of semaphores to signal */
			)
{
	struct k_args A;

	A.Comm = SIGNALM;
	A.Args.s1.list = group;
	KERNEL_ENTRY(&A);
}

/*******************************************************************************
*
* fiber_sem_give - signal a semaphore from a fiber
*
* This routine (to only be called from a fiber) signals a semaphore.  It
* requires a statically allocated command packet (from a command packet set)
* that is implicitly released once the command packet has been processed.
* To signal a semaphore from a task, task_sem_give() should be used instead.
*
* RETURNS: N/A
*/

FUNC_ALIAS(isr_sem_give, fiber_sem_give, void);

/*******************************************************************************
*
* isr_sem_give - signal a semaphore from an ISR
*
* This routine (to only be called from an ISR) signals a semaphore.  It
* requires a statically allocated command packet (from a command packet set)
* that is implicitly released once the command packet has been processed.
* To signal a semaphore from a task, task_sem_give() should be used instead.
*
* RETURNS: N/A
*/

void isr_sem_give(ksem_t sema, /* semaphore to signal */
					  struct cmd_pkt_set *pSet /* ptr to command
							       packet set */
					  )
{
	struct k_args *pCommand; /* ptr to command packet */

	/*
	 * The cmdPkt_t data structure was designed to work seamlessly with the
	 * struct k_args data structure and it is thus safe (and expected) to typecast
	 * the return value of _cmd_pkt_get() to "struct k_args *".
	 */

	pCommand = (struct k_args *)_cmd_pkt_get(pSet);
	pCommand->Comm = SIGNALS;
	pCommand->Args.s1.sema = sema;

	nano_isr_stack_push(&_k_command_stack, (uint32_t)pCommand);
}

/*******************************************************************************
*
* _k_sem_reset - handle semaphore reset request
*
* RETURNS: N/A
*/

void _k_sem_reset(struct k_args *A)
{
	uint32_t Sid = A->Args.s1.sema;

	_k_sem_list[OBJ_INDEX(Sid)].Level = 0;
}

/*******************************************************************************
*
* _k_sem_group_reset - handle semaphore group reset request
*
* RETURNS: N/A
*/

void _k_sem_group_reset(struct k_args *A)
{
	ksem_t *L = A->Args.s1.list;

	while ((A->Args.s1.sema = *L++) != ENDLIST)
		_k_sem_reset(A);
}

/*******************************************************************************
*
* task_sem_reset - reset semaphore count to zero
*
* This routine resets the signal count of the specified semaphore to zero.
*
* RETURNS: N/A
*/

void task_sem_reset(ksem_t sema /* semaphore to reset */
		  )
{
	struct k_args A;

	A.Comm = RESETS;
	A.Args.s1.sema = sema;
	KERNEL_ENTRY(&A);
}

/*******************************************************************************
*
* task_sem_group_reset - reset a group of semaphores
*
* This routine resets the signal count for a group of semaphores. A semaphore
* group is an array of semaphore names terminated by the predefined constant
* ENDLIST.
*
* RETURNS: N/A
*/

void task_sem_group_reset(ksemg_t group /* group of semaphores to reset */
		       )
{
	struct k_args A;

	A.Comm = RESETM;
	A.Args.s1.list = group;
	KERNEL_ENTRY(&A);
}

/*******************************************************************************
*
* _k_sem_inquiry - handle semaphore inquiry request
*
* RETURNS: N/A
*/

void _k_sem_inquiry(struct k_args *A)
{
	struct sem_struct *S;
	uint32_t Sid;

	Sid = A->Args.s1.sema;
	S = _k_sem_list + OBJ_INDEX(Sid);
	A->Time.rcode = S->Level;
}

/*******************************************************************************
*
* task_sem_count_get - read the semaphore signal count
*
* This routine reads the signal count of the specified semaphore.
*
* RETURNS: signal count
*/

int task_sem_count_get(ksem_t sema /* semaphore to query */
		  )
{
	struct k_args A;

	A.Comm = INQSEMA;
	A.Args.s1.sema = sema;
	KERNEL_ENTRY(&A);
	return A.Time.rcode;
}