/* Copyright (C) 1992, the Florida State University
   Distributed by the Florida State University under the terms of the
   GNU Library General Public License.

This file is part of Pthreads.

Pthreads is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation (version 2).

Pthreads is distributed "AS IS" in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with Pthreads; see the file COPYING.  If not, write
to the Free Software Foundation, 675 Mass Ave, Cambridge,
MA 02139, USA.

Report problems and direct all questions to:

  pthreads-bugs@ada.cs.fsu.edu

  @(#)mutex.h	1.16 3/5/93

*/
    
/*
 * Macros to lock and unlock a mutex
 * precondition: SET_KERNEL_FLAG
 * postcondition: call CLEAR_KERNEL_FLAG
 */

/*
 * locking mutex doesn't need test_and_set; it's protected by kernel flag
 */
#ifdef NOERR_CHECK
#define mac_mutex_lock(mutex, p) \
  MACRO_BEGIN \
    q_sleep(&mutex->queue, PRIMARY_QUEUE); \
  MACRO_END
#else
#ifdef MUT_SWITCH
#define mac_mutex_lock(mutex, p) \
  MACRO_BEGIN \
    if (!mutex->lock) { \
      mutex->lock = TRUE; \
      mutex->owner = p; \
      q_deq(&ready, p, PRIMARY_QUEUE); \
      q_primary_enq(&ready, p); \
    } \
    else \
      q_sleep_thread(&mutex->queue, p, PRIMARY_QUEUE); \
  MACRO_END
#else
#define mac_mutex_lock(mutex, p) \
  MACRO_BEGIN \
    if (!mutex->lock) { \
      mutex->lock = TRUE; \
      mutex->owner = p; \
    } \
    else \
      q_sleep_thread(&mutex->queue, p, PRIMARY_QUEUE); \
  MACRO_END
#endif
#endif

/*
 * second parameter is any C statement to be executed before q_wakeup()
 * preconditions: current thread owns the mutex, SET_KERNEL_FLAG
 * postcondition: call CLEAR_KERNEL_FLAG
 */
#ifdef NOERR_CHECK
#define mac_mutex_unlock(mutex, cmd) \
  MACRO_BEGIN \
    cmd; \
    mutex->owner = mutex->queue.head; \
    q_wakeup(&mutex->queue, PRIMARY_QUEUE); \
  MACRO_END
#else
#define mac_mutex_unlock(mutex, cmd) \
  MACRO_BEGIN \
    if (mutex->queue.head != NO_PTHREAD) { \
      cmd; \
      mutex->owner = mutex->queue.head; \
      q_wakeup(&mutex->queue, PRIMARY_QUEUE); \
    } \
    else { \
      mutex->owner = NO_PTHREAD; \
      mutex->lock = FALSE; \
      cmd; \
    } \
  MACRO_END
#endif
