123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #include <errno.h>
- #include <limits.h>
- #include "pthread.h"
- #include "implement.h"
- int
- pthread_rwlock_rdlock (pthread_rwlock_t * rwlock)
- {
- int result;
- pthread_rwlock_t rwl;
- if (rwlock == NULL || *rwlock == NULL)
- {
- return EINVAL;
- }
-
- if (*rwlock == PTHREAD_RWLOCK_INITIALIZER)
- {
- result = pte_rwlock_check_need_init (rwlock);
- if (result != 0 && result != EBUSY)
- {
- return result;
- }
- }
- rwl = *rwlock;
- if (rwl->nMagic != PTE_RWLOCK_MAGIC)
- {
- return EINVAL;
- }
- if ((result = pthread_mutex_lock (&(rwl->mtxExclusiveAccess))) != 0)
- {
- return result;
- }
- if (++rwl->nSharedAccessCount == INT_MAX)
- {
- if ((result =
- pthread_mutex_lock (&(rwl->mtxSharedAccessCompleted))) != 0)
- {
- (void) pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
- return result;
- }
- rwl->nSharedAccessCount -= rwl->nCompletedSharedAccessCount;
- rwl->nCompletedSharedAccessCount = 0;
- if ((result =
- pthread_mutex_unlock (&(rwl->mtxSharedAccessCompleted))) != 0)
- {
- (void) pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
- return result;
- }
- }
- return (pthread_mutex_unlock (&(rwl->mtxExclusiveAccess)));
- }
|