Milos RTOS v0.3.4a
Real Time Operating System
Functions
Collaboration diagram for Functions:

Functions

__PLOCK __lockCreate (__VOID)
 Creates a lock.
__STATIC u8 __lockWait (__PLOCK lock, u32 wait)
 Puts the calling thread to wait to the lock to be released.
__BOOL __lockOwn (__PLOCK lock, u32 wait)
 Claims a lock. If it is not released, it will put the calling thread into waiting state.
__BOOL __lockRelease (__PLOCK lock)
 Releases a lock.
__VOID __lockDestroy (__PLOCK lock)
 Destroys a lock.
u32 __lockGetCount (__VOID)
 Used to retrieve the total lock count.
__PLOCK_LIST __lockGetList (__VOID)
 Returns the list of active locks.

Function Documentation

Creates a lock.

Call this function to allocate a lock. Do not free the pointer returned, call __lockDestroy() function instead. A lock can always be created without calling this function, but it will not be managed by the __lockList list.

Returns:
A pointer to a lock.

Definition at line 81 of file lock.c.

{
__PLOCK         lock;
__PLOCK_LIST    list;
__PLOCK_LIST    list_root = __NULL;

    __systemStop();

    /* Alloc and assign */
    lock = __heapAlloc(sizeof(__LOCK));
    if (!lock)
    {
        __systemStart();
        return __NULL;
    }

    lock->state     = __LOCK_RELEASED;
    lock->type      = __LOCK_TYPE_NORMAL;
    lock->owner     = __NULL;
    lock->parent    = __threadGetCurrent();
    
    /* Add to list */
    list = __lockList;
    while(list != __NULL)
    {
        /* Remember last list */
        list_root = list;
        list = list->next;
    }

    list = __heapAllocZero(sizeof(__LOCK_LIST));
    if (!list)
    {
        __heapFree(lock);
        __systemStart();
        return __NULL;
    }

    list->lock = lock;
    list->next = __NULL;

    if (__lockList == __NULL)
    {
        __lockList = list;
    } else
    {
        list_root->next = list;
    }

    __lockCount++;

    __systemStart();

    return lock;
}

Here is the call graph for this function:

__STATIC u8 __lockWait ( __PLOCK  lock,
u32  wait 
)

Puts the calling thread to wait to the lock to be released.

Internal function called from __lockOwn() function. After waiting for the lock, the thread will be removed from the lock's priority list.

Parameters:
lockPointer to a lock.
waitTime to wait in milliseconds.
Returns:
__LOCK_WAIT_RELEASED if the lock has been released before the time expires. __LOCK_WAIT_TIMEOUT on timeout.

Definition at line 147 of file lock.c.

{
    u8 ret = __EVRET_TIMEOUT;

    __eventReset(&lock->event);
    ret = __eventWait(&lock->event, wait);
    return ret;
}

Here is the call graph for this function:

__BOOL __lockOwn ( __PLOCK  lock,
u32  wait 
)

Claims a lock. If it is not released, it will put the calling thread into waiting state.

Call this function to claim a lock. If it is not released it will put the calling thread into waiting state until released or the time elapses. The thread will be added to the lock's priority list and waked up in order of arrival. See the __lockAddToPriorityList() function.

Parameters:
lockPointer to a lock.
waitTime to wait in milliseconds.
Returns:
__TRUE on success claiming the lock, __FALSE otherwise.

Definition at line 168 of file lock.c.

{
    u32 time = 0;

    __systemStop();

    /* Check if __lockOwn is not being called from an already
     * "owner" thread.
     */
    if (lock->state == __LOCK_LOCKED && lock->owner == __threadGetCurrent()) {
        __systemStart();
        return __TRUE;
    }

    /* Here, in the case of a failed lock wait, we will
     * reuse the time left to wait again.
     * For example can happen that:
     *
     * 1-   Thread1 and Thread2 has the same priority, so they won't preempt
     *      each other.
     * 2-   Thread1 owns a lock, and goes suspended.
     * 3-   Thread2 wants to own the locked lock, but the lock
     *      is busy, so it will enter __lockWait().
     * 4-   Thread1 wakes up, and releases the lock (__lockRelease()),
     *      setting Thread2 ready to run. But soon after releasing, Thread1 owns the lock again,
     *      and goes suspended.
     * 5-   Thread2, waked up and ready to run by the __lockRelease() call (from Thread1)
     *      tries to own the lock, but it founds it locked.
     *
     */
    time = wait;
    while (lock->state != __LOCK_RELEASED && time)
    {
        __lockWait(lock, time);
        time = __threadGetCurrent()->th_wait;
    }

    if (lock->state != __LOCK_RELEASED)
    {
        __systemStart();
        return __FALSE;
    }

    lock->state = __LOCK_LOCKED;
    lock->owner = __threadGetCurrent();

    __systemStart();
    return __TRUE;
}

Here is the call graph for this function:

Releases a lock.

Call this function to release a previously locked event. It will wake up the first thread in the lock's priority list.

Parameters:
lockPointer to a lock.
Returns:
__TRUE on success, otherwise __FALSE.

Definition at line 226 of file lock.c.

{
    if (!lock) return __FALSE;

    __systemStop();
    lock->state = __LOCK_RELEASED;
    lock->owner = __NULL;
    
    /* wake threads waiting for this lock */
    __eventSetOne(&lock->event);
    __systemStart();

    return __TRUE;
}

Here is the call graph for this function:

Destroys a lock.

Call this function to destroy a lock created with the __lockCreate() function. It will be removed from the __lockList list.

Parameters:
lockPointer to a lock.
Returns:
Nothing.

Definition at line 249 of file lock.c.

{
__PLOCK_LIST list       = __NULL;
__PLOCK_LIST root_list  = __NULL;

    list = __lockList;
    root_list = list;

    __systemStop();
    while (list)
    {
        if (list->lock == lock)
        {
            __heapFree(lock);

            if (__lockList == list)
            {
                (list->next != __NULL) ? (__lockList = list->next) : (__lockList = __NULL);
            } else
            {
                (list->next != __NULL) ? (root_list->next = list->next) : (root_list->next = __NULL);
            }

            __heapFree(list);

            __lockCount--;

            __systemStart();
            return;
        }

        root_list = list;
        list = list->next;
    }
    __systemStart();
    return;
}

Here is the call graph for this function:

Used to retrieve the total lock count.

Returns:
The active lock count.

Definition at line 292 of file lock.c.

{
    return __lockCount;
}

Returns the list of active locks.

Returns:
The list of active locks.

Definition at line 302 of file lock.c.

{
    return __lockList;
}
 All Data Structures Files Functions Variables Typedefs Defines