Back to index

glibc  2.9
cancellation.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <setjmp.h>
00021 #include <stdlib.h>
00022 #include "pthreadP.h"
00023 
00024 
00025 /* The next two functions are similar to pthread_setcanceltype() but
00026    more specialized for the use in the cancelable functions like write().
00027    They do not need to check parameters etc.  */
00028 int
00029 attribute_hidden
00030 __pthread_enable_asynccancel (void)
00031 {
00032   struct pthread *self = THREAD_SELF;
00033   int oldval = THREAD_GETMEM (self, cancelhandling);
00034 
00035   while (1)
00036     {
00037       int newval = oldval | CANCELTYPE_BITMASK;
00038 
00039       if (newval == oldval)
00040        break;
00041 
00042       int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
00043                                          oldval);
00044       if (__builtin_expect (curval == oldval, 1))
00045        {
00046          if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval))
00047            {
00048              THREAD_SETMEM (self, result, PTHREAD_CANCELED);
00049              __do_cancel ();
00050            }
00051 
00052          break;
00053        }
00054 
00055       /* Prepare the next round.  */
00056       oldval = curval;
00057     }
00058 
00059   return oldval;
00060 }
00061 
00062 
00063 void
00064 internal_function attribute_hidden
00065 __pthread_disable_asynccancel (int oldtype)
00066 {
00067   /* If asynchronous cancellation was enabled before we do not have
00068      anything to do.  */
00069   if (oldtype & CANCELTYPE_BITMASK)
00070     return;
00071 
00072   struct pthread *self = THREAD_SELF;
00073   int oldval = THREAD_GETMEM (self, cancelhandling);
00074 
00075   while (1)
00076     {
00077       int newval = oldval & ~CANCELTYPE_BITMASK;
00078 
00079       if (newval == oldval)
00080        break;
00081 
00082       int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
00083                                          oldval);
00084       if (__builtin_expect (curval == oldval, 1))
00085        break;
00086 
00087       /* Prepare the next round.  */
00088       oldval = curval;
00089     }
00090 }