key.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later */
  2. /*
  3. * The contents of this file are subject to the Mozilla Public
  4. * License Version 1.1 (the "License"); you may not use this file
  5. * except in compliance with the License. You may obtain a copy of
  6. * the License at http://www.mozilla.org/MPL/
  7. *
  8. * Software distributed under the License is distributed on an "AS
  9. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing
  11. * rights and limitations under the License.
  12. *
  13. * The Original Code is the Netscape Portable Runtime library.
  14. *
  15. * The Initial Developer of the Original Code is Netscape
  16. * Communications Corporation. Portions created by Netscape are
  17. * Copyright (C) 1994-2000 Netscape Communications Corporation. All
  18. * Rights Reserved.
  19. *
  20. * Contributor(s): Silicon Graphics, Inc.
  21. *
  22. * Portions created by SGI are Copyright (C) 2000-2001 Silicon
  23. * Graphics, Inc. All Rights Reserved.
  24. *
  25. * Alternatively, the contents of this file may be used under the
  26. * terms of the GNU General Public License Version 2 or later (the
  27. * "GPL"), in which case the provisions of the GPL are applicable
  28. * instead of those above. If you wish to allow use of your
  29. * version of this file only under the terms of the GPL and not to
  30. * allow others to use your version of this file under the MPL,
  31. * indicate your decision by deleting the provisions above and
  32. * replace them with the notice and other provisions required by
  33. * the GPL. If you do not delete the provisions above, a recipient
  34. * may use your version of this file under either the MPL or the
  35. * GPL.
  36. */
  37. /*
  38. * This file is derived directly from Netscape Communications Corporation,
  39. * and consists of extensive modifications made during the year(s) 1999-2000.
  40. */
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. #include "common.h"
  44. /*
  45. * Destructor table for per-thread private data
  46. */
  47. static _st_destructor_t _st_destructors[ST_KEYS_MAX];
  48. static int key_max = 0;
  49. /*
  50. * Return a key to be used for thread specific data
  51. */
  52. int st_key_create(int *keyp, _st_destructor_t destructor)
  53. {
  54. if (key_max >= ST_KEYS_MAX) {
  55. errno = EAGAIN;
  56. return -1;
  57. }
  58. *keyp = key_max++;
  59. _st_destructors[*keyp] = destructor;
  60. return 0;
  61. }
  62. int st_key_getlimit(void)
  63. {
  64. return ST_KEYS_MAX;
  65. }
  66. int st_thread_setspecific(int key, void *value)
  67. {
  68. _st_thread_t *me = _ST_CURRENT_THREAD();
  69. return st_thread_setspecific2(me, key, value);
  70. }
  71. int st_thread_setspecific2(_st_thread_t *me, int key, void *value)
  72. {
  73. if (key < 0 || key >= key_max) {
  74. errno = EINVAL;
  75. return -1;
  76. }
  77. if (value != me->private_data[key]) {
  78. /* free up previously set non-NULL data value */
  79. if (me->private_data[key] && _st_destructors[key]) {
  80. (*_st_destructors[key])(me->private_data[key]);
  81. }
  82. me->private_data[key] = value;
  83. }
  84. return 0;
  85. }
  86. void *st_thread_getspecific(int key)
  87. {
  88. if (key < 0 || key >= key_max)
  89. return NULL;
  90. return ((_ST_CURRENT_THREAD())->private_data[key]);
  91. }
  92. /*
  93. * Free up all per-thread private data
  94. */
  95. void _st_thread_cleanup(_st_thread_t *thread)
  96. {
  97. int key;
  98. for (key = 0; key < key_max; key++) {
  99. if (thread->private_data[key] && _st_destructors[key]) {
  100. (*_st_destructors[key])(thread->private_data[key]);
  101. thread->private_data[key] = NULL;
  102. }
  103. }
  104. }