summaryrefslogtreecommitdiff
path: root/cleanup.c
blob: f670b97a37cecb70693de5f91dc338118823f442 (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
/*
 * cleanup.c
 *
 * Description:
 * This translation unit implements routines associated cleaning up
 * threads.
 */

#include "pthread.h"
#include "implement.h"

void
_pthread_cleanup_push(void (*routine)(void *), void *arg)
{
  _pthread_cleanup_node_t * next;
  int t;

  t = _pthread_getthreadindex(pthread_self());

  next = (_pthread_cleanup_node_t *) malloc(sizeof(_pthread_cleanup_node_t));
  if (next == NULL) {
    /* FIXME: INTERNAL ERROR */
  }

  next->next = _pthread_threads_table[t]->cleanupstack->first;
  next->routine = routine;
  next->arg = arg;
  _pthread_threads_table[t]->cleanupstack->first = next;
}

void
_pthread_cleanup_pop(int execute)
{
  _pthread_cleanup_node_t * handler;
  void (* func)(void *);
  void * arg;
  int t;

  t = _pthread_getthreadindex(pthread_self());
  handler = _pthread_threads_table[t]->cleanupstack->first;

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

    free(handler);

    if (execute != 0)
      (void) func(arg);

    _pthread_threads_table[t]->cleanupstack->first = next;
  }
}

void
_pthread_do_cancellation(int tindex)
{
  _pthread_cleanup_stack_t * stack;

  stack = _pthread_threads_table[tindex]->cleanupstack;

  /* Run all the cleanup handlers */
  while (stack->first != NULL) {
    _pthread_cleanup_pop(1);
  }
}