/** * @brief Output queue write with timeout. * @details The function writes data from a buffer to an output queue. The * operation completes when the specified amount of data has been * transferred or after the specified timeout or if the queue has * been reset. * @note The function is not atomic, if you need atomicity it is suggested * to use a semaphore or a mutex for mutual exclusion. * @note The callback is invoked after writing each character into the * buffer. * * @param[in] oqp pointer to an @p output_queue_t structure * @param[out] bp pointer to the data buffer * @param[in] n the maximum amount of data to be transferred, the * value 0 is reserved * @param[in] time the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The number of bytes effectively transferred. * * @api */ size_t chOQWriteTimeout(output_queue_t *oqp, const uint8_t *bp, size_t n, systime_t time) { qnotify_t nfy = oqp->q_notify; size_t w = 0; chDbgCheck(n > 0); chSysLock(); while (true) { while (chOQIsFullI(oqp)) { if (chThdEnqueueTimeoutS(&oqp->q_waiting, time) != Q_OK) { chSysUnlock(); return w; } } oqp->q_counter--; *oqp->q_wrptr++ = *bp++; if (oqp->q_wrptr >= oqp->q_top) oqp->q_wrptr = oqp->q_buffer; if (nfy) nfy(oqp); chSysUnlock(); /* Gives a preemption chance in a controlled point.*/ w++; if (--n == 0) return w; chSysLock(); } }
/** * @brief Input queue read with timeout. * @details The function reads data from an input queue into a buffer. The * operation completes when the specified amount of data has been * transferred or after the specified timeout or if the queue has * been reset. * @note The function is not atomic, if you need atomicity it is suggested * to use a semaphore or a mutex for mutual exclusion. * @note The callback is invoked before reading each character from the * buffer or before entering the state @p CH_STATE_WTQUEUE. * * @param[in] iqp pointer to an @p input_queue_t structure * @param[out] bp pointer to the data buffer * @param[in] n the maximum amount of data to be transferred, the * value 0 is reserved * @param[in] time the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The number of bytes effectively transferred. * * @api */ size_t chIQReadTimeout(input_queue_t *iqp, uint8_t *bp, size_t n, systime_t time) { qnotify_t nfy = iqp->q_notify; size_t r = 0; chDbgCheck(n > 0); chSysLock(); while (true) { if (nfy) nfy(iqp); while (chIQIsEmptyI(iqp)) { if (chThdEnqueueTimeoutS(&iqp->q_waiting, time) != Q_OK) { chSysUnlock(); return r; } } iqp->q_counter--; *bp++ = *iqp->q_rdptr++; if (iqp->q_rdptr >= iqp->q_top) iqp->q_rdptr = iqp->q_buffer; chSysUnlock(); /* Gives a preemption chance in a controlled point.*/ r++; if (--n == 0) return r; chSysLock(); } }
/** * @brief Input queue read with timeout. * @details This function reads a byte value from an input queue. If the queue * is empty then the calling thread is suspended until a byte arrives * in the queue or a timeout occurs. * @note The callback is invoked before reading the character from the * buffer or before entering the state @p CH_STATE_WTQUEUE. * * @param[in] iqp pointer to an @p input_queue_t structure * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return A byte value from the queue. * @retval Q_TIMEOUT if the specified time expired. * @retval Q_RESET if the queue has been reset. * * @api */ msg_t chIQGetTimeout(input_queue_t *iqp, systime_t timeout) { uint8_t b; chSysLock(); if (iqp->notify != NULL) { iqp->notify(iqp); } while (chIQIsEmptyI(iqp)) { msg_t msg = chThdEnqueueTimeoutS(&iqp->waiting, timeout); if (msg < Q_OK) { chSysUnlock(); return msg; } } iqp->counter--; b = *iqp->rdptr++; if (iqp->rdptr >= iqp->top) { iqp->rdptr = iqp->buffer; } chSysUnlock(); return (msg_t)b; }
/** * @brief Retrieves a message from a mailbox. * @details The invoking thread waits until a message is posted in the mailbox * or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[out] msgp pointer to a message variable for the received message * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly fetched. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @sclass */ msg_t chMBFetchTimeoutS(mailbox_t *mbp, msg_t *msgp, sysinterval_t timeout) { msg_t rdymsg; chDbgCheckClassS(); chDbgCheck((mbp != NULL) && (msgp != NULL)); do { /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a message in queue? if so then fetch.*/ if (chMBGetUsedCountI(mbp) > (size_t)0) { *msgp = *mbp->rdptr++; if (mbp->rdptr >= mbp->top) { mbp->rdptr = mbp->buffer; } mbp->cnt--; /* If there is a writer waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qw, MSG_OK); chSchRescheduleS(); return MSG_OK; } /* No message in the queue, waiting for a message to become available.*/ rdymsg = chThdEnqueueTimeoutS(&mbp->qr, timeout); } while (rdymsg == MSG_OK); return rdymsg; }
/** * @brief Posts an high priority message into a mailbox. * @details The invoking thread waits until a empty slot in the mailbox becomes * available or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly posted. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @sclass */ msg_t chMBPostAheadTimeoutS(mailbox_t *mbp, msg_t msg, sysinterval_t timeout) { msg_t rdymsg; chDbgCheckClassS(); chDbgCheck(mbp != NULL); do { /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a free message slot in queue? if so then post.*/ if (chMBGetFreeCountI(mbp) > (size_t)0) { if (--mbp->rdptr < mbp->buffer) { mbp->rdptr = mbp->top - 1; } *mbp->rdptr = msg; mbp->cnt++; /* If there is a reader waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qr, MSG_OK); chSchRescheduleS(); return MSG_OK; } /* No space in the queue, waiting for a slot to become available.*/ rdymsg = chThdEnqueueTimeoutS(&mbp->qw, timeout); } while (rdymsg == MSG_OK); return rdymsg; }
/** * @brief Output queue write with timeout. * @details This function writes a byte value to an output queue. If the queue * is full then the calling thread is suspended until there is space * in the queue or a timeout occurs. * @note The callback is invoked after writing the character into the * buffer. * * @param[in] oqp pointer to an @p output_queue_t structure * @param[in] b the byte value to be written in the queue * @param[in] time the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval Q_OK if the operation succeeded. * @retval Q_TIMEOUT if the specified time expired. * @retval Q_RESET if the queue has been reset. * * @api */ msg_t chOQPutTimeout(output_queue_t *oqp, uint8_t b, systime_t time) { chSysLock(); while (chOQIsFullI(oqp)) { msg_t msg; if ((msg = chThdEnqueueTimeoutS(&oqp->q_waiting, time)) < Q_OK) { chSysUnlock(); return msg; } } oqp->q_counter--; *oqp->q_wrptr++ = b; if (oqp->q_wrptr >= oqp->q_top) oqp->q_wrptr = oqp->q_buffer; if (oqp->q_notify) oqp->q_notify(oqp); chSysUnlock(); return Q_OK; }
/** * @brief Input queue read with timeout. * @details This function reads a byte value from an input queue. If the queue * is empty then the calling thread is suspended until a byte arrives * in the queue or a timeout occurs. * @note The callback is invoked before reading the character from the * buffer or before entering the state @p CH_STATE_WTQUEUE. * * @param[in] iqp pointer to an @p input_queue_t structure * @param[in] time the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return A byte value from the queue. * @retval Q_TIMEOUT if the specified time expired. * @retval Q_RESET if the queue has been reset. * * @api */ msg_t chIQGetTimeout(input_queue_t *iqp, systime_t time) { uint8_t b; chSysLock(); if (iqp->q_notify) iqp->q_notify(iqp); while (chIQIsEmptyI(iqp)) { msg_t msg; if ((msg = chThdEnqueueTimeoutS(&iqp->q_waiting, time)) < Q_OK) { chSysUnlock(); return msg; } } iqp->q_counter--; b = *iqp->q_rdptr++; if (iqp->q_rdptr >= iqp->q_top) iqp->q_rdptr = iqp->q_buffer; chSysUnlock(); return b; }
/** * @brief Output queue write with timeout. * @details This function writes a byte value to an output queue. If the queue * is full then the calling thread is suspended until there is space * in the queue or a timeout occurs. * @note The callback is invoked after writing the character into the * buffer. * * @param[in] oqp pointer to an @p output_queue_t structure * @param[in] b the byte value to be written in the queue * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval Q_OK if the operation succeeded. * @retval Q_TIMEOUT if the specified time expired. * @retval Q_RESET if the queue has been reset. * * @api */ msg_t chOQPutTimeout(output_queue_t *oqp, uint8_t b, systime_t timeout) { chSysLock(); while (chOQIsFullI(oqp)) { msg_t msg = chThdEnqueueTimeoutS(&oqp->waiting, timeout); if (msg < Q_OK) { chSysUnlock(); return msg; } } oqp->counter--; *oqp->wrptr++ = b; if (oqp->wrptr >= oqp->top) { oqp->wrptr = oqp->buffer; } if (oqp->notify != NULL) { oqp->notify(oqp); } chSysUnlock(); return Q_OK; }