Back to index

glibc  2.9
unwind-forcedunwind.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2005, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>.
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 License as
00007    published by the Free Software Foundation; either version 2.1 of the
00008    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; see the file COPYING.LIB.  If not,
00017    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.  */
00019 
00020 #include <dlfcn.h>
00021 #include <stdio.h>
00022 #include <unwind.h>
00023 #include <pthreadP.h>
00024 
00025 static void (*libgcc_s_resume) (struct _Unwind_Exception *exc);
00026 static _Unwind_Reason_Code (*libgcc_s_personality)
00027   (int, _Unwind_Action, _Unwind_Exception_Class, struct _Unwind_Exception *,
00028    struct _Unwind_Context *);
00029 static _Unwind_Reason_Code (*libgcc_s_forcedunwind)
00030   (struct _Unwind_Exception *, _Unwind_Stop_Fn, void *);
00031 static _Unwind_Word (*libgcc_s_getcfa) (struct _Unwind_Context *);
00032 
00033 void
00034 __attribute_noinline__
00035 pthread_cancel_init (void)
00036 {
00037   void *resume, *personality, *forcedunwind, *getcfa;
00038   void *handle;
00039 
00040   if (__builtin_expect (libgcc_s_getcfa != NULL, 1))
00041     {
00042       /* Force gcc to reload all values.  */
00043       asm volatile ("" ::: "memory");
00044       return;
00045     }
00046 
00047   handle = __libc_dlopen ("libgcc_s.so.1");
00048 
00049   if (handle == NULL
00050       || (resume = __libc_dlsym (handle, "_Unwind_Resume")) == NULL
00051       || (personality = __libc_dlsym (handle, "__gcc_personality_v0")) == NULL
00052       || (forcedunwind = __libc_dlsym (handle, "_Unwind_ForcedUnwind"))
00053         == NULL
00054       || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA")) == NULL
00055 #ifdef ARCH_CANCEL_INIT
00056       || ARCH_CANCEL_INIT (handle)
00057 #endif
00058       )
00059     __libc_fatal ("libgcc_s.so.1 must be installed for pthread_cancel to work\n");
00060 
00061   libgcc_s_resume = resume;
00062   libgcc_s_personality = personality;
00063   libgcc_s_forcedunwind = forcedunwind;
00064   /* Make sure libgcc_s_getcfa is written last.  Otherwise,
00065      pthread_cancel_init might return early even when the pointer the
00066      caller is interested in is not initialized yet.  */
00067   atomic_write_barrier ();
00068   libgcc_s_getcfa = getcfa;
00069 }
00070 
00071 void
00072 _Unwind_Resume (struct _Unwind_Exception *exc)
00073 {
00074   if (__builtin_expect (libgcc_s_resume == NULL, 0))
00075     pthread_cancel_init ();
00076 
00077   libgcc_s_resume (exc);
00078 }
00079 
00080 _Unwind_Reason_Code
00081 __gcc_personality_v0 (int version, _Unwind_Action actions,
00082                     _Unwind_Exception_Class exception_class,
00083                       struct _Unwind_Exception *ue_header,
00084                       struct _Unwind_Context *context)
00085 {
00086   if (__builtin_expect (libgcc_s_personality == NULL, 0))
00087     pthread_cancel_init ();
00088 
00089   return libgcc_s_personality (version, actions, exception_class,
00090                             ue_header, context);
00091 }
00092 
00093 _Unwind_Reason_Code
00094 _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
00095                     void *stop_argument)
00096 {
00097   if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
00098     pthread_cancel_init ();
00099 
00100   return libgcc_s_forcedunwind (exc, stop, stop_argument);
00101 }
00102 
00103 _Unwind_Word
00104 _Unwind_GetCFA (struct _Unwind_Context *context)
00105 {
00106   if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
00107     pthread_cancel_init ();
00108 
00109   return libgcc_s_getcfa (context);
00110 }