Nrk-api-signals-semaphores

Version 30 (Anthony Rowe, 06/08/2007 11:04 pm)

1 11 Anthony Rowe
= Signals =
2 1 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 6 Anthony Rowe
must register the signal using nrk_sig_register().  There are a few special case signals that are
9 6 Anthony Rowe
generated by the kernel used to support event timeouts and notification of special actions.  See the
10 15 Anthony Rowe
'''Special Kernel Signals''' subsection for more information.  For more information about signals, please refer
11 14 Anthony Rowe
to the basic_signals project that comes with Nano-RK: 
12 14 Anthony Rowe
http://www.nanork.org:8000/nano-RK/browser/nano-RK/projects/basic_signals/main.c
13 1 Anthony Rowe
14 11 Anthony Rowe
15 28 Anthony Rowe
'''Signal and Semaphore Types'''
16 28 Anthony Rowe
17 5 Anthony Rowe
{{{
18 5 Anthony Rowe
#!c
19 5 Anthony Rowe
// This is a macro used to convert a signal into a bitmask for nrk_event_wait()
20 5 Anthony Rowe
// See nrk_event_wait() for an example.
21 5 Anthony Rowe
#define SIG(x)  ((uint32_t)1)<<x 
22 5 Anthony Rowe
23 5 Anthony Rowe
// These are typedefs used to represent signals and semaphores
24 5 Anthony Rowe
typedef int8_t nrk_sig_t;
25 5 Anthony Rowe
typedef uint32_t nrk_sig_mask_t;
26 5 Anthony Rowe
typedef int8_t nrk_sem_t;
27 5 Anthony Rowe
}}}
28 5 Anthony Rowe
29 5 Anthony Rowe
30 23 Anthony Rowe
'''nrk_sig_t nrk_signal_create();'''
31 1 Anthony Rowe
32 23 Anthony Rowe
This function creates a signal.  Upon failure, this function returns NRK_ERROR.  Upon success a positive value
33 23 Anthony Rowe
representing the signal is returned.
34 23 Anthony Rowe
35 24 Anthony Rowe
{{{
36 24 Anthony Rowe
  nrk_sig_t signal_one;
37 24 Anthony Rowe
  nrk_sig_t signal_two;
38 24 Anthony Rowe
  ...
39 24 Anthony Rowe
                    
40 24 Anthony Rowe
  signal_one=nrk_signal_create();
41 24 Anthony Rowe
  signal_two=nrk_signal_create();
42 24 Anthony Rowe
}}}
43 24 Anthony Rowe
44 26 Anthony Rowe
'''int8_t nrk_signal_delete(nrk_sig_t sig_id);'''
45 1 Anthony Rowe
46 23 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.
47 1 Anthony Rowe
48 1 Anthony Rowe
'''int8_t nrk_event_signal(nrk_sig_t sig_id);'''
49 1 Anthony Rowe
50 1 Anthony Rowe
This function is used to signal tasks that are waiting on events using nrk_event_wait().
51 23 Anthony Rowe
''sig_id'' is the signal that is sent.  This function returns NRK_OK upon success and NRK_ERROR upon failure.
52 23 Anthony Rowe
If a lower priority task signals a higher priority task, the high priority task will begin to execute at 
53 23 Anthony Rowe
the next context swap.  Normally this happens when the low priority task suspends, but it is also possible
54 23 Anthony Rowe
that a medium priority task could preempt the low priority task causing a context swap that would then
55 23 Anthony Rowe
schedule the waiting high priority task.  For this reason, processing that needs to be complete before the signaled task
56 23 Anthony Rowe
executes should be done before the signals are sent. 
57 29 Anthony Rowe
 * Errorno
58 29 Anthony Rowe
  * 1 Signal was not created
59 29 Anthony Rowe
  * 2 No task was waiting on signal
60 1 Anthony Rowe
61 24 Anthony Rowe
{{{
62 24 Anthony Rowe
#!c
63 24 Anthony Rowe
   v=nrk_event_signal( signal_one );
64 25 Anthony Rowe
   if(v==NRK_ERROR) nrk_kprintf( PSTR( "nrk_event_signal failed\r\n" ));
65 24 Anthony Rowe
}}}
66 4 Anthony Rowe
67 1 Anthony Rowe
'''nrk_sig_mask_t nrk_event_wait(nrk_sig_mask_t event_mask);'''
68 1 Anthony Rowe
69 1 Anthony Rowe
This function will wait for a set events. nano-RK supports up to 32 signals. Each signal
70 23 Anthony Rowe
represents a bit in a 32 bit number so it is possible to logically
71 1 Anthony Rowe
OR multiple signals together if you wish to wait on a combination of events. The 32
72 23 Anthony Rowe
bit number returned by nrk_event_wait() corresponds to the signal or signals that were returned.
73 23 Anthony Rowe
When waiting on multiple signals, the return value can be used to determine which signal
74 1 Anthony Rowe
triggered the wakeup. All signals need to be registered in order for a task to receive them.
75 1 Anthony Rowe
This function returns 0 upon failure if a signal is specified that does not exist, or is not
76 1 Anthony Rowe
registered.
77 23 Anthony Rowe
78 24 Anthony Rowe
{{{
79 24 Anthony Rowe
#!c
80 24 Anthony Rowe
  nrk_sig_mask_t my_sigs;
81 24 Anthony Rowe
  int8_t v;
82 23 Anthony Rowe
83 24 Anthony Rowe
  // Don't forget to register signal for reception
84 24 Anthony Rowe
  v=nrk_signal_register(signal_one);
85 24 Anthony Rowe
  if(v==NRK_ERROR) nrk_kprintf( PSTR( "nrk_signal_register failed\r\n" ));
86 24 Anthony Rowe
  v=nrk_signal_register(signal_two);
87 24 Anthony Rowe
  if(v==NRK_ERROR) nrk_kprintf( PSTR( "nrk_signal_register failed\r\n" ));
88 24 Anthony Rowe
89 24 Anthony Rowe
  ...
90 24 Anthony Rowe
  // Waiting on signal_one OR signal_two
91 24 Anthony Rowe
  my_sigs=nrk_event_wait( SIG(signal_one) | SIG(signal_two) );
92 24 Anthony Rowe
93 24 Anthony Rowe
  if(my_sigs==0) nrk_kprintf( PSTR( "nrk_event_wait failed\r\n" ));
94 24 Anthony Rowe
  if(my_sigs & SIG(signal_one))
95 24 Anthony Rowe
     nrk_kprintf( PSTR( "Task got signal 1\r\n") );
96 24 Anthony Rowe
  if(my_sigs & SIG(signal_two))
97 24 Anthony Rowe
     nrk_kprintf( PSTR( "Task got timeout signal2\r\n") );
98 24 Anthony Rowe
}}}
99 24 Anthony Rowe
100 23 Anthony Rowe
'''int8_t nrk_signal_register(nrk_sig_t sig_id);'''
101 23 Anthony Rowe
102 23 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.
103 24 Anthony Rowe
104 24 Anthony Rowe
{{{
105 24 Anthony Rowe
#!c
106 24 Anthony Rowe
  v=nrk_signal_register(signal_two);
107 24 Anthony Rowe
  if(v==NRK_ERROR) nrk_kprintf( PSTR( "Error calling nrk_signal_register\r\n" ));
108 24 Anthony Rowe
}}}
109 24 Anthony Rowe
110 23 Anthony Rowe
111 23 Anthony Rowe
'''int8_t nrk_signal_unregister(nrk_sig_t sig_id);'''
112 23 Anthony Rowe
113 23 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.
114 23 Anthony Rowe
115 23 Anthony Rowe
116 27 Anthony Rowe
'''nrk_sig_mask_t nrk_signal_get_registered_mask();'''
117 23 Anthony Rowe
118 23 Anthony Rowe
This function returns the current registered signal mask for a task.
119 11 Anthony Rowe
120 11 Anthony Rowe
= Semaphores =
121 11 Anthony Rowe
122 11 Anthony Rowe
A semaphore is a protected variable and constitutes the classic method for restricting
123 11 Anthony Rowe
access to shared resources (e.g. storage,actuators etc) in a multiprogramming environment.
124 11 Anthony Rowe
Nano-RK implements semaphores and signals such that
125 12 Anthony Rowe
tasks that are suspended on an event or waiting for access to a semaphore will not be
126 12 Anthony Rowe
scheduled until the corresponding signal is sent or semaphore becomes available. For more information
127 12 Anthony Rowe
on using semaphores please refer to the basic_sem project that comes with the Nano-RK distribution: 
128 11 Anthony Rowe
http://www.nanork.org:8000/nano-RK/browser/nano-RK/projects/basic_sem/main.c
129 16 Anthony Rowe
130 16 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.'' 
131 10 Anthony Rowe
132 1 Anthony Rowe
'''nrk_sem_t* nrk_sem_create(uint8_t count, uint8_t ceiling_priority);'''
133 10 Anthony Rowe
134 10 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
135 10 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:
136 10 Anthony Rowe
137 10 Anthony Rowe
{{{
138 10 Anthony Rowe
#!c
139 10 Anthony Rowe
   nrk_sem_t *my_semaphore;
140 10 Anthony Rowe
   ...
141 10 Anthony Rowe
   my_semaphore = nrk_sem_create(1,4);
142 10 Anthony Rowe
   if(my_semaphore==NULL) nrk_kprintf( PSTR("Error creating Semaphore\r\n" ));
143 10 Anthony Rowe
}}}
144 10 Anthony Rowe
145 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.
146 1 Anthony Rowe
147 1 Anthony Rowe
'''int8_t nrk_sem_pend(nrk_sem_t *rsrc );''' 
148 8 Anthony Rowe
149 8 Anthony Rowe
Semaphore pend takes the address of the created semaphore and attempts to access the resource.  If the resource is available,
150 8 Anthony Rowe
pend will decrement the resource counter and allow the program to continue, otherwise pend will suspend until the resource is
151 1 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:
152 30 Anthony Rowe
 * Errorno
153 30 Anthony Rowe
  * 1 Signal Not Found
154 30 Anthony Rowe
  * 2 Signal Index too large
155 1 Anthony Rowe
156 1 Anthony Rowe
{{{
157 1 Anthony Rowe
#!c
158 1 Anthony Rowe
   nrk_kprintf( PSTR("Task accessing semaphore\r\n"));
159 1 Anthony Rowe
   v = nrk_sem_pend(my_semaphore);
160 1 Anthony Rowe
   if(v==NRK_ERROR) nrk_kprintf( PSTR("Error calling pend\r\n"));
161 9 Anthony Rowe
   nrk_kprintf( PSTR("Task is now holding semaphore\r\n"));
162 9 Anthony Rowe
}}}
163 9 Anthony Rowe
164 9 Anthony Rowe
'''int8_t nrk_sem_post(nrk_sem_t* rsrc);'''
165 9 Anthony Rowe
166 9 Anthony Rowe
Semaphore post takes the address of a created semaphore and releases access to the resource.
167 1 Anthony Rowe
This should be called after exiting a critical section that was pended.  Below is an example of a task posting a semaphore:
168 30 Anthony Rowe
 * Errorno
169 30 Anthony Rowe
  * 1 Signal Not Found
170 30 Anthony Rowe
  * 2 Signal Index too large
171 9 Anthony Rowe
{{{
172 9 Anthony Rowe
#!c
173 9 Anthony Rowe
   v = nrk_sem_post(my_semaphore);
174 9 Anthony Rowe
   if(v==NRK_ERROR) nrk_kprintf( PSTR("Error calling post\r\n"));
175 8 Anthony Rowe
   nrk_kprintf( PSTR("Task released semaphore\r\n"));
176 8 Anthony Rowe
}}}
177 11 Anthony Rowe
178 8 Anthony Rowe
= Special Kernel Signals =
179 18 Anthony Rowe
180 20 Anthony Rowe
'''nrk_wakeup_signal'''
181 19 Anthony Rowe
182 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, it is not globally broadcast to all tasks.  See below for an example of how to use it as an event  timeout.
183 19 Anthony Rowe
184 20 Anthony Rowe
'''int8_t nrk_set_next_wakeup(nrk_time_t timeout);'''
185 19 Anthony Rowe
186 19 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
187 18 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. 
188 17 Anthony Rowe
189 17 Anthony Rowe
{{{
190 17 Anthony Rowe
#!c
191 8 Anthony Rowe
   nrk_time_t timeout;
192 17 Anthony Rowe
193 17 Anthony Rowe
   timeout.secs=10;
194 1 Anthony Rowe
   timeout.nano_secs=0;
195 1 Anthony Rowe
   ...
196 1 Anthony Rowe
197 17 Anthony Rowe
   nrk_set_next_wakeup(timeout);
198 17 Anthony Rowe
   my_sigs=nrk_event_wait( SIG(signal_one) | SIG(nrk_wakeup_signal) );
199 21 Anthony Rowe
200 18 Anthony Rowe
   // Lets check which signal we got...
201 18 Anthony Rowe
   if(my_sigs==0)                        nrk_kprintf( PSTR( "Error calling nrk_event_wait()\r\n" ));
202 18 Anthony Rowe
   if(my_sigs & SIG(signal_one))         nrk_kprintf( PSTR( "Task got signal_one\r\n") );
203 17 Anthony Rowe
   if(my_sigs & SIG(nrk_wakeup_signal))  nrk_kprintf( PSTR( "Task got timeout signal! \r\n") );
204 1 Anthony Rowe
}}}
205 1 Anthony Rowe
206 1 Anthony Rowe
| [wiki:nrk-api Contents] | [wiki:nrk-api-device-drivers Device Drivers] |