aboutsummaryrefslogtreecommitdiffstats
path: root/os/rt
diff options
context:
space:
mode:
authorGiovanni Di Sirio <gdisirio@gmail.com>2016-02-24 14:44:50 +0000
committerGiovanni Di Sirio <gdisirio@gmail.com>2016-02-24 14:44:50 +0000
commit6d6284c9e6d1e3d1f0083c153ee21235771e1014 (patch)
tree32ab7da6461deb5264075e4221fb1f2aa6e666a0 /os/rt
parenta2072b5560adf07640b43107e00582b31658540d (diff)
downloadChibiOS-6d6284c9e6d1e3d1f0083c153ee21235771e1014.tar.gz
ChibiOS-6d6284c9e6d1e3d1f0083c153ee21235771e1014.tar.bz2
ChibiOS-6d6284c9e6d1e3d1f0083c153ee21235771e1014.zip
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@8941 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os/rt')
-rw-r--r--os/rt/include/chdynamic.h4
-rw-r--r--os/rt/include/chevents.h8
-rw-r--r--os/rt/include/chthreads.h14
-rw-r--r--os/rt/src/chdynamic.c129
-rw-r--r--os/rt/src/chsys.c1
-rw-r--r--os/rt/src/chthreads.c74
6 files changed, 175 insertions, 55 deletions
diff --git a/os/rt/include/chdynamic.h b/os/rt/include/chdynamic.h
index fa42bf3ae..9b653cc5c 100644
--- a/os/rt/include/chdynamic.h
+++ b/os/rt/include/chdynamic.h
@@ -71,16 +71,16 @@
#ifdef __cplusplus
extern "C" {
#endif
+ thread_t *chThdAddRef(thread_t *tp);
+ void chThdRelease(thread_t *tp);
#if CH_CFG_USE_HEAP == TRUE
thread_t *chThdCreateFromHeap(memory_heap_t *heapp, size_t size,
const char *name, tprio_t prio,
tfunc_t pf, void *arg);
- void chThdFreeToHeap(thread_t *tp);
#endif
#if CH_CFG_USE_MEMPOOLS == TRUE
thread_t *chThdCreateFromMemoryPool(memory_pool_t *mp, const char *name,
tprio_t prio, tfunc_t pf, void *arg);
- void chThdFreeToMemoryPool(thread_t *tp, memory_pool_t *mp);
#endif
#ifdef __cplusplus
}
diff --git a/os/rt/include/chevents.h b/os/rt/include/chevents.h
index fa8e4d7a5..8432eea8e 100644
--- a/os/rt/include/chevents.h
+++ b/os/rt/include/chevents.h
@@ -37,14 +37,6 @@
/* Module constants. */
/*===========================================================================*/
-/**
- * @brief Event bit reserved for thread termination request.
- * @note The most significant bit of the events mask is conventionally
- * used for thread termination but it can eventually be used for
- * other events.
- */
-#define CH_EVENT_TERMINATE ~(((eventmask_t)-1) >> 1U)
-
/*===========================================================================*/
/* Module pre-compile time settings. */
/*===========================================================================*/
diff --git a/os/rt/include/chthreads.h b/os/rt/include/chthreads.h
index 8d632d59e..d4f2a68d0 100644
--- a/os/rt/include/chthreads.h
+++ b/os/rt/include/chthreads.h
@@ -182,6 +182,7 @@ extern "C" {
tprio_t prio, tfunc_t pf, void *arg);
thread_t *chThdStart(thread_t *tp);
tprio_t chThdSetPriority(tprio_t newprio);
+ void chThdTerminate(thread_t *tp);
msg_t chThdSuspendS(thread_reference_t *trp);
msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout);
void chThdResumeI(thread_reference_t *trp, msg_t msg);
@@ -277,6 +278,19 @@ static inline bool chThdTerminatedX(thread_t *tp) {
}
/**
+ * @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()->flags & CH_FLAG_TERMINATE) != (tmode_t)0);
+}
+
+/**
* @brief Resumes a thread created with @p chThdCreateI().
*
* @param[in] tp pointer to the thread
diff --git a/os/rt/src/chdynamic.c b/os/rt/src/chdynamic.c
index 414cfe2c8..60b0cbe19 100644
--- a/os/rt/src/chdynamic.c
+++ b/os/rt/src/chdynamic.c
@@ -54,6 +54,78 @@
/* Module exported functions. */
/*===========================================================================*/
+/**
+ * @brief Adds a reference to a thread object.
+ * @pre The configuration option @p CH_CFG_USE_DYNAMIC must be enabled in
+ * order to use this function.
+ *
+ * @param[in] tp pointer to the thread
+ * @return The same thread pointer passed as parameter
+ * representing the new reference.
+ *
+ * @api
+ */
+thread_t *chThdAddRef(thread_t *tp) {
+
+ chSysLock();
+ chDbgAssert(tp->refs < (trefs_t)255, "too many references");
+ tp->refs++;
+ chSysUnlock();
+
+ return tp;
+}
+
+/**
+ * @brief Releases a reference to a thread object.
+ * @details If the references counter reaches zero <b>and</b> the thread
+ * is in the @p CH_STATE_FINAL state then the thread's memory is
+ * returned to the proper allocator.
+ * @pre The configuration option @p CH_CFG_USE_DYNAMIC must be enabled in
+ * order to use this function.
+ * @note Static threads are not affected.
+ *
+ * @param[in] tp pointer to the thread
+ *
+ * @api
+ */
+void chThdRelease(thread_t *tp) {
+ trefs_t refs;
+
+ chSysLock();
+ chDbgAssert(tp->refs > (trefs_t)0, "not referenced");
+ tp->refs--;
+ refs = tp->refs;
+ chSysUnlock();
+
+ /* If the references counter reaches zero and the thread is in its
+ terminated state then the memory can be returned to the proper
+ allocator. Of course static threads are not affected.*/
+ if ((refs == (trefs_t)0) && (tp->state == CH_STATE_FINAL)) {
+ switch (tp->flags & CH_FLAG_MODE_MASK) {
+#if CH_CFG_USE_HEAP == TRUE
+ case CH_FLAG_MODE_HEAP:
+#if CH_CFG_USE_REGISTRY == TRUE
+ REG_REMOVE(tp);
+#endif
+ chHeapFree(chthdGetStackLimitX(tp));
+ break;
+#endif
+#if CH_CFG_USE_MEMPOOLS == TRUE
+ case CH_FLAG_MODE_MPOOL:
+#if CH_CFG_USE_REGISTRY == TRUE
+ REG_REMOVE(tp);
+#endif
+ chPoolFree(tp->mpool, chthdGetStackLimitX(tp));
+ break;
+#endif
+ default:
+ /* Nothing to do for static threads, those are removed from the
+ registry on exit.*/
+ break;
+ }
+ }
+}
+
#if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__)
/**
* @brief Creates a new thread allocating the memory from the heap.
@@ -82,6 +154,7 @@
thread_t *chThdCreateFromHeap(memory_heap_t *heapp, size_t size,
const char *name, tprio_t prio,
tfunc_t pf, void *arg) {
+ thread_t *tp;
void *wsp;
wsp = chHeapAllocAligned(heapp, size, PORT_WORKING_AREA_ALIGN);
@@ -98,24 +171,19 @@ thread_t *chThdCreateFromHeap(memory_heap_t *heapp, size_t size,
arg
};
- return chThdCreate(&td);
-}
-
-/**
- * @brief Releases a thread working area into the owner heap.
- * @pre The thread must have been created using @p chThdCreateFromHeap().
- * @pre The thread must be in the state @p CH_STATE_FINAL (terminated).
- *
- * @param[in] tp the pointer to the thread
- *
- * @api
- */
-void chThdFreeToHeap(thread_t *tp) {
+#if CH_DBG_FILL_THREADS == TRUE
+ _thread_memfill((uint8_t *)wsp,
+ (uint8_t *)wsp + size,
+ CH_DBG_STACK_FILL_VALUE);
+#endif
- chDbgCheck(tp != NULL);
- chDbgAssert(tp->state == CH_STATE_FINAL, "not terminated");
+ chSysLock();
+ tp = chThdCreateSuspendedI(&td);
+ tp->flags = CH_FLAG_MODE_HEAP;
+ chSchWakeupS(tp, MSG_OK);
+ chSysUnlock();
- chHeapFree(chthdGetStackLimitX(tp));
+ return tp;
}
#endif /* CH_CFG_USE_HEAP == TRUE */
@@ -148,6 +216,7 @@ void chThdFreeToHeap(thread_t *tp) {
*/
thread_t *chThdCreateFromMemoryPool(memory_pool_t *mp, const char *name,
tprio_t prio, tfunc_t pf, void *arg) {
+ thread_t *tp;
void *wsp;
chDbgCheck(mp != NULL);
@@ -166,25 +235,21 @@ thread_t *chThdCreateFromMemoryPool(memory_pool_t *mp, const char *name,
arg
};
- return chThdCreate(&td);
-}
+#if CH_DBG_FILL_THREADS == TRUE
+ _thread_memfill((uint8_t *)wsp,
+ (uint8_t *)wsp + mp->object_size,
+ CH_DBG_STACK_FILL_VALUE);
+#endif
-/**
- * @brief Releases a thread working area into a memory pool.
- * @pre The thread must have been created using @p chThdCreateFromMemoryPool().
- * @pre The thread must be in the state @p CH_STATE_FINAL (terminated).
- *
- * @param[in] tp the pointer to the thread
- * @param[in] mp pointer to a @p memory_pool_t structure
- *
- * @api
- */
-void chThdFreeToMemoryPool(thread_t *tp, memory_pool_t *mp) {
- chDbgCheck((tp != NULL) && (mp != NULL));
- chDbgAssert(tp->state == CH_STATE_FINAL, "not terminated");
+ chSysLock();
+ tp = chThdCreateSuspendedI(&td);
+ tp->flags = CH_FLAG_MODE_MPOOL;
+ tp->mpool = mp;
+ chSchWakeupS(tp, MSG_OK);
+ chSysUnlock();
- chPoolFree(mp, (void *)chthdGetStackLimitX(tp));
+ return tp;
}
#endif /* CH_CFG_USE_MEMPOOLS == TRUE */
diff --git a/os/rt/src/chsys.c b/os/rt/src/chsys.c
index 18459849d..d9ca6d81a 100644
--- a/os/rt/src/chsys.c
+++ b/os/rt/src/chsys.c
@@ -129,7 +129,6 @@ void chSysInit(void) {
/* Setting up the caller as current thread.*/
currp->state = CH_STATE_CURRENT;
- currp->flags = CH_FLAG_MODE_STATIC;
/* Port layer initialization last because it depend on some of the
initializations performed before.*/
diff --git a/os/rt/src/chthreads.c b/os/rt/src/chthreads.c
index 0fff76604..ed5b19e15 100644
--- a/os/rt/src/chthreads.c
+++ b/os/rt/src/chthreads.c
@@ -87,22 +87,27 @@
*/
thread_t *_thread_init(thread_t *tp, const char *name, tprio_t prio) {
- tp->prio = prio;
+ tp->prio = prio;
+ tp->state = CH_STATE_WTSTART;
+ tp->flags = CH_FLAG_MODE_STATIC;
#if CH_CFG_TIME_QUANTUM > 0
- tp->preempt = (tslices_t)CH_CFG_TIME_QUANTUM;
+ tp->preempt = (tslices_t)CH_CFG_TIME_QUANTUM;
#endif
#if CH_CFG_USE_MUTEXES == TRUE
- tp->realprio = prio;
- tp->mtxlist = NULL;
+ tp->realprio = prio;
+ tp->mtxlist = NULL;
#endif
#if CH_CFG_USE_EVENTS == TRUE
- tp->epending = (eventmask_t)0;
+ tp->epending = (eventmask_t)0;
#endif
#if CH_DBG_THREADS_PROFILING == TRUE
- tp->time = (systime_t)0;
+ tp->time = (systime_t)0;
+#endif
+#if CH_CFG_USE_DYNAMIC == TRUE
+ tp->refs = (trefs_t)1;
#endif
#if CH_CFG_USE_REGISTRY == TRUE
- tp->name = name;
+ tp->name = name;
REG_INSERT(tp);
#endif
#if CH_CFG_USE_WAITEXIT == TRUE
@@ -173,10 +178,6 @@ thread_t *chThdCreateSuspendedI(const thread_descriptor_t *tdp) {
tp = (thread_t *)((uint8_t *)tdp->wend -
MEM_ALIGN_NEXT(sizeof (thread_t), PORT_STACK_ALIGN));
- /* Initial state.*/
- tp->state = CH_STATE_WTSTART;
- tp->flags = CH_FLAG_MODE_STATIC;
-
/* Stack boundary.*/
tp->stklimit = tdp->wbase;
@@ -380,6 +381,25 @@ tprio_t chThdSetPriority(tprio_t newprio) {
}
/**
+ * @brief Requests a thread termination.
+ * @pre The target thread must be written to invoke periodically
+ * @p chThdShouldTerminate() and terminate cleanly if it returns
+ * @p true.
+ * @post The specified thread will terminate after detecting the termination
+ * condition.
+ *
+ * @param[in] tp pointer to the thread
+ *
+ * @api
+ */
+void chThdTerminate(thread_t *tp) {
+
+ chSysLock();
+ tp->flags |= CH_FLAG_TERMINATE;
+ chSysUnlock();
+}
+
+/**
* @brief Suspends the invoking thread for the specified time.
*
* @param[in] time the delay in system ticks, the special values are
@@ -501,14 +521,20 @@ void chThdExitS(msg_t msg) {
thread_t *tp = currp;
tp->u.exitcode = msg;
+#if defined(CH_CFG_THREAD_EXIT_HOOK)
CH_CFG_THREAD_EXIT_HOOK(tp);
+#endif
#if CH_CFG_USE_WAITEXIT == TRUE
while (list_notempty(&tp->waiting)) {
(void) chSchReadyI(list_remove(&tp->waiting));
}
#endif
#if CH_CFG_USE_REGISTRY == TRUE
- REG_REMOVE(tp);
+ /* Static threads are immediately removed from the registry because
+ there is no memory to recover.*/
+ if ((tp->flags & CH_FLAG_MODE_MASK) == CH_FLAG_MODE_STATIC) {
+ REG_REMOVE(tp);
+ }
#endif
chSchGoSleepS(CH_STATE_FINAL);
@@ -520,12 +546,28 @@ void chThdExitS(msg_t msg) {
/**
* @brief Blocks the execution of the invoking thread until the specified
* thread terminates then the exit code is returned.
+ * @details This function waits for the specified thread to terminate then
+ * decrements its reference counter, if the counter reaches zero then
+ * the thread working area is returned to the proper allocator.<br>
+ * The memory used by the exited thread is handled in different ways
+ * depending on the API that spawned the thread:
+ * - If the thread was spawned by @p chThdCreateStatic() or by
+ * @p chThdCreateI() then nothing happens and the thread working
+ * area is not released or modified in any way. This is the
+ * default, totally static, behavior.
+ * - If the thread was spawned by @p chThdCreateFromHeap() then
+ * the working area is returned to the system heap.
+ * - If the thread was spawned by @p chThdCreateFromMemoryPool()
+ * then the working area is returned to the owning memory pool.
+ * .
* @pre The configuration option @p CH_CFG_USE_WAITEXIT must be enabled in
* order to use this function.
* @post Enabling @p chThdWait() requires 2-4 (depending on the
* architecture) extra bytes in the @p thread_t structure.
* @post After invoking @p chThdWait() the thread pointer becomes invalid
* and must not be used as parameter for further system calls.
+ * @note If @p CH_CFG_USE_DYNAMIC is not specified this function just waits for
+ * the thread termination, no memory allocators are involved.
*
* @param[in] tp pointer to the thread
* @return The exit code from the terminated thread.
@@ -539,6 +581,9 @@ msg_t chThdWait(thread_t *tp) {
chSysLock();
chDbgAssert(tp != currp, "waiting self");
+#if CH_CFG_USE_DYNAMIC == TRUE
+ chDbgAssert(tp->refs > (trefs_t)0, "not referenced");
+#endif
if (tp->state != CH_STATE_FINAL) {
list_insert(currp, &tp->waiting);
chSchGoSleepS(CH_STATE_WTEXIT);
@@ -546,6 +591,11 @@ msg_t chThdWait(thread_t *tp) {
msg = tp->u.exitcode;
chSysUnlock();
+#if CH_CFG_USE_DYNAMIC == TRUE
+ /* Releasing a lock if it is a dynamic thread.*/
+ chThdRelease(tp);
+#endif
+
return msg;
}
#endif /* CH_CFG_USE_WAITEXIT */