OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Mutex with “pause” assembly instruction
PostPosted: Wed Mar 01, 2023 9:40 pm 
Offline
Member
Member

Joined: Tue Sep 13, 2022 9:29 pm
Posts: 61
I am implementing a lock function for my memory allocator but was curious what exactly the lock function should do. I am aware that it is intended to prevent other changes to memory while the current change is in progress. However, any implementation of a mutex simply tells the CPU to pause and I was wondering if this would be correct. To my knowledge, pausing the CPU would prevent even the current task with memory from executing and nothing would be accomplished. Sorry if this is a stupid question, just thought I’d ask.

Example Code:
Code:
typedef volatile int mutex_t;

void acquire_mutex(mutex_t* mutex)
{
   while(!__sync_bool_compare_and_swap(mutex, 0, 1))
   {
      asm("pause");
   }
}

void release_mutex(mutex_t* mutex)
{
   *mutex = 0;
}


Top
 Profile  
 
 Post subject: Re: Mutex with “pause” assembly instruction
PostPosted: Wed Mar 01, 2023 10:06 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
FunnyGuy9796 wrote:
However, any implementation of a mutex simply tells the CPU to pause and I was wondering if this would be correct.

It tells the CPU to pause while another CPU holds the lock. This is the correct behavior when the lock will be held by another CPU for only a short period of time, since there's nothing useful the current CPU can do while it waits for the other CPU to release the lock.

In systems where there is only one CPU, or systems where the lock is likely to be held for a long time, you might instead choose to yield the current time slice while waiting for the lock to be released.

FunnyGuy9796 wrote:
Example Code:

That example code is old and bad. You should use stdatomic.h (in C) or std::atomic (in C++) to implement your mutex. In fact, how about I fix that on the wiki...


Top
 Profile  
 
 Post subject: Re: Mutex with “pause” assembly instruction
PostPosted: Thu Mar 02, 2023 12:40 am 
Offline
Member
Member

Joined: Sat Feb 04, 2012 5:03 pm
Posts: 111
PAUSE on intel processors, as Octocontrabass says, delays execution of the next instruction, and may give the processor a hint how to predict the next branch instruction. It's even backward-compatible with old processors, which will see it as a NOP.

HLT on intel processors prevents execution, which might have been what you were thinking of. HLT is sometimes used to place the CPU in a halted state until the next interrupt wakes the CPU up.


Top
 Profile  
 
 Post subject: Re: Mutex with “pause” assembly instruction
PostPosted: Thu Mar 02, 2023 1:18 am 
Offline
Member
Member

Joined: Wed Oct 01, 2008 1:55 pm
Posts: 3192
The example code locks more like a spinlock than a mutex. A mutex should be blocked (by the scheduler) when the mutex is busy, and when the mutex is released, any blocked thread should be resumed. A spinlock can be used in the implementation of the mutex, or in the scheduler, to ensure multicore operation. A spinlock should generally also disable interrupts to avoid excessive spinning if an interrupt happens when the critical code is executed.

An alternative to using spinlocks is to write lock-free code by using xchg or locked instructions.


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