Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
/*
 * Copyright (c) 1997-2010, 2012-2015 Wind River Systems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file
 *
 * @brief Semaphore kernel services.
 */

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

#include <micro_private.h>

/**
 *
 * @brief Update value of semaphore structure
 *
 * This routine updates the value of the semaphore by 0 or more units, then
 * gives the semaphore to any waiting tasks that can now be satisfied.
 *
 * @param n      Number of additional times semaphore has been given.
 * @param sema   Semaphore structure to update.
 *
 * @return N/A
 */
void _k_sem_struct_value_update(int n, struct _k_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->next;

#ifdef CONFIG_SYS_CLOCK_EXISTS
		if (A->Comm == _K_SVC_SEM_WAIT_REQUEST
		    || A->Comm == _K_SVC_SEM_WAIT_REPLY_TIMEOUT)
#else
		if (A->Comm == _K_SVC_SEM_WAIT_REQUEST)
#endif
		{
			S->level--;
			if (Y) {
				Y->next = X;
			} else {
				S->waiters = X;
			}
#ifdef CONFIG_SYS_CLOCK_EXISTS
			if (A->Time.timer) {
				_k_timeout_cancel(A);
				A->Comm = _K_SVC_SEM_WAIT_REPLY;
			} else {
#endif
				A->Time.rcode = RC_OK;
				_k_state_bit_reset(A->Ctxt.task, TF_SEMA);
#ifdef CONFIG_SYS_CLOCK_EXISTS
			}
#endif
		} else if (A->Comm == _K_SVC_SEM_GROUP_WAIT_REQUEST) {
			S->level--;
			A->Comm = _K_SVC_SEM_GROUP_WAIT_READY;
			GETARGS(Y);
			*Y = *A;
			SENDARGS(Y);
			Y = A;
		} else {
			Y = A;
		}
		A = X;
	}
}

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

	FREEARGS(R);
	if (--(A->args.s1.nsem) == 0) {
		_k_state_bit_reset(A->Ctxt.task, TF_LIST);
	}
}

void _k_sem_group_wait_cancel(struct k_args *A)
{
	struct _k_sem_struct *S = (struct _k_sem_struct *)A->args.s1.sema;
	struct k_args *X = S->waiters;
	struct k_args *Y = NULL;

	while (X && (X->priority <= A->priority)) {
		if (X->Ctxt.args == A->Ctxt.args) {
			if (Y) {
				Y->next = X->next;
			} else {
				S->waiters = X->next;
			}
			if (X->Comm == _K_SVC_SEM_GROUP_WAIT_REQUEST
			    || X->Comm == _K_SVC_SEM_GROUP_WAIT_READY) {
				if (X->Comm == _K_SVC_SEM_GROUP_WAIT_READY) {
					/* 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 _K_SVC_SEM_GROUP_WAIT_READY, but before
 * the _K_SVC_SEM_GROUP_WAIT_READY packet has been processed.
 * This will occur if a _K_SVC_SEM_GROUP_WAIT_TIMEOUT
 * 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 {
						_k_sem_struct_value_update(1, S);
					}
				}

				_k_sem_group_wait(X);
			} else {
				FREEARGS(X); /* ERROR */
			}
			FREEARGS(A);
			return;
		}

		Y = X;
		X = X->next;
	}
	A->next = X;
	if (Y) {
		Y->next = A;
	} else {
		S->waiters = A;
	}
}

void _k_sem_group_wait_accept(struct k_args *A)
{
	struct _k_sem_struct *S = (struct _k_sem_struct *)A->args.s1.sema;
	struct k_args *X = S->waiters;
	struct k_args *Y = NULL;

	while (X && (X->priority <= A->priority)) {
		if (X->Ctxt.args == A->Ctxt.args) {
			if (Y) {
				Y->next = X->next;
			} else {
				S->waiters = X->next;
			}
			if (X->Comm == _K_SVC_SEM_GROUP_WAIT_READY) {
				_k_sem_group_wait(X);
			} else {
				FREEARGS(X); /* ERROR */
			}
			FREEARGS(A);
			return;
		}

		Y = X;
		X = X->next;
	}
	/* ERROR */
}

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->priority = A->priority;
		R->Comm =
			((*L == A->args.s1.sema) ?
			    _K_SVC_SEM_GROUP_WAIT_ACCEPT : _K_SVC_SEM_GROUP_WAIT_CANCEL);
		R->Ctxt.args = A;
		R->args.s1.sema = *L++;
		SENDARGS(R);
	}
}

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 = _K_SVC_SEM_GROUP_WAIT_TIMEOUT;
#ifdef CONFIG_SYS_CLOCK_EXISTS
		if (A->Time.timer) {
			_k_timeout_cancel(A);
		} else
#endif
			_k_sem_group_wait_timeout(A);
	}
	FREEARGS(R);
}

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 == _K_SVC_SEM_WAIT_REPLY_TIMEOUT) {
		REMOVE_ELM(A);
		A->Time.rcode = RC_TIME;
	} else
#endif
		A->Time.rcode = RC_OK;
	_k_state_bit_reset(A->Ctxt.task, TF_SEMA);
}

void _k_sem_wait_reply_timeout(struct k_args *A)
{
	_k_sem_wait_reply(A);
}

void _k_sem_group_wait_request(struct k_args *A)
{
	struct _k_sem_struct *S = (struct _k_sem_struct *)A->args.s1.sema;
	struct k_args *X = S->waiters;
	struct k_args *Y = NULL;

	while (X && (X->priority <= A->priority)) {
		if (X->Ctxt.args == A->Ctxt.args) {
			if (Y) {
				Y->next = X->next;
			} else {
				S->waiters = X->next;
			}
			if (X->Comm == _K_SVC_SEM_GROUP_WAIT_CANCEL) {
				_k_sem_group_wait(X);
			} else {
				FREEARGS(X); /* ERROR */
			}
			FREEARGS(A);
			return;
		}

		Y = X;
		X = X->next;
	}
	A->next = X;
	if (Y) {
		Y->next = A;
	} else {
		S->waiters = A;
	}
	_k_sem_struct_value_update(0, S);
}

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->priority = _k_current_task->priority;
		R->Comm = _K_SVC_SEM_GROUP_WAIT_REQUEST;
		R->Ctxt.args = A;
		R->args.s1.sema = *L++;
		SENDARGS(R);
		(A->args.s1.nsem)++;
	}

	A->Ctxt.task = _k_current_task;
	_k_state_bit_set(_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 = _K_SVC_SEM_GROUP_WAIT_TIMEOUT;
			_k_timeout_alloc(A);
		}
	}
#endif
}

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

	Sid = A->args.s1.sema;
	S = (struct _k_sem_struct *)Sid;

	if (S->level) {
		S->level--;
		A->Time.rcode = RC_OK;
	} else if (A->Time.ticks != TICKS_NONE) {
		A->Ctxt.task = _k_current_task;
		A->priority = _k_current_task->priority;
		_k_state_bit_set(_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 = _K_SVC_SEM_WAIT_REPLY_TIMEOUT;
			_k_timeout_alloc(A);
		}
#endif
		return;
	} else {
		A->Time.rcode = RC_FAIL;
	}
}

int task_sem_take(ksem_t sema, int32_t timeout)
{
	struct k_args A;

	A.Comm = _K_SVC_SEM_WAIT_REQUEST;
	A.Time.ticks = timeout;
	A.args.s1.sema = sema;
	KERNEL_ENTRY(&A);
	return A.Time.rcode;
}

ksem_t task_sem_group_take(ksemg_t group, int32_t timeout)
{
	struct k_args A;

	A.Comm = _K_SVC_SEM_GROUP_WAIT_ANY;
	A.priority = _k_current_task->priority;
	A.Time.ticks = timeout;
	A.args.s1.list = group;
	KERNEL_ENTRY(&A);
	return A.args.s1.sema;
}

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

	_k_sem_struct_value_update(1, S);
}

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);
	}
}

void task_sem_give(ksem_t sema)
{
	struct k_args A;

	A.Comm = _K_SVC_SEM_SIGNAL;
	A.args.s1.sema = sema;
	KERNEL_ENTRY(&A);
}

void task_sem_group_give(ksemg_t group)
{
	struct k_args A;

	A.Comm = _K_SVC_SEM_GROUP_SIGNAL;
	A.args.s1.list = group;
	KERNEL_ENTRY(&A);
}

FUNC_ALIAS(isr_sem_give, fiber_sem_give, void);

void isr_sem_give(ksem_t sema)
{
	_COMMAND_STACK_SIZE_CHECK();

	nano_isr_stack_push(&_k_command_stack,
						(uint32_t)sema | KERNEL_CMD_SEMAPHORE_TYPE);
}

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

	S->level = 0;
}

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);
	}
}

void task_sem_reset(ksem_t sema)
{
	struct k_args A;

	A.Comm = _K_SVC_SEM_RESET;
	A.args.s1.sema = sema;
	KERNEL_ENTRY(&A);
}

void task_sem_group_reset(ksemg_t group)
{
	struct k_args A;

	A.Comm = _K_SVC_SEM_GROUP_RESET;
	A.args.s1.list = group;
	KERNEL_ENTRY(&A);
}

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

	Sid = A->args.s1.sema;
	S = (struct _k_sem_struct *)Sid;
	A->Time.rcode = S->level;
}

int task_sem_count_get(ksem_t sema)
{
	struct k_args A;

	A.Comm = _K_SVC_SEM_INQUIRY;
	A.args.s1.sema = sema;
	KERNEL_ENTRY(&A);
	return A.Time.rcode;
}