1 /*
2  * pthread_rwlock_rdlock.c
3  *
4  * Description:
5  * This translation unit implements read/write lock primitives.
6  *
7  * --------------------------------------------------------------------------
8  *
9  *      Pthreads4w - POSIX Threads for Windows
10  *      Copyright 1998 John E. Bossom
11  *      Copyright 1999-2018, Pthreads4w contributors
12  *
13  *      Homepage: https://sourceforge.net/projects/pthreads4w/
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  *
20  *      https://sourceforge.net/p/pthreads4w/wiki/Contributors/
21  *
22  * Licensed under the Apache License, Version 2.0 (the "License");
23  * you may not use this file except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *     http://www.apache.org/licenses/LICENSE-2.0
27  *
28  * Unless required by applicable law or agreed to in writing, software
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  */
34 
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38 
39 #include <limits.h>
40 
41 #include "pthread.h"
42 #include "implement.h"
43 
44 int
pthread_rwlock_rdlock(pthread_rwlock_t * rwlock)45 pthread_rwlock_rdlock (pthread_rwlock_t * rwlock)
46 {
47   int result;
48   pthread_rwlock_t rwl;
49 
50   if (rwlock == NULL || *rwlock == NULL)
51     {
52       return EINVAL;
53     }
54 
55   /*
56    * We do a quick check to see if we need to do more work
57    * to initialise a static rwlock. We check
58    * again inside the guarded section of __ptw32_rwlock_check_need_init()
59    * to avoid race conditions.
60    */
61   if (*rwlock == PTHREAD_RWLOCK_INITIALIZER)
62     {
63       result = __ptw32_rwlock_check_need_init (rwlock);
64 
65       if (result != 0 && result != EBUSY)
66 	{
67 	  return result;
68 	}
69     }
70 
71   rwl = *rwlock;
72 
73   if (rwl->nMagic !=  __PTW32_RWLOCK_MAGIC)
74     {
75       return EINVAL;
76     }
77 
78   if ((result = pthread_mutex_lock (&(rwl->mtxExclusiveAccess))) != 0)
79     {
80       return result;
81     }
82 
83   if (++rwl->nSharedAccessCount == INT_MAX)
84     {
85       if ((result =
86 	   pthread_mutex_lock (&(rwl->mtxSharedAccessCompleted))) != 0)
87 	{
88 	  (void) pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
89 	  return result;
90 	}
91 
92       rwl->nSharedAccessCount -= rwl->nCompletedSharedAccessCount;
93       rwl->nCompletedSharedAccessCount = 0;
94 
95       if ((result =
96 	   pthread_mutex_unlock (&(rwl->mtxSharedAccessCompleted))) != 0)
97 	{
98 	  (void) pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
99 	  return result;
100 	}
101     }
102 
103   return (pthread_mutex_unlock (&(rwl->mtxExclusiveAccess)));
104 }
105