This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/CC3220: How to create a priority-aware semaphore using the POSIX API of TI-RTOS

Part Number: CC3220


Tool/software: TI-RTOS

I'm using the POSIX interface for TI-RTOS. When I create a semaphore using sem_init() a simple (FIFO) semaphore is created. But I need a priority-aware semaphore. How can I accomplish this?

  • Hi Harry,

    What do you mean by priority-aware? Do you mean if a lower priority task owns the semaphore and a higher priority task tries to obtain it, then the lower priority task is raised to the same level as the higher priority task (to avoid priority inversion).

    Todd
  • Hi Todd,

    No, I'm referring to paragraph 4.1 of the TI-RTOS Kernel User's Guide http://www.ti.com/lit/ug/spruex3u/spruex3u.pdf. Which states: "Optionally, you can create "priority" semaphores that insert pending tasks into the waiting list before the first task that has a lower priority. As a result, tasks of equal priority pend in FIFO order, but tasks of higher priority are readied before tasks of lower priority."

    I know how to do this using the SYS/BIOS config file (semParams.mode = Semaphore_Mode_COUNTING_PRIORITY;). But how can I create such a semaphore using the POSIX API call sem_init()?

    Harry

  • Hi Harry,

    Thanks for the clarification. I don't think POSIX semaphore interface supports that capability. I know for sure that the TI-RTOS POSIX shim layer does not support it.

    Please note that you can use the native TI-RTOS calls if you need this feature.

    Todd
  • Hi Todd,

    Thanks for your answer. But should a real-time operating system not behave differently? Tasks waiting on a semaphore are now released in FIFO order. It seems to me that they should be released in priority order. I do understand that this behavior is an option in native TI-RTOS. But when I use the POSIX shim layer I would expect real-time aware semaphores. The POSIX documentation for sem_post (https://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_post.html) says that for real-time systems: "the highest priority waiting thread shall be unblocked, and if there is more than one highest priority thread blocked waiting for the semaphore, then the highest priority thread that has been waiting the longest shall be unblocked".

    The SYS/BIOS POSIX Thread (pthread) Support page (http://processors.wiki.ti.com/index.php/SYS/BIOS_POSIX_Thread_(pthread)_Support) states: " SYS/BIOS has only a priority-based scheduling policy". This also suggests that threads are scheduled in priority order.

    Anyway I wrote my own version of sem_init called sem_priority_init which will create a POSIX semaphore with priority-aware behavior.

    // Implementation of sem_init which uses a priority based waiting queue
    int sem_priority_init(sem_t *semaphore, int pshared, unsigned value)
    {
        typedef struct {
            Semaphore_Struct sem;
        } sem_obj;
        sem_obj *obj = (sem_obj*)(&semaphore->sysbios);
    
        /* object size validation */
        Assert_isTrue(sizeof(sem_obj) <= sizeof(sem_t), NULL);
    
        if (value > 65535) {
            errno = EINVAL;
            return -1;
        }
    
        /* semaphore mode is Semaphore_Mode_COUNTING_PRIORITY */
        Semaphore_Params sem_pars;
        Semaphore_Params_init(&sem_pars);
        sem_pars.mode = Semaphore_Mode_COUNTING_PRIORITY;
        Semaphore_construct(&(obj->sem), (int)value, &sem_pars);
        return 0;
    }

    Please not that the option supportsPriority must be set in the .cfg file for this to work:

    Semaphore.supportsPriority = true;

    Please let me know your thoughts about this.

    Harry

  • Hi Harry,

    You solution for the TI-RTOS kernel looks fine. I'll discuss your issue with the current implementation with the development team.

    Todd