OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 2:11 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Thread sleeping
PostPosted: Tue May 19, 2020 10:29 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Hello,
I am trying to make a thread sleeping system. It will work with timer and IO. I was wondering what the best way to make it is.
Thank you for your help

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: Thread sleeping
PostPosted: Tue May 19, 2020 11:38 am 
Offline
Member
Member
User avatar

Joined: Mon Jun 05, 2006 11:00 pm
Posts: 2293
Location: USA (and Australia)
A simple solution:

Have a linked list of awake threads. Each time the scheduler is called, enter the next thread on this list.

When threads go to 'sleep', remove them from this linked list.

To wake a thread up, add it back to this linked list.

Somewhere in your thread object, have a boolean if the thread is awake/asleep so you don't add to the link list twice.

IO is a little more complicated. You could send messages to threads and have a "Get Next Message" function that sleeps if there are no messages. (And the sender will wake it if it's sleeping for a message.)

_________________
My OS is Perception.


Top
 Profile  
 
 Post subject: Re: Thread sleeping
PostPosted: Wed May 20, 2020 5:06 pm 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 402
nexos wrote:
Hello,
I am trying to make a thread sleeping system. It will work with timer and IO. I was wondering what the best way to make it is.
Thank you for your help


I use condition variables in general.

Then your interrupt handler signals the condition variable, it'll wake up your thread that is waiting for the interrupt.

So long as your condition variable and mutex are properly implemented, you should never lose any interrupt signals, and will be a good test of your synchronization primitives to ensure they work in an interrupt environment.

My timer is a bit simpler, using just a spinlock to protect timer structures and running callbacks in the interrupt context. But even there, for a thread sleep, I still use a condition variable to communicate between the timer callback and the thread that is sleeping. That way, you can also interrupt the sleep by signalling the condition variable early before the timer expires.


Top
 Profile  
 
 Post subject: Re: Thread sleeping
PostPosted: Thu May 21, 2020 1:50 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
I just finished it. It uses a event system, which is somewhat modeled after Windows's WaitForSingleObject. You can find it at https://github.com/NexSuite/NexNix/blob/dev-codebasefix/kernel/proc/event.c. Thank you all for your help!

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 219 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group