aboutsummaryrefslogtreecommitdiffstats
path: root/os
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2013-08-16 17:41:56 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2013-08-16 17:41:56 +0000
commitbfa3ba9f00d263a15d6d6b2dec2aa0b81058df27 (patch)
tree817fc88a260e36d897b48dce522f2057b15346e0 /os
parent752b44ba0e550159fb6d2372c54a278fb94080eb (diff)
downloadChibiOS-bfa3ba9f00d263a15d6d6b2dec2aa0b81058df27.tar.gz
ChibiOS-bfa3ba9f00d263a15d6d6b2dec2aa0b81058df27.tar.bz2
ChibiOS-bfa3ba9f00d263a15d6d6b2dec2aa0b81058df27.zip
Turned more macros in inline functions.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@6166 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os')
-rw-r--r--os/hal/osal/chibios/osal.c4
-rw-r--r--os/rt/include/ch.h2
-rw-r--r--os/rt/include/chglobal.h144
-rw-r--r--os/rt/include/chmtx.h9
-rw-r--r--os/rt/include/chthreads.h352
-rw-r--r--os/rt/src/chthreads.c8
6 files changed, 270 insertions, 249 deletions
diff --git a/os/hal/osal/chibios/osal.c b/os/hal/osal/chibios/osal.c
index 65912ce7e..da3746eaf 100644
--- a/os/hal/osal/chibios/osal.c
+++ b/os/hal/osal/chibios/osal.c
@@ -98,9 +98,9 @@ msg_t osalThreadSuspendS(thread_reference_t *trp) {
chDbgAssert(*trp == NULL, "not NULL");
- *trp = (thread_reference_t)chThdSelf();
+ *trp = (thread_reference_t)chThdGetSelfX();
chSchGoSleepS(CH_STATE_SUSPENDED);
- return chThdSelf()->p_msg;
+ return chThdGetSelfX()->p_msg;
}
/**
diff --git a/os/rt/include/ch.h b/os/rt/include/ch.h
index 6af6b8d4d..3e3b90983 100644
--- a/os/rt/include/ch.h
+++ b/os/rt/include/ch.h
@@ -114,9 +114,9 @@ typedef struct virtual_timer virtual_timer_t;
#include "chsys.h"
#include "chglobal.h"
#include "chvt.h"
-#include "chthreads.h"
#include "chlists.h"
#include "chschd.h"
+#include "chthreads.h"
#include "chregistry.h"
#include "chsem.h"
#include "chbsem.h"
diff --git a/os/rt/include/chglobal.h b/os/rt/include/chglobal.h
index 0d49d9a45..9803aa8fb 100644
--- a/os/rt/include/chglobal.h
+++ b/os/rt/include/chglobal.h
@@ -48,7 +48,6 @@
* @brief Generic threads single link list, it works like a stack.
*/
typedef struct {
-
thread_t *p_next; /**< @brief Next in the list/queue. */
} threads_list_t;
@@ -83,6 +82,149 @@ typedef struct {
} ready_list_t;
/**
+ * @extends threads_queue_t
+ *
+ * @brief 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_t structure.
+ */
+typedef struct thread {
+ thread_t *p_next; /**< @brief Next in the list/queue. */
+ /* End of the fields shared with the threads_list_t structure.*/
+ thread_t *p_prev; /**< @brief Previous in the queue. */
+ /* End of the fields shared with the threads_queue_t structure.*/
+ tprio_t p_prio; /**< @brief Thread priority. */
+ struct context p_ctx; /**< @brief Processor context. */
+#if CH_CFG_USE_REGISTRY || defined(__DOXYGEN__)
+ thread_t *p_newer; /**< @brief Newer registry element. */
+ thread_t *p_older; /**< @brief Older registry element. */
+#endif
+ /* End of the fields shared with the ReadyList structure. */
+#if CH_CFG_USE_REGISTRY || defined(__DOXYGEN__)
+ /**
+ * @brief Thread name or @p NULL.
+ */
+ const char *p_name;
+#endif
+#if CH_DBG_ENABLE_STACK_CHECK || defined(__DOXYGEN__)
+ /**
+ * @brief Thread stack boundary.
+ */
+ stkalign_t *p_stklimit;
+#endif
+ /**
+ * @brief Current thread state.
+ */
+ tstate_t p_state;
+ /**
+ * @brief Various thread flags.
+ */
+ tmode_t p_flags;
+#if CH_CFG_USE_DYNAMIC || defined(__DOXYGEN__)
+ /**
+ * @brief References to this thread.
+ */
+ trefs_t p_refs;
+#endif
+ /**
+ * @brief Number of ticks remaining to this thread.
+ */
+#if (CH_CFG_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
+ tslices_t p_preempt;
+#endif
+#if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__)
+ /**
+ * @brief Thread consumed time in ticks.
+ * @note This field can overflow.
+ */
+ volatile systime_t p_time;
+#endif
+ /**
+ * @brief State-specific fields.
+ * @note All the fields declared in this union are only valid in the
+ * specified state or condition and are thus volatile.
+ */
+ union {
+ /**
+ * @brief Thread wakeup code.
+ * @note This field contains the low level message sent to the thread
+ * by the waking thread or interrupt handler. The value is valid
+ * after exiting the @p chSchWakeupS() function.
+ */
+ msg_t rdymsg;
+ /**
+ * @brief Thread exit code.
+ * @note The thread termination code is stored in this field in order
+ * to be retrieved by the thread performing a @p chThdWait() on
+ * this thread.
+ */
+ msg_t exitcode;
+ /**
+ * @brief Pointer to a generic "wait" object.
+ * @note This field is used to get a generic pointer to a synchronization
+ * object and is valid when the thread is in one of the wait
+ * states.
+ */
+ void *wtobjp;
+#if CH_CFG_USE_EVENTS || defined(__DOXYGEN__)
+ /**
+ * @brief Enabled events mask.
+ * @note This field is only valid while the thread is in the
+ * @p CH_STATE_WTOREVT or @p CH_STATE_WTANDEVT states.
+ */
+ eventmask_t ewmask;
+#endif
+ } p_u;
+#if CH_CFG_USE_WAITEXIT || defined(__DOXYGEN__)
+ /**
+ * @brief Termination waiting list.
+ */
+ threads_list_t p_waiting;
+#endif
+#if CH_CFG_USE_MESSAGES || defined(__DOXYGEN__)
+ /**
+ * @brief Messages queue.
+ */
+ threads_queue_t p_msgqueue;
+ /**
+ * @brief Thread message.
+ */
+ msg_t p_msg;
+#endif
+#if CH_CFG_USE_EVENTS || defined(__DOXYGEN__)
+ /**
+ * @brief Pending events mask.
+ */
+ eventmask_t p_epending;
+#endif
+#if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__)
+ /**
+ * @brief List of the mutexes owned by this thread.
+ * @note The list is terminated by a @p NULL in this field.
+ */
+ struct mutex *p_mtxlist;
+ /**
+ * @brief Thread's own, non-inherited, priority.
+ */
+ tprio_t p_realprio;
+#endif
+#if (CH_CFG_USE_DYNAMIC && CH_CFG_USE_MEMPOOLS) || defined(__DOXYGEN__)
+ /**
+ * @brief Memory Pool where the thread workspace is returned.
+ */
+ void *p_mpool;
+#endif
+#if CH_DBG_STATISTICS || defined(__DOXYGEN__)
+ time_measurement_t p_stats;
+#endif
+#if defined(CH_CFG_THREAD_EXTRA_FIELDS)
+ /* Extra fields defined in chconf.h.*/
+ CH_CFG_THREAD_EXTRA_FIELDS
+#endif
+} thread_t;
+
+/**
* @brief Virtual timers list header.
* @note The timers list is implemented as a double link bidirectional list
* in order to make the unlink time constant, the reset of a virtual
diff --git a/os/rt/include/chmtx.h b/os/rt/include/chmtx.h
index a4ff5e49f..0444d936c 100644
--- a/os/rt/include/chmtx.h
+++ b/os/rt/include/chmtx.h
@@ -48,16 +48,21 @@
/*===========================================================================*/
/**
+ * @brief Type of a mutex structure.
+ */
+typedef struct mutex mutex_t;
+
+/**
* @brief Mutex structure.
*/
-typedef struct mutex {
+struct mutex {
threads_queue_t m_queue; /**< @brief Queue of the threads sleeping
on this mutex. */
thread_t *m_owner; /**< @brief Owner @p thread_t pointer or
@p NULL. */
mutex_t *m_next; /**< @brief Next @p mutex_t into an
owner-list or @p NULL. */
-} mutex_t;
+};
/*===========================================================================*/
/* Module macros. */
diff --git a/os/rt/include/chthreads.h b/os/rt/include/chthreads.h
index 22f499edf..d27de04a9 100644
--- a/os/rt/include/chthreads.h
+++ b/os/rt/include/chthreads.h
@@ -39,22 +39,23 @@
*/
#define CH_STATE_READY 0 /**< @brief Waiting on the ready list. */
#define CH_STATE_CURRENT 1 /**< @brief Currently running. */
-#define CH_STATE_SUSPENDED 2 /**< @brief Created in suspended state. */
-#define CH_STATE_WTSEM 3 /**< @brief Waiting on a semaphore. */
-#define CH_STATE_WTMTX 4 /**< @brief Waiting on a mutex. */
-#define CH_STATE_WTCOND 5 /**< @brief Waiting on a condition
+#define CH_STATE_WTSTART 2 /**< @brief Created but not started. */
+#define CH_STATE_SUSPENDED 3 /**< @brief Created in suspended state. */
+#define CH_STATE_WTSEM 4 /**< @brief Waiting on a semaphore. */
+#define CH_STATE_WTMTX 5 /**< @brief Waiting on a mutex. */
+#define CH_STATE_WTCOND 6 /**< @brief Waiting on a condition
variable. */
-#define CH_STATE_SLEEPING 6 /**< @brief Waiting in @p chThdSleep()
+#define CH_STATE_SLEEPING 7 /**< @brief Waiting in @p chThdSleep()
or @p chThdSleepUntil(). */
-#define CH_STATE_WTEXIT 7 /**< @brief Waiting in @p chThdWait(). */
-#define CH_STATE_WTOREVT 8 /**< @brief Waiting for an event. */
-#define CH_STATE_WTANDEVT 9 /**< @brief Waiting for several events. */
-#define CH_STATE_SNDMSGQ 10 /**< @brief Sending a message, in queue.*/
-#define CH_STATE_SNDMSG 11 /**< @brief Sent a message, waiting
+#define CH_STATE_WTEXIT 8 /**< @brief Waiting in @p chThdWait(). */
+#define CH_STATE_WTOREVT 9 /**< @brief Waiting for an event. */
+#define CH_STATE_WTANDEVT 10 /**< @brief Waiting for several events. */
+#define CH_STATE_SNDMSGQ 11 /**< @brief Sending a message, in queue.*/
+#define CH_STATE_SNDMSG 12 /**< @brief Sent a message, waiting
answer. */
-#define CH_STATE_WTMSG 12 /**< @brief Waiting for a message. */
-#define CH_STATE_WTQUEUE 13 /**< @brief Waiting on an I/O queue. */
-#define CH_STATE_FINAL 14 /**< @brief Thread terminated. */
+#define CH_STATE_WTMSG 13 /**< @brief Waiting for a message. */
+#define CH_STATE_WTQUEUE 14 /**< @brief Waiting on an I/O queue. */
+#define CH_STATE_FINAL 15 /**< @brief Thread terminated. */
/**
* @brief Thread states as array of strings.
@@ -62,9 +63,9 @@
* indexed using the numeric thread state values.
*/
#define CH_STATE_NAMES \
- "READY", "CURRENT", "SUSPENDED", "WTSEM", "WTMTX", "WTCOND", "SLEEPING", \
- "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", "SNDMSG", "WTMSG", "WTQUEUE", \
- "FINAL"
+ "READY", "WTSTART", "CURRENT", "SUSPENDED", "WTSEM", "WTMTX", "WTCOND", \
+ "SLEEPING", "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", "SNDMSG", \
+ "WTMSG", "WTQUEUE", "FINAL"
/** @} */
/**
@@ -92,154 +93,6 @@
/* Module data structures and types. */
/*===========================================================================*/
-/* Forward declaration required by the mutexes stack structure present
- in every thread.*/
-#if CH_CFG_USE_MUTEXES
-typedef struct mutex mutex_t;
-#endif
-
-/**
- * @extends threads_queue_t
- *
- * @brief 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_t structure.
- */
-typedef struct thread {
- thread_t *p_next; /**< @brief Next in the list/queue. */
- /* End of the fields shared with the threads_list_t structure.*/
- thread_t *p_prev; /**< @brief Previous in the queue. */
- /* End of the fields shared with the threads_queue_t structure.*/
- tprio_t p_prio; /**< @brief Thread priority. */
- struct context p_ctx; /**< @brief Processor context. */
-#if CH_CFG_USE_REGISTRY || defined(__DOXYGEN__)
- thread_t *p_newer; /**< @brief Newer registry element. */
- thread_t *p_older; /**< @brief Older registry element. */
-#endif
- /* End of the fields shared with the ReadyList structure. */
-#if CH_CFG_USE_REGISTRY || defined(__DOXYGEN__)
- /**
- * @brief Thread name or @p NULL.
- */
- const char *p_name;
-#endif
-#if CH_DBG_ENABLE_STACK_CHECK || defined(__DOXYGEN__)
- /**
- * @brief Thread stack boundary.
- */
- stkalign_t *p_stklimit;
-#endif
- /**
- * @brief Current thread state.
- */
- tstate_t p_state;
- /**
- * @brief Various thread flags.
- */
- tmode_t p_flags;
-#if CH_CFG_USE_DYNAMIC || defined(__DOXYGEN__)
- /**
- * @brief References to this thread.
- */
- trefs_t p_refs;
-#endif
- /**
- * @brief Number of ticks remaining to this thread.
- */
-#if (CH_CFG_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
- tslices_t p_preempt;
-#endif
-#if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__)
- /**
- * @brief Thread consumed time in ticks.
- * @note This field can overflow.
- */
- volatile systime_t p_time;
-#endif
- /**
- * @brief State-specific fields.
- * @note All the fields declared in this union are only valid in the
- * specified state or condition and are thus volatile.
- */
- union {
- /**
- * @brief Thread wakeup code.
- * @note This field contains the low level message sent to the thread
- * by the waking thread or interrupt handler. The value is valid
- * after exiting the @p chSchWakeupS() function.
- */
- msg_t rdymsg;
- /**
- * @brief Thread exit code.
- * @note The thread termination code is stored in this field in order
- * to be retrieved by the thread performing a @p chThdWait() on
- * this thread.
- */
- msg_t exitcode;
- /**
- * @brief Pointer to a generic "wait" object.
- * @note This field is used to get a generic pointer to a synchronization
- * object and is valid when the thread is in one of the wait
- * states.
- */
- void *wtobjp;
-#if CH_CFG_USE_EVENTS || defined(__DOXYGEN__)
- /**
- * @brief Enabled events mask.
- * @note This field is only valid while the thread is in the
- * @p CH_STATE_WTOREVT or @p CH_STATE_WTANDEVT states.
- */
- eventmask_t ewmask;
-#endif
- } p_u;
-#if CH_CFG_USE_WAITEXIT || defined(__DOXYGEN__)
- /**
- * @brief Termination waiting list.
- */
- threads_list_t p_waiting;
-#endif
-#if CH_CFG_USE_MESSAGES || defined(__DOXYGEN__)
- /**
- * @brief Messages queue.
- */
- threads_queue_t p_msgqueue;
- /**
- * @brief Thread message.
- */
- msg_t p_msg;
-#endif
-#if CH_CFG_USE_EVENTS || defined(__DOXYGEN__)
- /**
- * @brief Pending events mask.
- */
- eventmask_t p_epending;
-#endif
-#if CH_CFG_USE_MUTEXES || defined(__DOXYGEN__)
- /**
- * @brief List of the mutexes owned by this thread.
- * @note The list is terminated by a @p NULL in this field.
- */
- mutex_t *p_mtxlist;
- /**
- * @brief Thread's own, non-inherited, priority.
- */
- tprio_t p_realprio;
-#endif
-#if (CH_CFG_USE_DYNAMIC && CH_CFG_USE_MEMPOOLS) || defined(__DOXYGEN__)
- /**
- * @brief Memory Pool where the thread workspace is returned.
- */
- void *p_mpool;
-#endif
-#if CH_DBG_STATISTICS || defined(__DOXYGEN__)
- time_measurement_t p_stats;
-#endif
-#if defined(CH_CFG_THREAD_EXTRA_FIELDS)
- /* Extra fields defined in chconf.h.*/
- CH_CFG_THREAD_EXTRA_FIELDS
-#endif
-} thread_t;
/**
* @brief Thread function.
@@ -255,80 +108,6 @@ typedef msg_t (*tfunc_t)(void *);
* @{
*/
/**
- * @brief Returns a pointer to the current @p thread_t.
- * @note Can be invoked in any context.
- *
- * @special
- */
-#define chThdSelf() currp
-
-/**
- * @brief Returns the current thread priority.
- * @note Can be invoked in any context.
- *
- * @special
- */
-#define chThdGetPriority() (currp->p_prio)
-
-/**
- * @brief Returns the number of ticks consumed by the specified thread.
- * @note This function is only available when the
- * @p CH_DBG_THREADS_PROFILING configuration option is enabled.
- * @note Can be invoked in any context.
- *
- * @param[in] tp pointer to the thread
- *
- * @special
- */
-#define chThdGetTicks(tp) ((tp)->p_time)
-
-/**
- * @brief Verifies if the specified thread is in the @p CH_STATE_FINAL state.
- * @note Can be invoked in any context.
- *
- * @param[in] tp pointer to the thread
- * @retval true thread terminated.
- * @retval false thread not terminated.
- *
- * @special
- */
-#define chThdTerminated(tp) ((tp)->p_state == CH_STATE_FINAL)
-
-/**
- * @brief Verifies if the current thread has a termination request pending.
- * @note Can be invoked in any context.
- *
- * @retval true termination request pending.
- * @retval false termination request not pending.
- *
- * @special
- */
-#define chThdShouldTerminate() (currp->p_flags & CH_FLAG_TERMINATE)
-
-/**
- * @brief Resumes a thread created with @p chThdCreateI().
- *
- * @param[in] tp pointer to the thread
- *
- * @iclass
- */
-#define chThdResumeI(tp) chSchReadyI(tp)
-
-/**
- * @brief Suspends the invoking thread for the specified time.
- *
- * @param[in] time the delay in system ticks, the special values are
- * handled as follow:
- * - @a TIME_INFINITE the thread enters an infinite sleep
- * state.
- * - @a TIME_IMMEDIATE this value is not allowed.
- * .
- *
- * @sclass
- */
-#define chThdSleepS(time) chSchGoSleepTimeoutS(CH_STATE_SLEEPING, time)
-
-/**
* @brief Delays the invoking thread for the specified number of seconds.
* @note The specified time is rounded up to a value allowed by the real
* system tick clock.
@@ -401,6 +180,103 @@ extern "C" {
/* Module inline functions. */
/*===========================================================================*/
+ /**
+ * @brief Returns a pointer to the current @p thread_t.
+ *
+ * @xclass
+ */
+static inline thread_t *chThdGetSelfX(void) {
+
+ return ch.rlist.r_current;
+}
+
+/**
+ * @brief Returns the current thread priority.
+ * @note Can be invoked in any context.
+ *
+ * @xclass
+ */
+static inline tprio_t chThdGetPriorityX(void) {
+
+ return chThdGetSelfX()->p_prio;
+}
+
+/**
+ * @brief Returns the number of ticks consumed by the specified thread.
+ * @note This function is only available when the
+ * @p CH_DBG_THREADS_PROFILING configuration option is enabled.
+ *
+ * @param[in] tp pointer to the thread
+ *
+ * @xclass
+ */
+#if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__)
+static inline systime_t chThdGetTicksX(thread_t *tp) {
+
+ return tp->p_time;
+}
+#endif
+
+/**
+ * @brief Verifies if the specified thread is in the @p CH_STATE_FINAL state.
+ *
+ * @param[in] tp pointer to the thread
+ * @retval true thread terminated.
+ * @retval false thread not terminated.
+ *
+ * @xclass
+ */
+static inline bool chThdTerminatedX(thread_t *tp) {
+
+ return (bool)(tp->p_state == CH_STATE_FINAL);
+}
+
+/**
+ * @brief Verifies if the current thread has a termination request pending.
+ *
+ * @retval true termination request pending.
+ * @retval false termination request not pending.
+ *
+ * @xclass
+ */
+static inline bool chThdShouldTerminateX(void) {
+
+ return (bool)(chThdGetSelfX()->p_flags & CH_FLAG_TERMINATE);
+}
+
+/**
+ * @brief Resumes a thread created with @p chThdCreateI().
+ *
+ * @param[in] tp pointer to the thread
+ *
+ * @xclass
+ */
+static inline thread_t *chThdStartI(thread_t *tp) {
+
+ chDbgAssert(tp->p_state == CH_STATE_WTSTART, "wrong state");
+
+ return chSchReadyI(tp);
+}
+
+/**
+ * @brief Suspends the invoking thread for the specified time.
+ *
+ * @param[in] time the delay in system ticks, the special values are
+ * handled as follow:
+ * - @a TIME_INFINITE the thread enters an infinite sleep
+ * state.
+ * - @a TIME_IMMEDIATE this value is not allowed.
+ * .
+ *
+ * @sclass
+ */
+static inline void chThdSleepS(systime_t time) {
+
+ chDbgCheck(time != TIME_IMMEDIATE);
+
+ chSchGoSleepTimeoutS(CH_STATE_SLEEPING, time);
+}
+
#endif /* _CHTHREADS_H_ */
/** @} */
diff --git a/os/rt/src/chthreads.c b/os/rt/src/chthreads.c
index 073b6eed7..d6fbc880f 100644
--- a/os/rt/src/chthreads.c
+++ b/os/rt/src/chthreads.c
@@ -93,7 +93,7 @@
thread_t *_thread_init(thread_t *tp, tprio_t prio) {
tp->p_prio = prio;
- tp->p_state = CH_STATE_SUSPENDED;
+ tp->p_state = CH_STATE_WTSTART;
tp->p_flags = CH_FLAG_MODE_STATIC;
#if CH_CFG_TIME_QUANTUM > 0
tp->p_preempt = CH_CFG_TIME_QUANTUM;
@@ -153,9 +153,9 @@ void _thread_memfill(uint8_t *startp, uint8_t *endp, uint8_t v) {
/**
* @brief Creates a new thread into a static memory area.
* @details The new thread is initialized but not inserted in the ready list,
- * the initial state is @p CH_STATE_SUSPENDED.
+ * the initial state is @p CH_STATE_WTSTART.
* @post The initialized thread can be subsequently started by invoking
- * @p chThdResume(), @p chThdResumeI() or @p chSchWakeupS()
+ * @p chThdStart(), @p chThdStartI() or @p chSchWakeupS()
* depending on the execution context.
* @note A thread can terminate by calling @p chThdExit() or by simply
* returning from its main function.
@@ -310,8 +310,6 @@ void chThdTerminate(thread_t *tp) {
*/
void chThdSleep(systime_t time) {
- chDbgCheck(time != TIME_IMMEDIATE);
-
chSysLock();
chThdSleepS(time);
chSysUnlock();