« Previous - Version 22/45 (diff) - Next » - Current version
Anthony Rowe, 06/08/2007 08:53 pm

= Signals =

A signal is a message that a task can use to wakeup one or more tasks waiting on an
event or events. When waiting on an event, a task is suspended and does not consume
CPU time. Nano-RK supports 32 unique signals. It is possible to wait on multiple
signals such that any one of them can wake a task from sleep. All signals must be
created using nrk_sig_create() before they can be used. Any task that wishes to wakeup on a signal
must register the signal using nrk_sig_register(). There are a few special case signals that are
generated by the kernel used to support event timeouts and notification of special actions. See the
'''Special Kernel Signals''' subsection for more information. For more information about signals, please refer
to the basic_signals project that comes with Nano-RK:

// This is a macro used to convert a signal into a bitmask for nrk_event_wait()
// See nrk_event_wait() for an example.
#define SIG ((uint32_t)1)<<x

// These are typedefs used to represent signals and semaphores
typedef int8_t nrk_sig_t;
typedef uint32_t nrk_sig_mask_t;
typedef int8_t nrk_sem_t;

'''nrk_sig_mask_t nrk_get_active_signal_mask();'''

'''int8_t nrk_delete_signal(nrk_sig_t sig_id);'''

'''int8_t nrk_signal_unregister(nrk_sig_t sig_id);'''

'''int8_t nrk_signal_register(nrk_sig_t sig_id);'''

'''nrk_sig_t nrk_signal_create();'''

'''int8_t nrk_event_signal(nrk_sig_t sig_id);'''

This function is used to signal tasks that are waiting on events using nrk_event_wait().
See example below.

'''nrk_sig_mask_t nrk_event_wait(nrk_sig_mask_t event_mask);'''

This function will wait for a set events. nano-RK supports up to 32 signals. Each signal
represents a bit in a 32 bit number (bit 0 is reserved) so it is possible to logically
OR multiple signals together if you wish to wait on a combination of events. The 32
bit number returned by nrk_event_wait() corresponds to the signal that was returned.
When waiting on multiple signals, this should be used to determine which signal actually
triggered the wakeup. All signals need to be registered in order for a task to receive them.
This function returns 0 upon failure if a signal is specified that does not exist, or is not

= Semaphores =

A semaphore is a protected variable and constitutes the classic method for restricting
access to shared resources (e.g. storage,actuators etc) in a multiprogramming environment.
Nano-RK implements semaphores and signals such that
tasks that are suspended on an event or waiting for access to a semaphore will not be
scheduled until the corresponding signal is sent or semaphore becomes available. For more information
on using semaphores please refer to the basic_sem project that comes with the Nano-RK distribution:

''Note that the Atmel ISA does not have a test-and-set instruction, so we provide a best effort implementation by disabling interrupts.''

'''nrk_sem_t* nrk_sem_create(uint8_t count, uint8_t ceiling_priority);'''

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
using PCPE this should be the highest priority task accessing the critical section. Below is an example of declaring and creating a semaphore:

nrk_sem_t *my_semaphore;
my_semaphore = nrk_sem_create(1,4);
if(my_semaphore==NULL) nrk_kprintf( PSTR);

This created a semaphore with a count of 1 (also called a mutex or binary semaphore) with a priority ceiling value of 4.

'''int8_t nrk_sem_pend(nrk_sem_t *rsrc );'''

Semaphore pend takes the address of the created semaphore and attempts to access the resource. If the resource is available,
pend will decrement the resource counter and allow the program to continue, otherwise pend will suspend until the resource is
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:

nrk_kprintf( PSTR);
v = nrk_sem_pend(my_semaphore);
if(v==NRK_ERROR) nrk_kprintf( PSTR);
nrk_kprintf( PSTR);

'''int8_t nrk_sem_post(nrk_sem_t* rsrc);'''

Semaphore post takes the address of a created semaphore and releases access to the resource.
This should be called after exiting a critical section that was pended. Below is an example of a task posting a semaphore:

v = nrk_sem_post(my_semaphore);
if(v==NRK_ERROR) nrk_kprintf( PSTR);
nrk_kprintf( PSTR);

= Special Kernel Signals =


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.

'''int8_t nrk_set_next_wakeup(nrk_time_t timeout);'''

This function sets the task's next wakeup timer. It returns NRK_OK upon success and NRK_ERROR on failure. This function does not
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.

nrk_time_t timeout;

my_sigs=nrk_event_wait( SIG(signal_one) | SIG(nrk_wakeup_signal) );
// Lets check which signal we got...
if(my_sigs==0) nrk_kprintf( PSTR( "Error calling nrk_event_wait()\r\n" ));
if(my_sigs & SIG(signal_one)) nrk_kprintf( PSTR( "Task got signal_one\r\n") );
if(my_sigs & SIG(nrk_wakeup_signal)) nrk_kprintf( PSTR( "Task got timeout signal! \r\n") );
[wiki:nrk-api Contents] [wiki:nrk-api-device-drivers Device Drivers]