"Fossies" - the Fresh Open Source Software Archive

Member "apr-1.7.0/locks/os2/proc_mutex.c" (2 Jun 2017, 7600 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 #include "apr_general.h"
   18 #include "apr_lib.h"
   19 #include "apr_strings.h"
   20 #include "apr_portable.h"
   21 #include "apr_arch_proc_mutex.h"
   22 #include "apr_arch_file_io.h"
   23 #include <string.h>
   24 #include <stddef.h>
   25 
   26 #define CurrentTid (*_threadid)
   27 
   28 static char *fixed_name(const char *fname, apr_pool_t *pool)
   29 {
   30     char *semname;
   31 
   32     if (fname == NULL)
   33         semname = NULL;
   34     else {
   35         /* Semaphores don't live in the file system, fix up the name */
   36         while (*fname == '/' || *fname == '\\') {
   37             fname++;
   38         }
   39 
   40         semname = apr_pstrcat(pool, "/SEM32/", fname, NULL);
   41 
   42         if (semname[8] == ':') {
   43             semname[8] = '$';
   44         }
   45     }
   46 
   47     return semname;
   48 }
   49 
   50 
   51 
   52 APR_DECLARE(apr_status_t) apr_proc_mutex_cleanup(void *vmutex)
   53 {
   54     apr_proc_mutex_t *mutex = vmutex;
   55     return apr_proc_mutex_destroy(mutex);
   56 }
   57 
   58 APR_DECLARE(const char *) apr_proc_mutex_lockfile(apr_proc_mutex_t *mutex)
   59 {
   60     return NULL;
   61 }
   62 
   63 APR_DECLARE(apr_lockmech_e) apr_proc_mutex_mech(apr_proc_mutex_t *mutex)
   64 {
   65     return APR_LOCK_DEFAULT;
   66 }
   67 
   68 APR_DECLARE(const char *) apr_proc_mutex_name(apr_proc_mutex_t *mutex)
   69 {
   70     return "os2sem";
   71 }
   72 
   73 APR_DECLARE(const char *) apr_proc_mutex_defname(void)
   74 {
   75     return "os2sem";
   76 }
   77 
   78 
   79 APR_DECLARE(apr_status_t) apr_proc_mutex_create(apr_proc_mutex_t **mutex,
   80                                                 const char *fname,
   81                                                 apr_lockmech_e mech,
   82                                                 apr_pool_t *pool)
   83 {
   84     apr_proc_mutex_t *new;
   85     ULONG rc;
   86     char *semname;
   87 
   88     if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
   89         return APR_ENOTIMPL;
   90     }
   91 
   92     new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
   93     new->pool       = pool;
   94     new->owner      = 0;
   95     new->lock_count = 0;
   96     *mutex = new;
   97 
   98     semname = fixed_name(fname, pool);
   99     rc = DosCreateMutexSem(semname, &(new->hMutex), DC_SEM_SHARED, FALSE);
  100 
  101     if (!rc) {
  102         apr_pool_cleanup_register(pool, new, apr_proc_mutex_cleanup, apr_pool_cleanup_null);
  103     }
  104 
  105     return APR_FROM_OS_ERROR(rc);
  106 }
  107 
  108 
  109 
  110 APR_DECLARE(apr_status_t) apr_proc_mutex_child_init(apr_proc_mutex_t **mutex,
  111                                                     const char *fname,
  112                                                     apr_pool_t *pool)
  113 {
  114     apr_proc_mutex_t *new;
  115     ULONG rc;
  116     char *semname;
  117 
  118     new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
  119     new->pool       = pool;
  120     new->owner      = 0;
  121     new->lock_count = 0;
  122 
  123     semname = fixed_name(fname, pool);
  124     rc = DosOpenMutexSem(semname, &(new->hMutex));
  125     *mutex = new;
  126 
  127     if (!rc) {
  128         apr_pool_cleanup_register(pool, new, apr_proc_mutex_cleanup, apr_pool_cleanup_null);
  129     }
  130 
  131     return APR_FROM_OS_ERROR(rc);
  132 }
  133 
  134 
  135 
  136 APR_DECLARE(apr_status_t) apr_proc_mutex_lock(apr_proc_mutex_t *mutex)
  137 {
  138     ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT);
  139 
  140     if (rc == 0) {
  141         mutex->owner = CurrentTid;
  142         mutex->lock_count++;
  143     }
  144 
  145     return APR_FROM_OS_ERROR(rc);
  146 }
  147 
  148 
  149 
  150 APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex)
  151 {
  152     ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
  153 
  154     if (rc == 0) {
  155         mutex->owner = CurrentTid;
  156         mutex->lock_count++;
  157     }
  158 
  159     return (rc == ERROR_TIMEOUT) ? APR_EBUSY : APR_FROM_OS_ERROR(rc);
  160 }
  161 
  162 
  163 
  164 APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex,
  165                                                apr_interval_time_t timeout)
  166 {
  167     ULONG rc;
  168     
  169     if (timeout <= 0) {
  170         rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN);
  171     }
  172     else {
  173         rc = DosRequestMutexSem(mutex->hMutex, apr_time_as_msec(timeout));
  174     }
  175 
  176     if (rc == 0) {
  177         mutex->owner = CurrentTid;
  178         mutex->lock_count++;
  179     }
  180 
  181     return (rc == ERROR_TIMEOUT) ? APR_TIMEUP : APR_FROM_OS_ERROR(rc);
  182 }
  183 
  184 
  185 
  186 APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex)
  187 {
  188     ULONG rc;
  189 
  190     if (mutex->owner == CurrentTid && mutex->lock_count > 0) {
  191         mutex->lock_count--;
  192         rc = DosReleaseMutexSem(mutex->hMutex);
  193         return APR_FROM_OS_ERROR(rc);
  194     }
  195 
  196     return APR_SUCCESS;
  197 }
  198 
  199 
  200 
  201 APR_DECLARE(apr_status_t) apr_proc_mutex_destroy(apr_proc_mutex_t *mutex)
  202 {
  203     ULONG rc;
  204     apr_status_t status = APR_SUCCESS;
  205 
  206     if (mutex->owner == CurrentTid) {
  207         while (mutex->lock_count > 0 && status == APR_SUCCESS) {
  208             status = apr_proc_mutex_unlock(mutex);
  209         }
  210     }
  211 
  212     if (status != APR_SUCCESS) {
  213         return status;
  214     }
  215 
  216     if (mutex->hMutex == 0) {
  217         return APR_SUCCESS;
  218     }
  219 
  220     rc = DosCloseMutexSem(mutex->hMutex);
  221 
  222     if (!rc) {
  223         mutex->hMutex = 0;
  224     }
  225 
  226     return APR_FROM_OS_ERROR(rc);
  227 }
  228 
  229 APR_PERMS_SET_ENOTIMPL(proc_mutex)
  230 
  231 APR_POOL_IMPLEMENT_ACCESSOR(proc_mutex)
  232 
  233 
  234 
  235 /* Implement OS-specific accessors defined in apr_portable.h */
  236 
  237 APR_DECLARE(apr_status_t) apr_os_proc_mutex_get_ex(apr_os_proc_mutex_t *ospmutex, 
  238                                                    apr_proc_mutex_t *pmutex,
  239                                                    apr_lockmech_e *mech)
  240 {
  241     *ospmutex = pmutex->hMutex;
  242     if (mech) {
  243         *mech = APR_LOCK_DEFAULT;
  244     }
  245     return APR_SUCCESS;
  246 }
  247 
  248 APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
  249                                                 apr_proc_mutex_t *pmutex)
  250 {
  251     return apr_os_proc_mutex_get_ex(ospmutex, pmutex, NULL);
  252 }
  253 
  254 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put_ex(apr_proc_mutex_t **pmutex,
  255                                                 apr_os_proc_mutex_t *ospmutex,
  256                                                 apr_lockmech_e mech,
  257                                                 int register_cleanup,
  258                                                 apr_pool_t *pool)
  259 {
  260     apr_proc_mutex_t *new;
  261     if (pool == NULL) {
  262         return APR_ENOPOOL;
  263     }
  264     if (mech != APR_LOCK_DEFAULT && mech != APR_LOCK_DEFAULT_TIMED) {
  265         return APR_ENOTIMPL;
  266     }
  267 
  268     new = (apr_proc_mutex_t *)apr_palloc(pool, sizeof(apr_proc_mutex_t));
  269     new->pool       = pool;
  270     new->owner      = 0;
  271     new->lock_count = 0;
  272     new->hMutex     = *ospmutex;
  273     *pmutex = new;
  274 
  275     if (register_cleanup) {
  276         apr_pool_cleanup_register(pool, *pmutex, apr_proc_mutex_cleanup,
  277                                   apr_pool_cleanup_null);
  278     }
  279     return APR_SUCCESS;
  280 }
  281 
  282 APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
  283                                                 apr_os_proc_mutex_t *ospmutex,
  284                                                 apr_pool_t *pool)
  285 {
  286     return apr_os_proc_mutex_put_ex(pmutex, ospmutex, APR_LOCK_DEFAULT,
  287                                     0, pool);
  288 }
  289