Nrk-api-signals-semaphores

Version 44 (Anthony Rowe, 06/12/2013 03:24 am)

1 43 Anthony Rowe
h1. Signals
2 43 Anthony Rowe
3 1 Anthony Rowe
A signal is a message that a task can use to wakeup one or more tasks waiting on an
4 1 Anthony Rowe
event or events. When waiting on an event, a task is suspended and does not consume
5 4 Anthony Rowe
CPU time. Nano-RK supports 32 unique signals. It is possible to wait on multiple
6 1 Anthony Rowe
signals such that any one of them can wake a task from sleep. All signals must be
7 6 Anthony Rowe
created using nrk_sig_create() before they can be used.  Any task that wishes to wakeup on a signal
8 1 Anthony Rowe
must register the signal using nrk_sig_register().  There are a few special case signals that are
9 1 Anthony Rowe
generated by the kernel used to support event timeouts and notification of special actions.  See the
10 43 Anthony Rowe
*Special Kernel Signals* subsection for more information.  For more information about signals, please refer
11 1 Anthony Rowe
to the basic_signals project that comes with Nano-RK: 
12 43 Anthony Rowe
"basic_signals":http://www.nanork.org/browser/nano-RK/projects/examples/signals
13 1 Anthony Rowe
14 1 Anthony Rowe
Each signal and semaphore requires a system resource.  These are statically defined and must be declared in the nrk_cfg.h file as shown below:
15 43 Anthony Rowe
<pre>
16 43 Anthony Rowe
<code class="c">
17 42 Anthony Rowe
#define NRK_MAX_RESOURCE_CNT       5
18 43 Anthony Rowe
</code></pre>
19 28 Anthony Rowe
20 43 Anthony Rowe
*Signal and Semaphore Types*
21 5 Anthony Rowe
22 43 Anthony Rowe
<pre>
23 43 Anthony Rowe
<code class="c">
24 1 Anthony Rowe
// This is a macro used to convert a signal into a bitmask for nrk_event_wait()
25 1 Anthony Rowe
// See nrk_event_wait() for an example.
26 1 Anthony Rowe
#define SIG(x)  ((uint32_t)1)<<x 
27 5 Anthony Rowe
28 5 Anthony Rowe
// These are typedefs used to represent signals and semaphores
29 5 Anthony Rowe
typedef int8_t nrk_sig_t;
30 1 Anthony Rowe
typedef uint32_t nrk_sig_mask_t;
31 5 Anthony Rowe
typedef int8_t nrk_sem_t;
32 43 Anthony Rowe
</code></pre>
33 36 Anthony Rowe
34 43 Anthony Rowe
35 43 Anthony Rowe
h3. nrk_signal_create
36 43 Anthony Rowe
37 1 Anthony Rowe
|| void nrk_signal_create() ||
38 43 Anthony Rowe
|| _Parameters_: none ||
39 43 Anthony Rowe
|| _Return Values:_ nrk_sig_t new signal id ||
40 1 Anthony Rowe
41 1 Anthony Rowe
This function creates a signal.  Upon failure, this function returns NRK_ERROR.  Upon success a positive value
42 1 Anthony Rowe
representing the signal is returned.
43 1 Anthony Rowe
44 43 Anthony Rowe
<pre>
45 1 Anthony Rowe
  nrk_sig_t signal_one;
46 1 Anthony Rowe
  nrk_sig_t signal_two;
47 1 Anthony Rowe
  ...
48 36 Anthony Rowe
                    
49 36 Anthony Rowe
  signal_one=nrk_signal_create();
50 1 Anthony Rowe
  signal_two=nrk_signal_create();
51 43 Anthony Rowe
</code></pre>
52 1 Anthony Rowe
53 43 Anthony Rowe
54 43 Anthony Rowe
h3. nrk_signal_delete
55 43 Anthony Rowe
56 1 Anthony Rowe
|| int8_t nrk_signal_delete(nrk_sig_t sig_id) ||
57 43 Anthony Rowe
|| _Parameters_: nrk_sig_t signal id of signal to remove ||
58 43 Anthony Rowe
|| _Return Values:_ int8_t NRK_OK upon success and NRK_ERROR upon failure ||
59 1 Anthony Rowe
60 43 Anthony Rowe
This function deletes a signal _sig_id_ so that it can be reused by different tasks.  This function returns NRK_OK upon success and NRK_ERROR on failure.
61 36 Anthony Rowe
62 43 Anthony Rowe
63 43 Anthony Rowe
h3. nrk_event_signal
64 43 Anthony Rowe
65 23 Anthony Rowe
|| int8_t nrk_event_signal(nrk_sig_t sig_id) ||
66 43 Anthony Rowe
|| _Parameters_: nrk_sig_t signal id of signal to be sent ||
67 43 Anthony Rowe
|| _Return Values:_ int8_t NRK_OK upon success and NRK_ERROR upon failure ||
68 1 Anthony Rowe
69 1 Anthony Rowe
This function is used to signal tasks that are waiting on events using nrk_event_wait().
70 43 Anthony Rowe
_sig_id_ is the signal that is sent.  This function returns NRK_OK upon success and NRK_ERROR upon failure.
71 31 Anthony Rowe
If a lower priority task signals a higher priority task, the high priority task will begin to execute at 
72 29 Anthony Rowe
the next context swap.  Normally this happens when the low priority task suspends, but it is also possible
73 29 Anthony Rowe
that a medium priority task could preempt the low priority task causing a context swap that would then
74 1 Anthony Rowe
schedule the waiting high priority task.  For this reason, processing that needs to be complete before the signaled task
75 24 Anthony Rowe
executes should be done before the signals are sent. 
76 43 Anthony Rowe
* Errno
77 43 Anthony Rowe
** 1 Signal was not created
78 43 Anthony Rowe
** 2 No task was waiting on signal
79 24 Anthony Rowe
80 43 Anthony Rowe
<pre>
81 43 Anthony Rowe
<code class="c">
82 1 Anthony Rowe
   v=nrk_event_signal( signal_one );
83 1 Anthony Rowe
   if(v==NRK_ERROR) nrk_kprintf( PSTR( "nrk_event_signal failed\r\n" ));
84 43 Anthony Rowe
</code></pre>
85 1 Anthony Rowe
86 43 Anthony Rowe
87 43 Anthony Rowe
h3. nrk_event_wait
88 43 Anthony Rowe
89 1 Anthony Rowe
|| nrk_sig_mask_t nrk_event_signal(nrk_sig_mask_t event_mask) ||
90 43 Anthony Rowe
|| _Parameters_: nrk_sig_mask_t event mask of signal to wait on, use SIG() macro with signal value ||
91 43 Anthony Rowe
|| _Return Values:_ nrk_sig_mask_t signal mask that triggered wakeup ||
92 1 Anthony Rowe
93 24 Anthony Rowe
This function will wait for a set events. nano-RK supports up to 32 signals. Each signal
94 1 Anthony Rowe
represents a bit in a 32 bit number so it is possible to logically
95 1 Anthony Rowe
OR multiple signals together if you wish to wait on a combination of events. The 32
96 24 Anthony Rowe
bit number returned by nrk_event_wait() corresponds to the signal or signals that were returned.
97 1 Anthony Rowe
When waiting on multiple signals, the return value can be used to determine which signal
98 24 Anthony Rowe
triggered the wakeup. All signals need to be registered in order for a task to receive them.
99 24 Anthony Rowe
This function returns 0 upon failure if a signal is specified that does not exist, or is not
100 24 Anthony Rowe
registered.
101 1 Anthony Rowe
102 43 Anthony Rowe
<pre>
103 43 Anthony Rowe
<code class="c">
104 24 Anthony Rowe
  nrk_sig_mask_t my_sigs;
105 24 Anthony Rowe
  int8_t v;
106 24 Anthony Rowe
107 24 Anthony Rowe
  // Don't forget to register signal for reception
108 24 Anthony Rowe
  v=nrk_signal_register(signal_one);
109 1 Anthony Rowe
  if(v==NRK_ERROR) nrk_kprintf( PSTR( "nrk_signal_register failed\r\n" ));
110 1 Anthony Rowe
  v=nrk_signal_register(signal_two);
111 1 Anthony Rowe
  if(v==NRK_ERROR) nrk_kprintf( PSTR( "nrk_signal_register failed\r\n" ));
112 1 Anthony Rowe
113 1 Anthony Rowe
  ...
114 1 Anthony Rowe
  // Waiting on signal_one OR signal_two
115 23 Anthony Rowe
  my_sigs=nrk_event_wait( SIG(signal_one) | SIG(signal_two) );
116 24 Anthony Rowe
117 24 Anthony Rowe
  if(my_sigs==0) nrk_kprintf( PSTR( "nrk_event_wait failed\r\n" ));
118 1 Anthony Rowe
  if(my_sigs & SIG(signal_one))
119 23 Anthony Rowe
     nrk_kprintf( PSTR( "Task got signal 1\r\n") );
120 23 Anthony Rowe
  if(my_sigs & SIG(signal_two))
121 1 Anthony Rowe
     nrk_kprintf( PSTR( "Task got timeout signal2\r\n") );
122 43 Anthony Rowe
</code></pre>
123 1 Anthony Rowe
124 43 Anthony Rowe
*int8_t nrk_signal_register(nrk_sig_t sig_id);*
125 1 Anthony Rowe
126 43 Anthony Rowe
This function registers a signal _sig_id_ so that a task is able to receive it.  This function returns NRK_OK upon success and NRK_ERROR upon failure if the signal does not exist.  A signal only needs to be registered for reception and not transmission.
127 1 Anthony Rowe
128 43 Anthony Rowe
<pre>
129 43 Anthony Rowe
<code class="c">
130 1 Anthony Rowe
  v=nrk_signal_register(signal_two);
131 11 Anthony Rowe
  if(v==NRK_ERROR) nrk_kprintf( PSTR( "Error calling nrk_signal_register\r\n" ));
132 43 Anthony Rowe
</code></pre>
133 12 Anthony Rowe
134 1 Anthony Rowe
135 43 Anthony Rowe
*int8_t nrk_signal_unregister(nrk_sig_t sig_id);*
136 1 Anthony Rowe
137 43 Anthony Rowe
This function unregisters a signal _sig_id_ so that the task is no longer able to be unsuspended by that event.  This function returns NRK_OK upon success and NRK_ERROR uopn failure.
138 1 Anthony Rowe
139 10 Anthony Rowe
140 43 Anthony Rowe
*nrk_sig_mask_t nrk_signal_get_registered_mask();*
141 1 Anthony Rowe
142 10 Anthony Rowe
This function returns the current registered signal mask for a task.
143 1 Anthony Rowe
144 1 Anthony Rowe
145 43 Anthony Rowe
h1. Semaphores
146 43 Anthony Rowe
147 43 Anthony Rowe
148 10 Anthony Rowe
A semaphore is a protected variable and constitutes the classic method for restricting
149 1 Anthony Rowe
access to shared resources (e.g. storage,actuators etc) in a multiprogramming environment.
150 1 Anthony Rowe
Nano-RK implements semaphores and signals such that
151 1 Anthony Rowe
tasks that are suspended on an event or waiting for access to a semaphore will not be
152 10 Anthony Rowe
scheduled until the corresponding signal is sent or semaphore becomes available. For more information
153 1 Anthony Rowe
on using semaphores please refer to the basic_sem project that comes with the Nano-RK distribution: 
154 43 Anthony Rowe
"basic_sem":http://www.nanork.org/browser/nano-RK/projects/basic_sem/main.c
155 10 Anthony Rowe
156 43 Anthony Rowe
_Note that the Atmel ISA does not have a test-and-set instruction, so we provide a best effort implementation by disabling interrupts._ 
157 1 Anthony Rowe
158 43 Anthony Rowe
*nrk_sem_t* nrk_sem_create(uint8_t count, uint8_t ceiling_priority);*
159 1 Anthony Rowe
160 43 Anthony Rowe
This function creates a semaphore resource, with a priority ceiling value used by the task when accessing the resource. This facilitates the Priority Ceiling Protocol Emulation (PCPE) algorithm used in Nano-RK to avoid priority inversion. _count_ specifies the number of entries allowed into the critical section. _ceiling_priority_ sets the ceiling value for the task; Note if
161 37 Anthony Rowe
using PCPE this should be the highest priority task accessing the critical section.  Below is an example of declaring and creating a semaphore:
162 37 Anthony Rowe
163 43 Anthony Rowe
<pre>
164 43 Anthony Rowe
<code class="c">
165 37 Anthony Rowe
   nrk_sem_t *my_semaphore;
166 37 Anthony Rowe
   ...
167 37 Anthony Rowe
   my_semaphore = nrk_sem_create(1,4);
168 37 Anthony Rowe
   if(my_semaphore==NULL) nrk_kprintf( PSTR("Error creating Semaphore\r\n" ));
169 43 Anthony Rowe
</code></pre>
170 1 Anthony Rowe
171 1 Anthony Rowe
This created a semaphore with a count of 1 (also called a mutex or binary semaphore) with a priority ceiling value of 4.
172 8 Anthony Rowe
173 43 Anthony Rowe
*int8_t nrk_sem_delete(nrk_sem_t *rsrc );* 
174 8 Anthony Rowe
175 1 Anthony Rowe
This function deletes an existing semaphore which will free the resource for reuse.  This function returns NRK_OK upon success and NRK_ERROR on failure.  Errno is set depending on the error type:
176 43 Anthony Rowe
* Errno
177 43 Anthony Rowe
** 1 Signal Not Found
178 43 Anthony Rowe
** 2 Signal Index too large
179 1 Anthony Rowe
180 1 Anthony Rowe
181 43 Anthony Rowe
*int8_t nrk_sem_pend(nrk_sem_t *rsrc );* 
182 1 Anthony Rowe
183 1 Anthony Rowe
Semaphore pend takes the address of the created semaphore and attempts to access the resource.  If the resource is available,
184 1 Anthony Rowe
pend will decrement the resource counter and allow the program to continue, otherwise pend will suspend until the resource is
185 9 Anthony Rowe
posted by another task. This can be used to protect critical sections of code. Below is an example of a task pending on a semaphore:
186 43 Anthony Rowe
* Errno
187 43 Anthony Rowe
** 1 Signal Not Found
188 43 Anthony Rowe
** 2 Signal Index too large
189 9 Anthony Rowe
190 43 Anthony Rowe
<pre>
191 43 Anthony Rowe
<code class="c">
192 31 Anthony Rowe
   nrk_kprintf( PSTR("Task accessing semaphore\r\n"));
193 30 Anthony Rowe
   v = nrk_sem_pend(my_semaphore);
194 30 Anthony Rowe
   if(v==NRK_ERROR) nrk_kprintf( PSTR("Error calling pend\r\n"));
195 9 Anthony Rowe
   nrk_kprintf( PSTR("Task is now holding semaphore\r\n"));
196 43 Anthony Rowe
</code></pre>
197 9 Anthony Rowe
198 43 Anthony Rowe
*int8_t nrk_sem_post(nrk_sem_t* rsrc);*
199 8 Anthony Rowe
200 8 Anthony Rowe
Semaphore post takes the address of a created semaphore and releases access to the resource.
201 11 Anthony Rowe
This should be called after exiting a critical section that was pended.  Below is an example of a task posting a semaphore:
202 43 Anthony Rowe
* Errno
203 43 Anthony Rowe
** 1 Signal Not Found
204 43 Anthony Rowe
** 2 Signal Index too large
205 43 Anthony Rowe
<pre>
206 43 Anthony Rowe
<code class="c">
207 34 Anthony Rowe
   v = nrk_sem_post(my_semaphore);
208 34 Anthony Rowe
   if(v==NRK_ERROR) nrk_kprintf( PSTR("Error calling post\r\n"));
209 34 Anthony Rowe
   nrk_kprintf( PSTR("Task released semaphore\r\n"));
210 43 Anthony Rowe
</code></pre>
211 8 Anthony Rowe
212 18 Anthony Rowe
213 43 Anthony Rowe
*int8_t nrk_sem_query(nrk_sem_t *rsrc );* 
214 19 Anthony Rowe
215 33 Anthony Rowe
This returns the current count value of the semaphore.  This can be used to check if the semaphore will allow access without actually trying to lock it.
216 43 Anthony Rowe
* Errno
217 43 Anthony Rowe
** 1 Signal Not Found
218 43 Anthony Rowe
** 2 Signal Index too large
219 19 Anthony Rowe
220 18 Anthony Rowe
221 17 Anthony Rowe
222 43 Anthony Rowe
h1. Special Kernel Signals
223 17 Anthony Rowe
224 43 Anthony Rowe
225 43 Anthony Rowe
*nrk_wakeup_signal*
226 43 Anthony Rowe
227 1 Anthony Rowe
Sometimes it might be convenient to have a timeout associated with an nrk_event_wait() call.   This can be achieved using the kernel generated nrk_wakeup_signal.  This signal is sent on a per-task basis (not globally) when the task’s internal next-wakeup timer expires.  Normally, a task’s next wakeup is set for its next period, however this can be adjusted using the nrk_set_next_wakeup() function. nrk_wakeup_signal is created by the kernel during nrk_init().  In order for nrk_event_wait() to receive the nrk_wakeup_signal from a task it must simply be registered and muxed in like any other signal.  Note, each task has its own instance of the nrk_wakeup_signal.  Unlike other signals, nrk_wakeup_signal is not globally broadcast to all tasks.  See below for an example of how to use it as an event  timeout.
228 1 Anthony Rowe
229 43 Anthony Rowe
*int8_t nrk_set_next_wakeup(nrk_time_t timeout);*
230 17 Anthony Rowe
231 17 Anthony Rowe
This function sets the task's next wakeup timer.  It returns NRK_OK upon success and NRK_ERROR on failure.  This function does not
232 21 Anthony Rowe
suspend the task, it only changes when the next wakeup will happen if the task suspends on an event.  Calling other suspend functions like nrk_wait_until_next_period() will replace whatever wakeup value you might have previously set. 
233 18 Anthony Rowe
234 43 Anthony Rowe
<pre>
235 43 Anthony Rowe
<code class="c">
236 17 Anthony Rowe
   nrk_time_t timeout;
237 1 Anthony Rowe
238 1 Anthony Rowe
   timeout.secs=10;
239 1 Anthony Rowe
   timeout.nano_secs=0;
240 1 Anthony Rowe
   ...
241 1 Anthony Rowe
242 1 Anthony Rowe
   nrk_set_next_wakeup(timeout);
243 1 Anthony Rowe
   my_sigs=nrk_event_wait( SIG(signal_one) | SIG(nrk_wakeup_signal) );
244 1 Anthony Rowe
245 1 Anthony Rowe
   // Lets check which signal we got...
246 1 Anthony Rowe
   if(my_sigs==0)                        nrk_kprintf( PSTR( "Error calling nrk_event_wait()\r\n" ));
247 1 Anthony Rowe
   if(my_sigs & SIG(signal_one))         nrk_kprintf( PSTR( "Task got signal_one\r\n") );
248 1 Anthony Rowe
   if(my_sigs & SIG(nrk_wakeup_signal))  nrk_kprintf( PSTR( "Task got timeout signal! \r\n") );
249 1 Anthony Rowe
</code></pre>
250 44 Anthony Rowe
251 1 Anthony Rowe
252 43 Anthony Rowe
| [[nrk-api|Contents]  [wikinrk-api-device-drivers Device Drivers]] |