public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] Problem in program :: Please Help
@ 2005-08-02 14:50 R. Vamshi Krishna
  2005-08-02 15:27 ` Andrew Lunn
  0 siblings, 1 reply; 2+ messages in thread
From: R. Vamshi Krishna @ 2005-08-02 14:50 UTC (permalink / raw)
  To: ecos-discuss

Hello all,

I am stuck up at a problem.  This is what I wanted to do ::

    1.  An application runs in two different modes.
    2.  Each mode has 3 threads (distinct).
    3.  The application must switch modes  at  every  1000 ticks.
    4.  The individual threads of a mode will execute periodically.

    5. I have tried to use an alarm to indicate that 1000 ticks are over.
    6. Also each thread has its own alarm to indiacte that that it's 
period is over.
   
Where have I gone wrong ??

This is the o/p.
----------------------------------------------------------------------------------------------------


Mode A thread 2 :: Current Time is 0
Mode A thread 3 :: Current Time is 1
Mode A thread 1 :: Current Time is 1

$T0bthread:00000001;08:bae91000;04:501f1100;#ea

-----------------------------------------------------------------------------------------------------


I am sorry if what I am doing is foolish or elementary. I am new to this.



This is the program :



#include <cyg/kernel/kapi.h>

#include <stdio.h>
#include <stdlib.h>

#define RVK 8

cyg_thread thread_s[3];
char stack[3][4096];

int mode ; // 0 -> mode A and 1 -> mode B

cyg_handle_t modeA_thread1, modeA_thread2, modeA_thread3;
cyg_handle_t modeB_thread1, modeB_thread2, modeB_thread3;

cyg_thread_entry_t modeA_thread1_entry, modeA_thread2_entry, 
modeA_thread3_entry;
cyg_thread_entry_t modeB_thread1_entry, modeB_thread2_entry, 
modeB_thread3_entry;

void thread1A_alarm_func(cyg_handle_t, cyg_addrword_t);
void thread2A_alarm_func(cyg_handle_t, cyg_addrword_t);
void thread3A_alarm_func(cyg_handle_t, cyg_addrword_t);
void thread1B_alarm_func(cyg_handle_t, cyg_addrword_t);
void thread2B_alarm_func(cyg_handle_t, cyg_addrword_t);
void thread3B_alarm_func(cyg_handle_t, cyg_addrword_t);
void mode_change(cyg_handle_t, cyg_addrword_t);


cyg_handle_t thread_counterH[3], system_clockH[3], thread_alarmH[3];
cyg_alarm thread_alarm[3];

void cyg_user_start(void)
{

        cyg_handle_t test_counterH,tsystem_clockH,test_alarmH;
        cyg_alarm test_alarm;

        tsystem_clockH = cyg_real_time_clock();
        cyg_clock_to_counter(tsystem_clockH,&test_counterH);

        mode = 0;
        cyg_alarm_create(test_counterH,mode_change,(cyg_addrword_t) &mode,
                &test_alarmH, &test_alarm);

        cyg_thread_create(6,modeA_thread1_entry,(cyg_addrword_t) 0,"Mode 
A thread 1",
                (void *) stack[0],4096,&modeA_thread1,&thread_s[0]);

        cyg_thread_create(4,modeA_thread2_entry,(cyg_addrword_t) 0,"Mode 
A thread 2",
                (void *) stack[1],4096,&modeA_thread2,&thread_s[1]);

        cyg_thread_create(5,modeA_thread3_entry,(cyg_addrword_t) 0,"Mode 
A thread 3",
                (void *) stack[2],4096,&modeA_thread3,&thread_s[2]);


        
cyg_alarm_create(thread_counterH[0],thread1A_alarm_func,(cyg_addrword_t) 
&modeA_thread1,
                &thread_alarmH[0], &thread_alarm[0]);

        
cyg_alarm_create(thread_counterH[1],thread2A_alarm_func,(cyg_addrword_t) 
&modeA_thread2,
                &thread_alarmH[1], &thread_alarm[1]);

        
cyg_alarm_create(thread_counterH[2],thread3A_alarm_func,(cyg_addrword_t) 
&modeA_thread3,
                &thread_alarmH[2], &thread_alarm[2]);

        cyg_alarm_initialize(test_alarmH,cyg_current_time()+1000,1000);

        
cyg_alarm_initialize(thread_alarmH[0],cyg_current_time()+5*RVK,5*RVK);
        
cyg_alarm_initialize(thread_alarmH[1],cyg_current_time()+6*RVK,6*RVK);
        
cyg_alarm_initialize(thread_alarmH[2],cyg_current_time()+4*RVK,4*RVK);

        cyg_thread_resume(modeA_thread1);
        cyg_thread_resume(modeA_thread2);
        cyg_thread_resume(modeA_thread3);
}


void modeA_thread1_entry(cyg_addrword_t data)
{
        while(1)
        {
                diag_printf("Mode A thread 1 :: Current Time is %u 
\n",(unsigned int) cyg_current_time());
                cyg_thread_delay(1*RVK);
                cyg_thread_suspend(cyg_thread_self());
        }
}


void modeA_thread2_entry(cyg_addrword_t data)
{
        while(1)
        {
                diag_printf("Mode A thread 2 :: Current Time is %u 
\n",(unsigned int) cyg_current_time());
                cyg_thread_delay(2*RVK);
                cyg_thread_suspend(cyg_thread_self());
        }
}

void modeA_thread3_entry(cyg_addrword_t data)
{
        while(1)
        {
                diag_printf("Mode A thread 3 :: Current Time is %u 
\n",(unsigned int) cyg_current_time());
                cyg_thread_delay(1*RVK);
                cyg_thread_suspend(cyg_thread_self());
        }
}

void modeB_thread1_entry(cyg_addrword_t data)
{
        while(1)
        {
                diag_printf("Mode B thread 1 :: Current Time is %u 
\n",(unsigned int) cyg_current_time());
                cyg_thread_delay(1*RVK);
                cyg_thread_suspend(cyg_thread_self());
        }
}


void modeB_thread2_entry(cyg_addrword_t data)
{
        while(1)
        {
                diag_printf("Mode B thread 2 :: Current Time is %u 
\n",(unsigned int) cyg_current_time());
                cyg_thread_delay(1*RVK);
                cyg_thread_suspend(cyg_thread_self());
        }
}


void modeB_thread3_entry(cyg_addrword_t data)
{
        while(1)
        {
                diag_printf("Mode B thread 3 :: Current Time is %u 
\n",(unsigned int) cyg_current_time());
                cyg_thread_delay(1*RVK);
                cyg_thread_suspend(cyg_thread_self());
        }
}

void thread1A_alarm_func(cyg_handle_t alarm, cyg_addrword_t data)
{
        cyg_thread_resume(modeA_thread1);
}


void thread2A_alarm_func(cyg_handle_t alarm, cyg_addrword_t data)
{
        cyg_thread_resume(modeA_thread2);
}


void thread3A_alarm_func(cyg_handle_t alarm, cyg_addrword_t data)
{
        cyg_thread_resume(modeA_thread3);
}


void thread1B_alarm_func(cyg_handle_t alarm, cyg_addrword_t data)
{
        cyg_thread_resume(modeB_thread1);
}


void thread2B_alarm_func(cyg_handle_t alarm, cyg_addrword_t data)
{
        cyg_thread_resume(modeB_thread2);
}


void thread3B_alarm_func(cyg_handle_t alarm, cyg_addrword_t data)
{
        cyg_thread_resume(modeB_thread3);
}

void mode_change(cyg_handle_t alarm, cyg_addrword_t data)
{
        if(*((unsigned *) data) == 0)
        {
                diag_printf("Mode Change from A to B at %u 
\n",(unsigned) cyg_current_time());
                *((unsigned *) data) = 1;

                cyg_alarm_delete(thread_alarmH[0]);
                cyg_alarm_delete(thread_alarmH[1]);
                cyg_alarm_delete(thread_alarmH[2]);

                cyg_thread_delete(modeA_thread1);
                cyg_thread_delete(modeA_thread2);
                cyg_thread_delete(modeA_thread3);

                cyg_thread_create(5,modeB_thread1_entry,(cyg_addrword_t) 
0,"Mode B thread 1",
                        (void *) stack[0],4096,&modeB_thread1,&thread_s[0]);

                cyg_thread_create(6,modeB_thread2_entry,(cyg_addrword_t) 
0,"Mode B thread 2",
                        (void *) stack[1],4096,&modeB_thread2,&thread_s[1]);

                cyg_thread_create(4,modeB_thread3_entry,(cyg_addrword_t) 
0,"Mode B thread 3",
                        (void *) stack[2],4096,&modeB_thread3,&thread_s[2]);


                
cyg_alarm_create(thread_counterH[0],thread1B_alarm_func,(cyg_addrword_t) 
&modeB_thread1,
                        &thread_alarmH[0], &thread_alarm[0]);

                
cyg_alarm_create(thread_counterH[1],thread2B_alarm_func,(cyg_addrword_t) 
&modeB_thread2,
                        &thread_alarmH[1], &thread_alarm[1]);

                
cyg_alarm_create(thread_counterH[2],thread3B_alarm_func,(cyg_addrword_t) 
&modeB_thread3,
                        &thread_alarmH[2], &thread_alarm[2]);

                
cyg_alarm_initialize(thread_alarmH[0],cyg_current_time()+5*RVK,5*RVK);
                
cyg_alarm_initialize(thread_alarmH[1],cyg_current_time()+7*RVK,7*RVK);
                
cyg_alarm_initialize(thread_alarmH[2],cyg_current_time()+4*RVK,4*RVK);
                cyg_thread_resume(modeB_thread1);
                cyg_thread_resume(modeB_thread2);
                cyg_thread_resume(modeB_thread3);

        }
        else
        {
                diag_printf("Mode change from B to A at %u 
\n",(unsigned) cyg_current_time());
                *((unsigned *) data) = 0;

                cyg_alarm_delete(thread_alarmH[0]);
                cyg_alarm_delete(thread_alarmH[1]);
                cyg_alarm_delete(thread_alarmH[2]);

                cyg_thread_delete(modeB_thread1);
                cyg_thread_delete(modeB_thread2);
                cyg_thread_delete(modeB_thread3);

                cyg_thread_create(6,modeA_thread1_entry,(cyg_addrword_t) 
0,"Mode A thread 1",
                        (void *) stack[0],4096,&modeA_thread1,&thread_s[0]);

                cyg_thread_create(4,modeA_thread2_entry,(cyg_addrword_t) 
0,"Mode A thread 2",
                        (void *) stack[1],4096,&modeA_thread2,&thread_s[1]);

                cyg_thread_create(5,modeA_thread3_entry,(cyg_addrword_t) 
0,"Mode A thread 3",
                        (void *) stack[2],4096,&modeA_thread3,&thread_s[2]);


                
cyg_alarm_create(thread_counterH[0],thread1A_alarm_func,(cyg_addrword_t) 
&modeA_thread1,
                        &thread_alarmH[0], &thread_alarm[0]);

                
cyg_alarm_create(thread_counterH[1],thread2A_alarm_func,(cyg_addrword_t) 
&modeA_thread2,
                        &thread_alarmH[1], &thread_alarm[1]);

               
cyg_alarm_create(thread_counterH[2],thread3A_alarm_func,(cyg_addrword_t) 
&modeA_thread3,
                        &thread_alarmH[2], &thread_alarm[2]);

                
cyg_alarm_initialize(thread_alarmH[0],cyg_current_time()+5*RVK,5*RVK);
                
cyg_alarm_initialize(thread_alarmH[1],cyg_current_time()+6*RVK,6*RVK);
                
cyg_alarm_initialize(thread_alarmH[2],cyg_current_time()+4*RVK,4*RVK);

                cyg_thread_resume(modeA_thread1);
                cyg_thread_resume(modeA_thread2);
                cyg_thread_resume(modeA_thread3);
        }
}




-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [ECOS] Problem in program :: Please Help
  2005-08-02 14:50 [ECOS] Problem in program :: Please Help R. Vamshi Krishna
@ 2005-08-02 15:27 ` Andrew Lunn
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Lunn @ 2005-08-02 15:27 UTC (permalink / raw)
  To: R. Vamshi Krishna; +Cc: ecos-discuss

On Tue, Aug 02, 2005 at 08:22:43PM +0530, R. Vamshi Krishna wrote:
> Hello all,
> 
> I am stuck up at a problem.  This is what I wanted to do ::
> 
>    1.  An application runs in two different modes.
>    2.  Each mode has 3 threads (distinct).
>    3.  The application must switch modes  at  every  1000 ticks.
>    4.  The individual threads of a mode will execute periodically.
> 
>    5. I have tried to use an alarm to indicate that 1000 ticks are over.
>    6. Also each thread has its own alarm to indiacte that that it's 
> period is over.
>   
> Where have I gone wrong ??

Lots....

First of you are doing things in an alarm function you are not allowed
to do. See the documentation.

Secondly as the documentation says, you should only call
cyg_thread_delete on a thread that as existed.

The strucuture you currently have is very unsafe. I suggest you throw
away your current design and start again with something more sain.

        Andrew

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-08-02 15:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-02 14:50 [ECOS] Problem in program :: Please Help R. Vamshi Krishna
2005-08-02 15:27 ` Andrew Lunn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).