From 2706d240bbea25b21dac8e0bc123de9cb2080a13 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 16 May 2009 10:57:16 +0000 Subject: [PATCH] Added static initializers to thread queues, semaphores, mutexes, condvars and event sources. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@975 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- readme.txt | 6 ++--- src/include/condvars.h | 15 ++++++++++++ src/include/events.h | 16 +++++++++++++ src/include/lists.h | 50 ++++++++++++++++++++++------------------ src/include/mutexes.h | 16 +++++++++++++ src/include/semaphores.h | 20 +++++++++++++++- test/testevt.c | 8 ++++++- test/testmtx.c | 10 ++++++-- test/testsem.c | 7 +++++- todo.txt | 4 ++-- 10 files changed, 120 insertions(+), 32 deletions(-) diff --git a/readme.txt b/readme.txt index 9baa121b5..596122629 100644 --- a/readme.txt +++ b/readme.txt @@ -84,16 +84,16 @@ Win32-MinGW - ChibiOS/RT simulator and demo into a WIN32 process, - NEW: Abstract I/O Channels mechanism introduced. This mechanism allows to access I/O resources through a standard interface and hides implementation details. The existing serial drivers were modified to offer a standard - channel interface to the applications. + channel interface to the applications (the old APIs are retained as macros). - NEW: The I/O queues code was improved, now there are 2 separate structures: InputQueue and OutputQueue. There are some changes in the queue APIs in order to make them more symmetrical and functional. Improved the queues documentation. Some of the changes were needed in order to support the new channels mechanism as a backend for queued serial drivers. -- NEW: Added test cases for the improved queues. -- NEW: Added a code coverage analysis application under ./tests/coverage. - NEW: Added more test cases in order to improve the test suite code coverage (it was 74% in version 1.2.0, it is now close to 100%). +- NEW: Added test cases for the improved queues and serial drivers. +- NEW: Added a code coverage analysis application under ./tests/coverage. - NEW: Added the test suite documentation to the general documentation. - NEW: Added a new "naked" context switch benchmark that better defines the real context switch time, previous benchmarks introduced too much overhead diff --git a/src/include/condvars.h b/src/include/condvars.h index 90c78eccf..d68637f21 100644 --- a/src/include/condvars.h +++ b/src/include/condvars.h @@ -58,6 +58,21 @@ extern "C" { } #endif +/** + * @brief Data part of a static condition variable initializer. + * @details This macro should be used when statically initializing a condition + * variable that is part of a bigger structure. + */ +#define _CONDVAR_DATA(name) {_THREADSQUEUE_DATA(name.c_queue)} + +/** + * @brief Static condition variable initializer. + * @details Statically initialized condition variables require no explicit + * initialization using @p chCondInit(). + * @param name the name of the condition variable + */ +#define CONDVAR_DECL(name) CondVar name = _CONDVAR_DATA(name) + #endif /* CH_USE_CONDVARS && CH_USE_MUTEXES */ #endif /* _CONDVARS_H_ */ diff --git a/src/include/events.h b/src/include/events.h index ada7812d1..6b79553ed 100644 --- a/src/include/events.h +++ b/src/include/events.h @@ -103,6 +103,22 @@ extern "C" { } #endif +/** + * @brief Data part of a static event source initializer. + * @details This macro should be used when statically initializing an event + * source that is part of a bigger structure. + * @param name the name of the event source variable + */ +#define _EVENTSOURCE_DATA(name) {(EventListener *)&name} + +/** + * @brief Static event source initializer. + * @details Statically initialized event sources require no explicit + * initialization using @p chEvtInit(). + * @param name the name of the event source variable + */ +#define EVENTSOURCE_DECL(name) EventSource name = _EVENTSOURCE_DATA(name) + /** * Registers an Event Listener on an Event Source. * @param esp pointer to the @p EventSource structure diff --git a/src/include/lists.h b/src/include/lists.h index 89fdb84a7..3783f8749 100644 --- a/src/include/lists.h +++ b/src/include/lists.h @@ -29,10 +29,37 @@ typedef struct Thread Thread; -/* Macros good with both ThreadsQueue and ThreadsList.*/ +/** + * Threads queue initialization. + */ +#define queue_init(tqp) ((tqp)->p_next = (tqp)->p_prev = (Thread *)(tqp)); + +/** + * Macro evaluating to @p TRUE if the specified threads queue is empty. + */ #define isempty(p) ((p)->p_next == (Thread *)(p)) + +/** + * Macro evaluating to @p TRUE if the specified threads queue is not empty. + */ #define notempty(p) ((p)->p_next != (Thread *)(p)) +/** + * @brief Data part of a static threads queue initializer. + * @details This macro should be used when statically initializing a threads + * queue that is part of a bigger structure. + * @param name the name of the threads queue variable + */ +#define _THREADSQUEUE_DATA(name) {(Thread *)&name, (Thread *)&name} + +/** + * @brief Static threads queue initializer. + * @details Statically initialized threads queues require no explicit + * initialization using @p queue_init(). + * @param name the name of the threads queue variable + */ +#define THREADSQUEUE_DECL(name) ThreadsQueue name = _THREADSQUEUE_DATA(name) + /** * @brief Generic threads bidirectional linked list header and element. * @extends ThreadsList @@ -44,25 +71,6 @@ typedef struct { @p ThreadQueue when empty.*/ } ThreadsQueue; -/** - * @brief Generic threads single linked list. - * @details This list behaves like a stack. - */ -typedef struct { - Thread *p_next; /**< Last pushed @p Thread on the stack, - or @p ThreadList when empty.*/ -} ThreadsList; - -/** - * Queue initialization. - */ -#define queue_init(tqp) ((tqp)->p_next = (tqp)->p_prev = (Thread *)(tqp)); - -/** - * List initialization. - */ -#define list_init(tlp) ((tlp)->p_next = (Thread *)(tlp)) - #if !CH_OPTIMIZE_SPEED #ifdef __cplusplus @@ -73,8 +81,6 @@ extern "C" { Thread *fifo_remove(ThreadsQueue *tqp); Thread *lifo_remove(ThreadsQueue *tqp); Thread *dequeue(Thread *tp); - void list_insert(Thread *tp, ThreadsList *tlp); - Thread *list_remove(ThreadsList *tlp); #ifdef __cplusplus } #endif diff --git a/src/include/mutexes.h b/src/include/mutexes.h index 16b59f695..6ef6e48f4 100644 --- a/src/include/mutexes.h +++ b/src/include/mutexes.h @@ -56,6 +56,22 @@ extern "C" { } #endif +/** + * @brief Data part of a static mutex initializer. + * @details This macro should be used when statically initializing a mutex + * that is part of a bigger structure. + * @param name the name of the mutex variable + */ +#define _MUTEX_DATA(name) {_THREADSQUEUE_DATA(name.m_queue), NULL, NULL} + +/** + * @brief Static mutex initializer. + * @details Statically initialized mutexes require no explicit initialization + * using @p chMtxInit(). + * @param name the name of the mutex variable + */ +#define MUTEX_DECL(name) Mutex name = _MUTEX_DATA(name) + /** * Returns @p TRUE if the mutex queue contains at least a waiting thread. */ diff --git a/src/include/semaphores.h b/src/include/semaphores.h index 10f784ffc..833fc5cd2 100644 --- a/src/include/semaphores.h +++ b/src/include/semaphores.h @@ -57,6 +57,24 @@ extern "C" { } #endif +/** + * @brief Data part of a static semaphore initializer. + * @details This macro should be used when statically initializing a semaphore + * that is part of a bigger structure. + * @param name the name of the semaphore variable + * @param n the counter initial value, this value must be non-negative + */ +#define _SEMAPHORE_DATA(name, n) {_THREADSQUEUE_DATA(name.s_queue), n} + +/** + * @brief Static semaphore initializer. + * @details Statically initialized semaphores require no explicit initialization + * using @p chSemInit(). + * @param name the name of the semaphore variable + * @param n the counter initial value, this value must be non-negative + */ +#define SEMAPHORE_DECL(name, n) Semaphore name = _SEMAPHORE_DATA(name, n) + /** * Decreases the semaphore counter, this macro can be used when it is ensured * that the counter would not become negative. @@ -72,7 +90,7 @@ extern "C" { /** * Returns the semaphore counter current value. */ -#define chSemGetCounterI(sp) ((sp)->s_cnt) +#define chSemGetCounterI(sp) ((sp)->s_cnt) #endif /* CH_USE_SEMAPHORES */ diff --git a/test/testevt.c b/test/testevt.c index 31f8870b0..e053737a5 100644 --- a/test/testevt.c +++ b/test/testevt.c @@ -54,7 +54,13 @@ #define ALLOWED_DELAY MS2ST(5) -static EventSource es1, es2; +/* + * Note, the static initializers are not really required because the + * variables are explicitly initialized in each test case. It is done in order + * to test the macros. + */ +static EVENTSOURCE_DECL(es1); +static EVENTSOURCE_DECL(es2); /** * @page test_events_001 Events registration and dispatch diff --git a/test/testmtx.c b/test/testmtx.c index 030b8ab87..6027aca7e 100644 --- a/test/testmtx.c +++ b/test/testmtx.c @@ -63,8 +63,14 @@ #define ALLOWED_DELAY 5 -static Mutex m1, m2; -static CondVar c1; +/* + * Note, the static initializers are not really required because the + * variables are explicitly initialized in each test case. It is done in order + * to test the macros. + */ +static MUTEX_DECL(m1); +static MUTEX_DECL(m2); +static CONDVAR_DECL(c1); /** * @page test_mtx_001 Priority enqueuing test diff --git a/test/testsem.c b/test/testsem.c index 7cb103789..46c6a7941 100644 --- a/test/testsem.c +++ b/test/testsem.c @@ -53,7 +53,12 @@ #define ALLOWED_DELAY MS2ST(5) -static Semaphore sem1; +/* + * Note, the static initializers are not really required because the + * variables are explicitly initialized in each test case. It is done in order + * to test the macros. + */ +static SEMAPHORE_DECL(sem1, 0); /** * @page test_sem_001 Enqueuing test diff --git a/todo.txt b/todo.txt index 801ec4302..1d674618a 100644 --- a/todo.txt +++ b/todo.txt @@ -8,8 +8,8 @@ After 1.2.0: * Abstract I/O channels rather than just serial ports. ? Move the serial drivers implementations in library. Better keep the core as compact as possible. -X Add tests documentation to the general documentation via doxygen. -- Static object initializers. +* Add tests documentation to the general documentation via doxygen. +X Static object initializers. - Remove any instance of unnamed structures/unions. - Objects registry in the kernel. - OSEK-style simple tasks within the idle thread.