Page 1 of 1

Implementing Sleep Syscall

Posted: Fri Mar 21, 2008 12:58 pm
by gzaloprgm
Hi!

I have multitasking and I'm building the syscall handler, but I got stuck here.

Which is the better way to implement an sleep syscall?

This is my code so far and it runs in another thread (not task).

Code: Select all

void a(){
	while(1){
        	asm volatile("int $0x30" :: "a"(3));
                //Syscall Function 3 - Get System ticks

		unsigned int start;
		asm volatile("movl %%eax, %0" :: "g"(start));
                //Put current ticks in start

		unsigned int now;
		start+=1000;
                //Add 1000 for target time

		asm volatile("int $0x30" :: "a"(2), "b"('a'));
                //Syscall function 2, prints 'a'

		for(;;){
		        asm volatile("int $0x30" :: "a"(3));
			asm volatile("movl %%eax, %0" :: "g"(now));
			int a = start-now;
			if(a<0) break;
                        //The 'Wait' function
		}
	}
}
Is there any better way to implement it? What do you use in your kernel?

Cheers,
Gonzalo

Posted: Fri Mar 21, 2008 1:07 pm
by cyr1x
This halts the computer to the next task-switch completly. You should have a "sleep"-state or something in your scheduler. When a thread wants to sleep it gets removed from the task-queue and added to the sleep-queue. Then on each tick( or whenever you want) you check if the thread should be woken up by iterating through the queue or decrement the sleep counter if not.

Posted: Fri Mar 21, 2008 1:13 pm
by gzaloprgm
Thanks, so basically I should yield to next process as in cooperative multitasking,

Code: Select all

if(a<0) break;
yield();
Or let the kernel manage the times disabling the process until it should wake up.

Now my question is: Is there any way of forcing the IRQ0 to fire?

Thanks,
Gonzalo.

Posted: Fri Mar 21, 2008 1:24 pm
by cyr1x
gzaloprgm wrote: Now my question is: Is there any way of forcing the IRQ0 to fire?
:?:
Do you want to call the scheduler? Then "schedule()" or "asm("int $33")".

Posted: Fri Mar 21, 2008 8:03 pm
by gzaloprgm
Thanks, asm("int $32") was exactly what I was looking for.
Gonzalo