Implementing Sleep Syscall

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Implementing Sleep Syscall

Post 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
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Post 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.
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post 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.
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Post 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")".
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Post by gzaloprgm »

Thanks, asm("int $32") was exactly what I was looking for.
Gonzalo
Post Reply