| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Mon Jun 23, 2008 12:12 am Post subject: mutex implementation |
|
|
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
|
Posted: Mon Jun 23, 2008 10:41 am Post subject: Re: mutex implementation |
|
|
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
|
Posted: Thu Jul 03, 2008 12:19 am Post subject: Re: mutex implementation |
|
|
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
|
Posted: Thu Jul 03, 2008 11:05 am Post subject: Re: mutex implementation |
|
|
| 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
|
|
|
|
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
|

125 Attacks blocked
Powered by phpBB © 2001, 2005 phpBB Group
|