[ECOS] Problems with cyg_flag_timed_wait() in eCos 1.3.1

Chris Sekula chriss@turnpikeglobal.com
Tue Nov 13 02:38:00 GMT 2001


I'm writing an application where a worker thread performs a task, but
only when called upon by a particular event, set by a Main thread. I'm
using eCos 1.3.1. I decided to use a flag to block the worker thread
until the event occurred with a call to cyg_flag_wait() in the worker
thread. The application worked well, with no apparent flaws.
 
However, I thought it would be a good idea for the waiting on the flag
to be timed-out. I changed cyg_flag_wait() to cyg_flag_timed_wait() and
ran into some problems. I wrote some simple code to test my problem. In
my first test, I launch a main thread which delays for 4 seconds before
setting a flag to a particular pattern. This is done in a continuous
loop. The worker thread waits on this particular pattern, toggles an I/O
line after the pattern is detected and then resets the pattern. This is
also done continuously. The code worked as expected as I observed the
I/O line toggling at the times that I had expected from the code. I've
attached the code below.
 
To test the cyg_flag_timed_wait() portion, I removed the setting of the
flag in the main thread and just let the main thread spin continuously.
The worker thread employs cyg_flag_timed_wait() with a timeout of 7
seconds instead of cyg_flag_wait().  The code is attached below. I
expected that after 7 seconds, I would see the I/O line toggle followed
by another wait of 7 seconds and a toggle...etc. What I observed,
however, was that after 7 seconds, the I/O line went Hi, but remained
Hi.  The program appeared to hang. 
 
Why am I observing this behaviour? How can I get the time out to work
properly?
 
Thanks for any help,
 
CHRIS.
 
////// cyg_flag_wait() version ///////
 
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_io.h>
#define STACKSIZE 20240
static char stack1[STACKSIZE];
static char stack2[STACKSIZE];
static cyg_handle_t hmainThread, hToggleThread;
static cyg_thread mainThread_obj, toggleThread_obj;
static void MainProc(CYG_ADDRESS data);
static void Toggle(CYG_ADDRESS data);
cyg_flag_t g_toggleFlag;
 
void cyg_user_start(void)
{
     cyg_flag_init(&g_toggleFlag);
     cyg_flag_maskbits(&g_toggleFlag, 0x00000000);
 
     cyg_thread_create(4, MainProc, (cyg_addrword_t)0, "mainproc",
         (void*)&stack1, STACKSIZE, &hmainThread, 
         &mainThread_obj);
 
    cyg_thread_create(4, Toggle, (cyg_addrword_t)0, "toggle",
         (void*)&stack2, STACKSIZE, &hToggleThread, 
         &toggleThread_obj);
   
    cyg_thread_resume(hmainThread);
    cyg_thread_resume(hToggleThread);
}
static void MainProc(CYG_ADDRESS data)
{
    while (1)
   {
        cyg_thread_delay(400);
        cyg_flag_setbits(&g_toggleFlag, 0xFFFFFFFF);
   }
}
 
static void Toggle(CYG_ADDRESS data)
{
      int x;
     HAL_IO_REGISTER PortDOut = 0x80000003;
 
     HAL_WRITE_UINT8(PortDOut, 0x00);
     while (1)
    {
         cyg_flag_wait(&g_toggleFlag, 0xFFFFFFFF,
CYG_FLAG_WAITMODE_AND);

         HAL_WRITE_UINT8(PortDOut, 0x02);
         cyg_thread_delay(100);
         HAL_WRITE_UINT8(PortDOut, 0x00);
         cyg_flag_maskbits(&g_toggleFlag, 0x00000000);
    }
}
 
 
////// cyg_flag_timed_wait() version ///////
 
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_io.h>
#define STACKSIZE 20240
static char stack1[STACKSIZE];
static char stack2[STACKSIZE];
static cyg_handle_t hmainThread, hToggleThread;
static cyg_thread mainThread_obj, toggleThread_obj;
static void MainProc(CYG_ADDRESS data);
static void Toggle(CYG_ADDRESS data);
cyg_flag_t g_toggleFlag;
 
void cyg_user_start(void)
{
     cyg_flag_init(&g_toggleFlag);
     cyg_flag_maskbits(&g_toggleFlag, 0x00000000);
 
     cyg_thread_create(4, MainProc, (cyg_addrword_t)0, "mainproc",
         (void*)&stack1, STACKSIZE, &hmainThread, 
         &mainThread_obj);
 
    cyg_thread_create(4, Toggle, (cyg_addrword_t)0, "toggle",
         (void*)&stack2, STACKSIZE, &hToggleThread, 
         &toggleThread_obj);
   
    cyg_thread_resume(hmainThread);
    cyg_thread_resume(hToggleThread);
}
static void MainProc(CYG_ADDRESS data)
{
    while (1)
   {
    
   }
}
 
static void Toggle(CYG_ADDRESS data)
{
      int x;
     HAL_IO_REGISTER PortDOut = 0x80000003;
 
     HAL_WRITE_UINT8(PortDOut, 0x00);
     while (1)
    {
         cyg_flag_timed_wait(&g_toggleFlag, 0xFFFFFFFF,
CYG_FLAG_WAITMODE_AND, 700);
        HAL_WRITE_UINT8(PortDOut, 0x02);
         cyg_thread_delay(100);
         HAL_WRITE_UINT8(PortDOut, 0x00);
         cyg_flag_maskbits(&g_toggleFlag, 0x00000000);
    }
}


More information about the Ecos-discuss mailing list