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]

Re: Thread scheduling problem


On Tue, 2005-12-27 at 18:19 +0530, vasantha.rajan wrote:
> Hi Gary,
> 
> Thanks for your reply,but still i cant get the problem.
> 
> I will give two set of programs below.
> 
> program 1:
> 
> void cyg_user_start() 
> {
> 	
> 	cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack, STACKSIZE,     
> &handle, &thread);
> 	cyg_thread_resume(handle);
> 	printf("End of start");
> }
> 
> //      The thread resumed
> void counter_thread(cyg_addrword_t data)
> {
> 	printf("inside counter thread\n");
> 	cyg_thread_delay(10);
>        printf("delay completed\n");
> }
> 
> 
> program 2:
> 
> void cyg_user_start() 
> {
> 	cyg_semaphore_init(&sem,val);
> 	cyg_thread_create(10, &counter_thread, 0 ,"counter_thread", stack, STACKSIZE,     
> &handle, &thread);
> 	cyg_thread_resume(handle);
> 	cyg_semaphore_wait(&sem);       // Initial value of val is 0
>  	printf("End of start");
> }
> 
> //      The thread resumed
> void counter_thread(cyg_addrword_t data)
> {
> 	printf("inside counter thread\n");
> 	cyg_thread_delay(10);
> 	 printf("delay completed\n");
> }
> 
> 
> when i run program 1:The thread is resumed after scheduler  starts and the 
> thread waits for the stipulated amount of time and again resumes and prints 
> the statement "delay completed".
> 
> when i run program 2:The thread is resumed  before cyg_user_start() ends 
> ie,before the scheduler starts(I guess...) and it waits infinitely thereonly 
> and the statement "delay completed" is not printed.(I am not posting the 
> semaphore... I think that wont be a problem...)
> 
> So my question is
> 
> 1.What is the difference between the two codes??

You shouldn't call any function which may block (i.e. 
cyg_semaphore_wait) until the scheduler (and interrupts) have been 
started.

> 2.Does scheduler comes into picture in both the codes??
> 3.In code 2 why does the program waits infinitely?(It means the scheduler 
> doesnt have control over that particular thread)

No, it simply means you violated the rules for what can be called
when.  The scheduler isn't a separate thread (or any other sort of
special context) - it's merely the choide of what happens after some
sort of scheduling event, e.g. when one thread is suspended or a
timer fires and makes a thread ready to run, etc.  If you call a
function like cyg_semaphore_wait() which will cause one thread to
block and hence pass control (via scheduling to another thread) before
the interrupts have been enabled (a side effect of calling 
cyg_scheduler_start - which happens when cyg_user_start completes),
then yes the other thread will run, but since there are no interrupts,
the cyg_thread_delay will never complete.

-- 
------------------------------------------------------------
Gary Thomas                 |  Consulting for the
MLB Associates              |    Embedded world
------------------------------------------------------------


-- 
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]