summaryrefslogtreecommitdiff
path: root/cleanup.c
blob: b0817d7f96b8ed7c7b98d4d5a7a2318898b3c8fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * cleanup.c
 *
 * Description:
 * This translation unit implements routines associated cleaning up
 * threads.
 */

#include <errno.h>

#include <malloc.h>
#include "pthread.h"
#include "implement.h"

/*
 * Code contributed by John E. Bossom <JEB>.
 */

_pthread_cleanup_t *
_pthread_pop_cleanup (int execute)
     /*
      * ------------------------------------------------------
      * DOCPUBLIC
      *      This function pops the most recently pushed cleanup
      *      handler. If execute is nonzero, then the cleanup handler
      *      is executed if non-null.
      *
      * PARAMETERS
      *      execute
      *              if nonzero, execute the cleanup handler
      *
      *
      * DESCRIPTION
      *      This function pops the most recently pushed cleanup
      *      handler. If execute is nonzero, then the cleanup handler
      *      is executed if non-null.
      *      NOTE: specify 'execute' as nonzero to avoid duplication
      *                of common cleanup code.
      *
      * RESULTS
      *              N/A
      *
      * ------------------------------------------------------
      */
{
  _pthread_cleanup_t *cleanup;
  
  cleanup = pthread_getspecific (_pthread_cleanupKey);

  if (cleanup != NULL)
    {
      if (execute && (cleanup->routine != NULL))
        {

#ifdef _WIN32

          __try
          {
            /*
             * Run the caller's cleanup routine.
             */
            (*cleanup->routine) (cleanup->arg);
          }
          __except (EXCEPTION_EXECUTE_HANDLER)
          {
            /*
             * A system unexpected exception had occurred
             * running the user's cleanup routine.
             * We get control back within this block.
             */
          }
        }

#else

      /*
       * Run the caller's cleanup routine.
       */
      (*cleanup->routine) (cleanup->arg);

#endif /* _WIN32 */

      pthread_setspecific (_pthread_cleanupKey, cleanup->prev);
    }

  return (cleanup);

}                               /* _pthread_pop_cleanup */


void
_pthread_push_cleanup (_pthread_cleanup_t * cleanup,
		      void (*routine) (void *),
		      void *arg)
     /*
      * ------------------------------------------------------
      * DOCPUBLIC
      *      This function pushes a new cleanup handler onto the thread's stack
      *      of cleanup handlers. Each cleanup handler pushed onto the stack is
      *      popped and invoked with the argument 'arg' when
      *              a) the thread exits by calling 'pthread_exit',
      *              b) when the thread acts on a cancellation request,
      *              c) or when the thrad calls pthread_cleanup_pop with a nonzero
      *                 'execute' argument
      *
      * PARAMETERS
      *      cleanup
      *              a pointer to an instance of pthread_cleanup_t,
      *
      *      routine
      *              pointer to a cleanup handler,
      *
      *      arg
      *              parameter to be passed to the cleanup handler
      *
      *
      * DESCRIPTION
      *      This function pushes a new cleanup handler onto the thread's stack
      *      of cleanup handlers. Each cleanup handler pushed onto the stack is
      *      popped and invoked with the argument 'arg' when
      *              a) the thread exits by calling 'pthread_exit',
      *              b) when the thread acts on a cancellation request,
      *              c) or when the thrad calls pthread_cleanup_pop with a nonzero
      *                 'execute' argument
      *      NOTE: pthread_push_cleanup, pthread_pop_cleanup must be paired
      *                in the same lexical scope.
      *
      * RESULTS
      *              pthread_cleanup_t *
      *                              pointer to the previous cleanup
      *
      * ------------------------------------------------------
      */
{
  cleanup->routine = routine;
  cleanup->arg = arg;
  cleanup->prev = pthread_getspecific (_pthread_cleanupKey);

  pthread_setspecific (_pthread_cleanupKey, (void *) cleanup);

}                               /* _pthread_push_cleanup */

/* </JEB> */


#if 0 /* Pre Bossom */

int
_pthread_handler_push(int stack,
		      int poporder,
		      void (*routine)(void *), 
		      void *arg)
{
  /* Place the new handler into the list so that handlers are
     popped off in the order given by poporder. */
  _pthread_handler_node_t * new_handler;
  _pthread_handler_node_t * next;
  _pthread_handler_node_t ** stacktop;

  stacktop = _PTHREAD_STACK(stack);

  new_handler = 
    (_pthread_handler_node_t *) malloc(sizeof(_pthread_handler_node_t));

  if (new_handler == NULL)
    {
      return 0; /* NOMEM */
    }

  new_handler->routine = routine;
  new_handler->arg = arg;

  if (poporder == _PTHREAD_HANDLER_POP_LIFO)
    {
      /* Add the new node to the start of the list. */
      new_handler->next = *stacktop;
      *stacktop = new_handler;
    }
  else
    {
      /* Add the new node to the end of the list. */
      new_handler->next = NULL;

      if (*stacktop == NULL)
	{
	  *stacktop = new_handler;
	}
      else
	{
	  next = *stacktop;

	  while (next->next != NULL)
	    {
	      next = next->next;
	    }

	  next->next = new_handler;
	}
    }
  return 0;
}

void
_pthread_handler_pop(int stack, int execute)
{
  _pthread_handler_node_t ** stacktop;
  _pthread_handler_node_t * next;
  void (* func)(void *);
  void * arg;

  stacktop = _PTHREAD_STACK(stack);

  if (*stacktop != NULL)
    {
      func = (*stacktop)->routine;
      arg = (*stacktop)->arg;
      next = (*stacktop)->next;

      free(*stacktop);
      *stacktop = next;

      if (execute != 0 && func != NULL)
	{
	  (void) func(arg);
	}
    }
}

void
_pthread_handler_pop_all(int stack, int execute)
{
  /* Pop and possibly run all handlers on the given stack. */
  _pthread_handler_node_t ** stacktop;
  _pthread_handler_node_t * next;
  void (* func)(void *);
  void * arg;

  stacktop = _PTHREAD_STACK(stack);

  while (*stacktop != NULL)
    {
      func = (*stacktop)->routine;
      arg = (*stacktop)->arg;
      next = (*stacktop)->next;

      free(*stacktop);
      *stacktop = next;

      if (execute != 0 && func != NULL)
	{
	  (void) func(arg);
	}
    }
}

/* Run destructors for all non-NULL key values for the calling thread.
 */
void
_pthread_destructor_run_all()
{
  _pthread_tsd_key_t * key;
  int count;
  int dirty;

  /* This threads private keys */
  key = _pthread_tsd_key_table;

  /* Stop destructor execution at a finite time. POSIX allows us
     to ignore this if we like, even at the risk of an infinite loop.

     FIXME: We don't know when to stop yet.
   */
  for (count = 0; count < PTHREAD_DESTRUCTOR_ITERATIONS; count++)
    {
      int k;
      void * arg;

      dirty = 0;

      /* Loop through all keys. */
      for (k = 0; k < _POSIX_THREAD_KEYS_MAX; k++)
	{
	  /* CRITICAL SECTION */
	  pthread_mutex_lock(&_pthread_tsd_mutex);

	  switch (key->status)
	    {
	    case _PTHREAD_TSD_KEY_INUSE:
	      arg = pthread_getspecific((pthread_key_t) k);

	      if (arg != NULL && key->destructor != NULL)
		{
		  /* The destructor must be called with the mutex off. */
		  pthread_mutex_unlock(&_pthread_tsd_mutex);
		  /* END CRITICAL SECTION */

		  /* FIXME: Is the destructor supposed to set the key value
		     to NULL? How is this done when arg is the key value, not
		     a pointer to it? For now we assume that the destructor
		     always succeeds.
		     */
		  (void) (key->destructor)(arg);

		  /* CRITICAL SECTION */
		  pthread_mutex_lock(&_pthread_tsd_mutex);

		  pthread_setspecific((pthread_key_t) k, NULL);
#if 0
		  /* Only needed if we don't assume the destructor
		     always succeeds.
		     */
		  dirty = 1;
#endif
		}
	      break;

	    case _PTHREAD_TSD_KEY_DELETED:
	      key->status = _PTHREAD_TSD_KEY_INUSE;
	      pthread_setspecific((pthread_key_t) k, NULL);

	      if (key->in_use <= 0)
		{
		  /* This is the last thread to use this
		     deleted key. It can now be made available
		     for re-use.
		   */
		  key->status = _PTHREAD_TSD_KEY_REUSE;
		  _pthread_key_reuse[_pthread_key_reuse_top++] = k;
		}
	      else
		{
		  key->status = _PTHREAD_TSD_KEY_DELETED;
		}
	      break;

	    default:
	      break;
	    }

	  pthread_mutex_unlock(&_pthread_tsd_mutex);
	  /* END CRITICAL SECTION */

	  key++;
	}

      if (!dirty)
	break;
    }
}

#endif /* Pre Bossom */