key.c 3.2 KB

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