OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:33 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Thread safety with priorities
PostPosted: Mon Jun 20, 2016 9:52 am 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
Combuster wrote:
Tickets with target threads is as simple as dropping in circular_buffer[ticket_number % buffer_size] = my_thread_id before checking if you have the lock or you have to sleep instead. The exiting thread can remove its thread id slot and wake the next thread if there happens to be one registered.

Just to follow up on that, sleeping-waking code is a caveat. Something as simple as:
Code:
if (needs_to_sleep)
{
    sleep();
}
(...)
    wake_thread_if_sleeping();
Is a race condition, if the wake is performed between the if and the sleep. Instead you can modify wake to prevent the next sleep() call from occurring if the thread is running, or you can do something else/more complicated. It's actually a more fundamental primitive than setting up mutexes.

One question though: what about if we're interrupted after getting the ticket, but before placing our thread ID into the circular_buffer?

How would the unlocking thread then know to wake us up?


Top
 Profile  
 
 Post subject: Re: Thread safety with priorities
PostPosted: Tue Jun 21, 2016 3:46 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
If the thread gets interrupted, it does not sleep, so there's nothing requiring to be woken up in the first place.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: Thread safety with priorities
PostPosted: Tue Jun 21, 2016 11:13 am 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
Combuster wrote:
If the thread gets interrupted, it does not sleep, so there's nothing requiring to be woken up in the first place.

I meant to ask what happens if it gets woken up before it manages to place its ID in the buffer. But now I realised that this can easily be solved by proper order of operations in the unlock() function (basically, if it hasn't placed its ID in the buffer yet, it means it hasn't checked its ticket yet, and when it does it will see that it's now its turn, so it never goes to sleep).


Top
 Profile  
 
 Post subject: Re: Thread safety with priorities
PostPosted: Wed Jun 22, 2016 12:38 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Of course nobody is going to wake you either when you haven't even told them what your ID is, so why is that suddenly a scenario? :mrgreen:


I know, concurrency can be mindboggling.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: DotBot [Bot] and 28 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group