        -:    0:Source:/home/dsg/apr_trunk/threadproc/unix/thread.c
        -:    0:Graph:./threadproc/unix/.libs/thread.gcno
        -:    0:Data:./threadproc/unix/.libs/thread.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.h"
        -:   18:#include "apr_portable.h"
        -:   19:#include "apr_arch_threadproc.h"
        -:   20:
        -:   21:#if APR_HAS_THREADS
        -:   22:
        -:   23:#if APR_HAVE_PTHREAD_H
        -:   24:
        -:   25:/* Unfortunately the kernel headers do not export the TASK_COMM_LEN
        -:   26:   macro.  So we have to define it here. Used in apr_thread_name_get and
        -:   27:   apr_thread_name_set functions */
        -:   28:#define TASK_COMM_LEN 16
        -:   29:
        -:   30:/* Destroy the threadattr object */
    #####:   31:static apr_status_t threadattr_cleanup(void *data)
        -:   32:{
    #####:   33:    apr_threadattr_t *attr = data;
        -:   34:    apr_status_t rv;
        -:   35:
    #####:   36:    rv = pthread_attr_destroy(&attr->attr);
        -:   37:#ifdef HAVE_ZOS_PTHREADS
        -:   38:    if (rv) {
        -:   39:        rv = errno;
        -:   40:    }
        -:   41:#endif
    #####:   42:    return rv;
        -:   43:}
        -:   44:
    #####:   45:APR_DECLARE(apr_status_t) apr_threadattr_create(apr_threadattr_t **new,
        -:   46:                                                apr_pool_t *pool)
        -:   47:{
        -:   48:    apr_status_t stat;
        -:   49:
    #####:   50:    (*new) = apr_palloc(pool, sizeof(apr_threadattr_t));
    #####:   51:    (*new)->pool = pool;
    #####:   52:    stat = pthread_attr_init(&(*new)->attr);
        -:   53:
    #####:   54:    if (stat == 0) {
    #####:   55:        apr_pool_cleanup_register(pool, *new, threadattr_cleanup,
        -:   56:                                  apr_pool_cleanup_null);
    #####:   57:        return APR_SUCCESS;
        -:   58:    }
        -:   59:#ifdef HAVE_ZOS_PTHREADS
        -:   60:    stat = errno;
        -:   61:#endif
        -:   62:
    #####:   63:    return stat;
        -:   64:}
        -:   65:
        -:   66:#if defined(PTHREAD_CREATE_DETACHED)
        -:   67:#define DETACH_ARG(v) ((v) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE)
        -:   68:#else
        -:   69:#define DETACH_ARG(v) ((v) ? 1 : 0)
        -:   70:#endif
        -:   71:
    #####:   72:APR_DECLARE(apr_status_t) apr_threadattr_detach_set(apr_threadattr_t *attr,
        -:   73:                                                    apr_int32_t on)
        -:   74:{
        -:   75:    apr_status_t stat;
        -:   76:#ifdef HAVE_ZOS_PTHREADS
        -:   77:    int arg = DETACH_ARG(on);
        -:   78:
        -:   79:    if ((stat = pthread_attr_setdetachstate(&attr->attr, &arg)) == 0) {
        -:   80:#else
    #####:   81:    if ((stat = pthread_attr_setdetachstate(&attr->attr,
        -:   82:                                            DETACH_ARG(on))) == 0) {
        -:   83:#endif
    #####:   84:        return APR_SUCCESS;
        -:   85:    }
        -:   86:    else {
        -:   87:#ifdef HAVE_ZOS_PTHREADS
        -:   88:        stat = errno;
        -:   89:#endif
        -:   90:
    #####:   91:        return stat;
        -:   92:    }
        -:   93:}
        -:   94:
    #####:   95:APR_DECLARE(apr_status_t) apr_threadattr_detach_get(apr_threadattr_t *attr)
        -:   96:{
        -:   97:    int state;
        -:   98:
        -:   99:#ifdef PTHREAD_ATTR_GETDETACHSTATE_TAKES_ONE_ARG
        -:  100:    state = pthread_attr_getdetachstate(&attr->attr);
        -:  101:#else
    #####:  102:    pthread_attr_getdetachstate(&attr->attr, &state);
        -:  103:#endif
    #####:  104:    if (state == DETACH_ARG(1))
    #####:  105:        return APR_DETACH;
    #####:  106:    return APR_NOTDETACH;
        -:  107:}
        -:  108:
    #####:  109:APR_DECLARE(apr_status_t) apr_threadattr_stacksize_set(apr_threadattr_t *attr,
        -:  110:                                                       apr_size_t stacksize)
        -:  111:{
        -:  112:    int stat;
        -:  113:
    #####:  114:    stat = pthread_attr_setstacksize(&attr->attr, stacksize);
    #####:  115:    if (stat == 0) {
    #####:  116:        return APR_SUCCESS;
        -:  117:    }
        -:  118:#ifdef HAVE_ZOS_PTHREADS
        -:  119:    stat = errno;
        -:  120:#endif
        -:  121:
    #####:  122:    return stat;
        -:  123:}
        -:  124:
    #####:  125:APR_DECLARE(apr_status_t) apr_threadattr_guardsize_set(apr_threadattr_t *attr,
        -:  126:                                                       apr_size_t size)
        -:  127:{
        -:  128:#ifdef HAVE_PTHREAD_ATTR_SETGUARDSIZE
        -:  129:    apr_status_t rv;
        -:  130:
    #####:  131:    rv = pthread_attr_setguardsize(&attr->attr, size);
    #####:  132:    if (rv == 0) {
    #####:  133:        return APR_SUCCESS;
        -:  134:    }
        -:  135:#ifdef HAVE_ZOS_PTHREADS
        -:  136:    rv = errno;
        -:  137:#endif
    #####:  138:    return rv;
        -:  139:#else
        -:  140:    return APR_ENOTIMPL;
        -:  141:#endif
        -:  142:}
        -:  143:
    #####:  144:APR_DECLARE(apr_status_t) apr_threadattr_max_free_set(apr_threadattr_t *attr,
        -:  145:                                                      apr_size_t size)
        -:  146:{
    #####:  147:    attr->max_free = size;
    #####:  148:    return APR_SUCCESS;
        -:  149:}
        -:  150:
        -:  151:#if APR_HAS_THREAD_LOCAL
        -:  152:static APR_THREAD_LOCAL apr_thread_t *current_thread = NULL;
        -:  153:#endif
        -:  154:
      453:  155:static void *dummy_worker(void *opaque)
        -:  156:{
      453:  157:    apr_thread_t *thread = (apr_thread_t*)opaque;
        -:  158:    void *ret;
        -:  159:
        -:  160:#if APR_HAS_THREAD_LOCAL
      453:  161:    current_thread = thread;
        -:  162:#endif
        -:  163:
      453:  164:    apr_pool_owner_set(thread->pool, 0);
      454:  165:    ret = thread->func(thread, thread->data);
       26:  166:    if (thread->detached) {
    #####:  167:        apr_pool_destroy(thread->pool);
        -:  168:    }
        -:  169:
       26:  170:    return ret;
        -:  171:}
        -:  172:
      454:  173:static apr_status_t alloc_thread(apr_thread_t **new,
        -:  174:                                 apr_threadattr_t *attr,
        -:  175:                                 apr_thread_start_t func, void *data,
        -:  176:                                 apr_pool_t *pool)
        -:  177:{
        -:  178:    apr_status_t stat;
      454:  179:    apr_abortfunc_t abort_fn = apr_pool_abort_get(pool);
        -:  180:    apr_pool_t *p;
        -:  181:
        -:  182:    /* The thread can be detached anytime (from the creation or later with
        -:  183:     * apr_thread_detach), so it needs its own pool and allocator to not
        -:  184:     * depend on a parent pool which could be destroyed before the thread
        -:  185:     * exits. The allocator needs no mutex obviously since the pool should
        -:  186:     * not be used nor create children pools outside the thread. Passing
        -:  187:     * NULL allocator will create one like that.
        -:  188:     */
      454:  189:    stat = apr_pool_create_unmanaged_ex(&p, abort_fn, NULL);
      454:  190:    if (stat != APR_SUCCESS) {
    #####:  191:        return stat;
        -:  192:    }
     454*:  193:    if (attr && attr->max_free) {
    #####:  194:        apr_allocator_max_free_set(apr_pool_allocator_get(p), attr->max_free);
        -:  195:    }
        -:  196:
      454:  197:    (*new) = (apr_thread_t *)apr_pcalloc(p, sizeof(apr_thread_t));
      454:  198:    if ((*new) == NULL) {
    #####:  199:        apr_pool_destroy(p);
    #####:  200:        return APR_ENOMEM;
        -:  201:    }
        -:  202:
      454:  203:    (*new)->pool = p;
      454:  204:    (*new)->data = data;
      454:  205:    (*new)->func = func;
     454*:  206:    (*new)->detached = (attr && apr_threadattr_detach_get(attr) == APR_DETACH);
      454:  207:    (*new)->td = (pthread_t *)apr_pcalloc(p, sizeof(pthread_t));
      454:  208:    if ((*new)->td == NULL) {
    #####:  209:        apr_pool_destroy(p);
    #####:  210:        return APR_ENOMEM;
        -:  211:    }
        -:  212:
      454:  213:    return APR_SUCCESS;
        -:  214:}
        -:  215:
      454:  216:APR_DECLARE(apr_status_t) apr_thread_create(apr_thread_t **new,
        -:  217:                                            apr_threadattr_t *attr,
        -:  218:                                            apr_thread_start_t func,
        -:  219:                                            void *data,
        -:  220:                                            apr_pool_t *pool)
        -:  221:{
        -:  222:    apr_status_t stat;
        -:  223:    pthread_attr_t *temp;
        -:  224:
      454:  225:    stat = alloc_thread(new, attr, func, data, pool);
      454:  226:    if (stat != APR_SUCCESS) {
    #####:  227:        return stat;
        -:  228:    }
        -:  229:
      454:  230:    if (attr)
    #####:  231:        temp = &attr->attr;
        -:  232:    else
      454:  233:        temp = NULL;
        -:  234:
      454:  235:    if ((stat = pthread_create((*new)->td, temp, dummy_worker, (*new)))) {
        -:  236:#ifdef HAVE_ZOS_PTHREADS
        -:  237:        stat = errno;
        -:  238:#endif
    #####:  239:        apr_pool_destroy((*new)->pool);
    #####:  240:        return stat;
        -:  241:    }
        -:  242:
      454:  243:    return APR_SUCCESS;
        -:  244:}
        -:  245:
    #####:  246:APR_DECLARE(apr_status_t) apr_thread_current_create(apr_thread_t **current,
        -:  247:                                                    apr_threadattr_t *attr,
        -:  248:                                                    apr_pool_t *pool)
        -:  249:{
        -:  250:#if APR_HAS_THREAD_LOCAL
        -:  251:    apr_status_t stat;
        -:  252:
    #####:  253:    *current = apr_thread_current();
    #####:  254:    if (*current) {
    #####:  255:        return APR_EEXIST;
        -:  256:    }
        -:  257:
    #####:  258:    stat = alloc_thread(current, attr, NULL, NULL, pool);
    #####:  259:    if (stat != APR_SUCCESS) {
    #####:  260:        *current = NULL;
    #####:  261:        return stat;
        -:  262:    }
        -:  263:
    #####:  264:    *(*current)->td = apr_os_thread_current();
        -:  265:
    #####:  266:    current_thread = *current;
    #####:  267:    return APR_SUCCESS;
        -:  268:#else
        -:  269:    return APR_ENOTIMPL;
        -:  270:#endif
        -:  271:}
        -:  272:
      274:  273:APR_DECLARE(void) apr_thread_current_after_fork(void)
        -:  274:{
        -:  275:#if APR_HAS_THREAD_LOCAL
      274:  276:    current_thread = NULL;
        -:  277:#endif
      274:  278:}
        -:  279:
    #####:  280:APR_DECLARE(apr_thread_t *) apr_thread_current(void)
        -:  281:{
        -:  282:#if APR_HAS_THREAD_LOCAL
    #####:  283:    return current_thread;
        -:  284:#else
        -:  285:    return NULL;
        -:  286:#endif
        -:  287:}
        -:  288:
        4:  289:APR_DECLARE(apr_status_t) apr_thread_name_set(const char *name,
        -:  290:                                              apr_thread_t *thread,
        -:  291:                                              apr_pool_t *pool)
        -:  292:{
        -:  293:#if HAVE_PTHREAD_SETNAME_NP
        -:  294:    pthread_t td;
        -:  295:
        -:  296:    size_t name_len;
        4:  297:    if (!name) {
    #####:  298:        return APR_BADARG;
        -:  299:    }
        -:  300:
        4:  301:    if (thread) {
    #####:  302:        td = *thread->td;
        -:  303:    }
        -:  304:    else {
        4:  305:        td = pthread_self();
        -:  306:    }
        -:  307:
        4:  308:    name_len = strlen(name);
        4:  309:    if (name_len >= TASK_COMM_LEN) {
    #####:  310:        name = name + name_len - TASK_COMM_LEN + 1;
        -:  311:    }
        -:  312:
        4:  313:    return pthread_setname_np(td, name);
        -:  314:#else
        -:  315:    return APR_ENOTIMPL;
        -:  316:#endif
        -:  317:}
        -:  318:
        4:  319:APR_DECLARE(apr_status_t) apr_thread_name_get(char **name,
        -:  320:                                              apr_thread_t *thread,
        -:  321:                                              apr_pool_t *pool)
        -:  322:{
        -:  323:#if HAVE_PTHREAD_SETNAME_NP
        -:  324:    pthread_t td;
        4:  325:    if (thread) {
    #####:  326:        td = *thread->td;
        -:  327:    }
        -:  328:    else {
        4:  329:        td = pthread_self();
        -:  330:    }
        -:  331:
        4:  332:    *name = apr_pcalloc(pool, TASK_COMM_LEN);
        4:  333:    return pthread_getname_np(td, *name, TASK_COMM_LEN);
        -:  334:#else
        -:  335:    return APR_ENOTIMPL;
        -:  336:#endif
        -:  337:}
        -:  338:
    #####:  339:APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
        -:  340:{
    #####:  341:    return pthread_self();
        -:  342:}
        -:  343:
    #####:  344:APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
        -:  345:                                     apr_os_thread_t tid2)
        -:  346:{
    #####:  347:    return pthread_equal(tid1, tid2);
        -:  348:}
        -:  349:
      428:  350:APR_DECLARE(void) apr_thread_exit(apr_thread_t *thd,
        -:  351:                                  apr_status_t retval)
        -:  352:{
      428:  353:    thd->exitval = retval;
      428:  354:    if (thd->detached) {
    #####:  355:        apr_pool_destroy(thd->pool);
        -:  356:    }
      428:  357:    pthread_exit(NULL);
        -:  358:}
        -:  359:
      454:  360:APR_DECLARE(apr_status_t) apr_thread_join(apr_status_t *retval,
        -:  361:                                          apr_thread_t *thd)
        -:  362:{
        -:  363:    apr_status_t stat;
        -:  364:    void *thread_stat;
        -:  365:
      454:  366:    if (thd->detached) {
    #####:  367:        return APR_EINVAL;
        -:  368:    }
        -:  369:
      454:  370:    if ((stat = pthread_join(*thd->td, &thread_stat))) {
        -:  371:#ifdef HAVE_ZOS_PTHREADS
        -:  372:        stat = errno;
        -:  373:#endif
    #####:  374:        return stat;
        -:  375:    }
        -:  376:
      454:  377:    *retval = thd->exitval;
      454:  378:    apr_pool_destroy(thd->pool);
      454:  379:    return APR_SUCCESS;
        -:  380:}
        -:  381:
    #####:  382:APR_DECLARE(apr_status_t) apr_thread_detach(apr_thread_t *thd)
        -:  383:{
        -:  384:    apr_status_t stat;
        -:  385:
    #####:  386:    if (thd->detached) {
    #####:  387:        return APR_EINVAL;
        -:  388:    }
        -:  389:
        -:  390:#ifdef HAVE_ZOS_PTHREADS
        -:  391:    if ((stat = pthread_detach(thd->td)) == 0) {
        -:  392:#else
    #####:  393:    if ((stat = pthread_detach(*thd->td)) == 0) {
        -:  394:#endif
    #####:  395:        thd->detached = 1;
        -:  396:
    #####:  397:        return APR_SUCCESS;
        -:  398:    }
        -:  399:    else {
        -:  400:#ifdef HAVE_ZOS_PTHREADS
        -:  401:        stat = errno;
        -:  402:#endif
        -:  403:
    #####:  404:        return stat;
        -:  405:    }
        -:  406:}
        -:  407:
   340814:  408:APR_DECLARE(void) apr_thread_yield(void)
        -:  409:{
        -:  410:#ifdef HAVE_PTHREAD_YIELD
        -:  411:#ifdef HAVE_ZOS_PTHREADS
        -:  412:    pthread_yield(NULL);
        -:  413:#else
        -:  414:    pthread_yield();
        -:  415:#endif /* HAVE_ZOS_PTHREADS */
        -:  416:#else
        -:  417:#ifdef HAVE_SCHED_YIELD
   340814:  418:    sched_yield();
        -:  419:#endif
        -:  420:#endif
   381878:  421:}
        -:  422:
    #####:  423:APR_DECLARE(apr_status_t) apr_thread_data_get(void **data, const char *key,
        -:  424:                                              apr_thread_t *thread)
        -:  425:{
    #####:  426:    if (thread == NULL) {
    #####:  427:        *data = NULL;
    #####:  428:        return APR_ENOTHREAD;
        -:  429:    }
    #####:  430:    return apr_pool_userdata_get(data, key, thread->pool);
        -:  431:}
        -:  432:
       48:  433:APR_DECLARE(apr_status_t) apr_thread_data_set(void *data, const char *key,
        -:  434:                              apr_status_t (*cleanup)(void *),
        -:  435:                              apr_thread_t *thread)
        -:  436:{
       48:  437:    if (thread == NULL) {
    #####:  438:        return APR_ENOTHREAD;
        -:  439:    }
       48:  440:    return apr_pool_userdata_set(data, key, cleanup, thread->pool);
        -:  441:}
        -:  442:
    #####:  443:APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
        -:  444:                                            apr_thread_t *thd)
        -:  445:{
    #####:  446:    *thethd = thd->td;
    #####:  447:    return APR_SUCCESS;
        -:  448:}
        -:  449:
    #####:  450:APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
        -:  451:                                            apr_os_thread_t *thethd,
        -:  452:                                            apr_pool_t *pool)
        -:  453:{
    #####:  454:    if (pool == NULL) {
    #####:  455:        return APR_ENOPOOL;
        -:  456:    }
        -:  457:
    #####:  458:    if ((*thd) == NULL) {
    #####:  459:        (*thd) = (apr_thread_t *)apr_pcalloc(pool, sizeof(apr_thread_t));
    #####:  460:        (*thd)->pool = pool;
        -:  461:    }
        -:  462:
    #####:  463:    (*thd)->td = thethd;
    #####:  464:    return APR_SUCCESS;
        -:  465:}
        -:  466:
        2:  467:APR_DECLARE(apr_status_t) apr_thread_once_init(apr_thread_once_t **control,
        -:  468:                                               apr_pool_t *p)
        -:  469:{
        -:  470:    static const pthread_once_t once_init = PTHREAD_ONCE_INIT;
        -:  471:
        2:  472:    *control = apr_palloc(p, sizeof(**control));
        2:  473:    (*control)->once = once_init;
        2:  474:    return APR_SUCCESS;
        -:  475:}
        -:  476:
        8:  477:APR_DECLARE(apr_status_t) apr_thread_once(apr_thread_once_t *control,
        -:  478:                                          void (*func)(void))
        -:  479:{
        8:  480:    return pthread_once(&control->once, func);
        -:  481:}
        -:  482:
    #####:  483:APR_POOL_IMPLEMENT_ACCESSOR(thread)
        -:  484:
        -:  485:#endif  /* HAVE_PTHREAD_H */
        -:  486:#endif  /* APR_HAS_THREADS */
        -:  487:
        -:  488:#if !APR_HAS_THREADS
        -:  489:
        -:  490:/* avoid warning for no prototype */
        -:  491:APR_DECLARE(apr_status_t) apr_os_thread_get(void);
        -:  492:
        -:  493:APR_DECLARE(apr_status_t) apr_os_thread_get(void)
        -:  494:{
        -:  495:    return APR_ENOTIMPL;
        -:  496:}
        -:  497:
        -:  498:#endif
