Home | History | Annotate | Download | only in pthreads-win32
      1 /*
      2  * pthread_mutexattr_settype.c
      3  *
      4  * Description:
      5  * This translation unit implements mutual exclusion (mutex) primitives.
      6  *
      7  * --------------------------------------------------------------------------
      8  *
      9  *      Pthreads-win32 - POSIX Threads Library for Win32
     10  *      Copyright(C) 1998 John E. Bossom
     11  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
     12  *
     13  *      Contact Email: rpj (at) callisto.canberra.edu.au
     14  *
     15  *      The current list of contributors is contained
     16  *      in the file CONTRIBUTORS included with the source
     17  *      code distribution. The list can also be seen at the
     18  *      following World Wide Web location:
     19  *      http://sources.redhat.com/pthreads-win32/contributors.html
     20  *
     21  *      This library is free software; you can redistribute it and/or
     22  *      modify it under the terms of the GNU Lesser General Public
     23  *      License as published by the Free Software Foundation; either
     24  *      version 2 of the License, or (at your option) any later version.
     25  *
     26  *      This library is distributed in the hope that it will be useful,
     27  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
     28  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     29  *      Lesser General Public License for more details.
     30  *
     31  *      You should have received a copy of the GNU Lesser General Public
     32  *      License along with this library in the file COPYING.LIB;
     33  *      if not, write to the Free Software Foundation, Inc.,
     34  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
     35  */
     36 
     37 #include "pthread.h"
     38 #include "implement.h"
     39 
     40 
     41 int
     42 pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind)
     43      /*
     44       * ------------------------------------------------------
     45       *
     46       * DOCPUBLIC
     47       * The pthread_mutexattr_settype() and
     48       * pthread_mutexattr_gettype() functions  respectively set and
     49       * get the mutex type  attribute. This attribute is set in  the
     50       * type parameter to these functions.
     51       *
     52       * PARAMETERS
     53       *      attr
     54       *              pointer to an instance of pthread_mutexattr_t
     55       *
     56       *      type
     57       *              must be one of:
     58       *
     59       *                      PTHREAD_MUTEX_DEFAULT
     60       *
     61       *                      PTHREAD_MUTEX_NORMAL
     62       *
     63       *                      PTHREAD_MUTEX_ERRORCHECK
     64       *
     65       *                      PTHREAD_MUTEX_RECURSIVE
     66       *
     67       * DESCRIPTION
     68       * The pthread_mutexattr_settype() and
     69       * pthread_mutexattr_gettype() functions  respectively set and
     70       * get the mutex type  attribute. This attribute is set in  the
     71       * type  parameter to these functions. The default value of the
     72       * type  attribute is  PTHREAD_MUTEX_DEFAULT.
     73       *
     74       * The type of mutex is contained in the type  attribute of the
     75       * mutex attributes. Valid mutex types include:
     76       *
     77       * PTHREAD_MUTEX_NORMAL
     78       *          This type of mutex does  not  detect  deadlock.  A
     79       *          thread  attempting  to  relock  this mutex without
     80       *          first unlocking it will  deadlock.  Attempting  to
     81       *          unlock  a  mutex  locked  by  a  different  thread
     82       *          results  in  undefined  behavior.  Attempting   to
     83       *          unlock  an  unlocked  mutex  results  in undefined
     84       *          behavior.
     85       *
     86       * PTHREAD_MUTEX_ERRORCHECK
     87       *          This type of  mutex  provides  error  checking.  A
     88       *          thread  attempting  to  relock  this mutex without
     89       *          first unlocking it will return with  an  error.  A
     90       *          thread  attempting to unlock a mutex which another
     91       *          thread has locked will return  with  an  error.  A
     92       *          thread attempting to unlock an unlocked mutex will
     93       *          return with an error.
     94       *
     95       * PTHREAD_MUTEX_DEFAULT
     96       *          Same as PTHREAD_MUTEX_NORMAL.
     97       *
     98       * PTHREAD_MUTEX_RECURSIVE
     99       *          A thread attempting to relock this  mutex  without
    100       *          first  unlocking  it  will  succeed in locking the
    101       *          mutex. The relocking deadlock which can occur with
    102       *          mutexes of type  PTHREAD_MUTEX_NORMAL cannot occur
    103       *          with this type of mutex. Multiple  locks  of  this
    104       *          mutex  require  the  same  number  of  unlocks  to
    105       *          release  the  mutex  before  another  thread   can
    106       *          acquire the mutex. A thread attempting to unlock a
    107       *          mutex which another thread has locked will  return
    108       *          with  an  error. A thread attempting to  unlock an
    109       *          unlocked mutex will return  with  an  error.  This
    110       *          type  of mutex is only supported for mutexes whose
    111       *          process        shared         attribute         is
    112       *          PTHREAD_PROCESS_PRIVATE.
    113       *
    114       * RESULTS
    115       *              0               successfully set attribute,
    116       *              EINVAL          'attr' or 'type' is invalid,
    117       *
    118       * ------------------------------------------------------
    119       */
    120 {
    121   int result = 0;
    122 
    123   if ((attr != NULL && *attr != NULL))
    124     {
    125       switch (kind)
    126 	{
    127 	case PTHREAD_MUTEX_FAST_NP:
    128 	case PTHREAD_MUTEX_RECURSIVE_NP:
    129 	case PTHREAD_MUTEX_ERRORCHECK_NP:
    130 	  (*attr)->kind = kind;
    131 	  break;
    132 	default:
    133 	  result = EINVAL;
    134 	  break;
    135 	}
    136     }
    137   else
    138     {
    139       result = EINVAL;
    140     }
    141 
    142   return (result);
    143 }				/* pthread_mutexattr_settype */
    144