1*062c5bc4SJason Schmidlapp /*
2*062c5bc4SJason Schmidlapp * pthread_rwlock_destroy.c
3*062c5bc4SJason Schmidlapp *
4*062c5bc4SJason Schmidlapp * Description:
5*062c5bc4SJason Schmidlapp * This translation unit implements read/write lock 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 <stdlib.h>
44*062c5bc4SJason Schmidlapp #include <errno.h>
45*062c5bc4SJason Schmidlapp #include <limits.h>
46*062c5bc4SJason Schmidlapp
47*062c5bc4SJason Schmidlapp #include "pthread.h"
48*062c5bc4SJason Schmidlapp #include "implement.h"
49*062c5bc4SJason Schmidlapp
50*062c5bc4SJason Schmidlapp int
pthread_rwlock_destroy(pthread_rwlock_t * rwlock)51*062c5bc4SJason Schmidlapp pthread_rwlock_destroy (pthread_rwlock_t * rwlock)
52*062c5bc4SJason Schmidlapp {
53*062c5bc4SJason Schmidlapp pthread_rwlock_t rwl;
54*062c5bc4SJason Schmidlapp int result = 0, result1 = 0, result2 = 0;
55*062c5bc4SJason Schmidlapp
56*062c5bc4SJason Schmidlapp if (rwlock == NULL || *rwlock == NULL)
57*062c5bc4SJason Schmidlapp {
58*062c5bc4SJason Schmidlapp return EINVAL;
59*062c5bc4SJason Schmidlapp }
60*062c5bc4SJason Schmidlapp
61*062c5bc4SJason Schmidlapp if (*rwlock != PTHREAD_RWLOCK_INITIALIZER)
62*062c5bc4SJason Schmidlapp {
63*062c5bc4SJason Schmidlapp rwl = *rwlock;
64*062c5bc4SJason Schmidlapp
65*062c5bc4SJason Schmidlapp if (rwl->nMagic != PTE_RWLOCK_MAGIC)
66*062c5bc4SJason Schmidlapp {
67*062c5bc4SJason Schmidlapp return EINVAL;
68*062c5bc4SJason Schmidlapp }
69*062c5bc4SJason Schmidlapp
70*062c5bc4SJason Schmidlapp if ((result = pthread_mutex_lock (&(rwl->mtxExclusiveAccess))) != 0)
71*062c5bc4SJason Schmidlapp {
72*062c5bc4SJason Schmidlapp return result;
73*062c5bc4SJason Schmidlapp }
74*062c5bc4SJason Schmidlapp
75*062c5bc4SJason Schmidlapp if ((result =
76*062c5bc4SJason Schmidlapp pthread_mutex_lock (&(rwl->mtxSharedAccessCompleted))) != 0)
77*062c5bc4SJason Schmidlapp {
78*062c5bc4SJason Schmidlapp (void) pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
79*062c5bc4SJason Schmidlapp return result;
80*062c5bc4SJason Schmidlapp }
81*062c5bc4SJason Schmidlapp
82*062c5bc4SJason Schmidlapp /*
83*062c5bc4SJason Schmidlapp * Check whether any threads own/wait for the lock (wait for ex.access);
84*062c5bc4SJason Schmidlapp * report "BUSY" if so.
85*062c5bc4SJason Schmidlapp */
86*062c5bc4SJason Schmidlapp if (rwl->nExclusiveAccessCount > 0
87*062c5bc4SJason Schmidlapp || rwl->nSharedAccessCount > rwl->nCompletedSharedAccessCount)
88*062c5bc4SJason Schmidlapp {
89*062c5bc4SJason Schmidlapp result = pthread_mutex_unlock (&(rwl->mtxSharedAccessCompleted));
90*062c5bc4SJason Schmidlapp result1 = pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
91*062c5bc4SJason Schmidlapp result2 = EBUSY;
92*062c5bc4SJason Schmidlapp }
93*062c5bc4SJason Schmidlapp else
94*062c5bc4SJason Schmidlapp {
95*062c5bc4SJason Schmidlapp rwl->nMagic = 0;
96*062c5bc4SJason Schmidlapp
97*062c5bc4SJason Schmidlapp if ((result =
98*062c5bc4SJason Schmidlapp pthread_mutex_unlock (&(rwl->mtxSharedAccessCompleted))) != 0)
99*062c5bc4SJason Schmidlapp {
100*062c5bc4SJason Schmidlapp pthread_mutex_unlock (&rwl->mtxExclusiveAccess);
101*062c5bc4SJason Schmidlapp return result;
102*062c5bc4SJason Schmidlapp }
103*062c5bc4SJason Schmidlapp
104*062c5bc4SJason Schmidlapp if ((result =
105*062c5bc4SJason Schmidlapp pthread_mutex_unlock (&(rwl->mtxExclusiveAccess))) != 0)
106*062c5bc4SJason Schmidlapp {
107*062c5bc4SJason Schmidlapp return result;
108*062c5bc4SJason Schmidlapp }
109*062c5bc4SJason Schmidlapp
110*062c5bc4SJason Schmidlapp *rwlock = NULL; /* Invalidate rwlock before anything else */
111*062c5bc4SJason Schmidlapp result = pthread_cond_destroy (&(rwl->cndSharedAccessCompleted));
112*062c5bc4SJason Schmidlapp result1 = pthread_mutex_destroy (&(rwl->mtxSharedAccessCompleted));
113*062c5bc4SJason Schmidlapp result2 = pthread_mutex_destroy (&(rwl->mtxExclusiveAccess));
114*062c5bc4SJason Schmidlapp (void) free (rwl);
115*062c5bc4SJason Schmidlapp }
116*062c5bc4SJason Schmidlapp }
117*062c5bc4SJason Schmidlapp else
118*062c5bc4SJason Schmidlapp {
119*062c5bc4SJason Schmidlapp /*
120*062c5bc4SJason Schmidlapp * See notes in pte_rwlock_check_need_init() above also.
121*062c5bc4SJason Schmidlapp */
122*062c5bc4SJason Schmidlapp
123*062c5bc4SJason Schmidlapp pte_osMutexLock (pte_rwlock_test_init_lock);
124*062c5bc4SJason Schmidlapp
125*062c5bc4SJason Schmidlapp /*
126*062c5bc4SJason Schmidlapp * Check again.
127*062c5bc4SJason Schmidlapp */
128*062c5bc4SJason Schmidlapp if (*rwlock == PTHREAD_RWLOCK_INITIALIZER)
129*062c5bc4SJason Schmidlapp {
130*062c5bc4SJason Schmidlapp /*
131*062c5bc4SJason Schmidlapp * This is all we need to do to destroy a statically
132*062c5bc4SJason Schmidlapp * initialised rwlock that has not yet been used (initialised).
133*062c5bc4SJason Schmidlapp * If we get to here, another thread
134*062c5bc4SJason Schmidlapp * waiting to initialise this rwlock will get an EINVAL.
135*062c5bc4SJason Schmidlapp */
136*062c5bc4SJason Schmidlapp *rwlock = NULL;
137*062c5bc4SJason Schmidlapp }
138*062c5bc4SJason Schmidlapp else
139*062c5bc4SJason Schmidlapp {
140*062c5bc4SJason Schmidlapp /*
141*062c5bc4SJason Schmidlapp * The rwlock has been initialised while we were waiting
142*062c5bc4SJason Schmidlapp * so assume it's in use.
143*062c5bc4SJason Schmidlapp */
144*062c5bc4SJason Schmidlapp result = EBUSY;
145*062c5bc4SJason Schmidlapp }
146*062c5bc4SJason Schmidlapp
147*062c5bc4SJason Schmidlapp pte_osMutexUnlock(pte_rwlock_test_init_lock);
148*062c5bc4SJason Schmidlapp
149*062c5bc4SJason Schmidlapp }
150*062c5bc4SJason Schmidlapp
151*062c5bc4SJason Schmidlapp return ((result != 0) ? result : ((result1 != 0) ? result1 : result2));
152*062c5bc4SJason Schmidlapp }
153