Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Description

Deletes a semaphore. This function should be used with care as multiple tasks may rely on the presence of the semaphore. Generally speaking, before deleting a semaphore, first delete all the tasks that access the semaphore. As a rule, it is highly recommended to not delete kernel objects at run time. Anchor10031231003123

Deleting the semaphore will not de-allocate the object. In other words, storage for the variable will still remain at the same location unless the semaphore is allocated dynamically from the heap. The dynamic allocation of objects has its own set of problems. Specifically, it is not recommended for embedded systems to allocate (and de-allocate) objects from the heap given the high likelihood of fragmentation. Anchor10031271003127 Arguments Anchor10031281003128 p_sem Anchor10031301003130

Files

os.h/os_sem.c

Prototype

Code Block
OS_OBJ_QTY  OSSemDel (OS_SEM  *p_sem,
                      OS_OPT   opt,
                      OS_ERR  *p_err)

Arguments

p_sem

is a pointer to the semaphore.

...

1003131opt Anchor10031321003132

specifies one of two options: OS_OPT_DEL_NO_PEND or OS_OPT_DEL_ALWAYS.

...

...

OS_OPT_DEL_NO_PEND specifies to delete the semaphore only if no task is waiting on the semaphore. Because no task is “currently” waiting on the semaphore does not mean that a task will not attempt to wait for the semaphore later. How would such a task handle the situation waiting for a semaphore that was deleted? The application code will have to deal with this eventuality.

...

OS_OPT_DEL_ALWAYS specifies deleting the semaphore, regardless of whether tasks are waiting on the semaphore or not. If there are tasks waiting on the semaphore, these tasks will be made ready-to-run and informed (through an appropriate error code) that the reason the task is readied is that the semaphore it was waiting on was deleted. The same reasoning applies with the other option, how will the tasks handle the fact that the semaphore they want to wait for is no longer available?

...

10031451003145p_err Anchor10031461003146

is a pointer to a variable used to hold an error code. The error code may be one of the following:

...

classWebWorks_Indent_1

...

...

OS_ERR_NONE

...

classWebWorks_Indent_2

...

If the call is successful and the semaphore has been deleted.

...

classWebWorks_Indent_1

...

OS_ERR_DEL_ISR

...

classWebWorks_Indent_2

...

If OS_CFG_CALLED_FROM_ISR_CHK_EN set to

...

DEF_ENABLED in os_cfg.h: if attempting to delete the semaphore from an ISR.

...

classWebWorks_Indent_1

...

OS_ERR_ILLEGAL_DEL_RUN_TIME

If OS_SAFETY_CRITICAL_IEC61508 is defined: you called this after calling OSStart() and thus you are no longer allowed to delete kernel objects.

OS_ERR_OBJ_PTR_NULL

...

classWebWorks_Indent_2

...

If OS_CFG_ARG_CHK_EN is set to

...

DEF_ENABLED in os_cfg.h: if p_sem is a NULL pointer.

...

classWebWorks_Indent_1

...

OS_ERR_OBJ_TYPE

...

classWebWorks_Indent_2

...

If OS_CFG_OBJ_TYPE_CHK_EN is set to

...

DEF_ENABLED in os_cfg.h: if p_sem is not pointing to a semaphore.

...

...

OS_ERR_OPT_INVALID

...

classWebWorks_Indent_2

...

If OS_CFG_ARG_CHK_EN is set to

...

DEF_ENABLED in os_cfg.h: if one of the two options mentioned in the opt argument is not specified.

...

classWebWorks_Indent_1

...

OS_ERR_OS_NOT_RUNNING

If OS_CFG_INVALID_OS_CALLS_CHK_EN is set to DEF_ENABLED in os_cfg.h: if µC/OS-III is not running yet.

OS_ERR_TASK_WAITING

...

classWebWorks_Indent_2

...

If one or more tasks are waiting on the semaphore.

...

...

Returned Value

Anchor10031641003164 None Anchor10031651003165 Notes/Warnings Anchor10031671003167The number of tasks made ready-to-run by this function. Zero either indicates an error or that no tasks were pending on the semaphore.

Required Configuration

OS_CFG_SEM_EN and OS_CFG_SEM_DEL_EN must be enabled in os_cfg.h. Refer to µC-OS-III Configuration Manual.

Callers

Application.

Notes/Warnings

  1. Use this call with care because other tasks might expect the presence of the semaphore.

...

Example Usage

...

Code Block

...

OS_SEM SwSem;     void Task (void *p_arg) { OS_ERR err;     while

              while (DEF_ON)
{ : : OSSemDel(&SwSem,
 {
                  :
                  :
                  qty = OSSemDel(&SwSem,
                                  OS_OPT_DEL_ALWAYS,
&err); /* Check “err” */ : : } }tr

                                 &err);
                  /* Check "err" */
                  :
                  :
              }
          }
HTML Table
summary
classCode_Listing
Table Row (tr)
Table Cell (td)
rowspan16
Anchor
10031701003170
Anchor
10031711003171
Anchor
10031721003172
Anchor
10031731003173
Anchor
10031741003174
Anchor
10031751003175
Anchor
10031761003176
Anchor
10031771003177
Anchor
10031781003178
titleOSSemDel() example usage
          OS_SEM  SwSem;
           
           
          void Task (void *p_arg)
          {
              OS_ERR      err;
              OS_OBJ_QTY  qty;
           
           
              (void)&p_arg;
Anchor
10031791003179
Anchor
10031801003180
Anchor
10031811003181
Anchor
10031821003182
Anchor
10031831003183
Anchor
10031841003184
Anchor
10031851003185
Anchor
10031861003186
Anchor
10031871003187
Anchor
10031881003188
Anchor
10031891003189
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)
Table Row (tr)