Ejemplo n.º 1
0
void EMBX_OS_EventDelete(semaphore_t *sem)
{
    EMBX_Assert(sem);

    /* This should assert if there are waiters on the semaphore
     * but OS21 does not give us an interface to find this out
     * at the moment.
     */

    /* Drain the semaphore count to 0 so that we can
     * re-allocate it from the cache in a default state.
     */
    while(semaphore_value(sem) != 0)
    {
        semaphore_wait(sem);
    }


    task_lock();

    ((void **) sem)[-1] = (void *) cacheHead;
    cacheHead = sem;

    task_unlock();
}
Ejemplo n.º 2
0
void EMBX_OS_EventDelete(EMBX_EVENT ev)
{
    EMBX_OS_MUTEX_TAKE(&cacheLock);

    EMBX_Assert(ev);
    EMBX_Assert(ev->next == (EMBX_EVENT) EMBX_HANDLE_VALID);

    EMBX_Assert(semaphore_value(ev->event) == 0);
    
    ev->next = cacheHead;
    cacheHead = ev;
    
    EMBX_OS_MUTEX_RELEASE(&cacheLock);
}
Ejemplo n.º 3
0
semaphore_t *EMBX_OS_EventCreate(void)
{
    semaphore_t *sem = 0;

    task_lock();

    if (cacheHead != 0)
    {
        sem = cacheHead;

        EMBX_Assert(0 == semaphore_value(sem));
        cacheHead = (semaphore_t *) ((void **) cacheHead)[-1];

	task_unlock();
    }
    else
    {
        /* just in time initialization of the cachePartition
         * (our custom memory allocator)
         */
        if (0 == cachePartition)
        {
	    /* Try to create a partition, this may block in the OS allowing
	     * another task to run.
             */
            partition_t *p = partition_create_any(0, cacheAlloc,   cacheFree,
                                                     cacheRealloc, cacheStatus);


            /* Since we may have allowed another tasks to run it is possible
             * another task may have beaten us to it and already set
             * a partition. If this is the case then we release the
             * partition we just got and use the one that is now there.
             */
            if (0 == cachePartition)
            {
                EMBX_Assert(p != 0);

                cachePartition = p;
            }
            else
            {
                /* This is a really unlikely corner case, but we can
                 * still continue even if our own partition create failed!
		 * (because 0 == cachePartition the semaphore will be
		 * created from the C library heap)
                 */
                if(p != 0)
                {
                    partition_delete(p);
                }
            }
        }


	task_unlock();
        sem = semaphore_create_fifo_p((partition_t *)cachePartition, 0);
    }

    return sem;
}
Ejemplo n.º 4
0
bool lock_acquire(Lock *lock, int64_t timeout) {
  assert(semaphore_value(&lock->sema) <= 1);
  return semaphore_down(&lock->sema, 1, timeout);
}
Ejemplo n.º 5
0
void lock_release(Lock *lock) {
  assert(semaphore_value(&lock->sema) == 0);
  semaphore_up(&lock->sema, 1);
}