xref: /relibc/pthreads-emb/pthread_setcanceltype.c (revision a12978ccca4c576f02971f5cfc2cbefe23e4daba)
1062c5bc4SJason Schmidlapp /*
2062c5bc4SJason Schmidlapp  * pthread_setcanceltype.c
3062c5bc4SJason Schmidlapp  *
4062c5bc4SJason Schmidlapp  * Description:
5062c5bc4SJason Schmidlapp  * POSIX thread functions related to thread cancellation.
6062c5bc4SJason Schmidlapp  *
7062c5bc4SJason Schmidlapp  * --------------------------------------------------------------------------
8062c5bc4SJason Schmidlapp  *
9062c5bc4SJason Schmidlapp  *      Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
10062c5bc4SJason Schmidlapp  *      Copyright(C) 2008 Jason Schmidlapp
11062c5bc4SJason Schmidlapp  *
12062c5bc4SJason Schmidlapp  *      Contact Email: jschmidlapp@users.sourceforge.net
13062c5bc4SJason Schmidlapp  *
14062c5bc4SJason Schmidlapp  *
15062c5bc4SJason Schmidlapp  *      Based upon Pthreads-win32 - POSIX Threads Library for Win32
16062c5bc4SJason Schmidlapp  *      Copyright(C) 1998 John E. Bossom
17062c5bc4SJason Schmidlapp  *      Copyright(C) 1999,2005 Pthreads-win32 contributors
18062c5bc4SJason Schmidlapp  *
19062c5bc4SJason Schmidlapp  *      Contact Email: rpj@callisto.canberra.edu.au
20062c5bc4SJason Schmidlapp  *
21062c5bc4SJason Schmidlapp  *      The original list of contributors to the Pthreads-win32 project
22062c5bc4SJason Schmidlapp  *      is contained in the file CONTRIBUTORS.ptw32 included with the
23062c5bc4SJason Schmidlapp  *      source code distribution. The list can also be seen at the
24062c5bc4SJason Schmidlapp  *      following World Wide Web location:
25062c5bc4SJason Schmidlapp  *      http://sources.redhat.com/pthreads-win32/contributors.html
26062c5bc4SJason Schmidlapp  *
27062c5bc4SJason Schmidlapp  *      This library is free software; you can redistribute it and/or
28062c5bc4SJason Schmidlapp  *      modify it under the terms of the GNU Lesser General Public
29062c5bc4SJason Schmidlapp  *      License as published by the Free Software Foundation; either
30062c5bc4SJason Schmidlapp  *      version 2 of the License, or (at your option) any later version.
31062c5bc4SJason Schmidlapp  *
32062c5bc4SJason Schmidlapp  *      This library is distributed in the hope that it will be useful,
33062c5bc4SJason Schmidlapp  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
34062c5bc4SJason Schmidlapp  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35062c5bc4SJason Schmidlapp  *      Lesser General Public License for more details.
36062c5bc4SJason Schmidlapp  *
37062c5bc4SJason Schmidlapp  *      You should have received a copy of the GNU Lesser General Public
38062c5bc4SJason Schmidlapp  *      License along with this library in the file COPYING.LIB;
39062c5bc4SJason Schmidlapp  *      if not, write to the Free Software Foundation, Inc.,
40062c5bc4SJason Schmidlapp  *      59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
41062c5bc4SJason Schmidlapp  */
42062c5bc4SJason Schmidlapp 
43062c5bc4SJason Schmidlapp #include "pthread.h"
44062c5bc4SJason Schmidlapp #include "implement.h"
45062c5bc4SJason Schmidlapp 
46062c5bc4SJason Schmidlapp 
47062c5bc4SJason Schmidlapp int
pthread_setcanceltype(int type,int * oldtype)48062c5bc4SJason Schmidlapp pthread_setcanceltype (int type, int *oldtype)
49062c5bc4SJason Schmidlapp /*
50062c5bc4SJason Schmidlapp  * ------------------------------------------------------
51062c5bc4SJason Schmidlapp  * DOCPUBLIC
52062c5bc4SJason Schmidlapp  *      This function atomically sets the calling thread's
53062c5bc4SJason Schmidlapp  *      cancelability type to 'type' and returns the previous
54062c5bc4SJason Schmidlapp  *      cancelability type at the location referenced by
55062c5bc4SJason Schmidlapp  *      'oldtype'
56062c5bc4SJason Schmidlapp  *
57062c5bc4SJason Schmidlapp  * PARAMETERS
58062c5bc4SJason Schmidlapp  *      type,
59062c5bc4SJason Schmidlapp  *      oldtype
60062c5bc4SJason Schmidlapp  *              PTHREAD_CANCEL_DEFERRED
61062c5bc4SJason Schmidlapp  *                      only deferred cancelation is allowed,
62062c5bc4SJason Schmidlapp  *
63062c5bc4SJason Schmidlapp  *              PTHREAD_CANCEL_ASYNCHRONOUS
64062c5bc4SJason Schmidlapp  *                      Asynchronous cancellation is allowed
65062c5bc4SJason Schmidlapp  *
66062c5bc4SJason Schmidlapp  *
67062c5bc4SJason Schmidlapp  * DESCRIPTION
68062c5bc4SJason Schmidlapp  *      This function atomically sets the calling thread's
69062c5bc4SJason Schmidlapp  *      cancelability type to 'type' and returns the previous
70062c5bc4SJason Schmidlapp  *      cancelability type at the location referenced by
71062c5bc4SJason Schmidlapp  *      'oldtype'
72062c5bc4SJason Schmidlapp  *
73062c5bc4SJason Schmidlapp  *      NOTES:
74062c5bc4SJason Schmidlapp  *      1)      Use with caution; most code is not safe for use
75062c5bc4SJason Schmidlapp  *              with asynchronous cancelability.
76062c5bc4SJason Schmidlapp  *
77062c5bc4SJason Schmidlapp  * COMPATIBILITY ADDITIONS
78062c5bc4SJason Schmidlapp  *      If 'oldtype' is NULL then the previous type is not returned
79062c5bc4SJason Schmidlapp  *      but the function still succeeds. (Solaris)
80062c5bc4SJason Schmidlapp  *
81062c5bc4SJason Schmidlapp  * RESULTS
82062c5bc4SJason Schmidlapp  *              0               successfully set cancelability type,
83062c5bc4SJason Schmidlapp  *              EINVAL          'type' is invalid
84062c5bc4SJason Schmidlapp  *              EPERM           Async cancellation is not supported.
85062c5bc4SJason Schmidlapp  *
86062c5bc4SJason Schmidlapp  * ------------------------------------------------------
87062c5bc4SJason Schmidlapp  */
88062c5bc4SJason Schmidlapp {
89062c5bc4SJason Schmidlapp   int result = 0;
90062c5bc4SJason Schmidlapp   pthread_t self = pthread_self ();
91*a12978ccSJeremy Soller   pte_thread_t * sp = (pte_thread_t *) self;
92062c5bc4SJason Schmidlapp 
93062c5bc4SJason Schmidlapp #ifndef PTE_SUPPORT_ASYNC_CANCEL
94062c5bc4SJason Schmidlapp   if (type == PTHREAD_CANCEL_ASYNCHRONOUS)
95062c5bc4SJason Schmidlapp     {
96062c5bc4SJason Schmidlapp       /* Async cancellation is not supported at this time.  See notes in
97062c5bc4SJason Schmidlapp        * pthread_cancel.
98062c5bc4SJason Schmidlapp        */
99062c5bc4SJason Schmidlapp       return EPERM;
100062c5bc4SJason Schmidlapp     }
101062c5bc4SJason Schmidlapp #endif /* PTE_SUPPORT_ASYNC_CANCEL */
102062c5bc4SJason Schmidlapp 
103062c5bc4SJason Schmidlapp   if (sp == NULL
104062c5bc4SJason Schmidlapp       || (type != PTHREAD_CANCEL_DEFERRED
105062c5bc4SJason Schmidlapp           && type != PTHREAD_CANCEL_ASYNCHRONOUS))
106062c5bc4SJason Schmidlapp     {
107062c5bc4SJason Schmidlapp       return EINVAL;
108062c5bc4SJason Schmidlapp     }
109062c5bc4SJason Schmidlapp 
110062c5bc4SJason Schmidlapp   /*
111062c5bc4SJason Schmidlapp    * Lock for async-cancel safety.
112062c5bc4SJason Schmidlapp    */
113062c5bc4SJason Schmidlapp   (void) pthread_mutex_lock (&sp->cancelLock);
114062c5bc4SJason Schmidlapp 
115062c5bc4SJason Schmidlapp   if (oldtype != NULL)
116062c5bc4SJason Schmidlapp     {
117062c5bc4SJason Schmidlapp       *oldtype = sp->cancelType;
118062c5bc4SJason Schmidlapp     }
119062c5bc4SJason Schmidlapp 
120062c5bc4SJason Schmidlapp   sp->cancelType = type;
121062c5bc4SJason Schmidlapp 
122062c5bc4SJason Schmidlapp   /*
123062c5bc4SJason Schmidlapp    * Check if there is a pending asynchronous cancel
124062c5bc4SJason Schmidlapp    */
125062c5bc4SJason Schmidlapp 
126062c5bc4SJason Schmidlapp   if (sp->cancelState == PTHREAD_CANCEL_ENABLE
127062c5bc4SJason Schmidlapp       && (type == PTHREAD_CANCEL_ASYNCHRONOUS)
128062c5bc4SJason Schmidlapp       && (pte_osThreadCheckCancel(sp->threadId) == PTE_OS_INTERRUPTED) )
129062c5bc4SJason Schmidlapp     {
130062c5bc4SJason Schmidlapp       sp->state = PThreadStateCanceling;
131062c5bc4SJason Schmidlapp       sp->cancelState = PTHREAD_CANCEL_DISABLE;
132062c5bc4SJason Schmidlapp       (void) pthread_mutex_unlock (&sp->cancelLock);
133062c5bc4SJason Schmidlapp       pte_throw (PTE_EPS_CANCEL);
134062c5bc4SJason Schmidlapp 
135062c5bc4SJason Schmidlapp       /* Never reached */
136062c5bc4SJason Schmidlapp     }
137062c5bc4SJason Schmidlapp 
138062c5bc4SJason Schmidlapp   (void) pthread_mutex_unlock (&sp->cancelLock);
139062c5bc4SJason Schmidlapp 
140062c5bc4SJason Schmidlapp   return (result);
141062c5bc4SJason Schmidlapp 
142062c5bc4SJason Schmidlapp }				/* pthread_setcanceltype */
143