Coverage Report

Created: 2026-11-15 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nspr/pr/src/threads/prtpd.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/*
6
** Thread Private Data
7
**
8
** There is an aribitrary limit on the number of keys that will be allocated
9
** by the runtime. It's largish, so it is intended to be a sanity check, not
10
** an impediment.
11
**
12
** There is a counter, initialized to zero and incremented every time a
13
** client asks for a new key, that holds the high water mark for keys. All
14
** threads logically have the same high water mark and are permitted to
15
** ask for TPD up to that key value.
16
**
17
** The vector to hold the TPD are allocated when PR_SetThreadPrivate() is
18
** called. The size of the vector will be some value greater than or equal
19
** to the current high water mark. Each thread has its own TPD length and
20
** vector.
21
**
22
** Threads that get private data for keys they have not set (or perhaps
23
** don't even exist for that thread) get a NULL return. If the key is
24
** beyond the high water mark, an error will be returned.
25
*/
26
27
/*
28
** As of this time, BeOS has its own TPD implementation.  Integrating
29
** this standard one is a TODO for anyone with a bit of spare time on
30
** their hand.  For now, we just #ifdef out this whole file and use
31
** the routines in pr/src/btthreads/
32
*/
33
34
#include "primpl.h"
35
36
#include <string.h>
37
38
#if defined(WIN95)
39
/*
40
** Some local variables report warnings on Win95 because the code paths
41
** using them are conditioned on HAVE_CUSTOME_USER_THREADS.
42
** The pragma suppresses the warning.
43
**
44
*/
45
#  pragma warning(disable : 4101)
46
#endif
47
48
94
#define _PR_TPD_LIMIT 128             /* arbitary limit on the TPD slots */
49
static PRInt32 _pr_tpd_length = 0;    /* current length of destructor vector */
50
static PRInt32 _pr_tpd_highwater = 0; /* next TPD key to be assigned */
51
static PRThreadPrivateDTOR* _pr_tpd_destructors = NULL;
52
/* the destructors are associated with
53
    the keys, therefore asserting that
54
    the TPD key depicts the data's 'type' */
55
56
/*
57
** Initialize the thread private data manipulation
58
*/
59
21
void _PR_InitTPD(void) {
60
21
  _pr_tpd_destructors = (PRThreadPrivateDTOR*)PR_CALLOC(
61
21
      _PR_TPD_LIMIT * sizeof(PRThreadPrivateDTOR*));
62
21
  PR_ASSERT(NULL != _pr_tpd_destructors);
63
21
  _pr_tpd_length = _PR_TPD_LIMIT;
64
21
}
65
66
/*
67
** Clean up the thread private data manipulation
68
*/
69
0
void _PR_CleanupTPD(void) {} /* _PR_CleanupTPD */
70
71
/*
72
** This routine returns a new index for per-thread-private data table.
73
** The index is visible to all threads within a process. This index can
74
** be used with the PR_SetThreadPrivate() and PR_GetThreadPrivate() routines
75
** to save and retrieve data associated with the index for a thread.
76
**
77
** The index independently maintains specific values for each binding thread.
78
** A thread can only get access to its own thread-specific-data.
79
**
80
** Upon a new index return the value associated with the index for all threads
81
** is NULL, and upon thread creation the value associated with all indices for
82
** that thread is NULL.
83
**
84
**     "dtor" is the destructor function to invoke when the private
85
**       data is set or destroyed
86
**
87
** Returns PR_FAILURE if the total number of indices will exceed the maximun
88
** allowed.
89
*/
90
91
PR_IMPLEMENT(PRStatus)
92
39
PR_NewThreadPrivateIndex(PRUintn* newIndex, PRThreadPrivateDTOR dtor) {
93
39
  PRStatus rv;
94
39
  PRInt32 index;
95
96
39
  if (!_pr_initialized) {
97
0
    _PR_ImplicitInitialization();
98
0
  }
99
100
39
  PR_ASSERT(NULL != newIndex);
101
39
  PR_ASSERT(NULL != _pr_tpd_destructors);
102
103
39
  index = PR_ATOMIC_INCREMENT(&_pr_tpd_highwater) - 1; /* allocate index */
104
39
  if (_PR_TPD_LIMIT <= index) {
105
0
    PR_SetError(PR_TPD_RANGE_ERROR, 0);
106
0
    rv = PR_FAILURE; /* that's just wrong */
107
39
  } else {
108
39
    _pr_tpd_destructors[index] = dtor; /* record destructor @index */
109
39
    *newIndex = (PRUintn)index;        /* copy into client's location */
110
39
    rv = PR_SUCCESS;                   /* that's okay */
111
39
  }
112
113
39
  return rv;
114
39
}
115
116
/*
117
** Define some per-thread-private data.
118
**     "index" is an index into the per-thread private data table
119
**     "priv" is the per-thread-private data
120
**
121
** If the per-thread private data table has a previously registered
122
** destructor function and a non-NULL per-thread-private data value,
123
** the destructor function is invoked.
124
**
125
** This can return PR_FAILURE if index is invalid (ie., beyond the limit
126
** on the TPD slots) or memory is insufficient to allocate an expanded
127
** vector.
128
*/
129
130
34
PR_IMPLEMENT(PRStatus) PR_SetThreadPrivate(PRUintn index, void* priv) {
131
34
  PRThread* self = PR_GetCurrentThread();
132
133
  /*
134
  ** To improve performance, we don't check if the index has been
135
  ** allocated.
136
  */
137
34
  if (index >= _PR_TPD_LIMIT) {
138
0
    PR_SetError(PR_TPD_RANGE_ERROR, 0);
139
0
    return PR_FAILURE;
140
0
  }
141
142
34
  PR_ASSERT(((NULL == self->privateData) && (0 == self->tpdLength)) ||
143
34
            ((NULL != self->privateData) && (0 != self->tpdLength)));
144
145
  /*
146
  ** If this thread does not have a sufficient vector for the index
147
  ** being set, go ahead and extend this vector now.
148
  */
149
34
  if ((NULL == self->privateData) || (self->tpdLength <= index)) {
150
18
    void* extension = PR_CALLOC(_pr_tpd_length * sizeof(void*));
151
18
    if (NULL == extension) {
152
0
      PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
153
0
      return PR_FAILURE;
154
0
    }
155
18
    if (self->privateData) {
156
0
      (void)memcpy(extension, self->privateData,
157
0
                   self->tpdLength * sizeof(void*));
158
0
      PR_DELETE(self->privateData);
159
0
    }
160
18
    self->tpdLength = _pr_tpd_length;
161
18
    self->privateData = (void**)extension;
162
18
  }
163
  /*
164
  ** There wasn't much chance of having to call the destructor
165
  ** unless the slot already existed.
166
  */
167
16
  else if (self->privateData[index] && _pr_tpd_destructors[index]) {
168
16
    void* data = self->privateData[index];
169
16
    self->privateData[index] = NULL;
170
16
    (*_pr_tpd_destructors[index])(data);
171
16
  }
172
173
34
  PR_ASSERT(index < self->tpdLength);
174
34
  self->privateData[index] = priv;
175
176
34
  return PR_SUCCESS;
177
34
}
178
179
/*
180
** Recover the per-thread-private data for the current thread. "index" is
181
** the index into the per-thread private data table.
182
**
183
** The returned value may be NULL which is indistinguishable from an error
184
** condition.
185
**
186
*/
187
188
3.07M
PR_IMPLEMENT(void*) PR_GetThreadPrivate(PRUintn index) {
189
3.07M
  PRThread* self = PR_GetCurrentThread();
190
3.07M
  void* tpd = ((NULL == self->privateData) || (index >= self->tpdLength))
191
3.07M
                  ? NULL
192
3.07M
                  : self->privateData[index];
193
194
3.07M
  return tpd;
195
3.07M
}
196
197
/*
198
** Destroy the thread's private data, if any exists. This is called at
199
** thread termination time only. There should be no threading issues
200
** since this is being called by the thread itself.
201
*/
202
0
void _PR_DestroyThreadPrivate(PRThread* self) {
203
0
#define _PR_TPD_DESTRUCTOR_ITERATIONS 4
204
205
0
  if (NULL != self->privateData) /* we have some */
206
0
  {
207
0
    PRBool clean;
208
0
    PRUint32 index;
209
0
    PRInt32 passes = _PR_TPD_DESTRUCTOR_ITERATIONS;
210
0
    PR_ASSERT(0 != self->tpdLength);
211
0
    do {
212
0
      clean = PR_TRUE;
213
0
      for (index = 0; index < self->tpdLength; ++index) {
214
0
        void* priv = self->privateData[index]; /* extract */
215
0
        if (NULL != priv)                      /* we have data at this index */
216
0
        {
217
0
          if (NULL != _pr_tpd_destructors[index]) {
218
0
            self->privateData[index] = NULL;     /* precondition */
219
0
            (*_pr_tpd_destructors[index])(priv); /* destroy */
220
0
            clean = PR_FALSE;                    /* unknown side effects */
221
0
          }
222
0
        }
223
0
      }
224
0
    } while ((--passes > 0) && !clean); /* limit # of passes */
225
    /*
226
    ** We give up after a fixed number of passes. Any non-NULL
227
    ** thread-private data value with a registered destructor
228
    ** function is not destroyed.
229
    */
230
0
    memset(self->privateData, 0, self->tpdLength * sizeof(void*));
231
0
  }
232
0
} /* _PR_DestroyThreadPrivate */