Thread synchronization support. More...
Defines | |
#define | SIGNALED ((void *)-1) |
Signaled state definition. | |
#define | NUT_WAIT_INFINITE 0 |
Infinite waiting time definition. | |
#define | NutEventPostFromIrq(qp) |
Post an event to a specified queue from interrupt context. | |
Functions | |
void | NutEventTimeout (HANDLE timer, void *arg) |
Timer callback in case of event timeout. | |
int | NutEventWait (volatile HANDLE *qhp, uint32_t ms) |
Wait for an event in a specified queue. | |
int | NutEventWaitNext (volatile HANDLE *qhp, uint32_t ms) |
Wait for a new event in a specified queue. | |
int | NutEventPostAsync (volatile HANDLE *qhp) |
Asynchronously post an event to a specified queue. | |
int | NutEventPost (volatile HANDLE *qhp) |
Post an event to a specified queue. | |
int | NutEventBroadcastAsync (volatile HANDLE *qhp) |
Asynchronously broadcast an event to a specified queue. | |
int | NutEventBroadcast (volatile HANDLE *qhp) |
Broadcast an event to a specified queue. |
Thread synchronization support.
Threads may wait for events from other threads or interrupts or may post or broadcast events to other threads.
Waiting threads line up in priority ordered queues, so more than one thread may wait for the same event. A waiting queue is a simple linked list of waiting threads.
Events are posted to a waiting queue, moving the thread from waiting (sleeping) state to ready-to-run state. A running thread may also broadcast an event to a specified queue, waking up all threads on that queue.
Usually a woken up thread takes over the CPU, if it's priority is equal or higher than the currently running thread. However, events can be posted asynchronously, in which case the posting thread continues to run.
Interrupt can also post events, but have to use the specific function NutEventPostFromIrq().
#define SIGNALED ((void *)-1) |
Signaled state definition.
The root of an event queue is set to this value if an event is posted to an empty queue. As this may happen during interrupts, the root of an event queue must be considered volatile.
Timer handles in the THREADINFO structure are set to this value if a timeout occured while waiting for an event.
Referenced by NutDumpThreadList(), NutDumpThreadQueue(), NutEventBroadcastAsync(), NutEventPostAsync(), NutEventTimeout(), NutEventWait(), NutEventWaitNext(), NutSemDestroy(), NutThreadAddPriQueue(), NutThreadRemoveQueue(), NutThreadResume(), NutTwiMasterTranceive(), NutTwiSlaveListen(), and NutTwiSlaveRespond().
#define NUT_WAIT_INFINITE 0 |
Infinite waiting time definition.
Applications should use this value to disable timeout monitoring while waiting for an event.
Referenced by At45dNodeLock(), At45dNodeStatus(), At91SpiBus0Deselect(), At91SpiBus0Transfer(), AtCanOutput(), Avr32SpiBus0Deselect(), Avr32SpiBus0Transfer(), Avr32SpiBus1Deselect(), Avr32SpiBus1Transfer(), AvrSpiBus0Deselect(), AvrSpiBus0Transfer(), CAN_Tx(), CanOutput(), Cy2239xGetDivider(), Cy2239xGetFreq(), Cy2239xGetPll(), Cy2239xPllEnable(), Cy2239xPllGetFreq(), Cy2239xPllSetFreq(), Cy2239xSetDivider(), Cy2239xSetPll(), DS1307RamWrite(), DS1307RtcReadRegs(), DS1307RtcWrite(), FeederThread(), NutConditionWait(), NutDhcpClient(), NutMutexLock(), NutRegisterCanBus(), PcfRtcReadRegs(), PcfRtcWrite(), PerCiWrite(), Sc16is752UsartInterruptProcessing(), SSDPTask(), Stm32Usart3BusDeselect(), sys_key(), sys_led(), X12EepromRead(), X12EepromWrite(), X12RtcReadRegs(), and X12RtcWrite().
#define NutEventPostFromIrq | ( | qp | ) |
{ \ if (*qp == 0) { \ *qp = SIGNALED; \ } \ else if (*qp != SIGNALED) { \ NUTTHREADINFO *tp = (NUTTHREADINFO *)(*qp); \ tp->td_qpec++; \ } \ }
Post an event to a specified queue from interrupt context.
Wake up the thread with the highest priority waiting on the specified queue. This function is explicitly provided for IRQ handlers to wakeup waiting user threads.
Internally a counter is used to keep track of the posted events. This counter will be examined when the currently running thread is ready to release the CPU.
qp | Identifies the queue an event is posted to. |
Referenced by At91SpiBus0Interrupt(), Avr32SpiBus0Interrupt(), Avr32SpiBus1Interrupt(), DMA_Handler_IRQ(), EP3_OUT_Callback(), NutUnixThreadYieldHook(), SIG_INTERRUPT4(), SscSpiBus0Interrupt(), and SscSpiBus0PdcIrq().
void NutEventTimeout | ( | HANDLE | timer, |
void * | arg | ||
) |
Timer callback in case of event timeout.
Applications should not call this function. It is provided as a global to enable debugging code inspecting the callbacks in the timer list.
timer | Handle of the elapsed timeout timer. |
arg | Handle of an event queue. |
References NULL, NUTASSERT, NutEnterCritical, NutExitCritical, NutThreadAddPriQueue(), runQueue, SIGNALED, _NUTTHREADINFO::td_qnxt, _NUTTHREADINFO::td_qpec, _NUTTHREADINFO::td_state, _NUTTHREADINFO::td_timer, and TDS_READY.
Referenced by NutDumpTimerList(), and NutEventWait().
Wait for an event in a specified queue.
Give up the CPU until another thread or an interrupt routine posts an event to this queue or until a time-out occurs, whichever comes first.
If previously an event had been posted to this queue without any thread waiting, then the thread will not wait for a new event, but may still pass CPU control, if another thread with equal or higher priority is ready to run.
qhp | Identifies the queue to wait on. |
ms | Maximum wait time in milliseconds. To disable timeout, set this parameter to NUT_WAIT_INFINITE. |
References NULL, NUTASSERT, NutEnterCritical, NutEventTimeout(), NutExitCritical, NutThreadAddPriQueue(), NutThreadRemoveQueue(), NutThreadResume(), NutThreadYield(), NutTimerStart(), runningThread, runQueue, SIGNALED, _NUTTHREADINFO::td_state, _NUTTHREADINFO::td_timer, TDS_SLEEP, TM_ONESHOT, TRACE_ADD_ITEM, and TRACE_TAG_THREAD_WAIT.
Referenced by AhdlcAt91Read(), AhdlcAvrRead(), AhdlcRx(), At24cRead(), At24cWrite(), At45dNodeLock(), At91SpiBus0Select(), At91SpiBus0Transfer(), At91SpiTransfer2(), AtCanInput(), AtCanOutput(), Avr32SpiBus0Select(), Avr32SpiBus0Transfer(), Avr32SpiBus1Select(), Avr32SpiBus1Transfer(), Avr32SpiBusWait(), AvrSpiBus0Select(), AvrSpiBus0Transfer(), CAN_Tx(), CanInput(), CanOutput(), DmOutput(), EmacOutput(), EmacRxThread(), FATLock(), FeederThread(), GpioSpiBus0Select(), High(), IDELock(), LancOutput(), Low(), Lpc17xxEmacOutput(), Lpc17xxEmacRxThread(), main(), MmCardIOCtl(), NicRx(), NicRxAsix(), NicRxLanc(), NplSpiBusSelect(), NutArpCacheQuery(), NutConditionTimedWait(), NutConditionWait(), NutDhcpClient(), NutEventWaitNext(), NutIrGet(), NutMsgQGetMessage(), NutNetIfConfig2(), NutTcpReceive(), NutTcpSend(), NutTcpSm(), NutTcpStateActiveOpenEvent(), NutTcpStatePassiveOpenEvent(), NutTwiMasterRegRead(), NutTwiMasterRegWrite(), NutTwiMasterTranceive(), NutTwiSlaveListen(), NutTwiSlaveRespond(), NutUdpReceiveFrom(), OTGTimerEvent(), PerCiWrite(), PhatSectorLoad(), PhatSectorWrite(), RxThread(), Sc16is752UsartInterruptProcessing(), ShtCommand(), SJAInput(), Stm32Usart3BusSelect(), sys_key(), sys_led(), TimerEvent1(), TimerEvent2(), TimerEvent3(), TimerEvent4(), Tlv320DacFlush(), Tlv320DacWrite(), UartAvrFlush(), UartAvrInput(), UsartRead(), VsCodecRead(), VsCodecWaitReady(), and VsCodecWrite().
Wait for a new event in a specified queue.
Give up the CPU until another thread or an interrupt routine posts an event to this queue or until a time-out occurs, whichever comes first.
This call is similar to NutEventWait(), but will ignore the SIGNALED state of the queue. This way, previously posted events to an empty queue are not considered.
qhp | Identifies the queue to wait on. |
ms | Maximum wait time in milliseconds. To disable timeout, set this parameter to NUT_WAIT_INFINITE. |
References NULL, NUTASSERT, NutEnterCritical, NutEventWait(), NutExitCritical, and SIGNALED.
Referenced by AceFlush(), AceInput(), CFChange(), and NutMutexLock().
int NutEventPostAsync | ( | volatile HANDLE * | qhp | ) |
Asynchronously post an event to a specified queue.
Wake up the thread with the highest priority waiting on the specified queue. But even if the priority of the woken thread is higher than the current thread's priority, the current one continues running.
If no thread is waiting, then the queue will be set to the SIGNALED state.
qhp | Identifies the queue an event is posted to. |
References NULL, NUTASSERT, NutEnterCritical, NutExitCritical, NutThreadAddPriQueue(), NutTimerStop(), runQueue, SIGNALED, _NUTTHREADINFO::td_qnxt, _NUTTHREADINFO::td_qpec, _NUTTHREADINFO::td_state, _NUTTHREADINFO::td_timer, and TDS_READY.
Referenced by AceIOCtl(), KeyTimerCb(), NutEventBroadcastAsync(), NutEventPost(), NutMsgQGetMessage(), NutMsgQPost(), NutThreadResume(), OTGTimerCallback(), SJAOutput(), and TimerCallback().
int NutEventPost | ( | volatile HANDLE * | qhp | ) |
Post an event to a specified queue.
Wake up the thread with the highest priority waiting on this queue. If the priority of the waiting thread is higher or equal than the current thread's priority, then the current thread is stopped and CPU control is passed to the waiting thread.
If no thread is waiting, the queue will be set to the signaled state.
qhp | Identifies the queue an event is posted to. |
References NutEventPostAsync(), NutThreadYield(), and rc.
Referenced by AhdlcAt91IOCtl(), AhdlcAvrIOCtl(), At24cRead(), At24cWrite(), At45dNodeUnlock(), At91SpiBus0Deselect(), At91SpiBus0Select(), Avr32SpiBus0Deselect(), Avr32SpiBus0Select(), Avr32SpiBus1Deselect(), Avr32SpiBus1Select(), AvrSpiBus0Deselect(), AvrSpiBus0Select(), DmOutput(), EEInit(), EmacOutput(), EmacRxThread(), FATFree(), FATSemaInit(), FeederThread(), GpioSpiBus0Deselect(), GpioSpiBus0Select(), High(), IDEFree(), IDESemaInit(), IpcpClose(), IpcpLowerDown(), IpcpRxConfAck(), IpcpRxConfReq(), LancOutput(), Low(), Lpc17xxEmacOutput(), Lpc17xxEmacRxThread(), main(), MmCardIOCtl(), NicRxLanc(), NplSpiBusDeselect(), NplSpiBusSelect(), NutConditionSignal(), NutMutexUnlock(), NutRegisterSpiDevice(), NutRegisterTwiBus(), NutSemPost(), NutTcpStateMachine(), NutTwiMasterRegRead(), NutTwiMasterRegWrite(), NutTwiMasterTranceive(), NutUdpInput(), NutUdpSetSocketError(), PerCiOpen(), PerCiWrite(), PhatSectorLoad(), PhatSectorWrite(), PhatVolMount(), RawFsMount(), ShtCommand(), ShtInit(), Stm32Usart3BusDeselect(), Stm32Usart3BusSelect(), sys_key(), UFlashAttach(), UFlashFormat(), VsCodecIOCtl(), VsCodecRead(), VsCodecWrite(), and wlandrv_Attach().
int NutEventBroadcastAsync | ( | volatile HANDLE * | qhp | ) |
Asynchronously broadcast an event to a specified queue.
Wake up all threads waiting on this queue. But even if the priority of any woken thread is higher than the current thread's priority, the current one continues running.
In opposite to NutEventPostAsync(), the queue will be cleared in any case, even if it is in signaled state. Applications may use this call to make sure, that a queue is cleared before initiating some event triggering action.
qhp | Identifies the queue an event is broadcasted to. |
References NULL, NUTASSERT, NutEnterCritical, NutEventPostAsync(), NutExitCritical, rc, and SIGNALED.
Referenced by NutEventBroadcast().
int NutEventBroadcast | ( | volatile HANDLE * | qhp | ) |
Broadcast an event to a specified queue.
Wake up all threads waiting on this queue. If the priority of any waiting thread is higher or equal than the current thread's priority, then the current thread is stopped and CPU control is passed to the woken up thread with the highest priority.
In opposite to NutEventPost(), the queue will be cleared in any case, even if it is in signaled state. Applications may use this call to make sure, that a queue is cleared before initiating some event triggering action.
qhp | Identifies the queue an event is broadcasted to. |
References NutEventBroadcastAsync(), NutThreadYield(), and rc.
Referenced by NutArpCacheUpdate(), NutConditionBroadcast(), NutDhcpClient(), NutNetIfConfig2(), NutTcpAbortSocket(), and UsartClose().