The Semaphore manager makes available a set of functions that manipulate
semaphore objects. Semaphores can be used for task synchronization and
mutual exclusion.
Semaphores can be counting semaphores or binary semaphores. Counting
semaphores keep track of the number of times the semaphore has been posted
with post(). This is useful, for example, if you have a group of resources
that are shared between tasks. Such tasks might call pend() to see if a
resource is available before using one.
Binary semaphores can have only two states: available (count = 1) and
unavailable (count = 0). They can be used to share a single resource between
tasks. They can also be used for a basic signaling mechanism, where the
semaphore can be posted multiple times. Binary semaphores do not keep track
of the count; they simply track whether the semaphore has been posted or not.
The Mailbox module uses a counting semaphore internally to manage the
count of free (or full) mailbox elements. Another example of a counting
semaphore is an ISR that might fill multiple buffers of data for
consumption by a task. After filling each buffer, the ISR puts the buffer on
a queue and calls post(). The task waiting for the data calls pend(), which
simply decrements the semaphore count and returns or blocks if the count is
0. The semaphore count thus tracks the number of full buffers available for
the task.
pend() is used to wait for a semaphore. The timeout parameter allows the
task to wait until a timeout, wait indefinitely, or not wait at all. The
return value is used to indicate if the semaphore was signaled successfully.
post() is used to signal a semaphore. If a task is waiting for the
semaphore, post() removes the task from the semaphore queue and puts it on
the ready queue. If no tasks are waiting, post() simply increments the
semaphore count and returns. For a binary semaphore the count is always
set to one.
enum Semaphore.Mode |
|
Types of semaphores
XDCscript usage |
meta-domain |
values of type Semaphore.Mode
const Semaphore.Mode_COUNTING;
// Counting semaphore
const Semaphore.Mode_BINARY;
// Binary Semaphore
typedef enum Semaphore_Mode {
Semaphore_Mode_COUNTING,
// Counting semaphore
Semaphore_Mode_BINARY
// Binary Semaphore
} Semaphore_Mode;
metaonly struct Semaphore.BasicView |
|
XDCscript usage |
meta-domain |
var obj = new Semaphore.BasicView;
obj.label = String ...
obj.event = String ...
obj.eventId = String ...
obj.mode = String ...
obj.count = Int ...
obj.pendedTasks = String[] ...
config Semaphore.A_badContext // module-wide |
|
Asserted when pend is called with non-zero timeout from other
than a Task context
XDCscript usage |
meta-domain |
msg: "A_badContext: bad calling context. Must be called from a Task."
};
extern const Assert_Id Semaphore_A_badContext;
config Semaphore.A_invTimeout // module-wide |
|
Generated by pend if BIOS_EVENT_ACQUIRED timeout is used
with a Semaphore that has not been configured with an Event object
XDCscript usage |
meta-domain |
msg: "A_invTimeout: Can't use BIOS_EVENT_ACQUIRED with this Semaphore."
};
extern const Assert_Id Semaphore_A_invTimeout;
config Semaphore.A_noEvents // module-wide |
|
Generated by create if supportsEvents is false
and an ti.sysbios.ipc.Event object is passed to
create
XDCscript usage |
meta-domain |
msg: "A_noEvents: The Event.supportsEvents flag is disabled."
};
config Semaphore.LM_pend // module-wide |
|
Logged on calls to Semaphore_pend()
XDCscript usage |
meta-domain |
msg: "LM_pend: sem: 0x%x, count: %d, timeout: %d"
};
config Semaphore.LM_post // module-wide |
|
Logged on calls to Semaphore_post()
XDCscript usage |
meta-domain |
msg: "LM_post: sem: 0x%x, count: %d"
};
config Semaphore.supportsEvents // module-wide |
|
Support Semaphores with Events? Default is false
XDCscript usage |
meta-domain |
Semaphore.supportsEvents = Bool false;
extern const Bool Semaphore_supportsEvents;
metaonly config Semaphore.common$ // module-wide |
|
Common module configuration parameters
XDCscript usage |
meta-domain |
DETAILS
All modules have this configuration parameter. Its name
contains the '$' character to ensure it does not conflict with
configuration parameters declared by the module. This allows
new configuration parameters to be added in the future without
any chance of breaking existing modules.
metaonly config Semaphore.rovViewInfo // module-wide |
|
XDCscript usage |
meta-domain |
module-wide built-ins |
|
// Get this module's unique id
Bool Semaphore_Module_startupDone( );
// Test if this module has completed startup
// The heap from which this module allocates memory
Bool Semaphore_Module_hasMask( );
// Test whether this module has a diagnostics mask
Bits16 Semaphore_Module_getMask( );
// Returns the diagnostics mask for this module
Void Semaphore_Module_setMask( Bits16 mask );
// Set the diagnostics mask for this module
per-instance object types |
|
typedef struct Semaphore_Object Semaphore_Object;
// Opaque internal representation of an instance object
// Client reference to an instance object
typedef struct Semaphore_Struct Semaphore_Struct;
// Opaque client structure large enough to hold an instance object
// Convert this instance structure pointer into an instance handle
// Convert this instance handle into an instance structure pointer
per-instance config parameters |
|
XDCscript usage |
meta-domain |
var params = new Semaphore.Params;
// Instance config-params object
// event if using Events. Default is null
params.eventId = UInt 1;
// eventId if using Events. Default is 1
// Semaphore mode. Default is COUNTING
typedef struct Semaphore_Params {
// Instance config-params structure
// Common per-instance configs
// event if using Events. Default is null
UInt eventId;
// eventId if using Events. Default is 1
// Semaphore mode. Default is COUNTING
} Semaphore_Params;
// Initialize this config-params structure with supplier-specified defaults before instance creation
config Semaphore.event // per-instance |
|
event if using Events. Default is null
XDCscript usage |
meta-domain |
var params = new Semaphore.Params;
...
DETAILS
If event is non-null:
Event_post(sem->event, sem->eventId) will be invoked when
Semaphore_post() is called.
Event_pend(sem->event, 0, sem->eventId, timeout) will be
invoked when Semaphore_pend() is called.
config Semaphore.eventId // per-instance |
|
eventId if using Events. Default is 1
XDCscript usage |
meta-domain |
var params = new Semaphore.Params;
...
params.eventId = UInt 1;
config Semaphore.mode // per-instance |
|
Semaphore mode. Default is COUNTING
XDCscript usage |
meta-domain |
var params = new Semaphore.Params;
...
DETAILS
When mode is BINARY , the semaphore has only two states, available
and unavailable. When mode is COUNTING, the semaphore keeps track of
number of times a semaphore is posted.
per-instance creation |
|
XDCscript usage |
meta-domain |
var params =
new Semaphore.
Params;
// Allocate instance config-params
params.config = ...
// Assign individual configs
var inst = Semaphore.create( Int count, params );
// Create an instance-object
// Allocate and initialize a new instance object and return its handle
// Initialize a new instance object inside the provided structure
ARGUMENTS
count
initial semaphore count
params
per-instance config params, or NULL to select default values (target-domain only)
eb
active error-handling block, or NULL to select default policy (target-domain only)
DETAILS
This function creates a new Semaphore object which is initialized to
count.
per-instance deletion |
|
// Finalize and free this previously allocated instance object, setting the referenced handle to NULL
// Finalize the instance object inside the provided structure
Semaphore.getCount( ) // per-instance |
|
Get current semaphore count
ARGUMENTS
handle
handle of a previously-created Semaphore instance object
RETURNS
current semaphore count
DETAILS
This function returns the current value of the semaphore specified by
the handle.
Semaphore.pend( ) // per-instance |
|
Wait for a semaphore
ARGUMENTS
handle
handle of a previously-created Semaphore instance object
timeout
return after this many system time units
RETURNS
TRUE if successful, FALSE if timeout
DETAILS
If the semaphore count is greater than zero (available), this function
decrements the count and returns TRUE. If the semaphore count is zero
(unavailable), this function suspends execution of the current task
until post() is called or the timeout expires.
A timeout value of
BIOS_WAIT_FOREVER causes
the task to wait indefinitely for its semaphore to be posted.
A timeout value of
BIOS_NO_WAIT
causes Semaphore_pend to return immediately.
Semaphore.post( ) // per-instance |
|
Signal a semaphore
ARGUMENTS
handle
handle of a previously-created Semaphore instance object
DETAILS
Readies the first task waiting for the semaphore. If no task is
waiting, This function simply increments the semaphore count and returns
In case of binary semaphore the count has a maximum value of one.
Semaphore.registerEvent( ) // per-instance |
|
Register an Event Object with a semaphore
ARGUMENTS
handle
handle of a previously-created Semaphore instance object
event
Ptr to Event Object
eventId
Event ID
DETAILS
Ordinarily, an Event object and eventId are configured at
Semaphore create time.
This API is provided so that Semaphore-using middleware
can support implicit Event posting without having to be
retrofitted.
After the Event object and eventId are registered with the
Semaphore:
Event_post(event, eventId) will be invoked when
Semaphore_post(sem) is called.
Event_pend(event, eventId, 0, timeout) will be invoked when
Semaphore_pend(sem, timeout) is called.
per-instance built-ins |
|
Int Semaphore_Object_count( );
// The number of statically-created instance objects
// The handle of the i-th statically-created instance object (array == NULL)
// The handle of the first dynamically-created instance object, or NULL
// The handle of the next dynamically-created instance object, or NULL
// The heap used to allocate dynamically-created instance objects
// The label associated with this instance object
// The name of this instance object