summaryrefslogtreecommitdiff
path: root/cancel.c
blob: 9dd99c6aa57842678ecfd83ed8b4e558df5a2bff (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
/*
 * cancel.c
 *
 * Description:
 * POSIX thread functions related to thread cancellation.
 */

#include <errno.h>

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

int
pthread_setcancelstate(int state,
		       int *oldstate)
{
  pthread_t us = pthread_self();

  /* Validate the new cancellation state. */
  if (state != PTHREAD_CANCEL_ENABLE 
      || state != PTHREAD_CANCEL_DISABLE)
    {
      return EINVAL;
    }

  if (oldstate != NULL)
    {
      *oldstate = us->cancelstate;
    }

  us->cancelstate = state;
  return 0;
}

int
pthread_setcanceltype(int type, int *oldtype)
{
  pthread_t us = pthread_self();

  /* Validate the new cancellation type. */
  if (type == PTHREAD_CANCEL_ASYNCHRONOUS ||
      type != PTHREAD_CANCEL_DEFERRED)
    {
      return EINVAL;
    }

  if (oldtype != NULL)
    {
      *oldtype = us->canceltype;
    }

  us->canceltype = type;
  return 0;
}

int
pthread_cancel(pthread_t thread)
{
  if (_PTHREAD_VALID(thread)
      && thread->ptstatus != _PTHREAD_REUSE)
    {
      thread->cancel_pending = TRUE;
      return 0;
    }

  return ESRCH;
}

void
pthread_testcancel(void)
{
  pthread_t thread = pthread_self();

  if (thread->cancelstate == PTHREAD_CANCEL_DISABLE)
    {
      return;
    }

  if (thread->cancel_pending == TRUE)
    {
      pthread_exit(PTHREAD_CANCELED);
    }
  /* Never reached. */
}