apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@apache.org
Subject cvs commit: apr/locks/win32 thread_rwlock.c
Date Sat, 15 Sep 2001 00:58:38 GMT
rbb         01/09/14 17:58:38

  Modified:    include/arch/win32 thread_rwlock.h
               locks/win32 thread_rwlock.c
  Log:
  Implement read/write locks on Windows.  This passes the tests
  in CVS, but I'm not 100% convinced they are correct.  Could people
  with more Windows experience look these over.
  
  Revision  Changes    Path
  1.2       +5 -0      apr/include/arch/win32/thread_rwlock.h
  
  Index: thread_rwlock.h
  ===================================================================
  RCS file: /home/cvs/apr/include/arch/win32/thread_rwlock.h,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- thread_rwlock.h	2001/09/08 23:36:34	1.1
  +++ thread_rwlock.h	2001/09/15 00:58:38	1.2
  @@ -59,6 +59,11 @@
   
   struct apr_thread_rwlock_t {
       apr_pool_t *pool;
  +    HANDLE readevent;
  +    HANDLE mutex;
  +    HANDLE writemutex;
  +    int counter;
  +    int wrcounter;
   };
   
   #endif  /* THREAD_RWLOCK_H */
  
  
  
  1.2       +39 -5     apr/locks/win32/thread_rwlock.c
  
  Index: thread_rwlock.c
  ===================================================================
  RCS file: /home/cvs/apr/locks/win32/thread_rwlock.c,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- thread_rwlock.c	2001/09/08 23:36:35	1.1
  +++ thread_rwlock.c	2001/09/15 00:58:38	1.2
  @@ -59,15 +59,35 @@
   #include "win32/thread_rwlock.h"
   #include "apr_portable.h"
   
  +static apr_status_t thread_rwlock_cleanup(void *data)
  +{
  +    apr_thread_rwlock_t *rwlock = data;
  +    CloseHandle(rwlock->readevent);
  +    CloseHandle(rwlock->mutex);
  +    CloseHandle(rwlock->writemutex);
  +    return APR_SUCCESS;
  +}
  +
   APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
                                                      apr_pool_t *pool)
   {
  -    return APR_ENOTIMPL;
  +    (*rwlock) = apr_palloc(pool, sizeof(**rwlock));
  +    (*rwlock)->readevent=CreateEvent(NULL,TRUE,FALSE,NULL);
  +    (*rwlock)->mutex = CreateEvent(NULL,FALSE,TRUE,NULL);
  +    (*rwlock)->writemutex = CreateMutex(NULL,FALSE,NULL);
  +    (*rwlock)->counter = -1;
  +    (*rwlock)->wrcounter = 0;
  +    return APR_SUCCESS;
   }
   
   APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
   {
  -    return APR_ENOTIMPL;
  +    if (InterlockedIncrement(&rwlock->counter) == 0) {
  +        WaitForSingleObject(rwlock->mutex, INFINITE);
  +        SetEvent(rwlock->readevent);
  +    }
  +    WaitForSingleObject(rwlock->readevent,INFINITE);
  +    return APR_SUCCESS;
   }
   
   APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
  @@ -77,7 +97,10 @@
   
   APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
   {
  -    return APR_ENOTIMPL;
  +    WaitForSingleObject(rwlock->writemutex,INFINITE);
  +    WaitForSingleObject(rwlock->mutex, INFINITE);
  +    rwlock->wrcounter++;
  +    return APR_SUCCESS;
   }
   
   APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
  @@ -87,11 +110,22 @@
   
   APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
   {
  -    return APR_ENOTIMPL;
  +    if (rwlock->wrcounter) {
  +        /* If wrcounter is > 0, then we must have a writer lock */
  +        SetEvent(rwlock->mutex);
  +        ReleaseMutex(rwlock->writemutex);
  +    } 
  +    else {
  +        if (InterlockedDecrement(&rwlock->counter) < 0) {
  +            ResetEvent(rwlock->readevent);
  +            SetEvent(rwlock->mutex);
  +        }
  +    } 
  +    return APR_SUCCESS;
   }
   
   APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
   {
  -    return APR_ENOTIMPL;
  +    return apr_pool_cleanup_run(rwlock->pool, rwlock, thread_rwlock_cleanup);
   }
   
  
  
  

Mime
View raw message