        -:    0:Source:/home/dsg/apr_trunk/locks/unix/thread_mutex.c
        -:    0:Graph:./locks/unix/.libs/thread_mutex.gcno
        -:    0:Data:./locks/unix/.libs/thread_mutex.gcda
        -:    0:Runs:346
        -:    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_arch_thread_mutex.h"
        -:   18:#define APR_WANT_MEMFUNC
        -:   19:#include "apr_want.h"
        -:   20:
        -:   21:#if APR_HAS_THREADS
        -:   22:
      334:   23:static apr_status_t thread_mutex_cleanup(void *data)
        -:   24:{
      334:   25:    apr_thread_mutex_t *mutex = data;
        -:   26:    apr_status_t rv;
        -:   27:
      334:   28:    rv = pthread_mutex_destroy(&mutex->mutex);
        -:   29:#ifdef HAVE_ZOS_PTHREADS
        -:   30:    if (rv) {
        -:   31:        rv = errno;
        -:   32:    }
        -:   33:#endif
      334:   34:    return rv;
        -:   35:}
        -:   36:
      122:   37:APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
        -:   38:                                                  unsigned int flags,
        -:   39:                                                  apr_pool_t *pool)
        -:   40:{
        -:   41:    apr_thread_mutex_t *new_mutex;
        -:   42:    apr_status_t rv;
        -:   43:
        -:   44:#ifndef HAVE_PTHREAD_MUTEX_RECURSIVE
        -:   45:    if (flags & APR_THREAD_MUTEX_NESTED) {
        -:   46:        return APR_ENOTIMPL;
        -:   47:    }
        -:   48:#endif
        -:   49:
      122:   50:    new_mutex = apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
      122:   51:    new_mutex->pool = pool;
        -:   52:
        -:   53:#ifdef HAVE_PTHREAD_MUTEX_RECURSIVE
      122:   54:    if (flags & APR_THREAD_MUTEX_NESTED) {
        -:   55:        pthread_mutexattr_t mattr;
        -:   56:
       14:   57:        rv = pthread_mutexattr_init(&mattr);
      14*:   58:        if (rv) return rv;
        -:   59:
       14:   60:        rv = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
       14:   61:        if (rv) {
    #####:   62:            pthread_mutexattr_destroy(&mattr);
    #####:   63:            return rv;
        -:   64:        }
        -:   65:
       14:   66:        rv = pthread_mutex_init(&new_mutex->mutex, &mattr);
        -:   67:
       14:   68:        pthread_mutexattr_destroy(&mattr);
        -:   69:    } else
        -:   70:#endif
        -:   71:    {
        -:   72:#if defined(APR_THREAD_DEBUG)
        -:   73:        pthread_mutexattr_t mattr;
        -:   74:
        -:   75:        rv = pthread_mutexattr_init(&mattr);
        -:   76:        if (rv) return rv;
        -:   77:
        -:   78:        rv = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK);
        -:   79:        if (rv) {
        -:   80:            pthread_mutexattr_destroy(&mattr);
        -:   81:            return rv;
        -:   82:        }
        -:   83:
        -:   84:        rv = pthread_mutex_init(&new_mutex->mutex, &mattr);
        -:   85:
        -:   86:        pthread_mutexattr_destroy(&mattr);
        -:   87:#else
      108:   88:        rv = pthread_mutex_init(&new_mutex->mutex, NULL);
        -:   89:#endif
        -:   90:    }
        -:   91:
      122:   92:    if (rv) {
        -:   93:#ifdef HAVE_ZOS_PTHREADS
        -:   94:        rv = errno;
        -:   95:#endif
    #####:   96:        return rv;
        -:   97:    }
        -:   98:
        -:   99:#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
        -:  100:    if (flags & APR_THREAD_MUTEX_TIMED) {
        -:  101:        rv = apr_thread_cond_create(&new_mutex->cond, pool);
        -:  102:        if (rv) {
        -:  103:#ifdef HAVE_ZOS_PTHREADS
        -:  104:            rv = errno;
        -:  105:#endif
        -:  106:            pthread_mutex_destroy(&new_mutex->mutex);
        -:  107:            return rv;
        -:  108:        }
        -:  109:    }
        -:  110:#endif
        -:  111:
      122:  112:    apr_pool_cleanup_register(new_mutex->pool,
        -:  113:                              new_mutex, thread_mutex_cleanup,
        -:  114:                              apr_pool_cleanup_null);
        -:  115:
      122:  116:    *mutex = new_mutex;
      122:  117:    return APR_SUCCESS;
        -:  118:}
        -:  119:
  2910988:  120:APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
        -:  121:{
        -:  122:    apr_status_t rv;
        -:  123:
        -:  124:#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
        -:  125:    if (mutex->cond) {
        -:  126:        apr_status_t rv2;
        -:  127:
        -:  128:        rv = pthread_mutex_lock(&mutex->mutex);
        -:  129:        if (rv) {
        -:  130:#ifdef HAVE_ZOS_PTHREADS
        -:  131:            rv = errno;
        -:  132:#endif
        -:  133:            return rv;
        -:  134:        }
        -:  135:
        -:  136:        if (mutex->locked) {
        -:  137:            mutex->num_waiters++;
        -:  138:            rv = apr_thread_cond_wait(mutex->cond, mutex);
        -:  139:            mutex->num_waiters--;
        -:  140:        }
        -:  141:        else {
        -:  142:            mutex->locked = 1;
        -:  143:        }
        -:  144:
        -:  145:        rv2 = pthread_mutex_unlock(&mutex->mutex);
        -:  146:        if (rv2 && !rv) {
        -:  147:#ifdef HAVE_ZOS_PTHREADS
        -:  148:            rv = errno;
        -:  149:#else
        -:  150:            rv = rv2;
        -:  151:#endif
        -:  152:        }
        -:  153:
        -:  154:        return rv;
        -:  155:    }
        -:  156:#endif
        -:  157:
  2910988:  158:    rv = pthread_mutex_lock(&mutex->mutex);
        -:  159:#ifdef HAVE_ZOS_PTHREADS
        -:  160:    if (rv) {
        -:  161:        rv = errno;
        -:  162:    }
        -:  163:#endif
        -:  164:
  2932916:  165:    return rv;
        -:  166:}
        -:  167:
       12:  168:APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
        -:  169:{
        -:  170:    apr_status_t rv;
        -:  171:
        -:  172:#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
        -:  173:    if (mutex->cond) {
        -:  174:        apr_status_t rv2;
        -:  175:
        -:  176:        rv = pthread_mutex_lock(&mutex->mutex);
        -:  177:        if (rv) {
        -:  178:#ifdef HAVE_ZOS_PTHREADS
        -:  179:            rv = errno;
        -:  180:#endif
        -:  181:            return rv;
        -:  182:        }
        -:  183:
        -:  184:        if (mutex->locked) {
        -:  185:            rv = APR_EBUSY;
        -:  186:        }
        -:  187:        else {
        -:  188:            mutex->locked = 1;
        -:  189:        }
        -:  190:
        -:  191:        rv2 = pthread_mutex_unlock(&mutex->mutex);
        -:  192:        if (rv2) {
        -:  193:#ifdef HAVE_ZOS_PTHREADS
        -:  194:            rv = errno;
        -:  195:#else
        -:  196:            rv = rv2;
        -:  197:#endif
        -:  198:        }
        -:  199:
        -:  200:        return rv;
        -:  201:    }
        -:  202:#endif
        -:  203:
       12:  204:    rv = pthread_mutex_trylock(&mutex->mutex);
       12:  205:    if (rv) {
        -:  206:#ifdef HAVE_ZOS_PTHREADS
        -:  207:        rv = errno;
        -:  208:#endif
      10*:  209:        return (rv == EBUSY) ? APR_EBUSY : rv;
        -:  210:    }
        -:  211:
        2:  212:    return APR_SUCCESS;
        -:  213:}
        -:  214:
    79487:  215:APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,
        -:  216:                                                 apr_interval_time_t timeout)
        -:  217:{
    79487:  218:    apr_status_t rv = APR_ENOTIMPL;
        -:  219:
        -:  220:#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
    79487:  221:    if (timeout <= 0) {
    #####:  222:        rv = pthread_mutex_trylock(&mutex->mutex);
    #####:  223:        if (rv) {
        -:  224:#ifdef HAVE_ZOS_PTHREADS
        -:  225:            rv = errno;
        -:  226:#endif
    #####:  227:            if (rv == EBUSY) {
    #####:  228:                rv = APR_TIMEUP;
        -:  229:            }
        -:  230:        }
        -:  231:    }
        -:  232:    else {
        -:  233:        struct timespec abstime;
        -:  234:
    79487:  235:        timeout += apr_time_now();
    79064:  236:        abstime.tv_sec = apr_time_sec(timeout);
    79064:  237:        abstime.tv_nsec = apr_time_usec(timeout) * 1000; /* nanoseconds */
        -:  238:
    79064:  239:        rv = pthread_mutex_timedlock(&mutex->mutex, &abstime);
    80010:  240:        if (rv) {
        -:  241:#ifdef HAVE_ZOS_PTHREADS
        -:  242:            rv = errno;
        -:  243:#endif
        2:  244:            if (rv == ETIMEDOUT) {
        2:  245:                rv = APR_TIMEUP;
        -:  246:            }
        -:  247:        }
        -:  248:    }
        -:  249:
        -:  250:#else /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
        -:  251:
        -:  252:    if (mutex->cond) {
        -:  253:        rv = pthread_mutex_lock(&mutex->mutex);
        -:  254:        if (rv) {
        -:  255:#ifdef HAVE_ZOS_PTHREADS
        -:  256:            rv = errno;
        -:  257:#endif
        -:  258:            return rv;
        -:  259:        }
        -:  260:
        -:  261:        if (mutex->locked) {
        -:  262:            if (timeout <= 0) {
        -:  263:                rv = APR_TIMEUP;
        -:  264:            }
        -:  265:            else {
        -:  266:                mutex->num_waiters++;
        -:  267:                do {
        -:  268:                    rv = apr_thread_cond_timedwait(mutex->cond, mutex,
        -:  269:                                                   timeout);
        -:  270:                    if (rv) {
        -:  271:#ifdef HAVE_ZOS_PTHREADS
        -:  272:                        rv = errno;
        -:  273:#endif
        -:  274:                        break;
        -:  275:                    }
        -:  276:                } while (mutex->locked);
        -:  277:                mutex->num_waiters--;
        -:  278:            }
        -:  279:            if (rv) {
        -:  280:                pthread_mutex_unlock(&mutex->mutex);
        -:  281:                return rv;
        -:  282:            }
        -:  283:        }
        -:  284:
        -:  285:        mutex->locked = 1;
        -:  286:
        -:  287:        rv = pthread_mutex_unlock(&mutex->mutex);
        -:  288:        if (rv) {
        -:  289:#ifdef HAVE_ZOS_PTHREADS
        -:  290:            rv = errno;
        -:  291:#endif
        -:  292:            return rv;
        -:  293:        }
        -:  294:    }
        -:  295:
        -:  296:#endif /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
        -:  297:
    80010:  298:    return rv;
        -:  299:}
        -:  300:
  3011332:  301:APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
        -:  302:{
        -:  303:    apr_status_t status;
        -:  304:
        -:  305:#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
        -:  306:    if (mutex->cond) {
        -:  307:        status = pthread_mutex_lock(&mutex->mutex);
        -:  308:        if (status) {
        -:  309:#ifdef HAVE_ZOS_PTHREADS
        -:  310:            status = errno;
        -:  311:#endif
        -:  312:            return status;
        -:  313:        }
        -:  314:
        -:  315:        if (!mutex->locked) {
        -:  316:            status = APR_EINVAL;
        -:  317:        }
        -:  318:        else if (mutex->num_waiters) {
        -:  319:            status = apr_thread_cond_signal(mutex->cond);
        -:  320:        }
        -:  321:        if (status) {
        -:  322:            pthread_mutex_unlock(&mutex->mutex);
        -:  323:            return status;
        -:  324:        }
        -:  325:
        -:  326:        mutex->locked = 0;
        -:  327:    }
        -:  328:#endif
        -:  329:
  3011332:  330:    status = pthread_mutex_unlock(&mutex->mutex);
        -:  331:#ifdef HAVE_ZOS_PTHREADS
        -:  332:    if (status) {
        -:  333:        status = errno;
        -:  334:    }
        -:  335:#endif
        -:  336:
  2988521:  337:    return status;
        -:  338:}
        -:  339:
       54:  340:APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
        -:  341:{
       54:  342:    apr_status_t rv, rv2 = APR_SUCCESS;
        -:  343:
        -:  344:#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
        -:  345:    if (mutex->cond) {
        -:  346:        rv2 = apr_thread_cond_destroy(mutex->cond);
        -:  347:    }
        -:  348:#endif
        -:  349:
       54:  350:    rv = apr_pool_cleanup_run(mutex->pool, mutex, thread_mutex_cleanup);
       54:  351:    if (rv == APR_SUCCESS) {
       54:  352:        rv = rv2;
        -:  353:    }
        -:  354:
       54:  355:    return rv;
        -:  356:}
        -:  357:
    #####:  358:APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
        -:  359:
        -:  360:#endif /* APR_HAS_THREADS */
