Листинг 8.2. Функция pthread_rwlock_init: инициализация блокировки чтения-записи
//my_rwlock/pthread_rwlock_init.с
1 #include "unpipc.h"
2 #include "pthread_rwlock.h"
3 int
4 pthread_rwlock_init(pthread_rwlock_t *rw, pthread_rwlockattr_t *attr)
5 {
6 int result;
7 if (attr != NULL)
8 return(EINVAL); /* not supported */
9 if ((result = pthread_mutex_init(&rw->rw_mutex, NULL)) != 0)
10 goto err1;
11 if ((result = pthread_cond_init(&rw->rw_condreaders, NULL)) != 0)
12 goto err2;
13 if ((result = pthread_cond_init(&rw->rw_condwriters, NULL)) != 0)
14 goto err3;
15 rw->rw_nwaitreaders = 0;
16 rw->rw_nwaitwriters = 0;
17 rw->rw_refcount = 0;
18 rw->rw_magic = RW_MAGIC;
19 return(0);
20 err3:
21 pthread_cond_destroy(&rw->rw_condreaders);
22 err2;
23 pthread_mutex_destroy(&rw->rw_mutex);
24 err1:
25 return(result); /* значение errno */
26 }