Beispiel #1
0
nOS_Error nOS_QueueRead (nOS_Queue *queue, void *block, nOS_TickCounter timeout)
{
    nOS_Error       err;
    nOS_StatusReg   sr;
    nOS_Thread      *thread;

#if (NOS_CONFIG_SAFE > 0)
    if (queue == NULL) {
        err = NOS_E_INV_OBJ;
    } else if (block == NULL) {
        err = NOS_E_NULL;
    } else
#endif
    {
        nOS_EnterCritical(sr);
#if (NOS_CONFIG_SAFE > 0)
        if (queue->e.type != NOS_EVENT_QUEUE) {
            err = NOS_E_INV_OBJ;
        } else
#endif
        {
            /* No chance a thread waiting to read from queue if count is higher than 0 */
            if (queue->bcount > 0) {
                _Read(queue, block);
                /* Check if thread waiting to write in queue */
                thread = nOS_SendEvent((nOS_Event*)queue, NOS_OK);
                if (thread != NULL) {
                    /* Write thread's block in queue */
                    _Write(queue, thread->ext);
#if (NOS_CONFIG_HIGHEST_THREAD_PRIO > 0) && (NOS_CONFIG_SCHED_PREEMPTIVE_ENABLE > 0)
                    if ((thread->state == NOS_THREAD_READY) && (thread->prio > nOS_runningThread->prio)) {
                        nOS_Schedule();
                    }
#endif
                }
                err = NOS_OK;
            } else if (timeout == NOS_NO_WAIT) {
                err = NOS_E_EMPTY;
            } else if (nOS_isrNestingCounter > 0) {
                err = NOS_E_ISR;
            }
#if (NOS_CONFIG_SCHED_LOCK_ENABLE > 0)
            else if (nOS_lockNestingCounter > 0) {
                err = NOS_E_LOCKED;
            }
#endif
            else if (nOS_runningThread == &nOS_idleHandle) {
                err = NOS_E_IDLE;
            }
            else {
                nOS_runningThread->ext = block;
                err = nOS_WaitForEvent((nOS_Event*)queue, NOS_THREAD_READING_QUEUE, timeout);
            }
        }
        nOS_LeaveCritical(sr);
    }

    return err;
}
Beispiel #2
0
nOS_Error nOS_QueueRead (nOS_Queue *queue, void *block, nOS_TickCounter timeout)
{
    nOS_Error       err;
    nOS_StatusReg   sr;
    nOS_Thread      *thread;

#if (NOS_CONFIG_SAFE > 0)
    if (queue == NULL) {
        err = NOS_E_INV_OBJ;
    }
    else if (block == NULL) {
        err = NOS_E_NULL;
    } else
#endif
    {
        nOS_EnterCritical(sr);
#if (NOS_CONFIG_SAFE > 0)
        if (queue->e.type != NOS_EVENT_QUEUE) {
            err = NOS_E_INV_OBJ;
        } else
#endif
        /* No chance a thread waiting to read from queue if count is higher than 0 */
        if (queue->bcount > 0) {
            _Read(queue, block);
            /* Check if thread waiting to write in queue */
            thread = nOS_SendEvent((nOS_Event*)queue, NOS_OK);
            if (thread != NULL) {
                /* Write thread's block in queue */
                _Write(queue, thread->ext);
#if (NOS_CONFIG_SCHED_PREEMPTIVE_ENABLE > 0)
                /* Verify if a highest prio thread is ready to run */
                nOS_Schedule();
#endif
            }
            err = NOS_OK;
        }
        else if (timeout == NOS_NO_WAIT) {
            err = NOS_E_EMPTY;
        }
        else {
            nOS_runningThread->ext = block;
            err = nOS_WaitForEvent((nOS_Event*)queue,
                                   NOS_THREAD_READING_QUEUE
#if (NOS_CONFIG_WAITING_TIMEOUT_ENABLE > 0)
                                  ,timeout
#elif (NOS_CONFIG_SLEEP_ENABLE > 0) || (NOS_CONFIG_SLEEP_UNTIL_ENABLE > 0)
                                  ,NOS_WAIT_INFINITE
#endif
                                  );
        }
        nOS_LeaveCritical(sr);
    }

    return err;
}
Beispiel #3
0
nOS_Error nOS_SemGive (nOS_Sem *sem)
{
    nOS_Error       err;
    nOS_StatusReg   sr;
    nOS_Thread      *thread;

#if (NOS_CONFIG_SAFE > 0)
    if (sem == NULL) {
        err = NOS_E_INV_OBJ;
    } else
#endif
    {
        nOS_EnterCritical(sr);
#if (NOS_CONFIG_SAFE > 0)
        if (sem->e.type != NOS_EVENT_SEM) {
            err = NOS_E_INV_OBJ;
        } else
#endif
        {
            thread = nOS_SendEvent((nOS_Event*)sem, NOS_OK);
            if (thread != NULL) {
    #if (NOS_CONFIG_HIGHEST_THREAD_PRIO > 0) && (NOS_CONFIG_SCHED_PREEMPTIVE_ENABLE > 0)
                if ((thread->state == NOS_THREAD_READY) && (thread->prio > nOS_runningThread->prio)) {
                    nOS_Schedule();
                }
    #endif

                err = NOS_OK;
            } else if (sem->count < sem->max) {
                sem->count++;

                err = NOS_OK;
            } else if (sem->max > 0) {
                err = NOS_E_OVERFLOW;
            } else {
                /* No thread waiting to consume sem, inform producer */
                err = NOS_E_NO_CONSUMER;
            }
        }
        nOS_LeaveCritical(sr);
    }

    return err;
}
Beispiel #4
0
nOS_Error nOS_QueueWrite (nOS_Queue *queue, void *block, nOS_TickCounter timeout)
{
    nOS_Error       err;
    nOS_StatusReg   sr;
    nOS_Thread      *thread;

#if (NOS_CONFIG_SAFE > 0)
    if (queue == NULL) {
        err = NOS_E_INV_OBJ;
    }
    else if (block == NULL) {
        err = NOS_E_NULL;
    } else
#endif
    {
        nOS_EnterCritical(sr);
#if (NOS_CONFIG_SAFE > 0)
        if (queue->e.type != NOS_EVENT_QUEUE) {
            err = NOS_E_INV_OBJ;
        } else
#endif
        /* If count equal 0, there are chances some threads can wait to read from queue */
        if (queue->bcount == 0) {
            /* Check if thread waiting to read from queue */
            thread = nOS_SendEvent((nOS_Event*)queue, NOS_OK);
            if (thread != NULL) {
                /* Direct copy between thread's buffers */
                memcpy(thread->ext, block, queue->bsize);
#if (NOS_CONFIG_SCHED_PREEMPTIVE_ENABLE > 0)
                /* Verify if a highest prio thread is ready to run */
                nOS_Schedule();
#endif
                err = NOS_OK;
            }
            else if (queue->buffer != NULL) {
                /* No thread waiting to read from queue, then store it */
                _Write(queue, block);
                err = NOS_OK;
            }
            else {
                /* No thread waiting to consume message, inform producer */
                err = NOS_E_NO_CONSUMER;
            }
        }
        else if (queue->bcount < queue->bmax) {
            /* No chance a thread waiting to read from queue if count is higher than 0 */
            _Write(queue, block);
            err = NOS_OK;
        }
        else if (timeout == NOS_NO_WAIT) {
            err = NOS_E_FULL;
        }
        else {
            nOS_runningThread->ext = block;
            err = nOS_WaitForEvent((nOS_Event*)queue,
                                   NOS_THREAD_WRITING_QUEUE
#if (NOS_CONFIG_WAITING_TIMEOUT_ENABLE > 0)
                                  ,timeout
#elif (NOS_CONFIG_SLEEP_ENABLE > 0) || (NOS_CONFIG_SLEEP_UNTIL_ENABLE > 0)
                                  ,NOS_WAIT_INFINITE
#endif
                                  );
        }
        nOS_LeaveCritical(sr);
    }

    return err;
}
Beispiel #5
0
nOS_Error nOS_QueueWrite (nOS_Queue *queue, void *block, nOS_TickCounter timeout)
{
    nOS_Error       err;
    nOS_StatusReg   sr;
    nOS_Thread      *thread;

#if (NOS_CONFIG_SAFE > 0)
    if (queue == NULL) {
        err = NOS_E_INV_OBJ;
    } else if (block == NULL) {
        err = NOS_E_NULL;
    } else
#endif
    {
        nOS_EnterCritical(sr);
#if (NOS_CONFIG_SAFE > 0)
        if (queue->e.type != NOS_EVENT_QUEUE) {
            err = NOS_E_INV_OBJ;
        } else
#endif
        {
            /* If count equal 0, there are chances some threads can wait to read from queue */
            if (queue->bcount == 0) {
                /* Check if thread waiting to read from queue */
                thread = nOS_SendEvent((nOS_Event*)queue, NOS_OK);
                if (thread != NULL) {
                    /* Direct copy between thread's buffers */
                    memcpy(thread->ext, block, queue->bsize);
#if (NOS_CONFIG_HIGHEST_THREAD_PRIO > 0) && (NOS_CONFIG_SCHED_PREEMPTIVE_ENABLE > 0)
                    if ((thread->state == NOS_THREAD_READY) && (thread->prio > nOS_runningThread->prio)) {
                        nOS_Schedule();
                    }
#endif
                    err = NOS_OK;
                } else if (queue->buffer != NULL) {
                    /* No thread waiting to read from queue, then store it */
                    _Write(queue, block);
                    err = NOS_OK;
                } else {
                    /* No thread waiting to consume message, inform producer */
                    err = NOS_E_NO_CONSUMER;
                }
            } else if (queue->bcount < queue->bmax) {
                /* No chance a thread waiting to read from queue if count is higher than 0 */
                _Write(queue, block);
                err = NOS_OK;
            } else if (timeout == NOS_NO_WAIT) {
                err = NOS_E_FULL;
            } else if (nOS_isrNestingCounter > 0) {
                err = NOS_E_ISR;
            }
#if (NOS_CONFIG_SCHED_LOCK_ENABLE > 0)
            else if (nOS_lockNestingCounter > 0) {
                err = NOS_E_LOCKED;
            }
#endif
            else if (nOS_runningThread == &nOS_idleHandle) {
                /* Main threadv can't wait. */
                err = NOS_E_IDLE;
            } else {
                nOS_runningThread->ext = block;
                err = nOS_WaitForEvent((nOS_Event*)queue, NOS_THREAD_WRITING_QUEUE, timeout);
            }
        }
        nOS_LeaveCritical(sr);
    }

    return err;
}