"Fossies" - the Fresh Open Source Software Archive

Member "apr-1.7.0/locks/netware/thread_mutex.c" (21 Mar 2019, 5094 Bytes) of package /linux/www/apr-1.7.0.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "thread_mutex.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.6.5_vs_1.7.0.

    1 /* Licensed to the Apache Software Foundation (ASF) under one or more
    2  * contributor license agreements.  See the NOTICE file distributed with
    3  * this work for additional information regarding copyright ownership.
    4  * The ASF licenses this file to You under the Apache License, Version 2.0
    5  * (the "License"); you may not use this file except in compliance with
    6  * the License.  You may obtain a copy of the License at
    7  *
    8  *     http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 #include "apr.h"
   18 #include "apr_private.h"
   19 #include "apr_general.h"
   20 #include "apr_strings.h"
   21 #include "apr_arch_thread_mutex.h"
   22 #include "apr_thread_cond.h"
   23 #include "apr_portable.h"
   24 
   25 static apr_status_t thread_mutex_cleanup(void *data)
   26 {
   27     apr_thread_mutex_t *mutex = (apr_thread_mutex_t *)data;
   28 
   29     NXMutexFree(mutex->mutex);        
   30     return APR_SUCCESS;
   31 }
   32 
   33 APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
   34                                                   unsigned int flags,
   35                                                   apr_pool_t *pool)
   36 {
   37     apr_thread_mutex_t *new_mutex = NULL;
   38 
   39     /* XXX: Implement _UNNESTED flavor and favor _DEFAULT for performance
   40      */
   41     if (flags & APR_THREAD_MUTEX_UNNESTED) {
   42         return APR_ENOTIMPL;
   43     }
   44     new_mutex = (apr_thread_mutex_t *)apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
   45 
   46     if (new_mutex == NULL) {
   47         return APR_ENOMEM;
   48     }     
   49     new_mutex->pool = pool;
   50 
   51     new_mutex->mutex = NXMutexAlloc(NX_MUTEX_RECURSIVE, 0, NULL);
   52     
   53     if(new_mutex->mutex == NULL)
   54         return APR_ENOMEM;
   55 
   56     if (flags & APR_THREAD_MUTEX_TIMED) {
   57         apr_status_t rv = apr_thread_cond_create(&new_mutex->cond, pool);
   58         if (rv != APR_SUCCESS) {
   59             NXMutexFree(new_mutex->mutex);        
   60             return rv;
   61         }
   62     }
   63 
   64     apr_pool_cleanup_register(new_mutex->pool, new_mutex, 
   65                                 (void*)thread_mutex_cleanup,
   66                                 apr_pool_cleanup_null);
   67    *mutex = new_mutex;
   68     return APR_SUCCESS;
   69 }
   70 
   71 APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
   72 {
   73     if (mutex->cond) {
   74         apr_status_t rv;
   75         NXLock(mutex->mutex);
   76         if (mutex->locked) {
   77             mutex->num_waiters++;
   78             rv = apr_thread_cond_wait(mutex->cond, mutex);
   79             mutex->num_waiters--;
   80         }
   81         else {
   82             mutex->locked = 1;
   83             rv = APR_SUCCESS;
   84         }
   85         NXUnlock(mutex->mutex);
   86         return rv;
   87     }
   88 
   89     NXLock(mutex->mutex);
   90     return APR_SUCCESS;
   91 }
   92 
   93 APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
   94 {
   95     if (mutex->cond) {
   96         apr_status_t rv;
   97         NXLock(mutex->mutex);
   98         if (mutex->locked) {
   99             rv = APR_EBUSY;
  100         }
  101         else {
  102             mutex->locked = 1;
  103             rv = APR_SUCCESS;
  104         }
  105         NXUnlock(mutex->mutex);
  106         return rv;
  107     }
  108 
  109     if (!NXTryLock(mutex->mutex))
  110         return APR_EBUSY;
  111     return APR_SUCCESS;
  112 }
  113 
  114 APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,
  115                                                  apr_interval_time_t timeout)
  116 {
  117     if (mutex->cond) {
  118         apr_status_t rv = APR_SUCCESS;
  119 
  120         NXLock(mutex->mutex);
  121         if (mutex->locked) {
  122             if (timeout <= 0) {
  123                 rv = APR_TIMEUP;
  124             }
  125             else {
  126                 mutex->num_waiters++;
  127                 do {
  128                     rv = apr_thread_cond_timedwait(mutex->cond, mutex,
  129                                                    timeout);
  130                 } while (rv == APR_SUCCESS && mutex->locked);
  131                 mutex->num_waiters--;
  132             }
  133         }
  134         if (rv == APR_SUCCESS) {
  135             mutex->locked = 1;
  136         }
  137         NXUnlock(mutex->mutex);
  138         return rv;
  139     }
  140 
  141     return APR_ENOTIMPL;
  142 }
  143 
  144 APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
  145 {
  146     apr_status_t rv = APR_SUCCESS;
  147 
  148     if (mutex->cond) {
  149         NXLock(mutex->mutex);
  150 
  151         if (!mutex->locked) {
  152             rv = APR_EINVAL;
  153         }
  154         else if (mutex->num_waiters) {
  155             rv = apr_thread_cond_signal(mutex->cond);
  156         }
  157         if (rv == APR_SUCCESS) {
  158             mutex->locked = 0;
  159         }
  160     }
  161 
  162     NXUnlock(mutex->mutex);
  163     return rv;
  164 }
  165 
  166 APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
  167 {
  168     apr_status_t stat, rv = APR_SUCCESS;
  169     if (mutex->cond) {
  170         rv = apr_thread_cond_destroy(mutex->cond);
  171         mutex->cond = NULL;
  172     }
  173     stat = apr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup);
  174     if (stat == APR_SUCCESS && rv) {
  175         stat = rv;
  176     }
  177     return stat;
  178 }
  179 
  180 APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
  181