Improved messages subsystem.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2759 35acf78f-673a-0410-8e92-d51de3d6d3f4
master
gdisirio 2011-02-23 18:59:39 +00:00
parent b3b1028036
commit 6f6e1a6401
10 changed files with 122 additions and 120 deletions

View File

@ -122,15 +122,16 @@ static const ShellConfig shell_cfg2 = {
/*
* Console print server done using synchronous messages. This makes the access
* to the C printf() thread safe and the print operation atomic among threads.
* In this example the message is the zero termitated string itself.
* In this example the message is the zero terminated string itself.
*/
static msg_t console_thread(void *arg) {
(void)arg;
while (!chThdShouldTerminate()) {
puts((char *)chMsgWait());
Thread *tp = chMsgWait();
puts((char *)chMsgGet(tp));
fflush(stdout);
chMsgRelease(RDY_OK);
chMsgRelease(tp, RDY_OK);
}
return 0;
}

View File

@ -98,15 +98,15 @@ Settings: SYSCLK=72, ACR=0x12 (2 wait states)
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.1 (Benchmark, messages #1)
--- Score : 248573 msgs/S, 497146 ctxswc/S
--- Score : 248569 msgs/S, 497138 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.2 (Benchmark, messages #2)
--- Score : 201227 msgs/S, 402454 ctxswc/S
--- Score : 198998 msgs/S, 397996 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.3 (Benchmark, messages #3)
--- Score : 201227 msgs/S, 402454 ctxswc/S
--- Score : 198998 msgs/S, 397996 ctxswc/S
--- Result: SUCCESS
----------------------------------------------------------------------------
--- Test Case 11.4 (Benchmark, context switch)

View File

@ -2,13 +2,13 @@ Platform : PowerPC
OS Setup : Full kernel
Compiler : powerpc-eabi-gcc (Sourcery G++ Lite 4.4-79) 4.4.1
Options : -O2 -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 11744
Kernel Size = 11716
Platform : PowerPC
OS Setup : Full kernel
Compiler : powerpc-eabi-gcc (Sourcery G++ Lite 4.4-79) 4.4.1
Options : -O2 -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 11296
Kernel Size = 11244
Platform : PowerPC
OS Setup : Minimal kernel
@ -20,13 +20,13 @@ Platform : PowerPC
OS Setup : Full kernel
Compiler : powerpc-eabi-gcc (Sourcery G++ Lite 4.4-79) 4.4.1
Options : -Os -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 10260
Kernel Size = 10208
Platform : PowerPC
OS Setup : Full kernel
Compiler : powerpc-eabi-gcc (Sourcery G++ Lite 4.4-79) 4.4.1
Options : -Os -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 9768
Kernel Size = 9712
Platform : PowerPC
OS Setup : Minimal kernel
@ -38,13 +38,13 @@ Platform : ARM Cortex-M3
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 6172
Kernel Size = 6124
Platform : ARM Cortex-M3
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 5672
Kernel Size = 5624
Platform : ARM Cortex-M3
OS Setup : Minimal kernel
@ -56,13 +56,13 @@ Platform : ARM Cortex-M3
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 5568
Kernel Size = 5524
Platform : ARM Cortex-M3
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 5156
Kernel Size = 5112
Platform : ARM Cortex-M3
OS Setup : Minimal kernel
@ -74,13 +74,13 @@ Platform : ARM Cortex-M3
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 5348
Kernel Size = 5320
Platform : ARM Cortex-M3
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 4964
Kernel Size = 4936
Platform : ARM Cortex-M3
OS Setup : Minimal kernel
@ -92,13 +92,13 @@ Platform : ARM Cortex-M0
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 5432
Kernel Size = 5396
Platform : ARM Cortex-M0
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 5236
Kernel Size = 5196
Platform : ARM Cortex-M0
OS Setup : Minimal kernel
@ -110,13 +110,13 @@ Platform : ARM Cortex-M0
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 4992
Kernel Size = 4956
Platform : ARM Cortex-M0
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 4832
Kernel Size = 4792
Platform : ARM Cortex-M0
OS Setup : Minimal kernel
@ -128,13 +128,13 @@ Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 8988
Kernel Size = 8940
Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 8528
Kernel Size = 8472
Platform : ARM7TDMI (ARM mode)
OS Setup : Minimal kernel
@ -146,13 +146,13 @@ Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 8344
Kernel Size = 8288
Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 7988
Kernel Size = 7928
Platform : ARM7TDMI (ARM mode)
OS Setup : Minimal kernel
@ -164,13 +164,13 @@ Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 8704
Kernel Size = 8676
Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 8260
Kernel Size = 8220
Platform : ARM7TDMI (ARM mode)
OS Setup : Minimal kernel
@ -182,13 +182,13 @@ Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 8052
Kernel Size = 8016
Platform : ARM7TDMI (ARM mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 7716
Kernel Size = 7676
Platform : ARM7TDMI (ARM mode)
OS Setup : Minimal kernel
@ -200,13 +200,13 @@ Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -DCH_OPTIMIZE_SPEED=TRUE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 6008
Kernel Size = 5964
Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -DCH_OPTIMIZE_SPEED=FALSE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 5800
Kernel Size = 5752
Platform : ARM7TDMI (THUMB mode)
OS Setup : Minimal kernel
@ -218,13 +218,13 @@ Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -DCH_OPTIMIZE_SPEED=TRUE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 5580
Kernel Size = 5536
Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -DCH_OPTIMIZE_SPEED=FALSE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 5396
Kernel Size = 5348
Platform : ARM7TDMI (THUMB mode)
OS Setup : Minimal kernel
@ -236,13 +236,13 @@ Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=TRUE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 5852
Kernel Size = 5824
Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -O2 -mthumb -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=FALSE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 5652
Kernel Size = 5620
Platform : ARM7TDMI (THUMB mode)
OS Setup : Minimal kernel
@ -254,13 +254,13 @@ Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=TRUE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 5368
Kernel Size = 5340
Platform : ARM7TDMI (THUMB mode)
OS Setup : Full kernel
Compiler : arm-none-eabi-gcc (GCC) 4.5.2
Options : -Os -mthumb -ffixed-r7 -DCH_CURRP_REGISTER_CACHE=\"r7\" -DCH_OPTIMIZE_SPEED=FALSE -DTHUMB -DTHUMB_PRESENT -DTHUMB_NO_INTERWORKING
Kernel Size = 5212
Kernel Size = 5180
Platform : ARM7TDMI (THUMB mode)
OS Setup : Minimal kernel
@ -272,13 +272,13 @@ Platform : MSP430
OS Setup : Full kernel
Compiler : msp430-gcc (GCC) 3.2.3
Options : -O2 -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 5884
Kernel Size = 5840
Platform : MSP430
OS Setup : Full kernel
Compiler : msp430-gcc (GCC) 3.2.3
Options : -O2 -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 5476
Kernel Size = 5428
Platform : MSP430
OS Setup : Minimal kernel
@ -290,13 +290,13 @@ Platform : MSP430
OS Setup : Full kernel
Compiler : msp430-gcc (GCC) 3.2.3
Options : -Os -DCH_OPTIMIZE_SPEED=TRUE
Kernel Size = 5820
Kernel Size = 5776
Platform : MSP430
OS Setup : Full kernel
Compiler : msp430-gcc (GCC) 3.2.3
Options : -Os -DCH_OPTIMIZE_SPEED=FALSE
Kernel Size = 5440
Kernel Size = 5392
Platform : MSP430
OS Setup : Minimal kernel

View File

@ -39,20 +39,47 @@
((tp)->p_msgqueue.p_next != (Thread *)&(tp)->p_msgqueue)
/**
* @brief Returns the first message in the queue.
* @brief Returns the message carried by the specified thread.
* @pre This function must be invoked immediately after exiting a call
* to @p chMsgWait().
*
* @iclass
* @param[in] tp pointer to the thread
* @return The message carried by the sender.
*
* @api
*/
#define chMsgGetI(tp) \
((tp)->p_msgqueue.p_next->p_msg)
#define chMsgGet(tp) ((tp)->p_msg)
/**
* @brief Returns the message carried by the specified thread.
* @pre This function must be invoked immediately after exiting a call
* to @p chMsgWait().
*
* @param[in] tp pointer to the thread
* @return The message carried by the sender.
*
* @sclass
*/
#define chMsgGetS(tp) ((tp)->p_msg)
/**
* @brief Releases the thread waiting on top of the messages queue.
* @pre Invoke this function only after a message has been received
* using @p chMsgWait().
*
* @param[in] tp pointer to the thread
* @param[in] msg message to be returned to the sender
*
* @sclass
*/
#define chMsgReleaseS(tp, msg) chSchWakeupS(tp, msg)
#ifdef __cplusplus
extern "C" {
#endif
msg_t chMsgSend(Thread *tp, msg_t msg);
msg_t chMsgWait(void);
msg_t chMsgGet(void);
void chMsgRelease(msg_t msg);
Thread * chMsgWait(void);
void chMsgRelease(Thread *tp, msg_t msg);
#ifdef __cplusplus
}
#endif

View File

@ -176,12 +176,14 @@ struct Thread {
#define THD_STATE_WTOREVT 8
/** @brief Thread state: Waiting in @p chEvtWaitAllTimeout().*/
#define THD_STATE_WTANDEVT 9
/** @brief Thread state: Waiting in @p chMsgSend().*/
#define THD_STATE_SNDMSG 10
/** @brief Thread state: Waiting in @p chMsgSend() (queued).*/
#define THD_STATE_SNDMSGQ 10
/** @brief Thread state: Waiting in @p chMsgSend() (not queued).*/
#define THD_STATE_SNDMSG 11
/** @brief Thread state: Waiting in @p chMsgWait().*/
#define THD_STATE_WTMSG 11
#define THD_STATE_WTMSG 12
/** @brief Thread state: After termination.*/
#define THD_STATE_FINAL 12
#define THD_STATE_FINAL 13
/*
* Various flags into the thread p_flags field.
@ -242,7 +244,7 @@ extern "C" {
* @note This function is only available when the
* @p CH_DBG_THREADS_PROFILING configuration option is enabled.
*
* @param[in] tp the pointer to the thread
* @param[in] tp pointer to the thread
*
* @api
*/
@ -258,7 +260,7 @@ extern "C" {
/**
* @brief Verifies if the specified thread is in the @p THD_STATE_FINAL state.
*
* @param[in] tp the pointer to the thread
* @param[in] tp pointer to the thread
* @retval TRUE thread terminated.
* @retval FALSE thread not terminated.
*
@ -279,7 +281,7 @@ extern "C" {
/**
* @brief Resumes a thread created with @p chThdInit().
*
* @param[in] tp the pointer to the thread
* @param[in] tp pointer to the thread
*
* @iclass
*/
@ -305,7 +307,7 @@ extern "C" {
* system clock.
* @note The maximum specified value is implementation dependent.
*
* @param[in] sec the time in seconds
* @param[in] sec time in seconds
*
* @api
*/
@ -318,7 +320,7 @@ extern "C" {
* system clock.
* @note The maximum specified value is implementation dependent.
*
* @param[in] msec the time in milliseconds
* @param[in] msec time in milliseconds
*
* @api
*/
@ -331,7 +333,7 @@ extern "C" {
* system clock.
* @note The maximum specified value is implementation dependent.
*
* @param[in] usec the time in microseconds
* @param[in] usec time in microseconds
*
* @api
*/

View File

@ -75,7 +75,7 @@ msg_t chMsgSend(Thread *tp, msg_t msg) {
msg_insert(ctp, &tp->p_msgqueue);
if (tp->p_state == THD_STATE_WTMSG)
chSchReadyI(tp);
chSchGoSleepS(THD_STATE_SNDMSG);
chSchGoSleepS(THD_STATE_SNDMSGQ);
msg = ctp->p_u.rdymsg;
chSysUnlock();
return msg;
@ -83,69 +83,46 @@ msg_t chMsgSend(Thread *tp, msg_t msg) {
/**
* @brief Suspends the thread and waits for an incoming message.
* @post After receiving a message the function @p chMsgRelease() must be
* invoked in order to acknowledge the reception and send the answer.
* @post After receiving a message the function @p chMsgGet() must be
* called in order to retrieve the message and then @p chMsgRelease()
* must be invoked in order to acknowledge the reception and send
* the answer.
* @note If the message is a pointer then you can assume that the data
* pointed by the message is stable until you invoke @p chMsgRelease()
* because the sending thread is suspended until then.
*
* @return The message.
* @return A reference to the thread carrying the message.
*
* @api
*/
msg_t chMsgWait(void) {
msg_t msg;
Thread *chMsgWait(void) {
Thread *tp;
chSysLock();
if (!chMsgIsPendingI(currp))
chSchGoSleepS(THD_STATE_WTMSG);
#if defined(CH_ARCHITECTURE_STM8)
msg = chMsgGetI((volatile Thread *)currp); /* Temporary hack.*/
#else
msg = chMsgGetI(currp);
#endif
tp = fifo_remove(&currp->p_msgqueue);
tp->p_state = THD_STATE_SNDMSG;
chSysUnlock();
return msg;
}
/**
* @brief Returns the next message in the queue.
* @post After receiving a message the function @p chMsgRelease() must be
* invoked in order to acknowledge the reception and send the answer.
* @note If the message is a pointer then you can assume that the data
* pointed by the message is stable until you invoke @p chMsgRelease()
* because the sending thread is suspended until then.
*
* @return The message.
* @retval 0 if the queue is empty.
*
* @api
*/
msg_t chMsgGet(void) {
msg_t msg;
chSysLock();
msg = chMsgIsPendingI(currp) ? chMsgGetI(currp) : (msg_t)NULL;
chSysUnlock();
return msg;
return tp;
}
/**
* @brief Releases the thread waiting on top of the messages queue.
* @pre Invoke this function only after a message has been received
* using @p chMsgWait() or @p chMsgGet().
* using @p chMsgWait().
*
* @param[in] msg the message returned to the message sender
* @param[in] tp pointer to the thread
* @param[in] msg message to be returned to the sender
*
* @api
*/
void chMsgRelease(msg_t msg) {
void chMsgRelease(Thread *tp, msg_t msg) {
chSysLock();
chDbgAssert(chMsgIsPendingI(currp),
"chMsgRelease(), #1",
"no message pending");
chSchWakeupS(fifo_remove(&currp->p_msgqueue), msg);
chDbgAssert(tp->p_state == THD_STATE_SNDMSG,
"chMsgRelease(), #1", "invalid state");
chMsgReleaseS(tp, msg);
chSysUnlock();
}

View File

@ -141,7 +141,7 @@ void chMtxLockS(Mutex *mp) {
case THD_STATE_WTSEM:
#endif
#if CH_USE_MESSAGES_PRIORITY
case THD_STATE_SNDMSG:
case THD_STATE_SNDMSGQ:
#endif
/* Re-enqueues tp with its new priority on the queue.*/
prio_insert(dequeue(tp), (ThreadsQueue *)tp->p_u.wtobjp);

View File

@ -96,6 +96,8 @@
driver.
- CHANGE: Swapped the numeric values of the TIME_IMMEDIATE and TIME_INFINITE
constants. Fixed the relative documentation in various places.
- CHANGE: Slightly modified the messages API in order to allow the
processing of multiple messages at the same time.
*** 2.1.8 ***
- FIX: Fixed error in STM32 ADC driver macro names (bug 3160306)(backported

View File

@ -65,11 +65,14 @@ static Mutex mtx1;
#endif
static msg_t thread1(void *p) {
Thread *tp;
msg_t msg;
(void)p;
do {
chMsgRelease(msg = chMsgWait());
tp = chMsgWait();
msg = chMsgGet(tp);
chMsgRelease(tp, msg);
} while (msg);
return 0;
}

View File

@ -64,11 +64,11 @@ static msg_t thread(void *p) {
chMsgSend(p, 'A');
chMsgSend(p, 'B');
chMsgSend(p, 'C');
chMsgSend(p, 'D');
return 0;
}
static void msg1_execute(void) {
Thread *tp;
msg_t msg;
/*
@ -76,29 +76,19 @@ static void msg1_execute(void) {
*/
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority() + 1,
thread, chThdSelf());
chMsgRelease(msg = chMsgWait());
tp = chMsgWait();
msg = chMsgGet(tp);
chMsgRelease(tp, msg);
test_emit_token(msg);
chMsgRelease(msg = chMsgWait());
tp = chMsgWait();
msg = chMsgGet(tp);
chMsgRelease(tp, msg);
test_emit_token(msg);
chMsgRelease(msg = chMsgWait());
tp = chMsgWait();
msg = chMsgGet(tp);
chMsgRelease(tp, msg);
test_emit_token(msg);
test_assert_sequence(1, "ABC");
/*
* Testing message fetch using chMsgGet().
* Note, the following is valid because the sender has higher priority than
* the receiver.
*/
msg = chMsgGet();
test_assert(1, msg != 0, "no message");
chMsgRelease(0);
test_assert(2, msg == 'D', "wrong message");
/*
* Must not have pending messages.
*/
msg = chMsgGet();
test_assert(3, msg == 0, "unknown message");
}
ROMCONST struct testcase testmsg1 = {