"Fossies" - the Fresh Open Source Software Archive

Member "apr-1.7.0/locks/beos/proc_mutex.c" (2 Jun 2017, 7292 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 "proc_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 /*Read/Write locking implementation based on the MultiLock code from
   18  * Stephen Beaulieu <hippo@be.com>
   19  */
   20  
   21 #include "apr_arch_proc_mutex.h"
   22 #include "apr_strings.h"
   23 #include "apr_portable.h"
   24 
   25 static apr_status_t _proc_mutex_cleanup(void * data)
   26 {
   27     apr_proc_mutex_t *lock = (apr_proc_mutex_t*)data;
   28     if (lock->LockCount != 0) {
   29         /* we're still locked... */
   30         while (atomic_add(&lock->LockCount , -1) > 1){
   31             /* OK we had more than one person waiting on the lock so 
   32              * the sem is also locked. Release it until we have no more
   33              * locks left.
   34              */
   35             release_sem (lock->Lock);
   36         }
   37     }
   38     delete_sem(lock->Lock);
   39     return APR_SUCCESS;
   40 }    
   41 
   42 APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
   43                                                 const char *fname,
   44                                                 apr_lockmech_e mech,
   45                                                 apr_pool_t *pool)
   46 {
   47     apr_proc_mutex_t *new;
   48     apr_status_t stat = APR_SUCCESS;
   49   
   50     if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
   51         return APR_ENOTIMPL;
   52     }
   53 
   54     new = (apr_proc_mutex_t *)apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
   55     if (new == NULL){
   56         return APR_ENOMEM;
   57     }
   58     
   59     if ((stat = create_sem(0, "APR_Lock")) < B_NO_ERROR) {
   60         _proc_mutex_cleanup(new);
   61         return stat;
   62     }
   63     new->LockCount = 0;
   64     new->Lock = stat;  
   65     new->pool  = pool;
   66 
   67     apr_pool_cleanup_register(new->pool, (void *)new, _proc_mutex_cleanup,
   68                               apr_pool_cleanup_null);
   69 
   70     (*mutex) = new;
   71     return APR_SUCCESS;
   72 }
   73 
   74 APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
   75                                                     const char *fname,
   76                                                     apr_pool_t *pool)
   77 {
   78     return APR_SUCCESS;
   79 }
   80     
   81 APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
   82 {
   83     int32 stat;
   84     
   85     if (atomic_add(&mutex->LockCount, 1) > 0) {
   86         if ((stat = acquire_sem(mutex->Lock)) < B_NO_ERROR) {
   87             atomic_add(&mutex->LockCount, -1);
   88             return stat;
   89         }
   90     }
   91     return APR_SUCCESS;
   92 }
   93 
   94 APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
   95 {
   96     int32 stat;
   97 
   98     if (atomic_add(&mutex->LockCount, 1) > 0) {
   99         stat = acquire_sem_etc(mutex->Lock, 1, 0, 0);
  100         if (stat < B_NO_ERROR) {
  101             atomic_add(&mutex->LockCount, -1);
  102             if (stat == B_WOULD_BLOCK) {
  103                 stat = APR_EBUSY;
  104             }
  105             return stat;
  106         }
  107     }
  108     return APR_SUCCESS;
  109 }
  110 
  111 APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex,
  112                                                apr_interval_time_t timeout)
  113 {
  114     int32 stat;
  115 
  116     if (atomic_add(&mutex->LockCount, 1) > 0) {
  117         if (timeout <= 0) {
  118             stat = B_TIMED_OUT;
  119         }
  120         else {
  121             stat = acquire_sem_etc(mutex->Lock, 1, B_RELATIVE_TIMEOUT,
  122                                    timeout);
  123         }
  124         if (stat < B_NO_ERROR) {
  125             atomic_add(&mutex->LockCount, -1);
  126             if (stat == B_TIMED_OUT) {
  127                 stat = APR_TIMEUP;
  128             }
  129             return stat;
  130         }
  131     }
  132     return APR_SUCCESS;
  133 }
  134 
  135 APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
  136 {
  137     int32 stat;
  138     
  139     if (atomic_add(&mutex->LockCount, -1) > 1) {
  140         if ((stat = release_sem(mutex->Lock)) < B_NO_ERROR) {
  141             atomic_add(&mutex->LockCount, 1);
  142             return stat;
  143         }
  144     }
  145     return APR_SUCCESS;
  146 }
  147 
  148 APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
  149 {
  150     apr_status_t stat;
  151     if ((stat = _proc_mutex_cleanup(mutex)) == APR_SUCCESS) {
  152         apr_pool_cleanup_kill(mutex->pool, mutex, _proc_mutex_cleanup);
  153         return APR_SUCCESS;
  154     }
  155     return stat;
  156 }
  157 
  158 APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *mutex)
  159 {
  160     return _proc_mutex_cleanup(mutex);
  161 }
  162 
  163 
  164 APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
  165 {
  166     return NULL;
  167 }
  168 
  169 APR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex)
  170 {
  171     return APR_LOCK_DEFAULT;
  172 }
  173 
  174 APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
  175 {
  176     return "beossem";
  177 }
  178 
  179 APR_DECLARE(const char *) apr_proc_mutex_defname(void)
  180 {
  181     return "beossem";
  182 }
  183 
  184 APR_PERMS_SET_ENOTIMPL(proc_mutex)
  185 
  186 APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
  187 
  188 /* Implement OS-specific accessors defined in apr_portable.h */
  189 
  190 APR_DECLARE(apr_status_t) apr_os_proc_mutex_get_ex(apr_os_proc_mutex_t *ospmutex, 
  191                                                    apr_proc_mutex_t *pmutex,
  192                                                    apr_lockmech_e *mech)
  193 {
  194     ospmutex->sem = pmutex->Lock;
  195     ospmutex->ben = pmutex->LockCount;
  196     if (mech) {
  197         *mech = APR_LOCK_DEFAULT;
  198     }
  199     return APR_SUCCESS;
  200 }
  201 
  202 APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
  203                                                 apr_proc_mutex_t *pmutex)
  204 {
  205     return apr_os_proc_mutex_get_ex(ospmutex, pmutex, NULL);
  206 }
  207 
  208 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
  209                                                 apr_os_proc_mutex_t *ospmutex,
  210                                                 apr_lockmech_e mech,
  211                                                 int register_cleanup,
  212                                                 apr_pool_t *pool)
  213 {
  214     if (pool == NULL) {
  215         return APR_ENOPOOL;
  216     }
  217     if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
  218         return APR_ENOTIMPL;
  219     }
  220 
  221     if ((*pmutex) == NULL) {
  222         (*pmutex) = (apr_proc_mutex_t *)apr_pcalloc(pool, sizeof(apr_proc_mutex_t));
  223         (*pmutex)->pool = pool;
  224     }
  225     (*pmutex)->Lock = ospmutex->sem;
  226     (*pmutex)->LockCount = ospmutex->ben;
  227 
  228     if (register_cleanup) {
  229         apr_pool_cleanup_register(pool, *pmutex, _proc_mutex_cleanup,
  230                                   apr_pool_cleanup_null);
  231     }
  232     return APR_SUCCESS;
  233 }
  234 
  235 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
  236                                                 apr_os_proc_mutex_t *ospmutex,
  237                                                 apr_pool_t *pool)
  238 {
  239     return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT,
  240                                     0, pool);
  241 }
  242