Software Engineering Talk
Software Engineering Talk
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

mutex implementation

 
Post new topic   Reply to topic    Software Engineering Talk Forum Index -> Software Engineering Talk
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon Jun 23, 2008 12:12 am    Post subject: mutex implementation Reply with quote

I am implementing a kernel mutex which will block a thread if it
cannot acquire the mutex. Below is my attempt at this. I have just
wrapped the entire down() and up() functions within a common spinlock,
to stop race conditions between the actual mutex modification and the
wait queue operations. Is this code correct? And if so, is it
efficient (ie. is there a better way of doing this). I looked to the
Linux source for guidance but I found it too hard to follow.

void down(semaphore * sem)
{
spinlock_acquire(sem->spinlock);
if(atomic_dec(sem->count) >= 0)
{
spinlock_release(sem->spinlock);
return;
}
/*adds current thread t queue tail and marks as blocked */
add_waitq(current_thread, sem->waitq);
spinlock_release(sem->spinlock);
schedule();
}

void up(semaphore * sem)
{
spinlock_acquire(sem->spinlock);
if(atomic_inc(sem->count) <= 0)
{
/* removes thread from head of queue and marks runnable */
remove_waitq(sem->waitq);
}
spinlock_release(sem->spinlock);
}
Back to top
  Ads
Advertising
Sponsor


Alexei A. Frounze
Guest





PostPosted: Mon Jun 23, 2008 10:41 am    Post subject: Re: mutex implementation Reply with quote

On Jun 22, 5:12 pm, boroph...@gmail.com wrote:
Quote:
I am implementing a kernel mutex which will block a thread if it
cannot acquire the mutex. Below is my attempt at this. I have just
wrapped the entire down() and up() functions within a common spinlock,
to stop race conditions between the actual mutex modification and the
wait queue operations. Is this code correct? And if so, is it
efficient (ie. is there a better way of doing this). I looked to the
Linux source for guidance but I found it too hard to follow.

void down(semaphore * sem)
{
spinlock_acquire(sem->spinlock);
if(atomic_dec(sem->count) >= 0)
{
spinlock_release(sem->spinlock);
return;
}
/*adds current thread t queue tail and marks as blocked */
add_waitq(current_thread, sem->waitq);
spinlock_release(sem->spinlock);
schedule();

}

void up(semaphore * sem)
{
spinlock_acquire(sem->spinlock);
if(atomic_inc(sem->count) <= 0)
{
/* removes thread from head of queue and marks runnable */
remove_waitq(sem->waitq);
}
spinlock_release(sem->spinlock);

}

Assuming atomic_dec() and atomic_inc() both return the value after the
inc/dec it's correct.
However, I think that the spinlock can be removed if inc/dec are
implemented as a single instruction with the LOCK prefix.

Alex
Back to top
  Ads
Advertising
Sponsor


DG.Ward
Guest





PostPosted: Thu Jul 03, 2008 12:19 am    Post subject: Re: mutex implementation Reply with quote

Alexei A. Frounze wrote:
Quote:
On Jun 22, 5:12 pm, boroph...@gmail.com wrote:
I am implementing a kernel mutex which will block a thread if it
cannot acquire the mutex. Below is my attempt at this. I have just
wrapped the entire down() and up() functions within a common spinlock,
to stop race conditions between the actual mutex modification and the
wait queue operations. Is this code correct? And if so, is it
efficient (ie. is there a better way of doing this). I looked to the
Linux source for guidance but I found it too hard to follow.

void down(semaphore * sem)
{
spinlock_acquire(sem->spinlock);
if(atomic_dec(sem->count) >= 0)
{
spinlock_release(sem->spinlock);
return;
}
/*adds current thread t queue tail and marks as blocked */
add_waitq(current_thread, sem->waitq);
spinlock_release(sem->spinlock);
schedule();

}

void up(semaphore * sem)
{
spinlock_acquire(sem->spinlock);
if(atomic_inc(sem->count) <= 0)
{
/* removes thread from head of queue and marks runnable */
remove_waitq(sem->waitq);
}
spinlock_release(sem->spinlock);

}

Assuming atomic_dec() and atomic_inc() both return the value after the
inc/dec it's correct.
However, I think that the spinlock can be removed if inc/dec are
implemented as a single instruction with the LOCK prefix.

Alex

Hey, Am I wrong in thinking that most architectures have a
Bit-Te
-- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
Back to top
  Ads
Advertising
Sponsor


Maxim S. Shatskih
Guest





PostPosted: Thu Jul 03, 2008 11:05 am    Post subject: Re: mutex implementation Reply with quote

Quote:
Hey, Am I wrong in thinking that most architectures have a
Bit-Te

Yes, and this instruction is used to implement a spinlock.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com
Back to top
  Ads
Advertising
Sponsor


Display posts from previous:   
Post new topic   Reply to topic    Software Engineering Talk Forum Index -> Software Engineering Talk All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum

Australian Debt Consolidation Experts
medical insurance
Wedding
Search Escorts and girls (incall/OutCall) online with Oasi2000, ItaliaChiamami, BestAnnunci...
US Swinging
Computers
personal secured loans
Make Your Own Website
Cheap phone calls to India
Long island Cleaning service
Mold
UK Swingers Genuine Contacts Site
floor machines
Vacuum Cleaner Parts


Board Security

125 Attacks blocked

Powered by phpBB © 2001, 2005 phpBB Group