cocoOS  4.0.0
os_sem.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Peter Eckstrand
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted (subject to the limitations in the
8  * disclaimer below) provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the
16  * distribution.
17  *
18  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
19  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
20  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * This file is part of the cocoOS operating system.
33  * Author: Peter Eckstrand <info@cocoos.net>
34  */
35 
36 /***************************************************************************************
37 
38 
39  Change log:
40  2009-07-06: 1.0.0 First release
41 
42  2010-07-20: When waiting for semaphore, a pointer to the semaphore is passed as
43  a parameter for storing in the task tcb.
44 
45  2010-07-26: Adjusted the macros for the new semaphore handling. Renamed the semaphore
46  type.
47 
48  2012-01-04: Released under BSD license.
49 
50 
51 ***************************************************************************************
52 */
53 #ifndef OS_SEM_H
54 #define OS_SEM_H
55 
58 #include "cocoos.h"
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 #define SEM_OFS1 20000
65 #define SEM_OFS2 21000
66 
67 #define OS_WAIT_SEM(sem) do {\
68  if ( os_sem_larger_than_zero( sem ) )\
69  os_sem_decrement( sem );\
70  else\
71  {\
72  os_task_wait_sem_set( running_tid, sem );\
73  OS_SCHEDULE(SEM_OFS1);\
74  }\
75  } while (0)
76 
77 
78 #define OS_WAIT_SEM_NO_SCHEDULE(sem) do {\
79  if ( os_sem_larger_than_zero( sem ) ) {\
80  os_sem_decrement( sem );\
81  }\
82  else {\
83  os_task_wait_sem_set( running_tid, sem );\
84  }\
85  } while (0)
86 
87 
88 #define OS_SIGNAL_SEM(sem) do {\
89  if ( os_task_waiting_this_semaphore( sem ) == 0 ) {\
90  os_sem_increment( sem );\
91  }\
92  else {\
93  os_task_release_waiting_task( sem );\
94  OS_SCHEDULE(SEM_OFS2);\
95  }\
96  } while (0)
97 
98 
99 #define OS_SIGNAL_SEM_NO_SCHEDULE(sem) do {\
100  if ( os_task_waiting_this_semaphore( sem ) == 0 ) {\
101  os_sem_increment( sem );\
102  }\
103  else {\
104  os_task_release_waiting_task( sem );\
105  }\
106  } while (0)
107 
108 
109 
110 
111 typedef uint8_t Sem_t;
112 
113 
114 uint8_t os_sem_larger_than_zero( Sem_t sem );
115 void os_sem_decrement( Sem_t sem );
116 void os_sem_increment( Sem_t sem );
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif