Back to index

glibc  2.9
cleanup_defer_compat.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 "pthreadP.h"
00021 
00022 
00023 void
00024 _pthread_cleanup_push_defer (buffer, routine, arg)
00025      struct _pthread_cleanup_buffer *buffer;
00026      void (*routine) (void *);
00027      void *arg;
00028 {
00029   struct pthread *self = THREAD_SELF;
00030 
00031   buffer->__routine = routine;
00032   buffer->__arg = arg;
00033   buffer->__prev = 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   buffer->__canceltype = (cancelhandling & CANCELTYPE_BITMASK
00054                        ? PTHREAD_CANCEL_ASYNCHRONOUS
00055                        : PTHREAD_CANCEL_DEFERRED);
00056 
00057   THREAD_SETMEM (self, cleanup, buffer);
00058 }
00059 strong_alias (_pthread_cleanup_push_defer, __pthread_cleanup_push_defer)
00060 
00061 
00062 void
00063 _pthread_cleanup_pop_restore (buffer, execute)
00064      struct _pthread_cleanup_buffer *buffer;
00065      int execute;
00066 {
00067   struct pthread *self = THREAD_SELF;
00068 
00069   THREAD_SETMEM (self, cleanup, buffer->__prev);
00070 
00071   int cancelhandling;
00072   if (__builtin_expect (buffer->__canceltype != PTHREAD_CANCEL_DEFERRED, 0)
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 
00093   /* If necessary call the cleanup routine after we removed the
00094      current cleanup block from the list.  */
00095   if (execute)
00096     buffer->__routine (buffer->__arg);
00097 }
00098 strong_alias (_pthread_cleanup_pop_restore, __pthread_cleanup_pop_restore)