Altera_Forum
Honored Contributor
20 years agoMessage boxes in eCos
Hi all,
cyg_mbox_peek (), is always returning the count 1 less than the actual number of messages in the message box. When the message box is empty, then count is returned as 0. Why this is so? I am attaching the code below for reference. With regards, Sanjay /* **** Include standard header files **** */# include <cyg/kernel/kapi.h># include <cyg/infra/diag.h> /* **** Constants **** */# define TRUE 1 /* Size of a task's stack */# define STACKSIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 4096)# define NTHREADS 2 /* No. of threads */ /* **** User-defined data types **** */ typedef char stack_t; /* Each stack entry is 8-bit wide */ /* **** Function prototypes **** */ void task_a (cyg_addrword_t); void task_b (cyg_addrword_t); /* **** Gobal variables **** */ cyg_thread task_housekeeping_info[NTHREADS]; stack_t task_stack[NTHREADS+1][STACKSIZE]; cyg_handle_t mbox_handle; cyg_mbox mbox; int msg1=12, msg2=24; /* Startup function */ void cyg_user_start () { cyg_handle_t task_handle[NTHREADS]; cyg_mbox_create (&mbox_handle, &mbox); cyg_thread_create (0, task_a, (cyg_addrword_t) 0, "task_a", (void *) task_stack[0], STACKSIZE, &task_handle[0], &task_housekeeping_info[0]); cyg_thread_create (0, task_b, (cyg_addrword_t) 1, "task_b", (void *) task_stack[1], STACKSIZE, &task_handle[1], &task_housekeeping_info[1]); cyg_thread_resume (task_handle[0]); cyg_thread_resume (task_handle[1]); cyg_scheduler_start (); } void task_a (cyg_addrword_t data) { cyg_bool_t stat; long count; // Run this thread forever. while (TRUE) { // Delay for 1000 ticks. cyg_thread_delay (1000); count = cyg_mbox_peek (mbox_handle); diag_printf ("Count = %ld\n", count); // Send a message to Thread B. cyg_mbox_put (mbox_handle, &msg1); cyg_mbox_put (mbox_handle, &msg1); count = cyg_mbox_peek (mbox_handle); diag_printf ("Count = %ld\n", count); } } void task_b (cyg_addrword_t data) { void *message; // Run this thread forever. while (TRUE) { // Wait for the message. message = cyg_mbox_get (mbox_handle); // Make sure we received the message before attempting to process it. if (message != NULL) { // Process the message. diag_printf ("Message: %d\n", *((int*)message)); } } }