From 25ddb1c801f06a3be7171e20dcfd46d11a75f112 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Fri, 19 Jul 2013 14:51:35 +0000 Subject: First cleanup pass finished, queues and streams not yet removed. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@5999 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/kernel/src/chcond.c | 4 +- os/kernel/src/chdynamic.c | 4 +- os/kernel/src/chevents.c | 68 ++++++++++++++++---------------- os/kernel/src/chheap.c | 96 ++++++++++++++-------------------------------- os/kernel/src/chmboxes.c | 70 +++++++++++++++++++++------------ os/kernel/src/chmemcore.c | 22 +++++++++++ os/kernel/src/chmempools.c | 45 ++++++++++++++++------ os/kernel/src/chmsg.c | 20 ++++++++++ os/kernel/src/chmtx.c | 71 +++++++++++++++++++++++++--------- os/kernel/src/chregistry.c | 20 ++++++++++ os/kernel/src/chsem.c | 20 ++++++++++ os/kernel/src/chsys.c | 49 ++++++++++++++++++++++- 12 files changed, 327 insertions(+), 162 deletions(-) (limited to 'os/kernel/src') diff --git a/os/kernel/src/chcond.c b/os/kernel/src/chcond.c index 5903e062f..6b718c7f9 100644 --- a/os/kernel/src/chcond.c +++ b/os/kernel/src/chcond.c @@ -200,7 +200,7 @@ msg_t chCondWait(condition_variable_t *cp) { */ msg_t chCondWaitS(condition_variable_t *cp) { thread_t *ctp = currp; - Mutex *mp; + mutex_t *mp; msg_t msg; chDbgCheckClassS(); @@ -285,7 +285,7 @@ msg_t chCondWaitTimeout(condition_variable_t *cp, systime_t time) { * @sclass */ msg_t chCondWaitTimeoutS(condition_variable_t *cp, systime_t time) { - Mutex *mp; + mutex_t *mp; msg_t msg; chDbgCheckClassS(); diff --git a/os/kernel/src/chdynamic.c b/os/kernel/src/chdynamic.c index 76d4ba561..21c81fc24 100644 --- a/os/kernel/src/chdynamic.c +++ b/os/kernel/src/chdynamic.c @@ -144,7 +144,7 @@ void chThdRelease(thread_t *tp) { * * @api */ -thread_t *chThdCreateFromHeap(MemoryHeap *heapp, size_t size, +thread_t *chThdCreateFromHeap(memory_heap_t *heapp, size_t size, tprio_t prio, tfunc_t pf, void *arg) { void *wsp; thread_t *tp; @@ -193,7 +193,7 @@ thread_t *chThdCreateFromHeap(MemoryHeap *heapp, size_t size, * * @api */ -thread_t *chThdCreateFromMemoryPool(MemoryPool *mp, tprio_t prio, +thread_t *chThdCreateFromMemoryPool(memory_pool_t *mp, tprio_t prio, tfunc_t pf, void *arg) { void *wsp; thread_t *tp; diff --git a/os/kernel/src/chevents.c b/os/kernel/src/chevents.c index 5685f285f..1206b61fe 100644 --- a/os/kernel/src/chevents.c +++ b/os/kernel/src/chevents.c @@ -93,14 +93,16 @@ * @note Multiple Event Listeners can specify the same bits to be ORed to * different threads. * - * @param[in] esp pointer to the @p EventSource structure - * @param[in] elp pointer to the @p EventListener structure + * @param[in] esp pointer to the @p event_source_t structure + * @param[in] elp pointer to the @p event_listener_t structure * @param[in] mask the mask of event flags to be ORed to the thread when * the event source is broadcasted * * @api */ -void chEvtRegisterMask(EventSource *esp, EventListener *elp, eventmask_t mask) { +void chEvtRegisterMask(event_source_t *esp, + event_listener_t *elp, + eventmask_t mask) { chDbgCheck((esp != NULL) && (elp != NULL), "chEvtRegisterMask"); @@ -121,19 +123,19 @@ void chEvtRegisterMask(EventSource *esp, EventListener *elp, eventmask_t mask) { * operations in inverse order of the register operations (elements * are found on top of the list). * - * @param[in] esp pointer to the @p EventSource structure - * @param[in] elp pointer to the @p EventListener structure + * @param[in] esp pointer to the @p event_source_t structure + * @param[in] elp pointer to the @p event_listener_t structure * * @api */ -void chEvtUnregister(EventSource *esp, EventListener *elp) { - EventListener *p; +void chEvtUnregister(event_source_t *esp, event_listener_t *elp) { + event_listener_t *p; chDbgCheck((esp != NULL) && (elp != NULL), "chEvtUnregister"); - p = (EventListener *)esp; + p = (event_listener_t *)esp; chSysLock(); - while (p->el_next != (EventListener *)esp) { + while (p->el_next != (event_listener_t *)esp) { if (p->el_next == elp) { p->el_next = elp->el_next; break; @@ -186,27 +188,27 @@ eventmask_t chEvtAddEvents(eventmask_t mask) { * @brief Signals all the Event Listeners registered on the specified Event * Source. * @details This function variants ORs the specified event flags to all the - * threads registered on the @p EventSource in addition to the event - * flags specified by the threads themselves in the - * @p EventListener objects. + * threads registered on the @p event_source_t in addition to the + * event flags specified by the threads themselves in the + * @p event_listener_t objects. * @post This function does not reschedule so a call to a rescheduling * function must be performed before unlocking the kernel. Note that * interrupt handlers always reschedule on exit so an explicit * reschedule must not be performed in ISRs. * - * @param[in] esp pointer to the @p EventSource structure + * @param[in] esp pointer to the @p event_source_t structure * @param[in] flags the flags set to be added to the listener flags mask * * @iclass */ -void chEvtBroadcastFlagsI(EventSource *esp, flagsmask_t flags) { - EventListener *elp; +void chEvtBroadcastFlagsI(event_source_t *esp, eventflags_t flags) { + event_listener_t *elp; chDbgCheckClassI(); chDbgCheck(esp != NULL, "chEvtBroadcastMaskI"); elp = esp->es_next; - while (elp != (EventListener *)esp) { + while (elp != (event_listener_t *)esp) { elp->el_flags |= flags; chEvtSignalI(elp->el_listener, elp->el_mask); elp = elp->el_next; @@ -214,18 +216,18 @@ void chEvtBroadcastFlagsI(EventSource *esp, flagsmask_t flags) { } /** - * @brief Returns the flags associated to an @p EventListener. - * @details The flags are returned and the @p EventListener flags mask is + * @brief Returns the flags associated to an @p event_listener_t. + * @details The flags are returned and the @p event_listener_t flags mask is * cleared. * - * @param[in] elp pointer to the @p EventListener structure + * @param[in] elp pointer to the @p event_listener_t structure * @return The flags added to the listener by the associated * event source. * * @iclass */ -flagsmask_t chEvtGetAndClearFlags(EventListener *elp) { - flagsmask_t flags; +eventflags_t chEvtGetAndClearFlags(event_listener_t *elp) { + eventflags_t flags; chSysLock(); @@ -284,16 +286,16 @@ void chEvtSignalI(thread_t *tp, eventmask_t mask) { * @brief Signals all the Event Listeners registered on the specified Event * Source. * @details This function variants ORs the specified event flags to all the - * threads registered on the @p EventSource in addition to the event - * flags specified by the threads themselves in the - * @p EventListener objects. + * threads registered on the @p event_source_t in addition to the + * event flags specified by the threads themselves in the + * @p event_listener_t objects. * - * @param[in] esp pointer to the @p EventSource structure + * @param[in] esp pointer to the @p event_source_t structure * @param[in] flags the flags set to be added to the listener flags mask * * @api */ -void chEvtBroadcastFlags(EventSource *esp, flagsmask_t flags) { +void chEvtBroadcastFlags(event_source_t *esp, eventflags_t flags) { chSysLock(); chEvtBroadcastFlagsI(esp, flags); @@ -302,18 +304,18 @@ void chEvtBroadcastFlags(EventSource *esp, flagsmask_t flags) { } /** - * @brief Returns the flags associated to an @p EventListener. - * @details The flags are returned and the @p EventListener flags mask is + * @brief Returns the flags associated to an @p event_listener_t. + * @details The flags are returned and the @p event_listener_t flags mask is * cleared. * - * @param[in] elp pointer to the @p EventListener structure + * @param[in] elp pointer to the @p event_listener_t structure * @return The flags added to the listener by the associated * event source. * * @iclass */ -flagsmask_t chEvtGetAndClearFlagsI(EventListener *elp) { - flagsmask_t flags; +eventflags_t chEvtGetAndClearFlagsI(event_listener_t *elp) { + eventflags_t flags; flags = elp->el_flags; elp->el_flags = 0; @@ -444,8 +446,8 @@ eventmask_t chEvtWaitAll(eventmask_t mask) { * @details The function waits for one event among those specified in * @p mask to become pending then the event is cleared and returned. * @note One and only one event is served in the function, the one with the - * lowest event id. The function is meant to be invoked into a loop in - * order to serve all the pending events.
+ * lowest event id. The function is meant to be invoked into a loop + * in order to serve all the pending events.
* This means that Event Listeners with a lower event identifier have * an higher priority. * diff --git a/os/kernel/src/chheap.c b/os/kernel/src/chheap.c index 23bcff6b4..4f20d3920 100644 --- a/os/kernel/src/chheap.c +++ b/os/kernel/src/chheap.c @@ -29,10 +29,6 @@ * are functionally equivalent to the usual @p malloc() and @p free() * library functions. The main difference is that the OS heap APIs * are guaranteed to be thread safe.
- * By enabling the @p CH_USE_MALLOC_HEAP option the heap manager - * will use the runtime-provided @p malloc() and @p free() as - * back end for the heap APIs instead of the system provided - * allocator. * @pre In order to use the heap APIs the @p CH_USE_HEAP option must * be enabled in @p chconf.h. * @{ @@ -42,7 +38,9 @@ #if CH_USE_HEAP || defined(__DOXYGEN__) -#if !CH_USE_MALLOC_HEAP || defined(__DOXYGEN__) +/*===========================================================================*/ +/* Module local definitions. */ +/*===========================================================================*/ /* * Defaults on the best synchronization mechanism available. @@ -55,10 +53,30 @@ #define H_UNLOCK(h) chSemSignal(&(h)->h_sem) #endif +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + /** * @brief Default heap descriptor. */ -static MemoryHeap default_heap; +static memory_heap_t default_heap; + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ /** * @brief Initializes the default heap. @@ -80,8 +98,6 @@ void _heap_init(void) { * @brief Initializes a memory heap from a static memory area. * @pre Both the heap buffer base and the heap size must be aligned to * the @p stkalign_t type size. - * @pre In order to use this function the option @p CH_USE_MALLOC_HEAP - * must be disabled. * * @param[out] heapp pointer to the memory heap descriptor to be initialized * @param[in] buf heap buffer base @@ -89,7 +105,7 @@ void _heap_init(void) { * * @init */ -void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { +void chHeapInit(memory_heap_t *heapp, void *buf, size_t size) { union heap_header *hp; chDbgCheck(MEM_IS_ALIGNED(buf) && MEM_IS_ALIGNED(size), "chHeapInit"); @@ -122,7 +138,7 @@ void chHeapInit(MemoryHeap *heapp, void *buf, size_t size) { * * @api */ -void *chHeapAlloc(MemoryHeap *heapp, size_t size) { +void *chHeapAlloc(memory_heap_t *heapp, size_t size) { union heap_header *qp, *hp, *fp; if (heapp == NULL) @@ -186,7 +202,7 @@ void *chHeapAlloc(MemoryHeap *heapp, size_t size) { */ void chHeapFree(void *p) { union heap_header *qp, *hp; - MemoryHeap *heapp; + memory_heap_t *heapp; chDbgCheck(p != NULL, "chHeapFree"); @@ -229,8 +245,6 @@ void chHeapFree(void *p) { * @brief Reports the heap status. * @note This function is meant to be used in the test suite, it should * not be really useful for the application code. - * @note This function is not implemented when the @p CH_USE_MALLOC_HEAP - * configuration option is used (it always returns zero). * * @param[in] heapp pointer to a heap descriptor or @p NULL in order to * access the default heap. @@ -240,7 +254,7 @@ void chHeapFree(void *p) { * * @api */ -size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { +size_t chHeapStatus(memory_heap_t *heapp, size_t *sizep) { union heap_header *qp; size_t n, sz; @@ -259,60 +273,6 @@ size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { return n; } -#else /* CH_USE_MALLOC_HEAP */ - -#include - -#if CH_USE_MUTEXES -#define H_LOCK() chMtxLock(&hmtx) -#define H_UNLOCK() chMtxUnlock() -static Mutex hmtx; -#elif CH_USE_SEMAPHORES -#define H_LOCK() chSemWait(&hsem) -#define H_UNLOCK() chSemSignal(&hsem) -static Semaphore hsem; -#endif - -void _heap_init(void) { - -#if CH_USE_MUTEXES - chMtxInit(&hmtx); -#else - chSemInit(&hsem, 1); -#endif -} - -void *chHeapAlloc(MemoryHeap *heapp, size_t size) { - void *p; - - chDbgCheck(heapp == NULL, "chHeapAlloc"); - - H_LOCK(); - p = malloc(size); - H_UNLOCK(); - return p; -} - -void chHeapFree(void *p) { - - chDbgCheck(p != NULL, "chHeapFree"); - - H_LOCK(); - free(p); - H_UNLOCK(); -} - -size_t chHeapStatus(MemoryHeap *heapp, size_t *sizep) { - - chDbgCheck(heapp == NULL, "chHeapStatus"); - - if (sizep) - *sizep = 0; - return 0; -} - -#endif /* CH_USE_MALLOC_HEAP */ - #endif /* CH_USE_HEAP */ /** @} */ diff --git a/os/kernel/src/chmboxes.c b/os/kernel/src/chmboxes.c index 6f09a8d26..16f303daf 100644 --- a/os/kernel/src/chmboxes.c +++ b/os/kernel/src/chmboxes.c @@ -53,16 +53,38 @@ #include "ch.h" #if CH_USE_MAILBOXES || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /** - * @brief Initializes a Mailbox object. + * @brief Initializes a @p mailbox_t object. * - * @param[out] mbp the pointer to the Mailbox structure to be initialized + * @param[out] mbp the pointer to the @p mailbox_t structure to be + * initialized * @param[in] buf pointer to the messages buffer as an array of @p msg_t * @param[in] n number of elements in the buffer array * * @init */ -void chMBInit(Mailbox *mbp, msg_t *buf, cnt_t n) { +void chMBInit(mailbox_t *mbp, msg_t *buf, cnt_t n) { chDbgCheck((mbp != NULL) && (buf != NULL) && (n > 0), "chMBInit"); @@ -73,15 +95,15 @@ void chMBInit(Mailbox *mbp, msg_t *buf, cnt_t n) { } /** - * @brief Resets a Mailbox object. + * @brief Resets a @p mailbox_t object. * @details All the waiting threads are resumed with status @p RDY_RESET and * the queued messages are lost. * - * @param[in] mbp the pointer to an initialized Mailbox object + * @param[in] mbp the pointer to an initialized @p mailbox_t object * * @api */ -void chMBReset(Mailbox *mbp) { +void chMBReset(mailbox_t *mbp) { chDbgCheck(mbp != NULL, "chMBReset"); @@ -98,7 +120,7 @@ void chMBReset(Mailbox *mbp) { * @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 Mailbox object + * @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] time the number of ticks before the operation timeouts, * the following special values are allowed: @@ -112,7 +134,7 @@ void chMBReset(Mailbox *mbp) { * * @api */ -msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t time) { +msg_t chMBPost(mailbox_t *mbp, msg_t msg, systime_t time) { msg_t rdymsg; chSysLock(); @@ -126,7 +148,7 @@ msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t time) { * @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 Mailbox object + * @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] time the number of ticks before the operation timeouts, * the following special values are allowed: @@ -140,7 +162,7 @@ msg_t chMBPost(Mailbox *mbp, msg_t msg, systime_t time) { * * @sclass */ -msg_t chMBPostS(Mailbox *mbp, msg_t msg, systime_t time) { +msg_t chMBPostS(mailbox_t *mbp, msg_t msg, systime_t time) { msg_t rdymsg; chDbgCheckClassS(); @@ -162,7 +184,7 @@ msg_t chMBPostS(Mailbox *mbp, msg_t msg, systime_t time) { * @details This variant is non-blocking, the function returns a timeout * condition if the queue is full. * - * @param[in] mbp the pointer to an initialized Mailbox object + * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @return The operation status. * @retval RDY_OK if a message has been correctly posted. @@ -171,7 +193,7 @@ msg_t chMBPostS(Mailbox *mbp, msg_t msg, systime_t time) { * * @iclass */ -msg_t chMBPostI(Mailbox *mbp, msg_t msg) { +msg_t chMBPostI(mailbox_t *mbp, msg_t msg) { chDbgCheckClassI(); chDbgCheck(mbp != NULL, "chMBPostI"); @@ -191,7 +213,7 @@ msg_t chMBPostI(Mailbox *mbp, msg_t msg) { * @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 Mailbox object + * @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] time the number of ticks before the operation timeouts, * the following special values are allowed: @@ -205,7 +227,7 @@ msg_t chMBPostI(Mailbox *mbp, msg_t msg) { * * @api */ -msg_t chMBPostAhead(Mailbox *mbp, msg_t msg, systime_t time) { +msg_t chMBPostAhead(mailbox_t *mbp, msg_t msg, systime_t time) { msg_t rdymsg; chSysLock(); @@ -219,7 +241,7 @@ msg_t chMBPostAhead(Mailbox *mbp, msg_t msg, systime_t time) { * @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 Mailbox object + * @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] time the number of ticks before the operation timeouts, * the following special values are allowed: @@ -233,7 +255,7 @@ msg_t chMBPostAhead(Mailbox *mbp, msg_t msg, systime_t time) { * * @sclass */ -msg_t chMBPostAheadS(Mailbox *mbp, msg_t msg, systime_t time) { +msg_t chMBPostAheadS(mailbox_t *mbp, msg_t msg, systime_t time) { msg_t rdymsg; chDbgCheckClassS(); @@ -255,7 +277,7 @@ msg_t chMBPostAheadS(Mailbox *mbp, msg_t msg, systime_t time) { * @details This variant is non-blocking, the function returns a timeout * condition if the queue is full. * - * @param[in] mbp the pointer to an initialized Mailbox object + * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @return The operation status. * @retval RDY_OK if a message has been correctly posted. @@ -264,7 +286,7 @@ msg_t chMBPostAheadS(Mailbox *mbp, msg_t msg, systime_t time) { * * @iclass */ -msg_t chMBPostAheadI(Mailbox *mbp, msg_t msg) { +msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg) { chDbgCheckClassI(); chDbgCheck(mbp != NULL, "chMBPostAheadI"); @@ -284,7 +306,7 @@ msg_t chMBPostAheadI(Mailbox *mbp, msg_t msg) { * @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 Mailbox object + * @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] time the number of ticks before the operation timeouts, * the following special values are allowed: @@ -298,7 +320,7 @@ msg_t chMBPostAheadI(Mailbox *mbp, msg_t msg) { * * @api */ -msg_t chMBFetch(Mailbox *mbp, msg_t *msgp, systime_t time) { +msg_t chMBFetch(mailbox_t *mbp, msg_t *msgp, systime_t time) { msg_t rdymsg; chSysLock(); @@ -312,7 +334,7 @@ msg_t chMBFetch(Mailbox *mbp, msg_t *msgp, systime_t time) { * @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 Mailbox object + * @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] time the number of ticks before the operation timeouts, * the following special values are allowed: @@ -326,7 +348,7 @@ msg_t chMBFetch(Mailbox *mbp, msg_t *msgp, systime_t time) { * * @sclass */ -msg_t chMBFetchS(Mailbox *mbp, msg_t *msgp, systime_t time) { +msg_t chMBFetchS(mailbox_t *mbp, msg_t *msgp, systime_t time) { msg_t rdymsg; chDbgCheckClassS(); @@ -348,7 +370,7 @@ msg_t chMBFetchS(Mailbox *mbp, msg_t *msgp, systime_t time) { * @details This variant is non-blocking, the function returns a timeout * condition if the queue is empty. * - * @param[in] mbp the pointer to an initialized Mailbox object + * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[out] msgp pointer to a message variable for the received message * @return The operation status. * @retval RDY_OK if a message has been correctly fetched. @@ -357,7 +379,7 @@ msg_t chMBFetchS(Mailbox *mbp, msg_t *msgp, systime_t time) { * * @iclass */ -msg_t chMBFetchI(Mailbox *mbp, msg_t *msgp) { +msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp) { chDbgCheckClassI(); chDbgCheck((mbp != NULL) && (msgp != NULL), "chMBFetchI"); diff --git a/os/kernel/src/chmemcore.c b/os/kernel/src/chmemcore.c index 0ff26bc76..76f415743 100644 --- a/os/kernel/src/chmemcore.c +++ b/os/kernel/src/chmemcore.c @@ -48,9 +48,29 @@ #if CH_USE_MEMCORE || defined(__DOXYGEN__) +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + static uint8_t *nextmem; static uint8_t *endmem; +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /** * @brief Low level memory manager initialization. * @@ -60,10 +80,12 @@ void _core_init(void) { #if CH_MEMCORE_SIZE == 0 extern uint8_t __heap_base__[]; extern uint8_t __heap_end__[]; + nextmem = (uint8_t *)MEM_ALIGN_NEXT(__heap_base__); endmem = (uint8_t *)MEM_ALIGN_PREV(__heap_end__); #else static stkalign_t buffer[MEM_ALIGN_NEXT(CH_MEMCORE_SIZE)/MEM_ALIGN_SIZE]; + nextmem = (uint8_t *)&buffer[0]; endmem = (uint8_t *)&buffer[MEM_ALIGN_NEXT(CH_MEMCORE_SIZE)/MEM_ALIGN_SIZE]; #endif diff --git a/os/kernel/src/chmempools.c b/os/kernel/src/chmempools.c index 44faa057a..b52f0b6d9 100644 --- a/os/kernel/src/chmempools.c +++ b/os/kernel/src/chmempools.c @@ -39,10 +39,31 @@ #include "ch.h" #if CH_USE_MEMPOOLS || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /** * @brief Initializes an empty memory pool. * - * @param[out] mp pointer to a @p MemoryPool structure + * @param[out] mp pointer to a @p memory_pool_t structure * @param[in] size the size of the objects contained in this memory pool, * the minimum accepted size is the size of a pointer to * void. @@ -52,7 +73,7 @@ * * @init */ -void chPoolInit(MemoryPool *mp, size_t size, memgetfunc_t provider) { +void chPoolInit(memory_pool_t *mp, size_t size, memgetfunc_t provider) { chDbgCheck((mp != NULL) && (size >= sizeof(void *)), "chPoolInit"); @@ -68,13 +89,13 @@ void chPoolInit(MemoryPool *mp, size_t size, memgetfunc_t provider) { * memory pool. * @post The memory pool contains the elements of the input array. * - * @param[in] mp pointer to a @p MemoryPool structure + * @param[in] mp pointer to a @p memory_pool_t structure * @param[in] p pointer to the array first element * @param[in] n number of elements in the array * * @api */ -void chPoolLoadArray(MemoryPool *mp, void *p, size_t n) { +void chPoolLoadArray(memory_pool_t *mp, void *p, size_t n) { chDbgCheck((mp != NULL) && (n != 0), "chPoolLoadArray"); @@ -89,13 +110,13 @@ void chPoolLoadArray(MemoryPool *mp, void *p, size_t n) { * @brief Allocates an object from a memory pool. * @pre The memory pool must be already been initialized. * - * @param[in] mp pointer to a @p MemoryPool structure + * @param[in] mp pointer to a @p memory_pool_t structure * @return The pointer to the allocated object. * @retval NULL if pool is empty. * * @iclass */ -void *chPoolAllocI(MemoryPool *mp) { +void *chPoolAllocI(memory_pool_t *mp) { void *objp; chDbgCheckClassI(); @@ -112,13 +133,13 @@ void *chPoolAllocI(MemoryPool *mp) { * @brief Allocates an object from a memory pool. * @pre The memory pool must be already been initialized. * - * @param[in] mp pointer to a @p MemoryPool structure + * @param[in] mp pointer to a @p memory_pool_t structure * @return The pointer to the allocated object. * @retval NULL if pool is empty. * * @api */ -void *chPoolAlloc(MemoryPool *mp) { +void *chPoolAlloc(memory_pool_t *mp) { void *objp; chSysLock(); @@ -134,12 +155,12 @@ void *chPoolAlloc(MemoryPool *mp) { * memory pool. * @pre The object must be properly aligned to contain a pointer to void. * - * @param[in] mp pointer to a @p MemoryPool structure + * @param[in] mp pointer to a @p memory_pool_t structure * @param[in] objp the pointer to the object to be released * * @iclass */ -void chPoolFreeI(MemoryPool *mp, void *objp) { +void chPoolFreeI(memory_pool_t *mp, void *objp) { struct pool_header *php = objp; chDbgCheckClassI(); @@ -156,12 +177,12 @@ void chPoolFreeI(MemoryPool *mp, void *objp) { * memory pool. * @pre The object must be properly aligned to contain a pointer to void. * - * @param[in] mp pointer to a @p MemoryPool structure + * @param[in] mp pointer to a @p memory_pool_t structure * @param[in] objp the pointer to the object to be released * * @api */ -void chPoolFree(MemoryPool *mp, void *objp) { +void chPoolFree(memory_pool_t *mp, void *objp) { chSysLock(); chPoolFreeI(mp, objp); diff --git a/os/kernel/src/chmsg.c b/os/kernel/src/chmsg.c index 50e489915..b53f91979 100644 --- a/os/kernel/src/chmsg.c +++ b/os/kernel/src/chmsg.c @@ -48,12 +48,32 @@ #if CH_USE_MESSAGES || defined(__DOXYGEN__) +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + #if CH_USE_MESSAGES_PRIORITY #define msg_insert(tp, qp) prio_insert(tp, qp) #else #define msg_insert(tp, qp) queue_insert(tp, qp) #endif +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /** * @brief Sends a message to the specified thread. * @details The sender is stopped until the receiver executes a diff --git a/os/kernel/src/chmtx.c b/os/kernel/src/chmtx.c index f70dd9b20..84e868a3f 100644 --- a/os/kernel/src/chmtx.c +++ b/os/kernel/src/chmtx.c @@ -69,14 +69,34 @@ #if CH_USE_MUTEXES || defined(__DOXYGEN__) +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /** - * @brief Initializes s @p Mutex structure. + * @brief Initializes s @p mutex_t structure. * - * @param[out] mp pointer to a @p Mutex structure + * @param[out] mp pointer to a @p mutex_t structure * * @init */ -void chMtxInit(Mutex *mp) { +void chMtxInit(mutex_t *mp) { chDbgCheck(mp != NULL, "chMtxInit"); @@ -89,11 +109,11 @@ void chMtxInit(Mutex *mp) { * @post The mutex is locked and inserted in the per-thread stack of owned * mutexes. * - * @param[in] mp pointer to the @p Mutex structure + * @param[in] mp pointer to the @p mutex_t structure * * @api */ -void chMtxLock(Mutex *mp) { +void chMtxLock(mutex_t *mp) { chSysLock(); @@ -107,11 +127,11 @@ void chMtxLock(Mutex *mp) { * @post The mutex is locked and inserted in the per-thread stack of owned * mutexes. * - * @param[in] mp pointer to the @p Mutex structure + * @param[in] mp pointer to the @p mutex_t structure * * @sclass */ -void chMtxLockS(Mutex *mp) { +void chMtxLockS(mutex_t *mp) { thread_t *ctp = currp; chDbgCheckClassS(); @@ -123,18 +143,20 @@ void chMtxLockS(Mutex *mp) { boosting the priority of all the affected threads to equal the priority of the running thread requesting the mutex.*/ thread_t *tp = mp->m_owner; + /* Does the running thread have higher priority than the mutex owning thread? */ while (tp->p_prio < ctp->p_prio) { /* Make priority of thread tp match the running thread's priority.*/ tp->p_prio = ctp->p_prio; + /* The following states need priority queues reordering.*/ switch (tp->p_state) { case THD_STATE_WTMTX: /* Re-enqueues the mutex owner with its new priority.*/ queue_prio_insert(queue_dequeue(tp), (threads_queue_t *)tp->p_u.wtobjp); - tp = ((Mutex *)tp->p_u.wtobjp)->m_owner; + tp = ((mutex_t *)tp->p_u.wtobjp)->m_owner; continue; #if CH_USE_CONDVARS | \ (CH_USE_SEMAPHORES && CH_USE_SEMAPHORES_PRIORITY) | \ @@ -164,10 +186,12 @@ void chMtxLockS(Mutex *mp) { } break; } + /* Sleep on the mutex.*/ queue_prio_insert(ctp, &mp->m_queue); ctp->p_u.wtobjp = mp; chSchGoSleepS(THD_STATE_WTMTX); + /* It is assumed that the thread performing the unlock operation assigns the mutex to this thread.*/ chDbgAssert(mp->m_owner == ctp, "chMtxLockS(), #1", "not owner"); @@ -191,14 +215,14 @@ void chMtxLockS(Mutex *mp) { * priority inheritance mechanism because it does not try to * enter a sleep state. * - * @param[in] mp pointer to the @p Mutex structure + * @param[in] mp pointer to the @p mutex_t structure * @return The operation status. * @retval TRUE if the mutex has been successfully acquired * @retval FALSE if the lock attempt failed. * * @api */ -bool_t chMtxTryLock(Mutex *mp) { +bool_t chMtxTryLock(mutex_t *mp) { bool_t b; chSysLock(); @@ -219,20 +243,21 @@ bool_t chMtxTryLock(Mutex *mp) { * priority inheritance mechanism because it does not try to * enter a sleep state. * - * @param[in] mp pointer to the @p Mutex structure + * @param[in] mp pointer to the @p mutex_t structure * @return The operation status. * @retval TRUE if the mutex has been successfully acquired * @retval FALSE if the lock attempt failed. * * @sclass */ -bool_t chMtxTryLockS(Mutex *mp) { +bool_t chMtxTryLockS(mutex_t *mp) { chDbgCheckClassS(); chDbgCheck(mp != NULL, "chMtxTryLockS"); if (mp->m_owner != NULL) return FALSE; + mp->m_owner = currp; mp->m_next = currp->p_mtxlist; currp->p_mtxlist = mp; @@ -249,21 +274,24 @@ bool_t chMtxTryLockS(Mutex *mp) { * * @api */ -Mutex *chMtxUnlock(void) { +mutex_t *chMtxUnlock(void) { thread_t *ctp = currp; - Mutex *ump, *mp; + mutex_t *ump, *mp; chSysLock(); + chDbgAssert(ctp->p_mtxlist != NULL, "chMtxUnlock(), #1", "owned mutexes list empty"); chDbgAssert(ctp->p_mtxlist->m_owner == ctp, "chMtxUnlock(), #2", "ownership failure"); - /* Removes the top Mutex from the thread's owned mutexes list and marks it + + /* Removes the top mutex from the thread's owned mutexes list and marks it as not owned.*/ ump = ctp->p_mtxlist; ctp->p_mtxlist = ump->m_next; + /* If a thread is waiting on the mutex then the fun part begins.*/ if (chMtxQueueNotEmptyS(ump)) { thread_t *tp; @@ -280,9 +308,11 @@ Mutex *chMtxUnlock(void) { newprio = mp->m_queue.p_next->p_prio; mp = mp->m_next; } + /* Assigns to the current thread the highest priority among all the waiting threads.*/ ctp->p_prio = newprio; + /* Awakens the highest priority thread waiting for the unlocked mutex and assigns the mutex to it.*/ tp = queue_fifo_remove(&ump->m_queue); @@ -309,9 +339,9 @@ Mutex *chMtxUnlock(void) { * * @sclass */ -Mutex *chMtxUnlockS(void) { +mutex_t *chMtxUnlockS(void) { thread_t *ctp = currp; - Mutex *ump, *mp; + mutex_t *ump, *mp; chDbgCheckClassS(); chDbgAssert(ctp->p_mtxlist != NULL, @@ -321,10 +351,11 @@ Mutex *chMtxUnlockS(void) { "chMtxUnlockS(), #2", "ownership failure"); - /* Removes the top Mutex from the owned mutexes list and marks it as not + /* Removes the top mutex from the owned mutexes list and marks it as not owned.*/ ump = ctp->p_mtxlist; ctp->p_mtxlist = ump->m_next; + /* If a thread is waiting on the mutex then the fun part begins.*/ if (chMtxQueueNotEmptyS(ump)) { thread_t *tp; @@ -339,9 +370,11 @@ Mutex *chMtxUnlockS(void) { priority will have at least that priority.*/ if (chMtxQueueNotEmptyS(mp) && (mp->m_queue.p_next->p_prio > newprio)) newprio = mp->m_queue.p_next->p_prio; + mp = mp->m_next; } ctp->p_prio = newprio; + /* Awakens the highest priority thread waiting for the unlocked mutex and assigns the mutex to it.*/ tp = queue_fifo_remove(&ump->m_queue); @@ -372,7 +405,7 @@ void chMtxUnlockAll(void) { chSysLock(); if (ctp->p_mtxlist != NULL) { do { - Mutex *ump = ctp->p_mtxlist; + mutex_t *ump = ctp->p_mtxlist; ctp->p_mtxlist = ump->m_next; if (chMtxQueueNotEmptyS(ump)) { thread_t *tp = queue_fifo_remove(&ump->m_queue); diff --git a/os/kernel/src/chregistry.c b/os/kernel/src/chregistry.c index d567393c1..832ef0ce9 100644 --- a/os/kernel/src/chregistry.c +++ b/os/kernel/src/chregistry.c @@ -50,9 +50,29 @@ #if CH_USE_REGISTRY || defined(__DOXYGEN__) +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + #define _offsetof(st, m) \ ((size_t)((char *)&((st *)0)->m - (char *)0)) +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /* * OS signature in ROM plus debug-related information. */ diff --git a/os/kernel/src/chsem.c b/os/kernel/src/chsem.c index 73d156c02..8ddba8523 100644 --- a/os/kernel/src/chsem.c +++ b/os/kernel/src/chsem.c @@ -61,12 +61,32 @@ #if CH_USE_SEMAPHORES || defined(__DOXYGEN__) +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + #if CH_USE_SEMAPHORES_PRIORITY #define sem_insert(tp, qp) prio_insert(tp, qp) #else #define sem_insert(tp, qp) queue_insert(tp, qp) #endif +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /** * @brief Initializes a semaphore with the specified counter value. * diff --git a/os/kernel/src/chsys.c b/os/kernel/src/chsys.c index f960d9b79..578de3ccf 100644 --- a/os/kernel/src/chsys.c +++ b/os/kernel/src/chsys.c @@ -35,12 +35,30 @@ #include "ch.h" +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + #if !CH_NO_IDLE_THREAD || defined(__DOXYGEN__) /** * @brief Idle thread working area. */ -WORKING_AREA(_idle_thread_wa, PORT_IDLE_THREAD_STACK_SIZE); +static WORKING_AREA(_idle_thread_wa, PORT_IDLE_THREAD_STACK_SIZE); +#endif /* CH_NO_IDLE_THREAD */ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ +#if !CH_NO_IDLE_THREAD || defined(__DOXYGEN__) /** * @brief This function implements the idle thread infinite loop. * @details The function puts the processor in the lowest power mode capable @@ -51,7 +69,7 @@ WORKING_AREA(_idle_thread_wa, PORT_IDLE_THREAD_STACK_SIZE); * * @param[in] p the thread parameter, unused in this scenario */ -void _idle_thread(void *p) { +static void _idle_thread(void *p) { (void)p; chRegSetThreadName("idle"); @@ -62,6 +80,10 @@ void _idle_thread(void *p) { } #endif /* CH_NO_IDLE_THREAD */ +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + /** * @brief ChibiOS/RT initialization. * @details After executing this function the current instructions stream @@ -116,6 +138,29 @@ void chSysInit(void) { #endif } +/** + * @brief Halts the system. + * @details This function is invoked by the operating system when an + * unrecoverable error is detected, for example because a programming + * error in the application code that triggers an assertion while + * in debug mode. + * @note Can be invoked from any system state. + * + * @special + */ +void chSysHalt(void) { + + chSysDisable(); + +#if defined(SYSTEM_HALT_HOOK) || defined(__DOXYGEN__) + SYSTEM_HALT_HOOK(); +#endif + + /* Harmless infinite loop.*/ + while (true) + ; +} + /** * @brief Handles time ticks for round robin preemption and timer increments. * @details Decrements the remaining time quantum of the running thread -- cgit v1.2.3