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