forked from jorya/raw-os
/
raw_sem.c
562 lines (394 loc) · 14.2 KB
/
raw_sem.c
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
/*
raw os - Copyright (C) Lingjun Chen(jorya_txj).
This file is part of raw os.
raw os is free software; you can redistribute it it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
raw os 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 GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. if not, write email to jorya.txj@gmail.com
---
A special exception to the LGPL can be applied should you wish to distribute
a combined work that includes raw os, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
/* 2012-4 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
/*
************************************************************************************************************************
* Create a semaphore object
*
* Description: This function is called to create a semphore object.
*
* Arguments :semaphore_ptr is the address of semphore object want to be initialized
* -----
* name_ptr is the semphore object name
* -----
* initial_count is the initial value of the semphore
*
* Returns
RAW_SUCCESS: raw os return success
* Note(s)
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_SEMAPHORE > 0)
RAW_OS_ERROR raw_semaphore_create(RAW_SEMAPHORE *semaphore_ptr, RAW_U8 *name_ptr, RAW_U32 initial_count)
{
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (initial_count == (RAW_PROCESSOR_UINT) - 1) {
return RAW_SEMAPHORE_OVERFLOW;
}
#endif
/*Init the list*/
list_init(&semaphore_ptr->common_block_obj.block_list);
/*Init resource*/
semaphore_ptr->count = initial_count;
semaphore_ptr->peak_count = initial_count;
semaphore_ptr->common_block_obj.name = name_ptr;
semaphore_ptr->common_block_obj.block_way = RAW_BLOCKED_WAY_PRIO;
semaphore_ptr->semphore_send_notify = 0;
semaphore_ptr->common_block_obj.object_type = RAW_SEM_OBJ_TYPE;
TRACE_SEMAPHORE_CREATE(raw_task_active, semaphore_ptr);
return RAW_SUCCESS;
}
RAW_OS_ERROR semaphore_put(RAW_SEMAPHORE *semaphore_ptr, RAW_U8 opt_wake_all)
{
LIST *block_list_head;
RAW_SR_ALLOC();
RAW_CRITICAL_ENTER();
if (semaphore_ptr->common_block_obj.object_type != RAW_SEM_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
block_list_head = &semaphore_ptr->common_block_obj.block_list;
/*if no block task on this list just return*/
if (is_list_empty(block_list_head)) {
if (semaphore_ptr->count == (RAW_PROCESSOR_UINT) - 1) {
RAW_CRITICAL_EXIT();
TRACE_SEMAPHORE_OVERFLOW(raw_task_active, semaphore_ptr);
return RAW_SEMAPHORE_OVERFLOW;
}
/*increase resource*/
semaphore_ptr->count++;
if (semaphore_ptr->count > semaphore_ptr->peak_count) {
semaphore_ptr->peak_count = semaphore_ptr->count;
}
RAW_CRITICAL_EXIT();
/*if semphore is registered with notify function just call it*/
if (semaphore_ptr->semphore_send_notify) {
semaphore_ptr->semphore_send_notify(semaphore_ptr);
}
TRACE_SEMAPHORE_COUNT_INCREASE(raw_task_active, semaphore_ptr);
return RAW_SUCCESS;
}
/*wake all the task blocked on this semphore*/
if (opt_wake_all) {
while (!is_list_empty(block_list_head)) {
raw_wake_object(raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list));
TRACE_SEM_WAKE_TASK(raw_task_active, raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list), opt_wake_all);
}
}
else {
/*Wake up the highest priority task block on the semaphore*/
raw_wake_object(raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list));
TRACE_SEM_WAKE_TASK(raw_task_active, raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list), opt_wake_all);
}
RAW_CRITICAL_EXIT();
raw_sched();
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Release a semaphore(wake up one blocked task)
*
* Description: This function is called to release a semphore and to wake up the highest priority task blocked on semphore if possible.
*
* Arguments :semaphore_ptr is the address of semphore object want to be initialized
* -----
*
*
* Returns RAW_SEMAPHORE_OVERFLOW: semphore value excedded 0xffffffff
* RAW_SUCCESS: raw os return success
* Note(s)
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_semaphore_put(RAW_SEMAPHORE *semaphore_ptr)
{
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
#endif
return semaphore_put(semaphore_ptr, WAKE_ONE_SEM);
}
/*
************************************************************************************************************************
* Notify function call back and release a semaphore.
*
* Description: This function is called to call back a registered notify function.
*
* Arguments :semaphore_ptr is the address of semphore object want to be initialized
* -----
*
*
* Returns RAW_SEMAPHORE_OVERFLOW: semphore value excedded 0xffffffff
* RAW_SUCCESS: raw os return success
* Note(s)
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_semaphore_put_notify(RAW_SEMAPHORE *semaphore_ptr)
{
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
#endif
return semaphore_put(semaphore_ptr, WAKE_ONE_SEM);
}
/*
************************************************************************************************************************
* Release a semaphore(wake up all blocked task)
*
* Description: This function is called to release a semphore and to wake up all tasks blocked on semphore if possible.
*
* Arguments :semaphore_ptr is the address of semphore object want to be initialized
* -----
*
*
* Returns RAW_SEMAPHORE_OVERFLOW: semphore value excedded 0xffffffff
RAW_SUCCESS: raw os return success
* Note(s)
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_semaphore_put_all(RAW_SEMAPHORE *semaphore_ptr)
{
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
#endif
return semaphore_put(semaphore_ptr, WAKE_ALL_SEM);
}
/*
************************************************************************************************************************
* Register notify function to semaphore
*
* Description: This function is called to Register notify function to semphore.
*
* Arguments :semaphore_ptr is the address of the semphore object
* -----
* notify_function is the function to be called whennever put semphore.
*
*
*
* Returns
* RAW_SUCCESS: raw os return success
*
* Note(s) This function is normally used to implement pending on multi object function.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_semphore_send_notify(RAW_SEMAPHORE *semaphore_ptr, SEMPHORE_SEND_NOTIFY notify_function)
{
RAW_SR_ALLOC();
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
RAW_CRITICAL_ENTER();
if (semaphore_ptr->common_block_obj.object_type != RAW_SEM_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
semaphore_ptr->semphore_send_notify = notify_function;
RAW_CRITICAL_EXIT();
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Get a semaphore
*
* Description: This function is called to get a semaphore.
*
* Arguments :semaphore_ptr is the address of semphore object want to be initialized
* -----
* wait_option: 0 means return immediately if not get semphore
*
* RAW_NO_WAIT (0x00000000)
* RAW_WAIT_FOREVER (0xFFFFFFFF)
* timeout value (0x00000001
* through
* 0xFFFFFFFE)
* Returns
* RAW_SUCCESS : Get semphore success.
* RAW_BLOCK_ABORT: semphore is aborted by other task or ISR.
* RAW_NO_PEND_WAIT: semphore is not got and option is RAW_NO_WAIT.
* RAW_SCHED_DISABLE: semphore is locked ant task is not allowed block.
* RAW_BLOCK_DEL: if this mutex is deleted
*
* Note(s)
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_semaphore_get(RAW_SEMAPHORE *semaphore_ptr, RAW_TICK_TYPE wait_option)
{
RAW_OS_ERROR error_status;
RAW_SR_ALLOC();
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
RAW_CRITICAL_ENTER();
if (semaphore_ptr->common_block_obj.object_type != RAW_SEM_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
if (semaphore_ptr->count) {
semaphore_ptr->count--;
RAW_CRITICAL_EXIT();
TRACE_SEMAPHORE_GET_SUCCESS(raw_task_active, semaphore_ptr);
return RAW_SUCCESS;
}
/*Cann't get semphore, and return immediately if wait_option is RAW_NO_WAIT*/
if (wait_option == RAW_NO_WAIT) {
RAW_CRITICAL_EXIT();
return RAW_NO_PEND_WAIT;
}
SYSTEM_LOCK_PROCESS();
raw_pend_object((RAW_COMMON_BLOCK_OBJECT *)semaphore_ptr, raw_task_active, wait_option);
RAW_CRITICAL_EXIT();
TRACE_SEMAPHORE_GET_BLOCK(raw_task_active, semaphore_ptr, wait_option);
raw_sched();
error_status = block_state_post_process(raw_task_active, 0);
return error_status;
}
/*
************************************************************************************************************************
* Set a semaphore count to specific semaphore
*
* Description: This function is called to set a semaphore count to specific semaphore.
*
* Arguments :semaphore_ptr is the address of the semphore object
* -----
* sem_count is semaphore count want to be set
*
*
*
* Returns
* RAW_SUCCESS: raw os return success.
* RAW_SEMAPHORE_TASK_WAITING: task is waiting for this semaphore, so semaphore count can not be set.
*
* Note(s) This function is normally used to reset the semaphore count.
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_SEMAPHORE_SET > 0)
RAW_OS_ERROR raw_semaphore_set(RAW_SEMAPHORE *semaphore_ptr, RAW_U32 sem_count)
{
LIST *block_list_head;
RAW_SR_ALLOC();
block_list_head = &semaphore_ptr->common_block_obj.block_list;
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
RAW_CRITICAL_ENTER();
if (semaphore_ptr->common_block_obj.object_type != RAW_SEM_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
if (semaphore_ptr->count) {
semaphore_ptr->count = sem_count;
}
else {
if (is_list_empty(block_list_head)) {
semaphore_ptr->count = sem_count;
}
else {
RAW_CRITICAL_EXIT();
return RAW_SEMAPHORE_TASK_WAITING;
}
}
RAW_CRITICAL_EXIT();
return RAW_SUCCESS;
}
#endif
/*
************************************************************************************************************************
* Delete a semaphore
*
* Description: This function is called to delete a semphore.
*
* Arguments :semaphore_ptr is the address of semphore object.
*
*
*
* Returns RAW_SUCCESS: raw os return success
* Note(s) Any task pended on this semphore will be waked up and will return RAW_BLOCK_DEL.
*
*
*
************************************************************************************************************************
*/
#if (CONFIG_RAW_SEMAPHORE_DELETE > 0)
RAW_OS_ERROR raw_semaphore_delete(RAW_SEMAPHORE *semaphore_ptr)
{
LIST *block_list_head;
RAW_SR_ALLOC();
#if (RAW_SEMA_FUNCTION_CHECK > 0)
if (semaphore_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
#endif
RAW_CRITICAL_ENTER();
if (semaphore_ptr->common_block_obj.object_type != RAW_SEM_OBJ_TYPE) {
RAW_CRITICAL_EXIT();
return RAW_ERROR_OBJECT_TYPE;
}
block_list_head = &semaphore_ptr->common_block_obj.block_list;
semaphore_ptr->common_block_obj.object_type = RAW_OBJ_TYPE_NONE;
/*All task blocked on this queue is waken up*/
while (!is_list_empty(block_list_head)) {
delete_pend_obj(raw_list_entry(block_list_head->next, RAW_TASK_OBJ, task_list));
}
RAW_CRITICAL_EXIT();
TRACE_SEMAPHORE_DELETE(raw_task_active, semaphore_ptr);
raw_sched();
return RAW_SUCCESS;
}
#endif
#endif