This is the mail archive of the ecos-discuss@sourceware.org mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Problem in program :: Please Help


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]