summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/cancel.c
blob: 32a9a908906678f75aeb34e89cefbfb9a81a2c57 (plain) (blame)
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
/*  cancel.c
 *
 */

#include <pthread.h>
#include <errno.h>
#include <rtems/score/chain.h>
#include <rtems/score/isr.h>
#include <rtems/score/thread.h>
#include <rtems/score/wkspace.h>
#include <rtems/posix/cancel.h>
#include <rtems/posix/pthread.h>
#include <rtems/posix/threadsup.h>

/*PAGE
 *
 *  POSIX_Thread_cancel_run
 *
 */

void POSIX_Thread_cancel_run( 
  Thread_Control *the_thread
)
{
  int                                old_cancel_state;
  POSIX_Cancel_Handler_control      *handler;
  Chain_Control                     *handler_stack;
  POSIX_API_Thread_Support_Control  *thread_support;
  ISR_Level                          level;
 
  thread_support = the_thread->API_Extensions[ THREAD_API_POSIX ];
 
  handler_stack = &thread_support->Cancellation_Handlers;
 
  old_cancel_state = thread_support->cancelability_state;

  thread_support->cancelability_state = PTHREAD_CANCEL_DISABLE;

  while ( !_Chain_Is_empty( handler_stack ) ) {
    _ISR_Disable( level );
      handler = (POSIX_Cancel_Handler_control *) _Chain_Tail( handler_stack );
      _Chain_Extract_unprotected( &handler->Node );
    _ISR_Enable( level );
 
    (*handler->routine)( handler->arg );

    _Workspace_Free( handler );
  }

  thread_support->cancelation_requested = 0;

  thread_support->cancelability_state = old_cancel_state;
}

/*PAGE
 *
 *  18.2.1 Canceling Execution of a Thread, P1003.1c/Draft 10, p. 181
 */

int pthread_cancel(
  pthread_t  thread
)
{
  Thread_Control                    *the_thread;
  POSIX_API_Thread_Support_Control  *thread_support;
  Objects_Locations                  location;

  the_thread = _POSIX_Threads_Get( &thread, &location );
  switch ( location ) {
    case OBJECTS_ERROR:
      return EINVAL;
    case OBJECTS_REMOTE:
      return POSIX_MP_NOT_IMPLEMENTED();
    case OBJECTS_LOCAL:
      thread_support = the_thread->API_Extensions[ THREAD_API_POSIX ];

      thread_support->cancelation_requested = 1;

      _Thread_Enable_dispatch();
      return 0;
  }
 
  return POSIX_BOTTOM_REACHED();
}

/*PAGE
 *
 *  18.2.2 Setting Cancelability State, P1003.1c/Draft 10, p. 183
 */

int pthread_setcancelstate( 
  int  state,
  int *oldstate
)
{
  POSIX_API_Thread_Support_Control  *thread_support;

  if ( !oldstate )
    return EINVAL;

  if ( state != PTHREAD_CANCEL_ENABLE && state != PTHREAD_CANCEL_DISABLE )
    return EINVAL;

  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
 
  *oldstate = thread_support->cancelability_state;
  thread_support->cancelability_state = state;
 
  if ( thread_support->cancelability_state == PTHREAD_CANCEL_ENABLE && 
       thread_support->cancelability_type == PTHREAD_CANCEL_ASYNCHRONOUS &&
       thread_support->cancelation_requested )
    POSIX_Thread_cancel_run( _Thread_Executing );
 
  return 0;
}

/*PAGE
 *
 *  18.2.2 Setting Cancelability State, P1003.1c/Draft 10, p. 183
 */

int pthread_setcanceltype(
  int  type,
  int *oldtype
)
{
  POSIX_API_Thread_Support_Control  *thread_support;
 
  if ( !oldtype )
    return EINVAL;
 
  if ( type != PTHREAD_CANCEL_DEFERRED && type != PTHREAD_CANCEL_ASYNCHRONOUS )
    return EINVAL;
 
  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];

  *oldtype = thread_support->cancelability_type;
  thread_support->cancelability_type = type;

  if ( thread_support->cancelability_state == PTHREAD_CANCEL_ENABLE && 
       thread_support->cancelability_type == PTHREAD_CANCEL_ASYNCHRONOUS &&
       thread_support->cancelation_requested )
    POSIX_Thread_cancel_run( _Thread_Executing );

  return 0;
}

/*PAGE
 *
 *  18.2.2 Setting Cancelability State, P1003.1c/Draft 10, p. 183
 */

void pthread_testcancel( void )
{
  POSIX_API_Thread_Support_Control  *thread_support;

  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
 
  if ( thread_support->cancelability_state == PTHREAD_CANCEL_ENABLE &&
       thread_support->cancelation_requested )
    POSIX_Thread_cancel_run( _Thread_Executing );
}

/*PAGE
 *
 *  18.2.3.1 Establishing Cancellation Handlers, P1003.1c/Draft 10, p. 184
 */

void pthread_cleanup_push(
  void   (*routine)( void * ),
  void    *arg
)
{
  POSIX_Cancel_Handler_control      *handler;
  Chain_Control                     *handler_stack;
  POSIX_API_Thread_Support_Control  *thread_support;
  
  if ( !routine )
    return;          /* XXX what to do really? */ 

  handler = _Workspace_Allocate( sizeof( POSIX_Cancel_Handler_control ) );

  if ( !handler )
    return;          /* XXX what to do really? */ 

  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];

  handler_stack = &thread_support->Cancellation_Handlers;

  handler->routine = routine;
  handler->arg = arg;

  _Chain_Append( handler_stack, &handler->Node );
}

/*PAGE
 *
 *  18.2.3.1 Establishing Cancellation Handlers, P1003.1c/Draft 10, p. 184
 */

void pthread_cleanup_pop(
  int    execute
)
{
  POSIX_Cancel_Handler_control      *handler;
  Chain_Control                     *handler_stack;
  POSIX_API_Thread_Support_Control  *thread_support;
  ISR_Level                          level;
 
  thread_support = _Thread_Executing->API_Extensions[ THREAD_API_POSIX ];
 
  handler_stack = &thread_support->Cancellation_Handlers;

  if ( _Chain_Is_empty( handler_stack ) )
    return;
 
  _ISR_Disable( level );
    handler = (POSIX_Cancel_Handler_control *) _Chain_Tail( handler_stack );
    _Chain_Extract_unprotected( &handler->Node );
  _ISR_Enable( level );

  if ( execute )
    (*handler->routine)( handler->arg );
 
  _Workspace_Free( handler );
}