Back to index

glibc  2.9
jmp-unwind.c
Go to the documentation of this file.
00001 /* _longjmp_unwind -- Clean up stack frames unwound by longjmp.  Hurd version.
00002    Copyright (C) 1995,1996,2005,2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
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 <jmpbuf-unwind.h>
00021 #include <hurd/userlink.h>
00022 #include <hurd/signal.h>
00023 #include <hurd/sigpreempt.h>
00024 #include <assert.h>
00025 #include <stdint.h>
00026 
00027 
00028 #ifndef _JMPBUF_UNWINDS
00029 #error "<jmpbuf-unwind.h> fails to define _JMPBUF_UNWINDS"
00030 #endif
00031 
00032 static inline uintptr_t
00033 demangle_ptr (uintptr_t x)
00034 {
00035 # ifdef PTR_DEMANGLE
00036   PTR_DEMANGLE (x);
00037 # endif
00038   return x;
00039 }
00040 
00041 /* This function is called by `longjmp' (with its arguments) to restore
00042    active resources to a sane state before the frames code using them are
00043    jumped out of.  */
00044 
00045 void
00046 _longjmp_unwind (jmp_buf env, int val)
00047 {
00048   struct hurd_sigstate *ss = _hurd_self_sigstate ();
00049   struct hurd_userlink *link;
00050 
00051   /* All access to SS->active_resources must take place inside a critical
00052      section where signal handlers cannot run.  */
00053   __spin_lock (&ss->lock);
00054   assert (! __spin_lock_locked (&ss->critical_section_lock));
00055   __spin_lock (&ss->critical_section_lock);
00056 
00057   /* Remove local signal preemptors being unwound past.  */
00058   while (ss->preemptors &&
00059         _JMPBUF_UNWINDS (env[0].__jmpbuf, ss->preemptors, demangle_ptr))
00060     ss->preemptors = ss->preemptors->next;
00061 
00062   __spin_unlock (&ss->lock);
00063 
00064   /* Iterate over the current thread's list of active resources.
00065      Process the head portion of the list whose links reside
00066      in stack frames being unwound by this jump.  */
00067 
00068   for (link = ss->active_resources;
00069        link && _JMPBUF_UNWINDS (env[0].__jmpbuf, link, demangle_ptr);
00070        link = link->thread.next)
00071     /* Remove this link from the resource's users list,
00072        since the frame using the resource is being unwound.
00073        This call returns nonzero if that was the last user.  */
00074     if (_hurd_userlink_unlink (link))
00075       /* One of the frames being unwound by the longjmp was the last user
00076         of its resource.  Call the cleanup function to deallocate it.  */
00077       (*link->cleanup) (link->cleanup_data, env, val);
00078 
00079   _hurd_critical_section_unlock (ss);
00080 }