diff options
author | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2008-07-22 09:55:51 +0000 |
---|---|---|
committer | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2008-07-22 09:55:51 +0000 |
commit | 49c3629538ee20a1df4ee5bbe664464e4904db5d (patch) | |
tree | 5c3379181ca4b45af5c1e8b2dde0e596e2220fcf /src/include | |
parent | 71a1820c4a0be292590ed63c284539a68cd22fbb (diff) | |
download | ChibiOS-49c3629538ee20a1df4ee5bbe664464e4904db5d.tar.gz ChibiOS-49c3629538ee20a1df4ee5bbe664464e4904db5d.tar.bz2 ChibiOS-49c3629538ee20a1df4ee5bbe664464e4904db5d.zip |
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@346 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/queues.h | 368 | ||||
-rw-r--r-- | src/include/scheduler.h | 162 | ||||
-rw-r--r-- | src/include/threads.h | 483 |
3 files changed, 508 insertions, 505 deletions
diff --git a/src/include/queues.h b/src/include/queues.h index 5c480e8cf..0a373b5ea 100644 --- a/src/include/queues.h +++ b/src/include/queues.h @@ -1,184 +1,184 @@ -/*
- ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
-
- This file is part of ChibiOS/RT.
-
- ChibiOS/RT is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
-
- ChibiOS/RT is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-/**
- * @addtogroup IOQueues
- * @{
- */
-
-#ifndef _QUEUES_H_
-#define _QUEUES_H_
-
-/** Queue notification callback type.*/
-typedef void (*qnotify_t)(void);
-
-/** Returned by the queue functions if the operation is successful.*/
-#define Q_OK RDY_OK
-/** Returned by the queue functions if a timeout occurs.*/
-#define Q_TIMEOUT RDY_TIMEOUT
-/** Returned by the queue functions if the queue is reset.*/
-#define Q_RESET RDY_RESET
-/** Returned by the queue functions if the queue is empty.*/
-#define Q_EMPTY -3
-/** Returned by the queue functions if the queue is full.*/
-#define Q_FULL -4
-
-#ifdef CH_USE_QUEUES
-/**
- * I/O queue structure, it is used by both Input and Output Queues,
- * the difference is on how the semaphore is initialized.
- */
-typedef struct {
- /** Pointer to the queue buffer.*/
- uint8_t *q_buffer;
- /** Pointer to the first location after the buffer.*/
- uint8_t *q_top;
- /** Write pointer.*/
- uint8_t *q_wrptr;
- /** Read pointer.*/
- uint8_t *q_rdptr;
- /** Counter semaphore.*/
- Semaphore q_sem;
- /** Data notification callback.*/
- qnotify_t q_notify;
-} Queue;
-
-/** Returns the queue's buffer size.*/
-#define chQSize(q) \
- ((q)->q_top - (q)->q_buffer)
-
-/** Returns the used space if used on an Input Queue and the empty space if
- * used on an Output Queue.*/
-#define chQSpace(q) \
- ((q)->q_sem.s_cnt)
-
-/** Evaluates to TRUE if the specified Input Queue is empty.*/
-#define chIQIsEmpty(q) \
- (chQSpace(q) <= 0)
-
-/** Evaluates to TRUE if the specified Input Queue is full.*/
-#define chIQIsFull(q) \
- (chQSpace(q) >= chQSize(q))
-
-/** Evaluates to TRUE if the specified Output Queue is empty.*/
-#define chOQIsEmpty(q) \
- (chQSpace(q) >= chQSize(q))
-
-/** Evaluates to TRUE if the specified Output Queue is full.*/
-#define chOQIsFull(q) \
- (chQSpace(q) <= 0)
-
-#ifdef __cplusplus
-extern "C" {
-#endif
- /*
- * Input Queues functions. An Input Queue is usually written into by an
- * interrupt handler and read from a thread.
- */
- void chIQInit(Queue *qp, uint8_t *buffer, size_t size, qnotify_t inotify);
- void chIQReset(Queue *qp);
- msg_t chIQPutI(Queue *qp, uint8_t b);
- msg_t chIQGet(Queue *qp);
- size_t chIQRead(Queue *qp, uint8_t *buffer, size_t n);
-#ifdef CH_USE_QUEUES_TIMEOUT
- msg_t chIQGetTimeout(Queue *qp, systime_t time);
-#endif
-
- /*
- * Output Queues functions. An Output Queue is usually written into by a
- * thread and read from an interrupt handler.
- */
- void chOQInit(Queue *queue, uint8_t *buffer, size_t size, qnotify_t onotify);
- void chOQReset(Queue *queue);
- void chOQPut(Queue *queue, uint8_t b);
- msg_t chOQGetI(Queue *queue);
- size_t chOQWrite(Queue *queue, uint8_t *buffer, size_t n);
-#ifdef __cplusplus
-}
-#endif
-#endif /* CH_USE_QUEUES */
-
-#ifdef CH_USE_QUEUES_HALFDUPLEX
-/**
- * Half duplex queue structure.
- */
-typedef struct {
- /** Pointer to the queue buffer.*/
- uint8_t *hdq_buffer;
- /** Pointer to the first location after the buffer.*/
- uint8_t *hdq_top;
- /** Write pointer.*/
- uint8_t *hdq_wrptr;
- /** Read pointer.*/
- uint8_t *hdq_rdptr;
- /** Input counter semaphore.*/
- Semaphore hdq_isem;
- /** Output counter semaphore.*/
- Semaphore hdq_osem;
- /** Input data notification callback.*/
- qnotify_t hdq_inotify;
- /** Output data notification callback.*/
- qnotify_t hdq_onotify;
-} HalfDuplexQueue;
-
-/** Returns the queue's buffer size.*/
-#define chHDQSize(q) \
- ((q)->hdq_top - (q)->hdq_buffer)
-
-/** Returns the queue space when in transmission mode.*/
-#define chHDQEmptySpace(q) \
- ((q)->hdq_osem.s_cnt)
-
-/** Returns the number of the bytes in the queue when in receive mode.*/
-#define chHDQFilledSpace(q) \
- ((q)->hdq_isem.s_cnt)
-
-/** Evaluates to TRUE if the queue is in transmit mode.*/
-#define chHDQIsTransmitting(q) \
- (chHDQEmptySpace(q) < chHDQSize(q))
-
-/** Evaluates to TRUE if the queue is in receive mode.*/
-#define chHDQIsReceiving(q) \
- (chHDQEmptySpaceQ(q) >= chHDQSize(q))
-
-/** Evaluates to TRUE if the receive queue is full.*/
-#define chHDQIsFullReceive(q) \
- (chHDQFilledSpace(q) >= chHDQSize(q))
-
-#ifdef __cplusplus
-extern "C" {
-#endif
- void chHDQInit(HalfDuplexQueue *qp, uint8_t *buffer, size_t size,
- qnotify_t inotify, qnotify_t onotify);
- msg_t chHDQGetReceive(HalfDuplexQueue *qp);
- void chHDQPutTransmit(HalfDuplexQueue *qp, uint8_t b);
- msg_t chHDQGetTransmitI(HalfDuplexQueue *qp);
- msg_t chHDQPutReceiveI(HalfDuplexQueue *qp, uint8_t b);
-#ifdef CH_USE_QUEUES_TIMEOUT
- msg_t chHDQGetReceiveTimeout(HalfDuplexQueue *qp, systime_t time);
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* CH_USE_QUEUES_HALFDUPLEX */
-
-#endif /* _QUEUES_H_ */
-
-/** @} */
+/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +/** + * @addtogroup IOQueues + * @{ + */ + +#ifndef _QUEUES_H_ +#define _QUEUES_H_ + +/** Queue notification callback type. */ +typedef void (*qnotify_t)(void); + +/** Returned by the queue functions if the operation is successful. */ +#define Q_OK RDY_OK +/** Returned by the queue functions if a timeout occurs. */ +#define Q_TIMEOUT RDY_TIMEOUT +/** Returned by the queue functions if the queue is reset. */ +#define Q_RESET RDY_RESET +/** Returned by the queue functions if the queue is empty. */ +#define Q_EMPTY -3 +/** Returned by the queue functions if the queue is full. */ +#define Q_FULL -4 + +#ifdef CH_USE_QUEUES +/** + * I/O queue structure, it is used by both Input and Output Queues, + * the difference is on how the semaphore is initialized. + */ +typedef struct { + /** Pointer to the queue buffer. */ + uint8_t *q_buffer; + /** Pointer to the first location after the buffer. */ + uint8_t *q_top; + /** Write pointer. */ + uint8_t *q_wrptr; + /** Read pointer. */ + uint8_t *q_rdptr; + /** Counter semaphore. */ + Semaphore q_sem; + /** Data notification callback. */ + qnotify_t q_notify; +} Queue; + +/** Returns the queue's buffer size. */ +#define chQSize(q) \ + ((q)->q_top - (q)->q_buffer) + +/** Returns the used space if used on an Input Queue and the empty space if + * used on an Output Queue. */ +#define chQSpace(q) \ + ((q)->q_sem.s_cnt) + +/** Evaluates to TRUE if the specified Input Queue is empty. */ +#define chIQIsEmpty(q) \ + (chQSpace(q) <= 0) + +/** Evaluates to TRUE if the specified Input Queue is full. */ +#define chIQIsFull(q) \ + (chQSpace(q) >= chQSize(q)) + +/** Evaluates to TRUE if the specified Output Queue is empty. */ +#define chOQIsEmpty(q) \ + (chQSpace(q) >= chQSize(q)) + +/** Evaluates to TRUE if the specified Output Queue is full. */ +#define chOQIsFull(q) \ + (chQSpace(q) <= 0) + +#ifdef __cplusplus +extern "C" { +#endif + /* + * Input Queues functions. An Input Queue is usually written into by an + * interrupt handler and read from a thread. + */ + void chIQInit(Queue *qp, uint8_t *buffer, size_t size, qnotify_t inotify); + void chIQReset(Queue *qp); + msg_t chIQPutI(Queue *qp, uint8_t b); + msg_t chIQGet(Queue *qp); + size_t chIQRead(Queue *qp, uint8_t *buffer, size_t n); +#ifdef CH_USE_QUEUES_TIMEOUT + msg_t chIQGetTimeout(Queue *qp, systime_t time); +#endif + + /* + * Output Queues functions. An Output Queue is usually written into by a + * thread and read from an interrupt handler. + */ + void chOQInit(Queue *queue, uint8_t *buffer, size_t size, qnotify_t onotify); + void chOQReset(Queue *queue); + void chOQPut(Queue *queue, uint8_t b); + msg_t chOQGetI(Queue *queue); + size_t chOQWrite(Queue *queue, uint8_t *buffer, size_t n); +#ifdef __cplusplus +} +#endif +#endif /* CH_USE_QUEUES */ + +#ifdef CH_USE_QUEUES_HALFDUPLEX +/** + * Half duplex queue structure. + */ +typedef struct { + /** Pointer to the queue buffer. */ + uint8_t *hdq_buffer; + /** Pointer to the first location after the buffer. */ + uint8_t *hdq_top; + /** Write pointer.*/ + uint8_t *hdq_wrptr; + /** Read pointer.*/ + uint8_t *hdq_rdptr; + /** Input counter semaphore. */ + Semaphore hdq_isem; + /** Output counter semaphore. */ + Semaphore hdq_osem; + /** Input data notification callback. */ + qnotify_t hdq_inotify; + /** Output data notification callback. */ + qnotify_t hdq_onotify; +} HalfDuplexQueue; + +/** Returns the queue's buffer size. */ +#define chHDQSize(q) \ + ((q)->hdq_top - (q)->hdq_buffer) + +/** Returns the queue space when in transmission mode. */ +#define chHDQEmptySpace(q) \ + ((q)->hdq_osem.s_cnt) + +/** Returns the number of the bytes in the queue when in receive mode. */ +#define chHDQFilledSpace(q) \ + ((q)->hdq_isem.s_cnt) + +/** Evaluates to TRUE if the queue is in transmit mode. */ +#define chHDQIsTransmitting(q) \ + (chHDQEmptySpace(q) < chHDQSize(q)) + +/** Evaluates to TRUE if the queue is in receive mode. */ +#define chHDQIsReceiving(q) \ + (chHDQEmptySpaceQ(q) >= chHDQSize(q)) + +/** Evaluates to TRUE if the receive queue is full. */ +#define chHDQIsFullReceive(q) \ + (chHDQFilledSpace(q) >= chHDQSize(q)) + +#ifdef __cplusplus +extern "C" { +#endif + void chHDQInit(HalfDuplexQueue *qp, uint8_t *buffer, size_t size, + qnotify_t inotify, qnotify_t onotify); + msg_t chHDQGetReceive(HalfDuplexQueue *qp); + void chHDQPutTransmit(HalfDuplexQueue *qp, uint8_t b); + msg_t chHDQGetTransmitI(HalfDuplexQueue *qp); + msg_t chHDQPutReceiveI(HalfDuplexQueue *qp, uint8_t b); +#ifdef CH_USE_QUEUES_TIMEOUT + msg_t chHDQGetReceiveTimeout(HalfDuplexQueue *qp, systime_t time); +#endif +#ifdef __cplusplus +} +#endif + +#endif /* CH_USE_QUEUES_HALFDUPLEX */ + +#endif /* _QUEUES_H_ */ + +/** @} */ diff --git a/src/include/scheduler.h b/src/include/scheduler.h index 0235a7c97..3a6486efe 100644 --- a/src/include/scheduler.h +++ b/src/include/scheduler.h @@ -1,80 +1,82 @@ -/*
- ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
-
- This file is part of ChibiOS/RT.
-
- ChibiOS/RT is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
-
- ChibiOS/RT is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-/**
- * @addtogroup Scheduler
- * @{
- */
-
-#ifndef _SCHEDULER_H_
-#define _SCHEDULER_H_
-
-/** Normal \p chSchReadyI() message.*/
-#define RDY_OK 0
-/** Returned if the thread was made ready because a timeout.*/
-#define RDY_TIMEOUT -1
-/** Returned if the thread was made ready because a reset.*/
-#define RDY_RESET -2
-
-#define firstprio(rlp) ((rlp)->p_next->p_prio)
-
-/**
- * Ready list header.
- */
-typedef struct {
- ThreadsQueue r_queue;
- tprio_t r_prio;
- cnt_t r_preempt;
-#ifndef CH_CURRP_REGISTER_CACHE
- Thread *r_current;
-#endif
-#ifdef CH_USE_SYSTEMTIME
- volatile systime_t r_stime;
-#endif
-} ReadyList;
-
-extern ReadyList rlist;
-
-/*
- * Scheduler APIs.
- */
-#ifdef __cplusplus
-extern "C" {
-#endif
- void chSchInit(void);
- Thread *chSchReadyI(Thread *tp);
- void chSchGoSleepS(tstate_t newstate);
- msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time);
- void chSchWakeupS(Thread *tp, msg_t msg);
- void chSchDoRescheduleI(void);
- void chSchRescheduleS(void);
- bool_t chSchRescRequiredI(void);
-#ifdef __cplusplus
-}
-#endif
-
-#ifdef CH_CURRP_REGISTER_CACHE
-register Thread *currp asm(CH_CURRP_REGISTER_CACHE);
-#else
-#define currp rlist.r_current
-#endif
-
-#endif /* _SCHEDULER_H_ */
-
-/** @} */
+/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +/** + * @addtogroup Scheduler + * @{ + */ + +#ifndef _SCHEDULER_H_ +#define _SCHEDULER_H_ + +/** Normal \p chSchReadyI() message. */ +#define RDY_OK 0 +/** Returned when the thread was made ready because of a timeout. */ +#define RDY_TIMEOUT -1 +/** Returned when the thread was made ready because of a reset. */ +#define RDY_RESET -2 + +/** The priority of the first thread on the given ready list. */ +#define firstprio(rlp) ((rlp)->p_next->p_prio) + +/** + * Ready list header. + */ +typedef struct { + ThreadsQueue r_queue; + tprio_t r_prio; + cnt_t r_preempt; +#ifndef CH_CURRP_REGISTER_CACHE + /** the currently running thread */ + Thread *r_current; +#endif +#ifdef CH_USE_SYSTEMTIME + volatile systime_t r_stime; +#endif +} ReadyList; + +extern ReadyList rlist; + +/* + * Scheduler APIs. + */ +#ifdef __cplusplus +extern "C" { +#endif + void chSchInit(void); + Thread *chSchReadyI(Thread *tp); + void chSchGoSleepS(tstate_t newstate); + msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t time); + void chSchWakeupS(Thread *tp, msg_t msg); + void chSchDoRescheduleI(void); + void chSchRescheduleS(void); + bool_t chSchRescRequiredI(void); +#ifdef __cplusplus +} +#endif + +#ifdef CH_CURRP_REGISTER_CACHE +register Thread *currp asm(CH_CURRP_REGISTER_CACHE); +#else +#define currp rlist.r_current +#endif + +#endif /* _SCHEDULER_H_ */ + +/** @} */ diff --git a/src/include/threads.h b/src/include/threads.h index 5bfd51423..b7b1a3e29 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -1,241 +1,242 @@ -/*
- ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
-
- This file is part of ChibiOS/RT.
-
- ChibiOS/RT is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
-
- ChibiOS/RT is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-/**
- * @addtogroup Threads
- * @{
- */
-
-#ifndef _THREADS_H_
-#define _THREADS_H_
-
-/**
- * Structure representing a thread.
- * @note Not all the listed fields are always needed, by switching off some
- * not needed ChibiOS/RT subsystems it is possible to save RAM space by
- * shrinking the \p Thread structure.
- */
-struct Thread {
- /** Next \p Thread in the threads list.*/
- Thread *p_next;
- /* End of the fields shared with the ThreadsList structure. */
- /** Previous \p Thread in the threads list.*/
- Thread *p_prev;
- /* End of the fields shared with the ThreadsQueue structure. */
- /** The thread priority.*/
- tprio_t p_prio;
- /* End of the fields shared with the ReadyList structure. */
- /** Thread identifier. */
- tid_t p_tid;
- /** Current thread state.*/
- tstate_t p_state;
- /** Mode flags.*/
- tmode_t p_flags;
- /** Machine dependent processor context.*/
- Context p_ctx;
- /*
- * The following fields are merged in unions because they are all
- * state-specific fields. This trick saves some extra space for each
- * thread in the system.
- */
- union {
- /** Thread wakeup code (only valid when exiting the \p PRREADY state).*/
- msg_t p_rdymsg;
- /** The thread exit code (only while in \p PREXIT state).*/
- msg_t p_exitcode;
-#ifdef CH_USE_SEMAPHORES
- /** Semaphore where the thread is waiting on (only in \p PRWTSEM state).*/
- Semaphore *p_wtsemp;
-#endif
-#ifdef CH_USE_MUTEXES
- /** Mutex where the thread is waiting on (only in \p PRWTMTX state).*/
- Mutex *p_wtmtxp;
-#endif
-#ifdef CH_USE_MESSAGES
- /** Destination thread for message send (only in \p PRSNDMSG state).*/
- Thread *p_wtthdp;
-#endif
-#ifdef CH_USE_EVENTS
- /** Enabled events mask (only while in \p PRWTEVENT state).*/
- eventmask_t p_ewmask;
-#endif
-#ifdef CH_USE_TRACE
- /** Kernel object where the thread is waiting on. It is only valid when
- the thread is some sleeping states.*/
- void *p_wtobjp;
-#endif
- };
- /*
- * Start of the optional fields.
- */
-#ifdef CH_USE_WAITEXIT
- /** The list of the threads waiting for this thread termination.*/
- ThreadsList p_waiting;
-#endif
-#ifdef CH_USE_EXIT_EVENT
- /** The thread termination \p EventSource.*/
- EventSource p_exitesource;
-#endif
-#ifdef CH_USE_MESSAGES
- ThreadsQueue p_msgqueue;
- msg_t p_msg;
-#endif
-#ifdef CH_USE_EVENTS
- /** Pending events mask.*/
- eventmask_t p_epending;
-#endif
-#ifdef CH_USE_MUTEXES
- /** Owner mutexes list, \p NULL terminated.*/
- Mutex *p_mtxlist;
- tprio_t p_realprio;
-#endif
-};
-
-/** Thread state: Thread in the ready list.*/
-#define PRREADY 0
-/** Thread state: Current.*/
-#define PRCURR 1
-/** Thread state: Thread created in suspended state.*/
-#define PRSUSPENDED 2
-/** Thread state: Waiting on a semaphore.*/
-#define PRWTSEM 3
-/** Thread state: Waiting on a mutex.*/
-#define PRWTMTX 4
-/** Thread state: Waiting in \p chThdSleep() or \p chThdSleepUntil().*/
-#define PRSLEEP 5
-/** Thread state: Waiting in \p chThdWait().*/
-#define PRWAIT 6
-/** Thread state: Waiting in \p chEvtWait().*/
-#define PRWTEVENT 7
-/** Thread state: Waiting in \p chMsgSend().*/
-#define PRSNDMSG 8
-/** Thread state: Waiting in \p chMsgWait().*/
-#define PRWTMSG 9
-/** Thread state: After termination.*/
-#define PREXIT 10
-
-#ifdef CH_USE_TERMINATE
-/** Thread option: Termination requested flag.*/
-#define P_TERMINATE 1
-#endif
-#ifdef CH_USE_RESUME
-/** Thread option: Create suspended thread.*/
-#define P_SUSPENDED 2
-#endif
-#ifdef CH_USE_MESSAGES_PRIORITY
-/** Thread option: Serve messages by priority instead of FIFO order.*/
-#define P_MSGBYPRIO 4
-#endif
-
-/** Pseudo priority used by the ready list header, do not use.*/
-#define NOPRIO 0
-/** Idle thread priority.*/
-#define IDLEPRIO 1
-/** Lowest user priority.*/
-#define LOWPRIO 2
-/** Normal user priority.*/
-#define NORMALPRIO 64
-/** Highest user priority.*/
-#define HIGHPRIO 127
-/** Greatest possible priority.*/
-#define ABSPRIO 255
-
-/* Not an API, don't use into the application code.*/
-void init_thread(tprio_t prio, tmode_t mode, Thread *tp);
-
-/** Thread function.*/
-typedef msg_t (*tfunc_t)(void *);
-
-/*
- * Threads APIs.
- */
-#ifdef __cplusplus
-extern "C" {
-#endif
- Thread *chThdCreate(tprio_t prio, tmode_t mode, void *workspace,
- size_t wsize, tfunc_t pf, void *arg);
- Thread *chThdCreateFast(tprio_t prio, void *workspace,
- size_t wsize, tfunc_t pf);
- void chThdSetPriority(tprio_t newprio);
- void chThdExit(msg_t msg);
-#ifdef CH_USE_RESUME
- void chThdResume(Thread *tp);
-#endif
-#ifdef CH_USE_SUSPEND
- void chThdSuspend(Thread **tpp);
-#endif
-#ifdef CH_USE_TERMINATE
- void chThdTerminate(Thread *tp);
-#endif
-#ifdef CH_USE_WAITEXIT
- msg_t chThdWait(Thread *tp);
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-/** Returns the pointer to the \p Thread currently in execution.*/
-#define chThdSelf() currp
-
-/** Returns the thread priority.*/
-#define chThdGetPriority() (currp->p_prio)
-
-/** Returns the pointer to the \p Thread local storage area, if any.*/
-#define chThdLS() (void *)(currp + 1)
-
-/** Verifies if the specified thread is in the \p PREXIT state.*/
-#define chThdTerminated(tp) ((tp)->p_state == PREXIT)
-
-/**
- * Verifies if the current thread has a termination request pending.
- */
-#define chThdShouldTerminate() (currp->p_flags & P_TERMINATE)
-
-/**
- * Returns the exit event source for the specified thread. The source is
- * signaled when the thread terminates.
- * @param tp the pointer to the thread
- * @note When registering on a thread termination make sure the thread
- * is still alive, if you do that after the thread termination
- * then you would miss the event. There are two ways to ensure
- * this:<br>
- * <ul>
- * <li>Create the thread suspended, register on the event source
- * and then resume the thread (recommended).</li>
- * <li>Create the thread with a lower priority then register on it.
- * This does not work if the hardware is capable of multiple
- * physical threads.</li>
- * </ul>
- * @note You dont need to unregister from a terminated thread because
- * the event source becomes inactive.
- * @note The function is available only if the \p CH_USE_EXIT_EVENT
- * option is enabled in \p chconf.h.
- */
-#define chThdGetExitEventSource(tp) (&(tp)->p_exitesource)
-
-/**
- * Resumes a thread created with the \p P_SUSPENDED option or suspended with
- * \p chThdSuspend().
- * @param tp the pointer to the thread
- */
-#define chThdResumeI(tp) chSchReadyI((tp), RDY_OK)
-
-#endif /* _THREADS_H_ */
-
-/** @} */
+/* + ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio. + + This file is part of ChibiOS/RT. + + ChibiOS/RT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + ChibiOS/RT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +/** + * @addtogroup Threads + * @{ + */ + +#ifndef _THREADS_H_ +#define _THREADS_H_ + +/** + * Structure representing a thread. + * @note Not all the listed fields are always needed, by switching off some + * not needed ChibiOS/RT subsystems it is possible to save RAM space by + * shrinking the \p Thread structure. + */ +struct Thread { + /** Next \p Thread in the threads list.*/ + Thread *p_next; + /* End of the fields shared with the ThreadsList structure. */ + /** Previous \p Thread in the threads list.*/ + Thread *p_prev; + /* End of the fields shared with the ThreadsQueue structure. */ + /** The thread priority.*/ + tprio_t p_prio; + /* End of the fields shared with the ReadyList structure. */ + /** Thread identifier. */ + tid_t p_tid; + /** Current thread state.*/ + tstate_t p_state; + /** Mode flags. */ + tmode_t p_flags; + /** Machine dependent processor context.*/ + Context p_ctx; + /* + * The following fields are merged in unions because they are all + * state-specific fields. This trick saves some extra space for each + * thread in the system. + */ + union { + /** Thread wakeup code (only valid when exiting the \p PRREADY state). */ + msg_t p_rdymsg; + /** The thread exit code (only while in \p PREXIT state).*/ + msg_t p_exitcode; +#ifdef CH_USE_SEMAPHORES + /** Semaphore where the thread is waiting on (only in \p PRWTSEM state). */ + Semaphore *p_wtsemp; +#endif +#ifdef CH_USE_MUTEXES + /** Mutex where the thread is waiting on (only in \p PRWTMTX state). */ + Mutex *p_wtmtxp; +#endif +#ifdef CH_USE_MESSAGES + /** Destination thread for message send (only in \p PRSNDMSG state). */ + Thread *p_wtthdp; +#endif +#ifdef CH_USE_EVENTS + /** Enabled events mask (only while in \p PRWTEVENT state). */ + eventmask_t p_ewmask; +#endif +#ifdef CH_USE_TRACE + /** Kernel object where the thread is waiting on. It is only valid when + the thread is some sleeping states. */ + void *p_wtobjp; +#endif + }; + /* + * Start of the optional fields. + */ +#ifdef CH_USE_WAITEXIT + /** The list of the threads waiting for this thread termination.*/ + ThreadsList p_waiting; +#endif +#ifdef CH_USE_EXIT_EVENT + /** The thread termination \p EventSource.*/ + EventSource p_exitesource; +#endif +#ifdef CH_USE_MESSAGES + ThreadsQueue p_msgqueue; + msg_t p_msg; +#endif +#ifdef CH_USE_EVENTS + /** Pending events mask.*/ + eventmask_t p_epending; +#endif +#ifdef CH_USE_MUTEXES + /** List of mutexes owned by this thread, \p NULL terminated.*/ + Mutex *p_mtxlist; + /** Thread's own, non-inherited, priority */ + tprio_t p_realprio; +#endif +}; + +/** Thread state: Thread in the ready list.*/ +#define PRREADY 0 +/** Thread state: Current.*/ +#define PRCURR 1 +/** Thread state: Thread created in suspended state.*/ +#define PRSUSPENDED 2 +/** Thread state: Waiting on a semaphore.*/ +#define PRWTSEM 3 +/** Thread state: Waiting on a mutex.*/ +#define PRWTMTX 4 +/** Thread state: Waiting in \p chThdSleep() or \p chThdSleepUntil().*/ +#define PRSLEEP 5 +/** Thread state: Waiting in \p chThdWait().*/ +#define PRWAIT 6 +/** Thread state: Waiting in \p chEvtWait().*/ +#define PRWTEVENT 7 +/** Thread state: Waiting in \p chMsgSend().*/ +#define PRSNDMSG 8 +/** Thread state: Waiting in \p chMsgWait().*/ +#define PRWTMSG 9 +/** Thread state: After termination.*/ +#define PREXIT 10 + +#ifdef CH_USE_TERMINATE +/** Thread option: Termination requested flag.*/ +#define P_TERMINATE 1 +#endif +#ifdef CH_USE_RESUME +/** Thread option: Create suspended thread.*/ +#define P_SUSPENDED 2 +#endif +#ifdef CH_USE_MESSAGES_PRIORITY +/** Thread option: Serve messages by priority instead of FIFO order.*/ +#define P_MSGBYPRIO 4 +#endif + +/** Pseudo priority used by the ready list header, do not use.*/ +#define NOPRIO 0 +/** Idle thread priority.*/ +#define IDLEPRIO 1 +/** Lowest user priority.*/ +#define LOWPRIO 2 +/** Normal user priority.*/ +#define NORMALPRIO 64 +/** Highest user priority.*/ +#define HIGHPRIO 127 +/** Greatest possible priority.*/ +#define ABSPRIO 255 + +/* Not an API, don't use into the application code.*/ +void init_thread(tprio_t prio, tmode_t mode, Thread *tp); + +/** Thread function.*/ +typedef msg_t (*tfunc_t)(void *); + +/* + * Threads APIs. + */ +#ifdef __cplusplus +extern "C" { +#endif + Thread *chThdCreate(tprio_t prio, tmode_t mode, void *workspace, + size_t wsize, tfunc_t pf, void *arg); + Thread *chThdCreateFast(tprio_t prio, void *workspace, + size_t wsize, tfunc_t pf); + void chThdSetPriority(tprio_t newprio); + void chThdExit(msg_t msg); +#ifdef CH_USE_RESUME + void chThdResume(Thread *tp); +#endif +#ifdef CH_USE_SUSPEND + void chThdSuspend(Thread **tpp); +#endif +#ifdef CH_USE_TERMINATE + void chThdTerminate(Thread *tp); +#endif +#ifdef CH_USE_WAITEXIT + msg_t chThdWait(Thread *tp); +#endif +#ifdef __cplusplus +} +#endif + +/** Returns the pointer to the \p Thread currently in execution.*/ +#define chThdSelf() currp + +/** Returns the thread priority.*/ +#define chThdGetPriority() (currp->p_prio) + +/** Returns the pointer to the \p Thread local storage area, if any.*/ +#define chThdLS() (void *)(currp + 1) + +/** Verifies if the specified thread is in the \p PREXIT state.*/ +#define chThdTerminated(tp) ((tp)->p_state == PREXIT) + +/** + * Verifies if the current thread has a termination request pending. + */ +#define chThdShouldTerminate() (currp->p_flags & P_TERMINATE) + +/** + * Returns the exit event source for the specified thread. The source is + * signaled when the thread terminates. + * @param tp the pointer to the thread + * @note When registering on a thread termination make sure the thread + * is still alive, if you do that after the thread termination + * then you would miss the event. There are two ways to ensure + * this:<br> + * <ul> + * <li>Create the thread suspended, register on the event source + * and then resume the thread (recommended).</li> + * <li>Create the thread with a lower priority then register on it. + * This does not work if the hardware is capable of multiple + * physical threads.</li> + * </ul> + * @note You dont need to unregister from a terminated thread because + * the event source becomes inactive. + * @note The function is available only if the \p CH_USE_EXIT_EVENT + * option is enabled in \p chconf.h. + */ +#define chThdGetExitEventSource(tp) (&(tp)->p_exitesource) + +/** + * Resumes a thread created with the \p P_SUSPENDED option or suspended with + * \p chThdSuspend(). + * @param tp the pointer to the thread + */ +#define chThdResumeI(tp) chSchReadyI((tp), RDY_OK) + +#endif /* _THREADS_H_ */ + +/** @} */ |