Results 1 to 3 of 3

Thread: semaphores not getting created?

  1. #1
    Join Date
    Sep 2008
    Posts
    7

    Default semaphores not getting created?

    I wanted to create a semaphore of my own to prevent multiple tasks from accessing a global structure, so I copied some code from WPILib in I2C.cpp
    Code:
    static SEM_ID semaphore = semMCreate(SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
    and put something similar in my code. When I ran my code, semMCreate() returned NULL and set errno to S_semLib_INVALID_OPTION (Invalid option was passed to semMCreate). The man page for semMCreate says this about the options (bold added by me):
    Semaphore options include the following:

    SEM_Q_PRIORITY (0x1)
    Queue pended tasks on the basis of their priority.

    SEM_Q_FIFO (0x0)
    Queue pended tasks on a first-in-first-out basis.

    SEM_DELETE_SAFE (0x4)
    Protect a task that owns the semaphore from unexpected deletion. This option enables an implicit taskSafe() for each semTake(), and an implicit taskUnsafe() for each semGive().

    SEM_INVERSION_SAFE (0x8)
    Protect the system from priority inversion. With this option, the task owning the semaphore will execute at the highest priority of the tasks pended on the semaphore, if it is higher than its current priority. This option must be accompanied by the SEM_Q_PRIORITY queuing mode.

    SEM_EVENTSEND_ERR_NOTIFY (0x10)
    When the semaphore is given, if a task is registered for events and the actual sending of events fails, a value of ERROR is returned and the errno is set accordingly. This option is off by default.

    SEM_INTERRUPTIBLE (0x20)
    Signal sent to an RTP task blocked on a semaphore created with this option, would make the task ready and return with ERROR and errno set to EINTR. This option has no affect for a kernel task blocked on the same semaphore created with this option. This option is off by default.
    So I added SEM_Q_PRIORITY to my options and the semaphore was created successfully.

    I didn't test the semaphores in WPILib, but I assume the places that create a semaphore and include SEM_INVERSION_SAFE, but do not include the SEM_Q_PRIORITY option, do not actually provide any synchronization because the semaphore does not exist. Calling semTake() on a NULL semaphore will immediately return a S_objLib_OBJ_ID_ERROR error.

  2. #2
    Join Date
    Sep 2008
    Location
    National Instruments: Austin, TX
    Posts
    445

    Default Re: semaphores not getting created?

    Good catch... thank you. There is also another problem with the I2C library... the semaphore is statically initialized, so it can cause problems if team use the I2C object statically. It will need to get some attention after build season.

  3. #3
    Join Date
    Sep 2008
    Location
    National Instruments: Austin, TX
    Posts
    445

    Default Re: semaphores not getting created?

    This problem affects more than just I2C. Solenoids, Analog Input, and the Dashboard interface. Also the DriverStationLCD Example. The Relay class was never protected... just had TODO's protecting those accesses. :/

    This seems pretty serious.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •