  557



  .
      [EINTR].



       
   - (   )  -
 .

typedef struct {

pthread_mutex_t lock;

pthread_cond_t rcond,
wcond;

int lock_count;

/* lock_count < 0 ..   . */
/* lock_count > 0 ..  lock_count 

*                                            . */
/* lock_count = 0 ..   . */
int waiting_writers; /*   

* . */
} rwlock;

void

waiting_reader_cleanup (void. *arg)
{

rwlock *1;

1 = (rwlock *) arg;

pthread_mutex_unlock (&l->lock) ;

}

void

lock_for_read (rwlock *1)
{

pthread_mutex_lock (&l->lock) ;

pthread_cleanup_push (waiting_reader_cleanup, 1) ;

while ((1->lock_count < 0) && (1->waiting_writers ! = 0) )
pthread_cond_wait (&l->rcond, &l->lock) ;

1->lock_count++ ;

/*

*    ,   pthread_cleanup_pop()

*    waiting_reader_cleanup().

*/
pthread_cleanup_pop(1) ;

}

void

release_read_lock (rwlock *1)

{

pthread_mutex_lock (&l->lock) ;

if (--1->lock_count == 0)

pthread_cond_signal (&l->wcond) ;

pthread_mutex_unlock (1);

}