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 Log_Event 
extern const Log_Event 
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:
  • 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

Types of semaphores

C synopsis target-domain
typedef enum Semaphore_Mode {
    Semaphore_Mode_COUNTING,
    // Counting semaphore
    Semaphore_Mode_BINARY
    // Binary Semaphore
} Semaphore_Mode;
config Semaphore_A_badContext  // module-wide

Asserted when pend is called with non-zero timeout from other than a Task context

C synopsis target-domain
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

C synopsis target-domain
extern const Assert_Id Semaphore_A_invTimeout;
config Semaphore_A_noEvents  // module-wide

Generated by create if supportsEvents is false and an ti.sysbios.knl.Event object is passed to create

C synopsis target-domain
extern const Assert_Id Semaphore_A_noEvents;
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? Default is false

C synopsis target-domain
extern const Bool Semaphore_supportsEvents;
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 if using Events. Default is null
    UInt eventId;
    // eventId if using Events. Default is 1
    Semaphore_Mode mode;
    // Semaphore mode. Default is COUNTING
} Semaphore_Params;
 
Void Semaphore_Params_init(Semaphore_Params *params);
// Initialize this config-params structure with supplier-specified defaults before instance creation
config Semaphore_event  // instance

event if using Events. Default is null

C synopsis target-domain
struct Semaphore_Params {
      ...
    Event_Handle event;
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  // instance

eventId if using Events. Default is 1

C synopsis target-domain
struct Semaphore_Params {
      ...
    UInt eventId;
config Semaphore_mode  // instance

Semaphore mode. Default is COUNTING

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.
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.
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, UInt 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 case of 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
 
XDCscript usage meta-domain sourced in ti/sysbios/knl/Semaphore.xdc
var Semaphore = xdc.useModule('ti.sysbios.knl.Semaphore');
module-wide constants & types
    values of type Semaphore.Mode// Types of semaphores
    var obj = new Semaphore.BasicView// ;
        obj.label = String  ...
        obj.event = String  ...
        obj.eventId = String  ...
        obj.mode = String  ...
        obj.count = Int  ...
        obj.pendedTasks = String[]  ...
module-wide config parameters
        msg: "A_badContext: bad calling context. Must be called from a Task."
    };
        msg: "A_invTimeout: Can't use BIOS_EVENT_ACQUIRED with this Semaphore."
    };
        msg: "A_noEvents: The Event.supportsEvents flag 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. Default is 1 = UInt 1;
per-instance creation
    var inst = Semaphore.create// Create an instance-object(Int count, params);
 
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
C SYNOPSIS
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
Semaphore.A_badContext = Assert.Desc {
    msg: "A_badContext: bad calling context. Must be called from a Task."
};
C SYNOPSIS
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
Semaphore.A_invTimeout = Assert.Desc {
    msg: "A_invTimeout: Can't use BIOS_EVENT_ACQUIRED with this Semaphore."
};
C SYNOPSIS
config Semaphore.A_noEvents  // module-wide

Generated by create if supportsEvents is false and an ti.sysbios.knl.Event object is passed to create

XDCscript usage meta-domain
Semaphore.A_noEvents = Assert.Desc {
    msg: "A_noEvents: The Event.supportsEvents flag is disabled."
};
C SYNOPSIS
config Semaphore.LM_pend  // module-wide

Logged on calls to Semaphore_pend()

XDCscript usage meta-domain
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()

XDCscript usage meta-domain
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? Default is false

XDCscript usage meta-domain
Semaphore.supportsEvents = Bool false;
C SYNOPSIS
metaonly config Semaphore.common$  // module-wide

Common module configuration parameters

XDCscript usage meta-domain
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.
metaonly config Semaphore.rovViewInfo  // module-wide
XDCscript usage meta-domain
Semaphore.rovViewInfo = ViewInfo.Instance ViewInfo.create;
Instance Config Parameters

XDCscript usage meta-domain
var params = new Semaphore.Params;
// Instance config-params object
    params.event = Event.Handle null;
    // event if using Events. Default is null
    params.eventId = UInt 1;
    // eventId if using Events. Default is 1
    params.mode = Semaphore.Mode Semaphore.Mode_COUNTING;
    // Semaphore mode. Default is COUNTING
config Semaphore.event  // instance

event if using Events. Default is null

XDCscript usage meta-domain
var params = new Semaphore.Params;
  ...
params.event = Event.Handle null;
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.
C SYNOPSIS
config Semaphore.eventId  // instance

eventId if using Events. Default is 1

XDCscript usage meta-domain
var params = new Semaphore.Params;
  ...
params.eventId = UInt 1;
C SYNOPSIS
config Semaphore.mode  // 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.
C SYNOPSIS
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
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.
generated on Thu, 03 Feb 2011 19:01:28 GMT