threadpriv.c revision 251875
1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more 2251875Speter * contributor license agreements. See the NOTICE file distributed with 3251875Speter * this work for additional information regarding copyright ownership. 4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0 5251875Speter * (the "License"); you may not use this file except in compliance with 6251875Speter * the License. You may obtain a copy of the License at 7251875Speter * 8251875Speter * http://www.apache.org/licenses/LICENSE-2.0 9251875Speter * 10251875Speter * Unless required by applicable law or agreed to in writing, software 11251875Speter * distributed under the License is distributed on an "AS IS" BASIS, 12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13251875Speter * See the License for the specific language governing permissions and 14251875Speter * limitations under the License. 15251875Speter */ 16251875Speter 17251875Speter#include "apr.h" 18251875Speter#include "apr_portable.h" 19251875Speter#include "apr_arch_threadproc.h" 20251875Speter 21251875Speter#if APR_HAS_THREADS 22251875Speter 23251875Speter#if APR_HAVE_PTHREAD_H 24251875SpeterAPR_DECLARE(apr_status_t) apr_threadkey_private_create(apr_threadkey_t **key, 25251875Speter void (*dest)(void *), 26251875Speter apr_pool_t *pool) 27251875Speter{ 28251875Speter (*key) = (apr_threadkey_t *)apr_pcalloc(pool, sizeof(apr_threadkey_t)); 29251875Speter 30251875Speter if ((*key) == NULL) { 31251875Speter return APR_ENOMEM; 32251875Speter } 33251875Speter 34251875Speter (*key)->pool = pool; 35251875Speter 36251875Speter return pthread_key_create(&(*key)->key, dest); 37251875Speter 38251875Speter} 39251875Speter 40251875SpeterAPR_DECLARE(apr_status_t) apr_threadkey_private_get(void **new, 41251875Speter apr_threadkey_t *key) 42251875Speter{ 43251875Speter#ifdef PTHREAD_GETSPECIFIC_TAKES_TWO_ARGS 44251875Speter if (pthread_getspecific(key->key,new)) 45251875Speter *new = NULL; 46251875Speter#else 47251875Speter (*new) = pthread_getspecific(key->key); 48251875Speter#endif 49251875Speter return APR_SUCCESS; 50251875Speter} 51251875Speter 52251875SpeterAPR_DECLARE(apr_status_t) apr_threadkey_private_set(void *priv, 53251875Speter apr_threadkey_t *key) 54251875Speter{ 55251875Speter apr_status_t stat; 56251875Speter 57251875Speter if ((stat = pthread_setspecific(key->key, priv)) == 0) { 58251875Speter return APR_SUCCESS; 59251875Speter } 60251875Speter else { 61251875Speter return stat; 62251875Speter } 63251875Speter} 64251875Speter 65251875SpeterAPR_DECLARE(apr_status_t) apr_threadkey_private_delete(apr_threadkey_t *key) 66251875Speter{ 67251875Speter#ifdef HAVE_PTHREAD_KEY_DELETE 68251875Speter apr_status_t stat; 69251875Speter 70251875Speter if ((stat = pthread_key_delete(key->key)) == 0) { 71251875Speter return APR_SUCCESS; 72251875Speter } 73251875Speter 74251875Speter return stat; 75251875Speter#else 76251875Speter return APR_ENOTIMPL; 77251875Speter#endif 78251875Speter} 79251875Speter 80251875SpeterAPR_DECLARE(apr_status_t) apr_threadkey_data_get(void **data, const char *key, 81251875Speter apr_threadkey_t *threadkey) 82251875Speter{ 83251875Speter return apr_pool_userdata_get(data, key, threadkey->pool); 84251875Speter} 85251875Speter 86251875SpeterAPR_DECLARE(apr_status_t) apr_threadkey_data_set(void *data, const char *key, 87251875Speter apr_status_t (*cleanup)(void *), 88251875Speter apr_threadkey_t *threadkey) 89251875Speter{ 90251875Speter return apr_pool_userdata_set(data, key, cleanup, threadkey->pool); 91251875Speter} 92251875Speter 93251875SpeterAPR_DECLARE(apr_status_t) apr_os_threadkey_get(apr_os_threadkey_t *thekey, 94251875Speter apr_threadkey_t *key) 95251875Speter{ 96251875Speter *thekey = key->key; 97251875Speter return APR_SUCCESS; 98251875Speter} 99251875Speter 100251875SpeterAPR_DECLARE(apr_status_t) apr_os_threadkey_put(apr_threadkey_t **key, 101251875Speter apr_os_threadkey_t *thekey, 102251875Speter apr_pool_t *pool) 103251875Speter{ 104251875Speter if (pool == NULL) { 105251875Speter return APR_ENOPOOL; 106251875Speter } 107251875Speter 108251875Speter if ((*key) == NULL) { 109251875Speter (*key) = (apr_threadkey_t *)apr_pcalloc(pool, sizeof(apr_threadkey_t)); 110251875Speter (*key)->pool = pool; 111251875Speter } 112251875Speter 113251875Speter (*key)->key = *thekey; 114251875Speter return APR_SUCCESS; 115251875Speter} 116251875Speter#endif /* APR_HAVE_PTHREAD_H */ 117251875Speter#endif /* APR_HAS_THREADS */ 118251875Speter 119251875Speter#if !APR_HAS_THREADS 120251875Speter 121251875Speter/* avoid warning for no prototype */ 122251875SpeterAPR_DECLARE(apr_status_t) apr_os_threadkey_get(void); 123251875Speter 124251875SpeterAPR_DECLARE(apr_status_t) apr_os_threadkey_get(void) 125251875Speter{ 126251875Speter return APR_ENOTIMPL; 127251875Speter} 128251875Speter 129251875Speter#endif 130