module ti.sysbios.knl.Semaphore

Semaphore Manager

The Semaphore manager makes available a set of functions that manipulate semaphore objects. Semaphores can be used for task synchronization and mutual exclusion. [ more ... ]
C synopsis target-domain sourced in ti/sysbios/knl/Semaphore.xdc
#include <ti/sysbios/knl/Semaphore.h>
Functions
Void
Void
Void
Int 
Void
Bool 
Void 
Void 
Void 
Functions common to all target instances
Functions common to all target modules
Typedefs
typedef enum
typedef struct
typedef struct
typedef struct
Constants
extern const Assert_Id 
extern const Assert_Id 
extern const Assert_Id 
extern const Assert_Id 
extern const Log_Event 
extern const Log_Event 
extern const Bool 
extern const Bool 
 
DETAILS
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.

Calling Context

Function Hwi Swi Task Main Startup
Params_init Y Y Y Y Y
construct N N Y Y N
create N N Y Y N
delete N N Y Y N
destruct N N Y Y N
getCount Y Y Y Y Y
pend N* N* Y N* N
post Y Y Y Y N
registerEvent N N Y Y Y
reset N N Y Y N
Definitions: (N* means OK to call iff the timeout parameter is set to '0'.)
  • Hwi: API is callable from a Hwi thread.
  • Swi: API is callable from a Swi thread.
  • Task: API is callable from a Task thread.
  • Main: API is callable during any of these phases:
    • In your module startup after this module is started (e.g. Semaphore_Module_startupDone() returns TRUE).
    • During xdc.runtime.Startup.lastFxns.
    • During main().
    • During BIOS.startupFxns.
  • Startup: API is callable during any of these phases:
    • During xdc.runtime.Startup.firstFxns.
    • In your module startup before this module is started (e.g. Semaphore_Module_startupDone() returns FALSE).
 
enum Semaphore_Mode

Semaphore types

C synopsis target-domain
typedef enum Semaphore_Mode {
    Semaphore_Mode_COUNTING,
    // Counting (FIFO)
    Semaphore_Mode_BINARY,
    // Binary (FIFO)
    Semaphore_Mode_COUNTING_PRIORITY,
    // Counting (priority-based)
    Semaphore_Mode_BINARY_PRIORITY
    // Binary (priority-based)
} Semaphore_Mode;
 
DETAILS
These enumerations specify the type of semaphore.
Tasks wait for the semaphore in FIFO order unless the PRIORITY option is chosen.
For PRIORITY semaphores, the pending task will be inserted in the waiting list before the first task that has lower priority. This ensures that tasks of equal priority will pend in FIFO order.
WARNING
PRIORITY semaphores have a potential to increase the interrupt latency in the system since interrupts are disabled while the list of tasks waiting on the semaphore is scanned for the proper insertion point. This is typically about a dozen instructions per waiting task. For example, if you have 10 tasks of higher priority waiting, then all 10 will be checked with interrupts disabled before the new task is entered onto the list.
 
config Semaphore_A_badContext  // module-wide

Assert raised if an operation is invalid in the current calling context

C synopsis target-domain
extern const Assert_Id Semaphore_A_badContext;
 
DETAILS
Asserted when pend is called with non-zero timeout from other than a Task context.
 
config Semaphore_A_noEvents  // module-wide

Assert raised if application uses Event but it's not supported

C synopsis target-domain
extern const Assert_Id Semaphore_A_noEvents;
 
DETAILS
This assertion is triggered by create if supportsEvents is false and an ti.sysbios.knl.Event object is passed to create.
 
config Semaphore_A_overflow  // module-wide

Assert raised if the semaphore count is incremented past 65535

C synopsis target-domain
extern const Assert_Id Semaphore_A_overflow;
 
DETAILS
Asserted when Semaphore_post() has been called when the 16 bit semaphore count is at its maxmium value of 65535.
 
config Semaphore_A_pendTaskDisabled  // module-wide

Asserted in Sempahore_pend()

C synopsis target-domain
extern const Assert_Id Semaphore_A_pendTaskDisabled;
 
DETAILS
Assert raised if Semaphore_pend() is called with the Task or Swi scheduler disabled.
 
config Semaphore_LM_pend  // module-wide

Logged on calls to Semaphore_pend()

C synopsis target-domain
extern const Log_Event Semaphore_LM_pend;
 
 
config Semaphore_LM_post  // module-wide

Logged on calls to Semaphore_post()

C synopsis target-domain
extern const Log_Event Semaphore_LM_post;
 
 
config Semaphore_supportsEvents  // module-wide

Support Semaphores with Events?

C synopsis target-domain
extern const Bool Semaphore_supportsEvents;
 
DETAILS
The default for this parameter is false.
 
config Semaphore_supportsPriority  // module-wide

Support Task priority pend queuing?

C synopsis target-domain
extern const Bool Semaphore_supportsPriority;
 
DETAILS
The default for this parameter is true.
Module-Wide Built-Ins

C synopsis target-domain
Types_ModuleId Semaphore_Module_id();
// Get this module's unique id
 
Bool Semaphore_Module_startupDone();
// Test if this module has completed startup
 
IHeap_Handle Semaphore_Module_heap();
// 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
Instance Object Types

C synopsis target-domain
typedef struct Semaphore_Object Semaphore_Object;
// Opaque internal representation of an instance object
 
typedef Semaphore_Object *Semaphore_Handle;
// Client reference to an instance object
 
typedef struct Semaphore_Struct Semaphore_Struct;
// Opaque client structure large enough to hold an instance object
 
Semaphore_Handle Semaphore_handle(Semaphore_Struct *structP);
// Convert this instance structure pointer into an instance handle
 
Semaphore_Struct *Semaphore_struct(Semaphore_Handle handle);
// Convert this instance handle into an instance structure pointer
Instance Config Parameters

C synopsis target-domain
typedef struct Semaphore_Params {
// Instance config-params structure
    IInstance_Params *instance;
    // Common per-instance configs
    Event_Handle event;
    // Event instance to use if non-NULL
    UInt eventId;
    // eventId if using Events
    Semaphore_Mode mode;
    // Semaphore mode
} Semaphore_Params;
 
Void Semaphore_Params_init(Semaphore_Params *params);
// Initialize this config-params structure with supplier-specified defaults before instance creation
 
config Semaphore_Params.event  // instance

Event instance to use if non-NULL

C synopsis target-domain
struct Semaphore_Params {
      ...
    Event_Handle event;
 
DETAILS
The default value of this parameter is null. 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_Params.eventId  // instance

eventId if using Events

C synopsis target-domain
struct Semaphore_Params {
      ...
    UInt eventId;
 
DETAILS
The default for this parameters is 1.
 
config Semaphore_Params.mode  // instance

Semaphore mode

C synopsis target-domain
struct Semaphore_Params {
      ...
    Semaphore_Mode mode;
 
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.
The default for this parameter is COUNTING.
Runtime Instance Creation

C synopsis target-domain
Semaphore_Handle Semaphore_create(Int count, const Semaphore_Params *params, Error_Block *eb);
// Allocate and initialize a new instance object and return its handle
 
Void Semaphore_construct(Semaphore_Struct *structP, Int count, const Semaphore_Params *params);
// 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.
NOTE
The "count" argument should not be a negative number as the Semaphore count is stored as a 16-bit unsigned integer inside the Semaphore object.
Instance Deletion

C synopsis target-domain
Void Semaphore_delete(Semaphore_Handle *handleP);
// Finalize and free this previously allocated instance object, setting the referenced handle to NULL
 
Void Semaphore_destruct(Semaphore_Struct *structP);
// Finalize the instance object inside the provided structure
 
Semaphore_getCount()  // instance

Get current semaphore count

C synopsis target-domain
Int Semaphore_getCount(Semaphore_Handle handle);
 
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()  // instance

Wait for a semaphore

C synopsis target-domain
Bool Semaphore_pend(Semaphore_Handle handle, UInt32 timeout);
 
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()  // instance

Signal a semaphore

C synopsis target-domain
Void Semaphore_post(Semaphore_Handle handle);
 
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 the case of a binary semaphore, the count has a maximum value of one.
 
Semaphore_registerEvent()  // instance

Register an Event Object with a semaphore

C synopsis target-domain
Void Semaphore_registerEvent(Semaphore_Handle handle, Event_Handle event, UInt eventId);
 
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.
 
Semaphore_reset()  // instance

Reset semaphore count

C synopsis target-domain
Void Semaphore_reset(Semaphore_Handle handle, Int count);
 
ARGUMENTS
handle — handle of a previously-created Semaphore instance object
count — semaphore count
DETAILS
Resets the semaphore count to count. No task switch occurs when calling SEM_reset.
CONSTRAINTS
count must be greater than or equal to 0.
No tasks should be waiting on the semaphore when Semaphore_reset is called.
Semaphore_reset cannot be called by a Hwi or a Swi.
Instance Built-Ins

C synopsis target-domain
Int Semaphore_Object_count();
// The number of statically-created instance objects
 
Semaphore_Handle Semaphore_Object_get(Semaphore_Object *array, Int i);
// The handle of the i-th statically-created instance object (array == NULL)
 
Semaphore_Handle Semaphore_Object_first();
// The handle of the first dynamically-created instance object, or NULL
 
Semaphore_Handle Semaphore_Object_next(Semaphore_Handle handle);
// The handle of the next dynamically-created instance object, or NULL
 
IHeap_Handle Semaphore_Object_heap();
// The heap used to allocate dynamically-created instance objects
 
Types_Label *Semaphore_Handle_label(Semaphore_Handle handle, Types_Label *buf);
// The label associated with this instance object
 
String Semaphore_Handle_name(Semaphore_Handle handle);
// The name of this instance object
 
Configuration settings sourced in ti/sysbios/knl/Semaphore.xdc
var Semaphore = xdc.useModule('ti.sysbios.knl.Semaphore');
module-wide constants & types
    values of type Semaphore.Mode// Semaphore types
module-wide config parameters
        msg: "A_badContext: bad calling context. Must be called from a Task."
    };
        msg: "A_noEvents: The Event.supportsEvents flag is disabled."
    };
        msg: "A_overflow: Count has exceeded 65535 and rolled over."
    };
        msg: "A_pendTaskDisabled: Cannot call Semaphore_pend() while the Task or Swi scheduler is disabled."
    };
        mask: Diags.USER1 | Diags.USER2,
        msg: "LM_pend: sem: 0x%x, count: %d, timeout: %d"
    };
        mask: Diags.USER1 | Diags.USER2,
        msg: "LM_post: sem: 0x%x, count: %d"
    };
 
per-instance config parameters
    var params = new Semaphore.Params// Instance config-params object;
        params.eventId// eventId if using Events = UInt 1;
per-instance creation
    var inst = Semaphore.create// Create an instance-object(Int count, params);
 
 
enum Semaphore.Mode

Semaphore types

Configuration settings
values of type Semaphore.Mode
    const Semaphore.Mode_COUNTING;
    // Counting (FIFO)
    const Semaphore.Mode_BINARY;
    // Binary (FIFO)
    const Semaphore.Mode_COUNTING_PRIORITY;
    // Counting (priority-based)
    const Semaphore.Mode_BINARY_PRIORITY;
    // Binary (priority-based)
 
DETAILS
These enumerations specify the type of semaphore.
Tasks wait for the semaphore in FIFO order unless the PRIORITY option is chosen.
For PRIORITY semaphores, the pending task will be inserted in the waiting list before the first task that has lower priority. This ensures that tasks of equal priority will pend in FIFO order.
WARNING
PRIORITY semaphores have a potential to increase the interrupt latency in the system since interrupts are disabled while the list of tasks waiting on the semaphore is scanned for the proper insertion point. This is typically about a dozen instructions per waiting task. For example, if you have 10 tasks of higher priority waiting, then all 10 will be checked with interrupts disabled before the new task is entered onto the list.
C SYNOPSIS
 
config Semaphore.A_badContext  // module-wide

Assert raised if an operation is invalid in the current calling context

Configuration settings
Semaphore.A_badContext = Assert.Desc {
    msg: "A_badContext: bad calling context. Must be called from a Task."
};
 
DETAILS
Asserted when pend is called with non-zero timeout from other than a Task context.
C SYNOPSIS
 
config Semaphore.A_noEvents  // module-wide

Assert raised if application uses Event but it's not supported

Configuration settings
Semaphore.A_noEvents = Assert.Desc {
    msg: "A_noEvents: The Event.supportsEvents flag is disabled."
};
 
DETAILS
This assertion is triggered by create if supportsEvents is false and an ti.sysbios.knl.Event object is passed to create.
C SYNOPSIS
 
config Semaphore.A_overflow  // module-wide

Assert raised if the semaphore count is incremented past 65535

Configuration settings
Semaphore.A_overflow = Assert.Desc {
    msg: "A_overflow: Count has exceeded 65535 and rolled over."
};
 
DETAILS
Asserted when Semaphore_post() has been called when the 16 bit semaphore count is at its maxmium value of 65535.
C SYNOPSIS
 
config Semaphore.A_pendTaskDisabled  // module-wide

Asserted in Sempahore_pend()

Configuration settings
Semaphore.A_pendTaskDisabled = Assert.Desc {
    msg: "A_pendTaskDisabled: Cannot call Semaphore_pend() while the Task or Swi scheduler is disabled."
};
 
DETAILS
Assert raised if Semaphore_pend() is called with the Task or Swi scheduler disabled.
C SYNOPSIS
 
config Semaphore.LM_pend  // module-wide

Logged on calls to Semaphore_pend()

Configuration settings
Semaphore.LM_pend = Log.EventDesc {
    mask: Diags.USER1 | Diags.USER2,
    msg: "LM_pend: sem: 0x%x, count: %d, timeout: %d"
};
 
C SYNOPSIS
 
config Semaphore.LM_post  // module-wide

Logged on calls to Semaphore_post()

Configuration settings
Semaphore.LM_post = Log.EventDesc {
    mask: Diags.USER1 | Diags.USER2,
    msg: "LM_post: sem: 0x%x, count: %d"
};
 
C SYNOPSIS
 
config Semaphore.supportsEvents  // module-wide

Support Semaphores with Events?

Configuration settings
Semaphore.supportsEvents = Bool false;
 
DETAILS
The default for this parameter is false.
C SYNOPSIS
 
config Semaphore.supportsPriority  // module-wide

Support Task priority pend queuing?

Configuration settings
Semaphore.supportsPriority = Bool true;
 
DETAILS
The default for this parameter is true.
C SYNOPSIS
 
metaonly config Semaphore.common$  // module-wide

Common module configuration parameters

Configuration settings
Semaphore.common$ = Types.Common$ undefined;
 
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.
Instance Config Parameters

Configuration settings
var params = new Semaphore.Params;
// Instance config-params object
    params.event = Event.Handle null;
    // Event instance to use if non-NULL
    params.eventId = UInt 1;
    // eventId if using Events
    params.mode = Semaphore.Mode Semaphore.Mode_COUNTING;
    // Semaphore mode
 
config Semaphore.Params.event  // instance

Event instance to use if non-NULL

Configuration settings
var params = new Semaphore.Params;
  ...
params.event = Event.Handle null;
 
DETAILS
The default value of this parameter is null. 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.
C SYNOPSIS
 
config Semaphore.Params.eventId  // instance

eventId if using Events

Configuration settings
var params = new Semaphore.Params;
  ...
params.eventId = UInt 1;
 
DETAILS
The default for this parameters is 1.
C SYNOPSIS
 
config Semaphore.Params.mode  // instance

Semaphore mode

Configuration settings
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.
The default for this parameter is COUNTING.
C SYNOPSIS
Static Instance Creation

Configuration settings
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
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.
NOTE
The "count" argument should not be a negative number as the Semaphore count is stored as a 16-bit unsigned integer inside the Semaphore object.
generated on Tue, 09 Oct 2018 20:57:33 GMT