threadpriv.c revision 256281
174462Salfred/* Licensed to the Apache Software Foundation (ASF) under one or more 274462Salfred * contributor license agreements. See the NOTICE file distributed with 374462Salfred * this work for additional information regarding copyright ownership. 474462Salfred * The ASF licenses this file to You under the Apache License, Version 2.0 574462Salfred * (the "License"); you may not use this file except in compliance with 674462Salfred * the License. You may obtain a copy of the License at 774462Salfred * 874462Salfred * http://www.apache.org/licenses/LICENSE-2.0 974462Salfred * 1074462Salfred * Unless required by applicable law or agreed to in writing, software 1174462Salfred * distributed under the License is distributed on an "AS IS" BASIS, 1274462Salfred * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1374462Salfred * See the License for the specific language governing permissions and 1474462Salfred * limitations under the License. 1574462Salfred */ 1674462Salfred 1774462Salfred#include "apr.h" 1874462Salfred#include "apr_portable.h" 1974462Salfred#include "apr_arch_threadproc.h" 2074462Salfred 2174462Salfred#if APR_HAS_THREADS 2274462Salfred 2374462Salfred#if APR_HAVE_PTHREAD_H 2474462SalfredAPR_DECLARE(apr_status_t) apr_threadkey_private_create(apr_threadkey_t **key, 2574462Salfred void (*dest)(void *), 2674462Salfred apr_pool_t *pool) 2774462Salfred{ 2874462Salfred (*key) = (apr_threadkey_t *)apr_pcalloc(pool, sizeof(apr_threadkey_t)); 2974462Salfred 3074462Salfred if ((*key) == NULL) { 31108533Sschweikh return APR_ENOMEM; 3274462Salfred } 3374462Salfred 3474462Salfred (*key)->pool = pool; 3574462Salfred 3674462Salfred return pthread_key_create(&(*key)->key, dest); 3774462Salfred 3874462Salfred} 3974462Salfred 4074462SalfredAPR_DECLARE(apr_status_t) apr_threadkey_private_get(void **new, 4174462Salfred apr_threadkey_t *key) 4274462Salfred{ 4374462Salfred#ifdef PTHREAD_GETSPECIFIC_TAKES_TWO_ARGS 4474462Salfred if (pthread_getspecific(key->key,new)) 4574462Salfred *new = NULL; 4674462Salfred#else 4774462Salfred (*new) = pthread_getspecific(key->key); 4874462Salfred#endif 4974462Salfred return APR_SUCCESS; 5074462Salfred} 5174462Salfred 5274462SalfredAPR_DECLARE(apr_status_t) apr_threadkey_private_set(void *priv, 5374462Salfred apr_threadkey_t *key) 5474462Salfred{ 5574462Salfred apr_status_t stat; 5674462Salfred 5774462Salfred if ((stat = pthread_setspecific(key->key, priv)) == 0) { 5874462Salfred return APR_SUCCESS; 5974462Salfred } 6074462Salfred else { 6174462Salfred return stat; 6274462Salfred } 6374462Salfred} 6474462Salfred 6574462SalfredAPR_DECLARE(apr_status_t) apr_threadkey_private_delete(apr_threadkey_t *key) 66173412Skevlo{ 67173412Skevlo#ifdef HAVE_PTHREAD_KEY_DELETE 6874462Salfred apr_status_t stat; 6974462Salfred 7074462Salfred if ((stat = pthread_key_delete(key->key)) == 0) { 7174462Salfred return APR_SUCCESS; 7274462Salfred } 7374462Salfred 7474462Salfred return stat; 7574462Salfred#else 7674462Salfred return APR_ENOTIMPL; 7774462Salfred#endif 7874462Salfred} 7974462Salfred 8074462SalfredAPR_DECLARE(apr_status_t) apr_threadkey_data_get(void **data, const char *key, 8174462Salfred apr_threadkey_t *threadkey) 8274462Salfred{ 8374462Salfred return apr_pool_userdata_get(data, key, threadkey->pool); 8474462Salfred} 8574462Salfred 8674462SalfredAPR_DECLARE(apr_status_t) apr_threadkey_data_set(void *data, const char *key, 8774462Salfred apr_status_t (*cleanup)(void *), 8874462Salfred apr_threadkey_t *threadkey) 8974462Salfred{ 9074462Salfred return apr_pool_userdata_set(data, key, cleanup, threadkey->pool); 9174462Salfred} 9274462Salfred 9374462SalfredAPR_DECLARE(apr_status_t) apr_os_threadkey_get(apr_os_threadkey_t *thekey, 9474462Salfred apr_threadkey_t *key) 9574462Salfred{ 9674462Salfred *thekey = key->key; 9774462Salfred return APR_SUCCESS; 9874462Salfred} 9974462Salfred 10074462SalfredAPR_DECLARE(apr_status_t) apr_os_threadkey_put(apr_threadkey_t **key, 10174462Salfred apr_os_threadkey_t *thekey, 10274462Salfred apr_pool_t *pool) 10374462Salfred{ 10474462Salfred if (pool == NULL) { 10574462Salfred return APR_ENOPOOL; 10674462Salfred } 10774462Salfred 10874462Salfred if ((*key) == NULL) { 10974462Salfred (*key) = (apr_threadkey_t *)apr_pcalloc(pool, sizeof(apr_threadkey_t)); 11074462Salfred (*key)->pool = pool; 11174462Salfred } 11274462Salfred 11374462Salfred (*key)->key = *thekey; 11474462Salfred return APR_SUCCESS; 11574462Salfred} 11674462Salfred#endif /* APR_HAVE_PTHREAD_H */ 11774462Salfred#endif /* APR_HAS_THREADS */ 11874462Salfred 11974462Salfred#if !APR_HAS_THREADS 12074462Salfred 12174462Salfred/* avoid warning for no prototype */ 12274462SalfredAPR_DECLARE(apr_status_t) apr_os_threadkey_get(void); 12374462Salfred 12474462SalfredAPR_DECLARE(apr_status_t) apr_os_threadkey_get(void) 12574462Salfred{ 12674462Salfred return APR_ENOTIMPL; 12774462Salfred} 12874462Salfred 12974462Salfred#endif 13074462Salfred