2
0

threadpriv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include "apr.h"
  17. #include "apr_portable.h"
  18. #include "apr_arch_threadproc.h"
  19. #if APR_HAS_THREADS
  20. #if APR_HAVE_PTHREAD_H
  21. APR_DECLARE(apr_status_t) apr_threadkey_private_create(apr_threadkey_t **key,
  22. void (*dest)(void *),
  23. apr_pool_t *pool)
  24. {
  25. (*key) = (apr_threadkey_t *)apr_pcalloc(pool, sizeof(apr_threadkey_t));
  26. if ((*key) == NULL) {
  27. return APR_ENOMEM;
  28. }
  29. (*key)->pool = pool;
  30. return pthread_key_create(&(*key)->key, dest);
  31. }
  32. APR_DECLARE(apr_status_t) apr_threadkey_private_get(void **new,
  33. apr_threadkey_t *key)
  34. {
  35. #ifdef PTHREAD_GETSPECIFIC_TAKES_TWO_ARGS
  36. if (pthread_getspecific(key->key,new))
  37. *new = NULL;
  38. #else
  39. (*new) = pthread_getspecific(key->key);
  40. #endif
  41. return APR_SUCCESS;
  42. }
  43. APR_DECLARE(apr_status_t) apr_threadkey_private_set(void *priv,
  44. apr_threadkey_t *key)
  45. {
  46. apr_status_t stat;
  47. if ((stat = pthread_setspecific(key->key, priv)) == 0) {
  48. return APR_SUCCESS;
  49. }
  50. else {
  51. return stat;
  52. }
  53. }
  54. APR_DECLARE(apr_status_t) apr_threadkey_private_delete(apr_threadkey_t *key)
  55. {
  56. #ifdef HAVE_PTHREAD_KEY_DELETE
  57. apr_status_t stat;
  58. if ((stat = pthread_key_delete(key->key)) == 0) {
  59. return APR_SUCCESS;
  60. }
  61. return stat;
  62. #else
  63. return APR_ENOTIMPL;
  64. #endif
  65. }
  66. APR_DECLARE(apr_status_t) apr_threadkey_data_get(void **data, const char *key,
  67. apr_threadkey_t *threadkey)
  68. {
  69. return apr_pool_userdata_get(data, key, threadkey->pool);
  70. }
  71. APR_DECLARE(apr_status_t) apr_threadkey_data_set(void *data, const char *key,
  72. apr_status_t (*cleanup)(void *),
  73. apr_threadkey_t *threadkey)
  74. {
  75. return apr_pool_userdata_set(data, key, cleanup, threadkey->pool);
  76. }
  77. APR_DECLARE(apr_status_t) apr_os_threadkey_get(apr_os_threadkey_t *thekey,
  78. apr_threadkey_t *key)
  79. {
  80. *thekey = key->key;
  81. return APR_SUCCESS;
  82. }
  83. APR_DECLARE(apr_status_t) apr_os_threadkey_put(apr_threadkey_t **key,
  84. apr_os_threadkey_t *thekey,
  85. apr_pool_t *pool)
  86. {
  87. if (pool == NULL) {
  88. return APR_ENOPOOL;
  89. }
  90. if ((*key) == NULL) {
  91. (*key) = (apr_threadkey_t *)apr_pcalloc(pool, sizeof(apr_threadkey_t));
  92. (*key)->pool = pool;
  93. }
  94. (*key)->key = *thekey;
  95. return APR_SUCCESS;
  96. }
  97. #endif /* APR_HAVE_PTHREAD_H */
  98. #endif /* APR_HAS_THREADS */
  99. #if !APR_HAS_THREADS
  100. /* avoid warning for no prototype */
  101. APR_DECLARE(apr_status_t) apr_os_threadkey_get(void);
  102. APR_DECLARE(apr_status_t) apr_os_threadkey_get(void)
  103. {
  104. return APR_ENOTIMPL;
  105. }
  106. #endif