OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libpthread/] [linuxthreads/] [cancel.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Linuxthreads - a simple clone()-based implementation of Posix        */
2
/* threads for Linux.                                                   */
3
/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
4
/*                                                                      */
5
/* This program is free software; you can redistribute it and/or        */
6
/* modify it under the terms of the GNU Library General Public License  */
7
/* as published by the Free Software Foundation; either version 2       */
8
/* of the License, or (at your option) any later version.               */
9
/*                                                                      */
10
/* This program is distributed in the hope that it will be useful,      */
11
/* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
12
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
13
/* GNU Library General Public License for more details.                 */
14
 
15
/* Thread cancellation */
16
 
17
#define __FORCE_GLIBC
18
#include <features.h>
19
#include <errno.h>
20
#include "pthread.h"
21
#include "internals.h"
22
#include "spinlock.h"
23
#include "restart.h"
24
#ifdef __UCLIBC_HAS_RPC__
25
#include <rpc/rpc.h>
26
extern void __rpc_thread_destroy(void);
27
#endif
28
 
29
 
30
int pthread_setcancelstate(int state, int * oldstate)
31
{
32
  pthread_descr self = thread_self();
33
  if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE)
34
    return EINVAL;
35
  if (oldstate != NULL) *oldstate = THREAD_GETMEM(self, p_cancelstate);
36
  THREAD_SETMEM(self, p_cancelstate, state);
37
  if (THREAD_GETMEM(self, p_canceled) &&
38
      THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
39
      THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
40
    pthread_exit(PTHREAD_CANCELED);
41
  return 0;
42
}
43
 
44
int pthread_setcanceltype(int type, int * oldtype)
45
{
46
  pthread_descr self = thread_self();
47
  if (type < PTHREAD_CANCEL_DEFERRED || type > PTHREAD_CANCEL_ASYNCHRONOUS)
48
    return EINVAL;
49
  if (oldtype != NULL) *oldtype = THREAD_GETMEM(self, p_canceltype);
50
  THREAD_SETMEM(self, p_canceltype, type);
51
  if (THREAD_GETMEM(self, p_canceled) &&
52
      THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
53
      THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
54
    pthread_exit(PTHREAD_CANCELED);
55
  return 0;
56
}
57
 
58
int pthread_cancel(pthread_t thread)
59
{
60
  pthread_handle handle = thread_handle(thread);
61
  int pid;
62
  int dorestart = 0;
63
  pthread_descr th;
64
  pthread_extricate_if *pextricate;
65
 
66
  __pthread_lock(&handle->h_lock, NULL);
67
  if (invalid_handle(handle, thread)) {
68
    __pthread_unlock(&handle->h_lock);
69
    return ESRCH;
70
  }
71
 
72
  th = handle->h_descr;
73
 
74
  if (th->p_canceled) {
75
    __pthread_unlock(&handle->h_lock);
76
    return 0;
77
  }
78
 
79
  pextricate = th->p_extricate;
80
  th->p_canceled = 1;
81
  pid = th->p_pid;
82
 
83
  /* If the thread has registered an extrication interface, then
84
     invoke the interface. If it returns 1, then we succeeded in
85
     dequeuing the thread from whatever waiting object it was enqueued
86
     with. In that case, it is our responsibility to wake it up.
87
     And also to set the p_woken_by_cancel flag so the woken thread
88
     can tell that it was woken by cancellation. */
89
 
90
  if (pextricate != NULL) {
91
    dorestart = pextricate->pu_extricate_func(pextricate->pu_object, th);
92
    th->p_woken_by_cancel = dorestart;
93
  }
94
 
95
  __pthread_unlock(&handle->h_lock);
96
 
97
  /* If the thread has suspended or is about to, then we unblock it by
98
     issuing a restart, instead of a cancel signal. Otherwise we send
99
     the cancel signal to unblock the thread from a cancellation point,
100
     or to initiate asynchronous cancellation. The restart is needed so
101
     we have proper accounting of restarts; suspend decrements the thread's
102
     resume count, and restart() increments it.  This also means that suspend's
103
     handling of the cancel signal is obsolete. */
104
 
105
  if (dorestart)
106
    restart(th);
107
  else
108
    kill(pid, __pthread_sig_cancel);
109
 
110
  return 0;
111
}
112
 
113
void pthread_testcancel(void)
114
{
115
  pthread_descr self = thread_self();
116
  if (THREAD_GETMEM(self, p_canceled)
117
      && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)
118
    pthread_exit(PTHREAD_CANCELED);
119
}
120
 
121
void _pthread_cleanup_push(struct _pthread_cleanup_buffer * buffer,
122
                           void (*routine)(void *), void * arg)
123
{
124
  pthread_descr self = thread_self();
125
  buffer->__routine = routine;
126
  buffer->__arg = arg;
127
  buffer->__prev = THREAD_GETMEM(self, p_cleanup);
128
  THREAD_SETMEM(self, p_cleanup, buffer);
129
}
130
 
131
void _pthread_cleanup_pop(struct _pthread_cleanup_buffer * buffer,
132
                          int execute)
133
{
134
  pthread_descr self = thread_self();
135
  if (execute) buffer->__routine(buffer->__arg);
136
  THREAD_SETMEM(self, p_cleanup, buffer->__prev);
137
}
138
 
139
void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * buffer,
140
                                 void (*routine)(void *), void * arg)
141
{
142
  pthread_descr self = thread_self();
143
  buffer->__routine = routine;
144
  buffer->__arg = arg;
145
  buffer->__canceltype = THREAD_GETMEM(self, p_canceltype);
146
  buffer->__prev = THREAD_GETMEM(self, p_cleanup);
147
  THREAD_SETMEM(self, p_canceltype, PTHREAD_CANCEL_DEFERRED);
148
  THREAD_SETMEM(self, p_cleanup, buffer);
149
}
150
 
151
void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * buffer,
152
                                  int execute)
153
{
154
  pthread_descr self = thread_self();
155
  if (execute) buffer->__routine(buffer->__arg);
156
  THREAD_SETMEM(self, p_cleanup, buffer->__prev);
157
  THREAD_SETMEM(self, p_canceltype, buffer->__canceltype);
158
  if (THREAD_GETMEM(self, p_canceled) &&
159
      THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
160
      THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
161
    pthread_exit(PTHREAD_CANCELED);
162
}
163
 
164
void __pthread_perform_cleanup(void)
165
{
166
  pthread_descr self = thread_self();
167
  struct _pthread_cleanup_buffer * c;
168
  for (c = THREAD_GETMEM(self, p_cleanup); c != NULL; c = c->__prev)
169
    c->__routine(c->__arg);
170
 
171
#ifdef __UCLIBC_HAS_RPC__
172
  /* And the TSD which needs special help.  */
173
  if (THREAD_GETMEM(self, p_libc_specific[_LIBC_TSD_KEY_RPC_VARS]) != NULL)
174
      __rpc_thread_destroy ();
175
#endif
176
}
177
 
178
#ifndef __PIC__
179
/* We need a hook to force the cancelation wrappers to be linked in when
180
   static libpthread is used.  */
181
extern const int __pthread_provide_wrappers;
182
static const int * const __pthread_require_wrappers =
183
  &__pthread_provide_wrappers;
184
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.