Back to index

glibc  2.9
cleanup_defer.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 <stdlib.h>
00021 #include "pthreadP.h"
00022 
00023 
00024 void
00025 __cleanup_fct_attribute
00026 __pthread_register_cancel_defer (__pthread_unwind_buf_t *buf)
00027 {
00028   struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
00029   struct pthread *self = THREAD_SELF;
00030 
00031   /* Store old info.  */
00032   ibuf->priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
00033   ibuf->priv.data.cleanup = THREAD_GETMEM (self, cleanup);
00034 
00035   int cancelhandling = THREAD_GETMEM (self, cancelhandling);
00036 
00037   /* Disable asynchronous cancellation for now.  */
00038   if (__builtin_expect (cancelhandling & CANCELTYPE_BITMASK, 0))
00039     while (1)
00040       {
00041        int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
00042                                           cancelhandling
00043                                           & ~CANCELTYPE_BITMASK,
00044                                           cancelhandling);
00045        if (__builtin_expect (curval == cancelhandling, 1))
00046          /* Successfully replaced the value.  */
00047          break;
00048 
00049        /* Prepare for the next round.  */
00050        cancelhandling = curval;
00051       }
00052 
00053   ibuf->priv.data.canceltype = (cancelhandling & CANCELTYPE_BITMASK
00054                             ? PTHREAD_CANCEL_ASYNCHRONOUS
00055                             : PTHREAD_CANCEL_DEFERRED);
00056 
00057   /* Store the new cleanup handler info.  */
00058   THREAD_SETMEM (self, cleanup_jmp_buf, (struct pthread_unwind_buf *) buf);
00059 }
00060 
00061 
00062 void
00063 __cleanup_fct_attribute
00064 __pthread_unregister_cancel_restore (__pthread_unwind_buf_t *buf)
00065 {
00066   struct pthread *self = THREAD_SELF;
00067   struct pthread_unwind_buf *ibuf = (struct pthread_unwind_buf *) buf;
00068 
00069   THREAD_SETMEM (self, cleanup_jmp_buf, ibuf->priv.data.prev);
00070 
00071   int cancelhandling;
00072   if (ibuf->priv.data.canceltype != PTHREAD_CANCEL_DEFERRED
00073       && ((cancelhandling = THREAD_GETMEM (self, cancelhandling))
00074          & CANCELTYPE_BITMASK) == 0)
00075     {
00076       while (1)
00077        {
00078          int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
00079                                             cancelhandling
00080                                             | CANCELTYPE_BITMASK,
00081                                             cancelhandling);
00082          if (__builtin_expect (curval == cancelhandling, 1))
00083            /* Successfully replaced the value.  */
00084            break;
00085 
00086          /* Prepare for the next round.  */
00087          cancelhandling = curval;
00088        }
00089 
00090       CANCELLATION_P (self);
00091     }
00092 }