OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Mar 19, 2024 5:20 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Spinlock with Memory Allocation
PostPosted: Thu Jan 12, 2023 6:19 pm 
Offline
Member
Member

Joined: Tue Sep 13, 2022 9:29 pm
Posts: 61
I was wondering how a spinlock would come into play in terms of locking or unlocking memory data structures. I read the spinlock article and kind of understand how it works. Although, I am curious as to how it does not lock all memory and only target a certain memory address.

Code:
acquireLock:
    lock bts [lock],0
    jc .spin_with_pause
    ret
 
.spin_with_pause:
    pause
    test dword [lock],1
    jnz .spin_with_pause
    jmp acquireLock
 
releaseLock:
    mov dword [lock],0
    ret


How does this code know what memory data structure to lock? Please excuse my ignorance.


Top
 Profile  
 
 Post subject: Re: Spinlock with Memory Allocation
PostPosted: Thu Jan 12, 2023 8:03 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5069
FunnyGuy9796 wrote:
I read the spinlock article

The example C code in that article is very old. Perhaps it's time for me to update it...

FunnyGuy9796 wrote:
How does this code know what memory data structure to lock?

It doesn't. You need a separate lock variable for each structure you want to lock. The example code always uses the same lock variable, so it can only lock one thing.


Top
 Profile  
 
 Post subject: Re: Spinlock with Memory Allocation
PostPosted: Thu Jan 12, 2023 8:37 pm 
Offline
Member
Member

Joined: Tue Sep 13, 2022 9:29 pm
Posts: 61
Thank you! I had realized how to change the lock variable shortly after posting and I am now kicking myself for not realizing it was a simple variable before :)


Top
 Profile  
 
 Post subject: Re: Spinlock with Memory Allocation
PostPosted: Fri Jan 13, 2023 7:56 am 
Offline
Member
Member

Joined: Wed Oct 01, 2008 1:55 pm
Posts: 3181
Spinlocks are only required when some data is shared between IRQs and mainline code. Spinlocks should not be used as a general lock. For this, it's better to implement semaphores / critical sections that never will waste CPU cycles by other CPUs spinning to get the lock. Instead, ordinary locks should block the thread until the lock becomes available.


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: Bing [Bot], SemrushBot [Bot] and 16 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