Back to index

glibc  2.9
unwind-forcedunwind.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003 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 static void (*libgcc_s_sjlj_register) (struct SjLj_Function_Context *);
00033 static void (*libgcc_s_sjlj_unregister) (struct SjLj_Function_Context *);
00034 
00035 void
00036 __attribute_noinline__
00037 pthread_cancel_init (void)
00038 {
00039   void *resume, *personality, *forcedunwind, *getcfa;
00040   void *handle;
00041   void *sjlj_register, *sjlj_unregister;
00042 
00043   if (__builtin_expect (libgcc_s_getcfa != NULL, 1))
00044     {
00045       /* Force gcc to reload all values.  */
00046       asm volatile ("" ::: "memory");
00047       return;
00048     }
00049 
00050   handle = __libc_dlopen ("libgcc_s.so.1");
00051 
00052   if (handle == NULL
00053       || (sjlj_register = __libc_dlsym (handle, "_Unwind_SjLj_Register")) == NULL
00054       || (sjlj_unregister = __libc_dlsym (handle, "_Unwind_SjLj_Unregister")) == NULL
00055       || (resume = __libc_dlsym (handle, "_Unwind_SjLj_Resume")) == NULL
00056       || (personality = __libc_dlsym (handle, "__gcc_personality_sj0")) == NULL
00057       || (forcedunwind = __libc_dlsym (handle, "_Unwind_SjLj_ForcedUnwind"))
00058         == NULL
00059       || (getcfa = __libc_dlsym (handle, "_Unwind_GetCFA")) == NULL
00060       )
00061     __libc_fatal ("libgcc_s.so.1 must be installed for pthread_cancel to work\n");
00062 
00063   libgcc_s_resume = resume;
00064   libgcc_s_personality = personality;
00065   libgcc_s_forcedunwind = forcedunwind;
00066   libgcc_s_sjlj_register = sjlj_register;
00067   libgcc_s_sjlj_unregister = sjlj_unregister;
00068   /* Make sure libgcc_s_getcfa is written last.  Otherwise,
00069      pthread_cancel_init might return early even when the pointer the
00070      caller is interested in is not initialized yet.  */
00071   atomic_write_barrier ();
00072   libgcc_s_getcfa = getcfa;
00073 }
00074 
00075 void
00076 _Unwind_Resume (struct _Unwind_Exception *exc)
00077 {
00078   if (__builtin_expect (libgcc_s_resume == NULL, 0))
00079     pthread_cancel_init ();
00080 
00081   libgcc_s_resume (exc);
00082 }
00083 
00084 _Unwind_Reason_Code
00085 __gcc_personality_v0 (int version, _Unwind_Action actions,
00086                     _Unwind_Exception_Class exception_class,
00087                       struct _Unwind_Exception *ue_header,
00088                       struct _Unwind_Context *context)
00089 {
00090   if (__builtin_expect (libgcc_s_personality == NULL, 0))
00091     pthread_cancel_init ();
00092 
00093   return libgcc_s_personality (version, actions, exception_class,
00094                             ue_header, context);
00095 }
00096 
00097 _Unwind_Reason_Code
00098 _Unwind_ForcedUnwind (struct _Unwind_Exception *exc, _Unwind_Stop_Fn stop,
00099                     void *stop_argument)
00100 {
00101   if (__builtin_expect (libgcc_s_forcedunwind == NULL, 0))
00102     pthread_cancel_init ();
00103 
00104   return libgcc_s_forcedunwind (exc, stop, stop_argument);
00105 }
00106 
00107 _Unwind_Word
00108 _Unwind_GetCFA (struct _Unwind_Context *context)
00109 {
00110   if (__builtin_expect (libgcc_s_getcfa == NULL, 0))
00111     pthread_cancel_init ();
00112 
00113   return libgcc_s_getcfa (context);
00114 }
00115 
00116 void
00117 _Unwind_SjLj_Register (struct SjLj_Function_Context *fc)
00118 {
00119   if (__builtin_expect (libgcc_s_sjlj_register == NULL, 0))
00120     pthread_cancel_init ();
00121 
00122   libgcc_s_sjlj_register (fc);
00123 }
00124 
00125 void
00126 _Unwind_SjLj_Unregister (struct SjLj_Function_Context *fc)
00127 {
00128   if (__builtin_expect (libgcc_s_sjlj_unregister == NULL, 0))
00129     pthread_cancel_init ();
00130 
00131   libgcc_s_sjlj_unregister (fc);
00132 }