Back to index

openldap  2.4.31
Classes | Defines | Functions
rdwr.c File Reference
#include "portable.h"
#include <ac/stdlib.h>
#include <ac/errno.h>
#include <ac/string.h>
#include <ac/time.h>
#include "ldap-int.h"
#include "ldap_pvt_thread.h"
#include "ldap_thr_debug.h"

Go to the source code of this file.

Classes

struct  ldap_int_thread_rdwr_s

Defines

#define LDAP_THREAD_RDWR_IMPLEMENTATION
#define LDAP_PVT_THREAD_RDWR_VALID   0x0bad

Functions

int ldap_pvt_thread_rdwr_init (ldap_pvt_thread_rdwr_t *rwlock)
int ldap_pvt_thread_rdwr_destroy (ldap_pvt_thread_rdwr_t *rwlock)
int ldap_pvt_thread_rdwr_rlock (ldap_pvt_thread_rdwr_t *rwlock)
int ldap_pvt_thread_rdwr_rtrylock (ldap_pvt_thread_rdwr_t *rwlock)
int ldap_pvt_thread_rdwr_runlock (ldap_pvt_thread_rdwr_t *rwlock)
int ldap_pvt_thread_rdwr_wlock (ldap_pvt_thread_rdwr_t *rwlock)
int ldap_pvt_thread_rdwr_wtrylock (ldap_pvt_thread_rdwr_t *rwlock)
int ldap_pvt_thread_rdwr_wunlock (ldap_pvt_thread_rdwr_t *rwlock)

Class Documentation

struct ldap_int_thread_rdwr_s

Definition at line 54 of file rdwr.c.

Class Members
ldap_pvt_thread_mutex_t ltrw_mutex
int ltrw_r_active
int ltrw_r_wait
ldap_pvt_thread_cond_t ltrw_read
int ltrw_valid
int ltrw_w_active
int ltrw_w_wait
ldap_pvt_thread_cond_t ltrw_write

Define Documentation

#define LDAP_PVT_THREAD_RDWR_VALID   0x0bad

Definition at line 59 of file rdwr.c.

Definition at line 44 of file rdwr.c.


Function Documentation

Definition at line 97 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );
       rw = *rwlock;

       assert( rw != NULL );
       assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );

       if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
              return LDAP_PVT_THREAD_EINVAL;

       ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );

       assert( rw->ltrw_w_active >= 0 ); 
       assert( rw->ltrw_w_wait >= 0 ); 
       assert( rw->ltrw_r_active >= 0 ); 
       assert( rw->ltrw_r_wait >= 0 ); 

       /* active threads? */
       if( rw->ltrw_r_active > 0 || rw->ltrw_w_active > 0) {
              ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
              return LDAP_PVT_THREAD_EBUSY;
       }

       /* waiting threads? */
       if( rw->ltrw_r_wait > 0 || rw->ltrw_w_wait > 0) {
              ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
              return LDAP_PVT_THREAD_EBUSY;
       }

       rw->ltrw_valid = 0;

       ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );

       ldap_pvt_thread_mutex_destroy( &rw->ltrw_mutex );
       ldap_pvt_thread_cond_destroy( &rw->ltrw_read );
       ldap_pvt_thread_cond_destroy( &rw->ltrw_write );

       LDAP_FREE(rw);
       *rwlock = NULL;
       return 0;
}

Here is the caller graph for this function:

Definition at line 74 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );

       rw = (struct ldap_int_thread_rdwr_s *) LDAP_CALLOC( 1,
              sizeof( struct ldap_int_thread_rdwr_s ) );
       if ( !rw )
              return LDAP_NO_MEMORY;

       /* we should check return results */
       ldap_pvt_thread_mutex_init( &rw->ltrw_mutex );
       ldap_pvt_thread_cond_init( &rw->ltrw_read );
       ldap_pvt_thread_cond_init( &rw->ltrw_write );

       rw->ltrw_valid = LDAP_PVT_THREAD_RDWR_VALID;

       *rwlock = rw;
       return 0;
}

Here is the caller graph for this function:

Definition at line 142 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );
       rw = *rwlock;

       assert( rw != NULL );
       assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );

       if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
              return LDAP_PVT_THREAD_EINVAL;

       ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );

       assert( rw->ltrw_w_active >= 0 ); 
       assert( rw->ltrw_w_wait >= 0 ); 
       assert( rw->ltrw_r_active >= 0 ); 
       assert( rw->ltrw_r_wait >= 0 ); 

       if( rw->ltrw_w_active > 0 ) {
              /* writer is active */

              rw->ltrw_r_wait++;

              do {
                     ldap_pvt_thread_cond_wait(
                            &rw->ltrw_read, &rw->ltrw_mutex );
              } while( rw->ltrw_w_active > 0 );

              rw->ltrw_r_wait--;
              assert( rw->ltrw_r_wait >= 0 ); 
       }

#ifdef LDAP_RDWR_DEBUG
       if( rw->ltrw_r_active < MAX_READERS )
              rw->ltrw_readers[rw->ltrw_r_active] = ldap_pvt_thread_self();
       else
              rw->ltrw_more_readers = 1;
#endif
       rw->ltrw_r_active++;


       ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );

       return 0;
}

Here is the caller graph for this function:

Definition at line 190 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );
       rw = *rwlock;

       assert( rw != NULL );
       assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );

       if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
              return LDAP_PVT_THREAD_EINVAL;

       ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );

       assert( rw->ltrw_w_active >= 0 ); 
       assert( rw->ltrw_w_wait >= 0 ); 
       assert( rw->ltrw_r_active >= 0 ); 
       assert( rw->ltrw_r_wait >= 0 ); 

       if( rw->ltrw_w_active > 0) {
              ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
              return LDAP_PVT_THREAD_EBUSY;
       }

#ifdef LDAP_RDWR_DEBUG
       if( rw->ltrw_r_active < MAX_READERS )
              rw->ltrw_readers[rw->ltrw_r_active] = ldap_pvt_thread_self();
       else
              rw->ltrw_more_readers = 1;
#endif
       rw->ltrw_r_active++;

       ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );

       return 0;
}

Definition at line 228 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );
       rw = *rwlock;

       assert( rw != NULL );
       assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );

       if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
              return LDAP_PVT_THREAD_EINVAL;

       ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );

       rw->ltrw_r_active--;
#ifdef LDAP_RDWR_DEBUG
       /* Remove us from the list of readers */
       {
              ldap_pvt_thread_t self = ldap_pvt_thread_self();
              int i, j;
              for( i = j = rw->ltrw_r_active; i >= 0; i--) {
                     if (rw->ltrw_readers[i] == self) {
                            rw->ltrw_readers[i] = rw->ltrw_readers[j];
                            rw->ltrw_readers[j] = 0;
                            break;
                     }
              }
              if( !rw->ltrw_more_readers )
                     assert( i >= 0 );
              else if( j == 0 )
                     rw->ltrw_more_readers = 0;
       }
#endif

       assert( rw->ltrw_w_active >= 0 ); 
       assert( rw->ltrw_w_wait >= 0 ); 
       assert( rw->ltrw_r_active >= 0 ); 
       assert( rw->ltrw_r_wait >= 0 ); 

       if (rw->ltrw_r_active == 0 && rw->ltrw_w_wait > 0 ) {
              ldap_pvt_thread_cond_signal( &rw->ltrw_write );
       }

       ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );

       return 0;
}

Here is the caller graph for this function:

Definition at line 277 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );
       rw = *rwlock;

       assert( rw != NULL );
       assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );

       if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
              return LDAP_PVT_THREAD_EINVAL;

       ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );

       assert( rw->ltrw_w_active >= 0 ); 
       assert( rw->ltrw_w_wait >= 0 ); 
       assert( rw->ltrw_r_active >= 0 ); 
       assert( rw->ltrw_r_wait >= 0 ); 

       if ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 ) {
              rw->ltrw_w_wait++;

              do {
                     ldap_pvt_thread_cond_wait(
                            &rw->ltrw_write, &rw->ltrw_mutex );
              } while ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 );

              rw->ltrw_w_wait--;
              assert( rw->ltrw_w_wait >= 0 ); 
       }

#ifdef LDAP_RDWR_DEBUG
       rw->ltrw_writer = ldap_pvt_thread_self();
#endif
       rw->ltrw_w_active++;

       ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );

       return 0;
}

Here is the caller graph for this function:

Definition at line 319 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );
       rw = *rwlock;

       assert( rw != NULL );
       assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );

       if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
              return LDAP_PVT_THREAD_EINVAL;

       ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );

       assert( rw->ltrw_w_active >= 0 ); 
       assert( rw->ltrw_w_wait >= 0 ); 
       assert( rw->ltrw_r_active >= 0 ); 
       assert( rw->ltrw_r_wait >= 0 ); 

       if ( rw->ltrw_w_active > 0 || rw->ltrw_r_active > 0 ) {
              ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );
              return LDAP_PVT_THREAD_EBUSY;
       }

#ifdef LDAP_RDWR_DEBUG
       rw->ltrw_writer = ldap_pvt_thread_self();
#endif
       rw->ltrw_w_active++;

       ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );

       return 0;
}

Definition at line 354 of file rdwr.c.

{
       struct ldap_int_thread_rdwr_s *rw;

       assert( rwlock != NULL );
       rw = *rwlock;

       assert( rw != NULL );
       assert( rw->ltrw_valid == LDAP_PVT_THREAD_RDWR_VALID );

       if( rw->ltrw_valid != LDAP_PVT_THREAD_RDWR_VALID )
              return LDAP_PVT_THREAD_EINVAL;

       ldap_pvt_thread_mutex_lock( &rw->ltrw_mutex );

       rw->ltrw_w_active--;

       assert( rw->ltrw_w_active >= 0 ); 
       assert( rw->ltrw_w_wait >= 0 ); 
       assert( rw->ltrw_r_active >= 0 ); 
       assert( rw->ltrw_r_wait >= 0 ); 

       if (rw->ltrw_r_wait > 0) {
              ldap_pvt_thread_cond_broadcast( &rw->ltrw_read );

       } else if (rw->ltrw_w_wait > 0) {
              ldap_pvt_thread_cond_signal( &rw->ltrw_write );
       }

#ifdef LDAP_RDWR_DEBUG
       assert( rw->ltrw_writer == ldap_pvt_thread_self() );
       rw->ltrw_writer = 0;
#endif
       ldap_pvt_thread_mutex_unlock( &rw->ltrw_mutex );

       return 0;
}

Here is the caller graph for this function: