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
|
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
**
** pth_cancel.c: Pth thread cancellation
*/
/* ``Study it forever and you'll still wonder.
Fly it once and you'll know.''
-- Henry Spencer */
#include "pth_p.h"
/* set cancellation state */
void pth_cancel_state(int newstate, int *oldstate)
{
if (oldstate != NULL)
*oldstate = pth_current->cancelstate;
if (newstate != 0)
pth_current->cancelstate = newstate;
return;
}
/* enter a cancellation point */
void pth_cancel_point(void)
{
if ( pth_current->cancelreq == TRUE
&& pth_current->cancelstate & PTH_CANCEL_ENABLE) {
/* avoid looping if cleanup handlers contain cancellation points */
pth_current->cancelreq = FALSE;
pth_debug2("pth_cancel_point: terminating cancelled thread \"%s\"", pth_current->name);
pth_exit(PTH_CANCELED);
}
return;
}
/* cancel a thread (the friendly way) */
int pth_cancel(pth_t thread)
{
pth_pqueue_t *q;
if (thread == NULL)
return pth_error(FALSE, EINVAL);
/* the current thread cannot be cancelled */
if (thread == pth_current)
return pth_error(FALSE, EINVAL);
/* the thread has to be at least still alive */
if (thread->state == PTH_STATE_DEAD)
return pth_error(FALSE, EPERM);
/* now mark the thread as cancelled */
thread->cancelreq = TRUE;
/* when cancellation is enabled in async mode we cancel the thread immediately */
if ( thread->cancelstate & PTH_CANCEL_ENABLE
&& thread->cancelstate & PTH_CANCEL_ASYNCHRONOUS) {
/* remove thread from its queue */
switch (thread->state) {
case PTH_STATE_NEW: q = &pth_NQ; break;
case PTH_STATE_READY: q = &pth_RQ; break;
case PTH_STATE_WAITING: q = &pth_WQ; break;
default: q = NULL;
}
if (q == NULL)
return pth_error(FALSE, ESRCH);
if (!pth_pqueue_contains(q, thread))
return pth_error(FALSE, ESRCH);
pth_pqueue_delete(q, thread);
/* execute cleanups */
pth_thread_cleanup(thread);
/* and now either kick it out or move it to dead queue */
if (!thread->joinable) {
pth_debug2("pth_cancel: kicking out cancelled thread \"%s\" immediately", thread->name);
pth_tcb_free(thread);
}
else {
pth_debug2("pth_cancel: moving cancelled thread \"%s\" to dead queue", thread->name);
thread->join_arg = PTH_CANCELED;
thread->state = PTH_STATE_DEAD;
pth_pqueue_insert(&pth_DQ, PTH_PRIO_STD, thread);
}
}
return TRUE;
}
/* abort a thread (the cruel way) */
int pth_abort(pth_t thread)
{
if (thread == NULL)
return pth_error(FALSE, EINVAL);
/* the current thread cannot be aborted */
if (thread == pth_current)
return pth_error(FALSE, EINVAL);
if (thread->state == PTH_STATE_DEAD && thread->joinable) {
/* if thread is already terminated, just join it */
if (!pth_join(thread, NULL))
return FALSE;
}
else {
/* else force it to be detached and cancel it asynchronously */
thread->joinable = FALSE;
thread->cancelstate = (PTH_CANCEL_ENABLE|PTH_CANCEL_ASYNCHRONOUS);
if (!pth_cancel(thread))
return FALSE;
}
return TRUE;
}
|