bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

a recursive lock prototype


From: moshez
Subject: a recursive lock prototype
Date: Thu, 5 Jul 2001 02:47:58 +0200 (CEST)

#include <cthreads.h>
#include <hurd/hurd_types.h>

#define RECURSIVE_LOCK_INITIALIZER = {0, 0, MUTEX_INITIALIZER};

struct recursive_lock
{
  spin_lock_t protection;
  int locked;
  struct mutex mutex;
};

void recursive_lock (struct recursive_lock *lock);
void recursive_unlock (struct recursive_lock *lock);

void
recursive_lock (struct recursive_lock *lock)
{
  spin_lock (&lock->protection);
  if (lock->locked)
    {
      lock->locked++;
      spin_unlock (&lock->protection);
    }
  else
    {
      spin_unlock (&lock->protection);
      mutex_lock (&lock->mutex) spin_lock (&lock->protection);
      lock->locked = 1;
      spin_unlock (&lock->protection);
    }
}

void
recursive_unlock (struct recursive_lock *lock)
{
  spin_lock (&lock->protection);
  if (!lock->locked)
    {
      spin_unlock (&lock->protection);
      return;
    }
  lock->locked--;
  if (!lock->locked)
    mutex_unlock (&lock->mutex) spin_unlock (&lock->protection);
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]