apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aa...@apache.org
Subject cvs commit: apr CHANGES
Date Wed, 17 Oct 2001 00:33:01 GMT
aaron       01/10/16 17:33:01

  Modified:    include  apr_thread_mutex.h
               include/arch/unix thread_mutex.h
               locks/beos thread_mutex.c
               locks/netware thread_mutex.c
               locks/os2 thread_mutex.c
               locks/unix thread_mutex.c
               locks/win32 thread_mutex.c
               test     testlock.c testlockperf.c
               .        CHANGES
  Log:
  Added a new parameter to apr_thread_mutex_init(). Mutexes are now by
  default not nested, but an init flag can enable them. I added a new test
  to testlockperf to show how much faster non-nested mutexes are. I also
  updated calls to apr_thread_mutex_init() wherever I could find them.
  
  This patch only implements this for Unix (nested locks already existed
  on Unix, so this patch just optionally enables/disables them). I did my
  best to change the function declaration on other platforms, but someone
  will have to double check me. Those other platforms will also have to
  either enable nested locks (sometimes available in their thread
  library) or just do what Unix does.
  
  Revision  Changes    Path
  1.4       +8 -0      apr/include/apr_thread_mutex.h
  
  Index: apr_thread_mutex.h
  ===================================================================
  RCS file: /home/cvs/apr/include/apr_thread_mutex.h,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- apr_thread_mutex.h	2001/09/26 20:55:33	1.3
  +++ apr_thread_mutex.h	2001/10/17 00:33:00	1.4
  @@ -78,13 +78,21 @@
   
   typedef struct apr_thread_mutex_t apr_thread_mutex_t;
   
  +#define APR_THREAD_MUTEX_DEFAULT  0x0
  +#define APR_THREAD_MUTEX_NESTED   0x1
  +
   /**
    * Create and initialize a mutex that can be used to synchronize threads.
    * @param mutex the memory address where the newly created mutex will be
    *        stored.
  + * @param flags Or'ed value of:
  + * <PRE>
  + *           APR_THREAD_MUTEX_NESTED    enable nested (recursive) locks.
  + * </PRE>
    * @param pool the pool from which to allocate the mutex.
    */
   APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  +                                                  unsigned int flags,
                                                     apr_pool_t *pool);
   /**
    * Acquire the lock for the given mutex. If the mutex is already locked,
  
  
  
  1.3       +1 -0      apr/include/arch/unix/thread_mutex.h
  
  Index: thread_mutex.h
  ===================================================================
  RCS file: /home/cvs/apr/include/arch/unix/thread_mutex.h,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- thread_mutex.h	2001/09/05 19:34:01	1.2
  +++ thread_mutex.h	2001/10/17 00:33:00	1.3
  @@ -71,6 +71,7 @@
       pthread_mutex_t mutex;
       apr_os_thread_t owner;
       int owner_ref;
  +    char nested; /* a boolean */
   };
   #endif
   
  
  
  
  1.3       +1 -0      apr/locks/beos/thread_mutex.c
  
  Index: thread_mutex.c
  ===================================================================
  RCS file: /home/cvs/apr/locks/beos/thread_mutex.c,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- thread_mutex.c	2001/09/26 20:55:33	1.2
  +++ thread_mutex.c	2001/10/17 00:33:00	1.3
  @@ -66,6 +66,7 @@
   }
   
   APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  +                                                  unsigned int flags,
                                                     apr_pool_t *pool)
   {
       return APR_ENOTIMPL;
  
  
  
  1.4       +2 -0      apr/locks/netware/thread_mutex.c
  
  Index: thread_mutex.c
  ===================================================================
  RCS file: /home/cvs/apr/locks/netware/thread_mutex.c,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- thread_mutex.c	2001/10/04 21:01:05	1.3
  +++ thread_mutex.c	2001/10/17 00:33:00	1.4
  @@ -68,6 +68,7 @@
   }
   
   APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  +                                                  unsigned int flags,
                                                     apr_pool_t *pool)
   {
       apr_thread_mutex_t *new_mutex = NULL;
  @@ -79,6 +80,7 @@
       }     
       new_mutex->pool = pool;
   
  +    /* FIXME: only use recursive locks if (flags & APR_THREAD_MUTEX_NESTED) */
       new_mutex->mutex = NXMutexAlloc(NX_MUTEX_RECURSIVE, NULL, NULL);
       
       if(new_mutex->mutex == NULL)
  
  
  
  1.4       +2 -0      apr/locks/os2/thread_mutex.c
  
  Index: thread_mutex.c
  ===================================================================
  RCS file: /home/cvs/apr/locks/os2/thread_mutex.c,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- thread_mutex.c	2001/09/26 20:55:33	1.3
  +++ thread_mutex.c	2001/10/17 00:33:00	1.4
  @@ -70,6 +70,7 @@
   
   
   APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  +                                                  unsigned int flags,
                                                     apr_pool_t *pool)
   {
       apr_thread_mutex_t *new_mutex;
  @@ -78,6 +79,7 @@
       new_mutex = (apr_thread_mutex_t *)apr_palloc(pool, sizeof(apr_thread_mutex_t));
       new_mutex->pool = pool;
   
  +    /* FIXME: Can OS/2 do nested (aka recursive) locks natively? */
       rc = DosCreateMutexSem(NULL, &(new_mutex->hMutex), 0, FALSE);
       *mutex = new_mutex;
   
  
  
  
  1.4       +32 -18    apr/locks/unix/thread_mutex.c
  
  Index: thread_mutex.c
  ===================================================================
  RCS file: /home/cvs/apr/locks/unix/thread_mutex.c,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- thread_mutex.c	2001/09/26 20:55:33	1.3
  +++ thread_mutex.c	2001/10/17 00:33:00	1.4
  @@ -74,6 +74,7 @@
   } 
   
   APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  +                                                  unsigned int flags,
                                                     apr_pool_t *pool)
   {
       apr_thread_mutex_t *new_mutex;
  @@ -88,6 +89,7 @@
       }
   
       new_mutex->pool = pool;
  +    new_mutex->nested = flags & APR_THREAD_MUTEX_NESTED;
   
       if ((stat = pthread_mutexattr_init(&mattr))) {
   #ifdef PTHREAD_SETS_ERRNO
  @@ -128,10 +130,12 @@
   #if APR_HAS_THREADS
       apr_os_thread_t my_thrid; /* save one call to apr_os_thread_current() */
   
  -    if (apr_os_thread_equal(mutex->owner,
  -                            (my_thrid = apr_os_thread_current()))) {
  -        mutex->owner_ref++;
  -        return APR_SUCCESS;
  +    if (mutex->nested) {
  +        if (apr_os_thread_equal(mutex->owner,
  +                                (my_thrid = apr_os_thread_current()))) {
  +            mutex->owner_ref++;
  +                return APR_SUCCESS;
  +        }
       }
   #endif
   
  @@ -144,8 +148,10 @@
       }
   
   #if APR_HAS_THREADS
  -    mutex->owner = my_thrid;
  -    mutex->owner_ref = 1;
  +    if (mutex->nested) {
  +        mutex->owner = my_thrid;
  +        mutex->owner_ref = 1;
  +    }
   #endif
   
       return stat;
  @@ -158,10 +164,12 @@
   #if APR_HAS_THREADS
       apr_os_thread_t my_thrid; /* save one call to apr_os_thread_current() */
   
  -    if (apr_os_thread_equal(mutex->owner,
  -                            (my_thrid = apr_os_thread_current()))) {
  -        mutex->owner_ref++;
  -        return APR_SUCCESS;
  +    if (mutex->nested) {
  +        if (apr_os_thread_equal(mutex->owner,
  +                                (my_thrid = apr_os_thread_current()))) {
  +            mutex->owner_ref++;
  +            return APR_SUCCESS;
  +        }
       }
   #endif
   
  @@ -178,8 +186,10 @@
       }
   
   #if APR_HAS_THREADS
  -    mutex->owner = my_thrid;
  -    mutex->owner_ref = 1;
  +    if (mutex->nested) {
  +        mutex->owner = my_thrid;
  +        mutex->owner_ref = 1;
  +    }
   #endif
   
       return stat;
  @@ -190,10 +200,12 @@
       apr_status_t status;
   
   #if APR_HAS_THREADS
  -    if (apr_os_thread_equal(mutex->owner, apr_os_thread_current())) {
  -        mutex->owner_ref--;
  -        if (mutex->owner_ref > 0)
  -            return APR_SUCCESS;
  +    if (mutex->nested) {
  +        if (apr_os_thread_equal(mutex->owner, apr_os_thread_current())) {
  +            mutex->owner_ref--;
  +            if (mutex->owner_ref > 0)
  +                return APR_SUCCESS;
  +        }
       }
   #endif
   
  @@ -206,8 +218,10 @@
       }
   
   #if APR_HAS_THREADS
  -    memset(&mutex->owner, 0, sizeof mutex->owner);
  -    mutex->owner_ref = 0;
  +    if (mutex->nested) {
  +        memset(&mutex->owner, 0, sizeof mutex->owner);
  +        mutex->owner_ref = 0;
  +    }
   #endif
       
       return status;
  
  
  
  1.6       +3 -0      apr/locks/win32/thread_mutex.c
  
  Index: thread_mutex.c
  ===================================================================
  RCS file: /home/cvs/apr/locks/win32/thread_mutex.c,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- thread_mutex.c	2001/09/26 20:55:33	1.5
  +++ thread_mutex.c	2001/10/17 00:33:00	1.6
  @@ -69,11 +69,14 @@
   }
   
   APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  +                                                  unsigned int flags,
                                                     apr_pool_t *pool)
   {
       (*mutex) = (apr_thread_mutex_t *)apr_palloc(pool, sizeof(**mutex));
   
       (*mutex)->pool = pool;
  +    /* FIXME: Implement nested (aka recursive) locks or use a native
  +     * win32 implementation if available. */
       InitializeCriticalSection(&(*mutex)->section);
       apr_pool_cleanup_register((*mutex)->pool, (*mutex), thread_mutex_cleanup,
                                 apr_pool_cleanup_null);
  
  
  
  1.14      +4 -4      apr/test/testlock.c
  
  Index: testlock.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testlock.c,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- testlock.c	2001/10/12 19:02:37	1.13
  +++ testlock.c	2001/10/17 00:33:00	1.14
  @@ -408,7 +408,7 @@
   
       printf("thread_mutex test\n");
       printf("%-60s", "    Initializing the mutex");
  -    s1 = apr_thread_mutex_create(&thread_mutex, pool);
  +    s1 = apr_thread_mutex_create(&thread_mutex, APR_THREAD_MUTEX_DEFAULT, pool);
   
       if (s1 != APR_SUCCESS) {
           printf("Failed!\n");
  @@ -505,7 +505,7 @@
   
       printf("thread_cond Tests\n");
       printf("%-60s", "    Initializing the first apr_thread_mutex_t");
  -    s1 = apr_thread_mutex_create(&put.mutex, pool);
  +    s1 = apr_thread_mutex_create(&put.mutex, APR_THREAD_MUTEX_DEFAULT, pool);
       if (s1 != APR_SUCCESS) {
           printf("Failed!\n");
           return s1;
  @@ -513,7 +513,7 @@
       printf("OK\n");
   
       printf("%-60s", "    Initializing the second apr_thread_mutex_t");
  -    s1 = apr_thread_mutex_create(&nready.mutex, pool);
  +    s1 = apr_thread_mutex_create(&nready.mutex, APR_THREAD_MUTEX_DEFAULT, pool);
       if (s1 != APR_SUCCESS) {
           printf("Failed!\n");
           return s1;
  @@ -579,7 +579,7 @@
   
       printf("thread_cond_timedwait Tests\n");
       printf("%-60s", "    Initializing the first apr_thread_mutex_t");
  -    s = apr_thread_mutex_create(&timeout_mutex, pool);
  +    s = apr_thread_mutex_create(&timeout_mutex, APR_THREAD_MUTEX_DEFAULT, pool);
       if (s != APR_SUCCESS) {
           printf("Failed!\n");
           return s;
  
  
  
  1.5       +58 -3     apr/test/testlockperf.c
  
  Index: testlockperf.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testlockperf.c,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- testlockperf.c	2001/09/16 17:15:39	1.4
  +++ testlockperf.c	2001/10/17 00:33:00	1.5
  @@ -205,7 +205,7 @@
   
       printf("apr_thread_mutex_t Tests\n");
       printf("%-60s", "    Initializing the apr_thread_mutex_t");
  -    s1 = apr_thread_mutex_create(&thread_lock, pool);
  +    s1 = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, pool);
       if (s1 != APR_SUCCESS) {
           printf("Failed!\n");
           return s1;
  @@ -245,6 +245,55 @@
       return APR_SUCCESS;
   }
   
  +int test_thread_mutex_nested(void)
  +{
  +    apr_thread_t *t1, *t2, *t3, *t4;
  +    apr_status_t s1, s2, s3, s4;
  +    apr_time_t time_start, time_stop;
  +
  +    mutex_counter = 0;
  +
  +    printf("apr_thread_mutex_t Tests\n");
  +    printf("%-60s", "    Initializing the apr_thread_mutex_t (NESTED)");
  +    s1 = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_NESTED, pool);
  +    if (s1 != APR_SUCCESS) {
  +        printf("Failed!\n");
  +        return s1;
  +    }
  +    printf("OK\n");
  +
  +    apr_thread_mutex_lock(thread_lock);
  +    /* set_concurrency(4)? -aaron */
  +    printf("%-60s","    Starting all the threads"); 
  +    s1 = apr_thread_create(&t1, NULL, thread_mutex_func, NULL, pool);
  +    s2 = apr_thread_create(&t2, NULL, thread_mutex_func, NULL, pool);
  +    s3 = apr_thread_create(&t3, NULL, thread_mutex_func, NULL, pool);
  +    s4 = apr_thread_create(&t4, NULL, thread_mutex_func, NULL, pool);
  +    if (s1 != APR_SUCCESS || s2 != APR_SUCCESS || 
  +        s3 != APR_SUCCESS || s4 != APR_SUCCESS) {
  +        printf("Failed!\n");
  +        return s1;
  +    }
  +    printf("OK\n");
  +
  +    time_start = apr_time_now();
  +    apr_thread_mutex_unlock(thread_lock);
  +
  +    /* printf("%-60s", "    Waiting for threads to exit"); */
  +    apr_thread_join(&s1, t1);
  +    apr_thread_join(&s2, t2);
  +    apr_thread_join(&s3, t3);
  +    apr_thread_join(&s4, t4);
  +    /* printf("OK\n"); */
  +
  +    time_stop = apr_time_now();
  +    printf("microseconds: %" APR_INT64_T_FMT " usec\n",
  +           (time_stop - time_start));
  +    if (mutex_counter != MAX_COUNTER * 4)
  +        printf("error: counter = %ld\n", mutex_counter);
  +
  +    return APR_SUCCESS;
  +}
   int test_inter_rwlock(void)
   {
       apr_thread_t *t1, *t2, *t3, *t4;
  @@ -389,16 +438,22 @@
           exit(-3);
       }
   
  +    if ((rv = test_thread_mutex_nested()) != APR_SUCCESS) {
  +        fprintf(stderr,"thread_mutex (NESTED) test failed : [%d] %s\n",
  +                rv, apr_strerror(rv, (char*)errmsg, 200));
  +        exit(-4);
  +    }
  +
       if ((rv = test_inter_rwlock()) != APR_SUCCESS) {
           fprintf(stderr,"apr_lock(INTRAPROCESS, READWRITE) test failed : [%d] %s\n",
                   rv, apr_strerror(rv, (char*)errmsg, 200));
  -        exit(-2);
  +        exit(-5);
       }
   
       if ((rv = test_thread_rwlock()) != APR_SUCCESS) {
           fprintf(stderr,"thread_rwlock test failed : [%d] %s\n",
                   rv, apr_strerror(rv, (char*)errmsg, 200));
  -        exit(-3);
  +        exit(-6);
       }
   
       return 0;
  
  
  
  1.173     +6 -0      apr/CHANGES
  
  Index: CHANGES
  ===================================================================
  RCS file: /home/cvs/apr/CHANGES,v
  retrieving revision 1.172
  retrieving revision 1.173
  diff -u -r1.172 -r1.173
  --- CHANGES	2001/10/16 12:24:32	1.172
  +++ CHANGES	2001/10/17 00:33:00	1.173
  @@ -1,5 +1,11 @@
   Changes with APR b1  
   
  +  *) Added a new parameter to apr_thread_mutex_init(). Now, by default,
  +     thread mutexes are not nested (sometimes called "recursive"). To
  +     enable nested mutexes, a flag must be passed to the init script.
  +     Non-nested mutexes are much faster than nested ones.
  +     [Aaron Bannert]
  +
     *) read_with_timeout in apr/file_io/win32/readwrite.c incorrectly 
        returned APR_SUCCESS instead of APR_EOF when PeekNamedPipe failed
        and the result from GetLastError() was ERROR_BROKEN_PIPE. Because 
  
  
  

Mime
View raw message