blob: 8700d3e68b62e31c52826b7440824095e364a6a2 (
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
|
/* $OpenBSD: uthread_cancel.c,v 1.5 2001/08/30 17:47:57 todd Exp $ */
/*
* David Leonard <d@openbsd.org>, 1999. Public domain.
*/
#include <sys/errno.h>
#include <pthread.h>
#include "pthread_private.h"
int
pthread_cancel(pthread)
pthread_t pthread;
{
int ret;
if ((ret = _find_thread(pthread))) {
} else if ((pthread->flags & PTHREAD_FLAGS_CANCELED) == 0) {
/* Set the thread's I've-been-cancelled flag: */
pthread->flags |= PTHREAD_FLAGS_CANCELED;
/* Check if we need to kick it back into the run queue: */
if ((pthread->cancelstate == PTHREAD_CANCEL_ENABLE) &&
((pthread->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) ||
(pthread->flags & PTHREAD_FLAGS_CANCELPT)))
switch (pthread->state) {
case PS_WAIT_WAIT:
case PS_FDR_WAIT:
case PS_FDW_WAIT:
case PS_SLEEP_WAIT:
case PS_SELECT_WAIT:
case PS_POLL_WAIT:
case PS_SIGSUSPEND:
/* Interrupt and resume: */
pthread->interrupted = 1;
if (pthread->flags & PTHREAD_FLAGS_IN_WORKQ)
PTHREAD_WORKQ_REMOVE(pthread);
PTHREAD_NEW_STATE(pthread,PS_RUNNING);
break;
case PS_MUTEX_WAIT:
case PS_COND_WAIT:
case PS_SIGWAIT:
case PS_FDLR_WAIT:
case PS_FDLW_WAIT:
case PS_FILE_WAIT:
case PS_JOIN:
case PS_SUSPENDED:
case PS_SIGTHREAD:
/* Simply wake: */
/* XXX may be incorrect */
PTHREAD_NEW_STATE(pthread,PS_RUNNING);
break;
case PS_RUNNING:
case PS_DEADLOCK:
case PS_SPINBLOCK:
case PS_DEAD:
/* Ignore */
break;
}
ret = 0;
}
return (ret);
}
int
pthread_setcancelstate(state, oldstate)
int state;
int *oldstate;
{
struct pthread *curthread = _get_curthread();
int ostate;
int ret;
ostate = curthread->cancelstate;
switch (state) {
case PTHREAD_CANCEL_ENABLE:
if (oldstate)
*oldstate = ostate;
curthread->cancelstate = PTHREAD_CANCEL_ENABLE;
if (curthread->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
_thread_cancellation_point();
ret = 0;
break;
case PTHREAD_CANCEL_DISABLE:
if (oldstate)
*oldstate = ostate;
curthread->cancelstate = PTHREAD_CANCEL_DISABLE;
ret = 0;
break;
default:
ret = EINVAL;
}
return (ret);
}
int
pthread_setcanceltype(type, oldtype)
int type;
int *oldtype;
{
struct pthread *curthread = _get_curthread();
int otype;
int ret;
otype = curthread->canceltype;
switch (type) {
case PTHREAD_CANCEL_ASYNCHRONOUS:
if (oldtype)
*oldtype = otype;
curthread->canceltype = PTHREAD_CANCEL_ASYNCHRONOUS;
_thread_cancellation_point();
ret = 0;
break;
case PTHREAD_CANCEL_DEFERRED:
if (oldtype)
*oldtype = otype;
curthread->canceltype = PTHREAD_CANCEL_DEFERRED;
ret = 0;
break;
default:
ret = EINVAL;
}
return (ret);
}
void
pthread_testcancel()
{
_thread_cancellation_point();
}
void
_thread_enter_cancellation_point()
{
struct pthread *curthread = _get_curthread();
/* Look for a cancellation before we block: */
_thread_cancellation_point();
curthread->flags |= PTHREAD_FLAGS_CANCELPT;
}
void
_thread_leave_cancellation_point()
{
struct pthread *curthread = _get_curthread();
curthread->flags &=~ PTHREAD_FLAGS_CANCELPT;
/* Look for a cancellation after we unblock: */
_thread_cancellation_point();
}
/*
* Must only be called when in asynchronous cancel mode, or
* from pthread_testcancel().
*/
void
_thread_cancellation_point()
{
struct pthread *curthread = _get_curthread();
if ((curthread->cancelstate == PTHREAD_CANCEL_ENABLE) &&
((curthread->flags & (PTHREAD_FLAGS_CANCELED|PTHREAD_EXITING)) ==
PTHREAD_FLAGS_CANCELED)) {
curthread->flags &=~ PTHREAD_FLAGS_CANCELED;
pthread_exit(PTHREAD_CANCELED);
PANIC("cancel");
}
}
|