preserveException.cpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "incls/_precompiled.incl"
26#include "incls/_preserveException.cpp.incl"
27
28// TODO: These three classes should be refactored
29
30PreserveExceptionMark::PreserveExceptionMark(Thread*& thread) {
31  thread     = Thread::current();
32  _thread    = thread;
33  _preserved_exception_oop = Handle(thread, _thread->pending_exception());
34  _thread->clear_pending_exception(); // Needed to avoid infinite recursion
35  _preserved_exception_line = _thread->exception_line();
36  _preserved_exception_file = _thread->exception_file();
37}
38
39
40PreserveExceptionMark::~PreserveExceptionMark() {
41  if (_thread->has_pending_exception()) {
42    oop exception = _thread->pending_exception();
43    _thread->clear_pending_exception(); // Needed to avoid infinite recursion
44    exception->print();
45    fatal("PreserveExceptionMark destructor expects no pending exceptions");
46  }
47  if (_preserved_exception_oop() != NULL) {
48    _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
49  }
50}
51
52
53// This code is cloned from PreserveExceptionMark, except that:
54//   returned pending exceptions do not cause a crash.
55//   thread is passed in, not set (not a reference parameter)
56//   and bug 6431341 has been addressed.
57
58CautiouslyPreserveExceptionMark::CautiouslyPreserveExceptionMark(Thread* thread) {
59  _thread    = thread;
60  _preserved_exception_oop = Handle(thread, _thread->pending_exception());
61  _preserved_exception_line = _thread->exception_line();
62  _preserved_exception_file = _thread->exception_file();
63  _thread->clear_pending_exception(); // Pending exceptions are checked in the destructor
64}
65
66
67CautiouslyPreserveExceptionMark::~CautiouslyPreserveExceptionMark() {
68  assert(!_thread->has_pending_exception(), "unexpected exception generated");
69  if (_thread->has_pending_exception()) {
70    _thread->clear_pending_exception();
71  }
72  if (_preserved_exception_oop() != NULL) {
73    _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
74  }
75}
76
77
78void WeakPreserveExceptionMark::preserve() {
79  _preserved_exception_oop = Handle(_thread, _thread->pending_exception());
80  _preserved_exception_line = _thread->exception_line();
81  _preserved_exception_file = _thread->exception_file();
82  _thread->clear_pending_exception();
83}
84
85void WeakPreserveExceptionMark::restore() {
86  if (!_thread->has_pending_exception()) {
87    _thread->set_pending_exception(_preserved_exception_oop(), _preserved_exception_file, _preserved_exception_line);
88  }
89}
90