2
0

threadpriv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "fspr_arch_threadproc.h"
  17. static struct beos_key key_table[BEOS_MAX_DATAKEYS];
  18. static struct beos_private_data *beos_data[BEOS_MAX_DATAKEYS];
  19. static sem_id lock;
  20. APR_DECLARE(fspr_status_t) fspr_threadkey_private_create(fspr_threadkey_t **key,
  21. void (*dest)(void *), fspr_pool_t *pool)
  22. {
  23. (*key) = (fspr_threadkey_t *)fspr_palloc(pool, sizeof(fspr_threadkey_t));
  24. if ((*key) == NULL) {
  25. return APR_ENOMEM;
  26. }
  27. (*key)->pool = pool;
  28. acquire_sem(lock);
  29. for ((*key)->key=0; (*key)->key < BEOS_MAX_DATAKEYS; (*key)->key++){
  30. if (key_table[(*key)->key].assigned == 0){
  31. key_table[(*key)->key].assigned = 1;
  32. key_table[(*key)->key].destructor = dest;
  33. release_sem(lock);
  34. return APR_SUCCESS;
  35. }
  36. }
  37. release_sem(lock);
  38. return APR_ENOMEM;
  39. }
  40. APR_DECLARE(fspr_status_t) fspr_threadkey_private_get(void **new, fspr_threadkey_t *key)
  41. {
  42. thread_id tid;
  43. int i, index=0;
  44. tid = find_thread(NULL);
  45. for (i=0;i<BEOS_MAX_DATAKEYS;i++){
  46. if (beos_data[i]->data){
  47. /* it's been used */
  48. if (beos_data[i]->td == tid){
  49. index = i;
  50. }
  51. }
  52. }
  53. if (index == 0){
  54. /* no storage for thread so we can't get anything... */
  55. return APR_ENOMEM;
  56. }
  57. if ((key->key < BEOS_MAX_DATAKEYS) && (key_table)){
  58. acquire_sem(key_table[key->key].lock);
  59. if (key_table[key->key].count){
  60. (*new) = (void*)beos_data[index]->data[key->key];
  61. } else {
  62. (*new) = NULL;
  63. }
  64. release_sem(key_table[key->key].lock);
  65. } else {
  66. (*new) = NULL;
  67. }
  68. return APR_SUCCESS;
  69. }
  70. APR_DECLARE(fspr_status_t) fspr_threadkey_private_set(void *priv, fspr_threadkey_t *key)
  71. {
  72. thread_id tid;
  73. int i,index = 0, ret = 0;
  74. tid = find_thread(NULL);
  75. for (i=0; i < BEOS_MAX_DATAKEYS; i++){
  76. if (beos_data[i]->data){
  77. if (beos_data[i]->td == tid){index = i;}
  78. }
  79. }
  80. if (index==0){
  81. /* not yet been allocated */
  82. for (i=0; i< BEOS_MAX_DATAKEYS; i++){
  83. if (! beos_data[i]->data){
  84. /* we'll take this one... */
  85. index = i;
  86. beos_data[i]->data = (const void **)malloc(sizeof(void *) * BEOS_MAX_DATAKEYS);
  87. memset((void *)beos_data[i]->data, 0, sizeof(void *) * BEOS_MAX_DATAKEYS);
  88. beos_data[i]->count = (int)malloc(sizeof(int));
  89. beos_data[i]->td = (thread_id)malloc(sizeof(thread_id));
  90. beos_data[i]->td = tid;
  91. }
  92. }
  93. }
  94. if (index == 0){
  95. /* we're out of luck.. */
  96. return APR_ENOMEM;
  97. }
  98. if ((key->key < BEOS_MAX_DATAKEYS) && (key_table)){
  99. acquire_sem(key_table[key->key].lock);
  100. if (key_table[key->key].count){
  101. if (beos_data[index]->data[key->key] == NULL){
  102. if (priv != NULL){
  103. beos_data[index]->count++;
  104. key_table[key->key].count++;
  105. }
  106. } else {
  107. if (priv == NULL){
  108. beos_data[index]->count--;
  109. key_table[key->key].count--;
  110. }
  111. }
  112. beos_data[index]->data[key->key] = priv;
  113. ret = 1;
  114. } else {
  115. ret = 0;
  116. }
  117. release_sem(key_table[key->key].lock);
  118. }
  119. if (ret)
  120. return APR_SUCCESS;
  121. return APR_ENOMEM;
  122. }
  123. APR_DECLARE(fspr_status_t) fspr_threadkey_private_delete(fspr_threadkey_t *key)
  124. {
  125. if (key->key < BEOS_MAX_DATAKEYS){
  126. acquire_sem(key_table[key->key].lock);
  127. if (key_table[key->key].count == 1){
  128. key_table[key->key].destructor = NULL;
  129. key_table[key->key].count = 0;
  130. }
  131. release_sem(key_table[key->key].lock);
  132. } else {
  133. return APR_ENOMEM;
  134. }
  135. return APR_SUCCESS;
  136. }
  137. APR_DECLARE(fspr_status_t) fspr_threadkey_data_get(void **data, const char *key,
  138. fspr_threadkey_t *threadkey)
  139. {
  140. return fspr_pool_userdata_get(data, key, threadkey->pool);
  141. }
  142. APR_DECLARE(fspr_status_t) fspr_threadkey_data_set(void *data, const char *key,
  143. fspr_status_t (*cleanup) (void *),
  144. fspr_threadkey_t *threadkey)
  145. {
  146. return fspr_pool_userdata_set(data, key, cleanup, threadkey->pool);
  147. }
  148. APR_DECLARE(fspr_status_t) fspr_os_threadkey_get(fspr_os_threadkey_t *thekey, fspr_threadkey_t *key)
  149. {
  150. *thekey = key->key;
  151. return APR_SUCCESS;
  152. }
  153. APR_DECLARE(fspr_status_t) fspr_os_threadkey_put(fspr_threadkey_t **key,
  154. fspr_os_threadkey_t *thekey, fspr_pool_t *pool)
  155. {
  156. if (pool == NULL) {
  157. return APR_ENOPOOL;
  158. }
  159. if ((*key) == NULL) {
  160. (*key) = (fspr_threadkey_t *)fspr_pcalloc(pool, sizeof(fspr_threadkey_t));
  161. (*key)->pool = pool;
  162. }
  163. (*key)->key = *thekey;
  164. return APR_SUCCESS;
  165. }