sc_method_reset_throw.cpp revision 12855:588919e0e4aa
1
2/*****************************************************************************
3
4  The following code is derived, directly or indirectly, from the SystemC
5  source code Copyright (c) 1996-2014 by all Contributors.
6  All Rights reserved.
7
8  The contents of this file are subject to the restrictions and limitations
9  set forth in the SystemC Open Source License (the "License");
10  You may not use this file except in compliance with such restrictions and
11  limitations. You may obtain instructions on how to receive a copy of the
12  License at http://www.accellera.org/. Software distributed by Contributors
13  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
14  ANY KIND, either express or implied. See the License for the specific
15  language governing rights and limitations under the License.
16
17 *****************************************************************************/
18
19/*****************************************************************************
20
21  sc_method_reset_throw.cpp --
22
23  Original Author: Bishnupriya Bhattacharya, Cadence Design Systems, 2012-08-07
24
25 *****************************************************************************/
26
27#define SC_INCLUDE_DYNAMIC_PROCESSES
28#include <systemc.h>
29
30class my_exception
31{
32public:
33  explicit my_exception(const char* s) : s_(s) { }
34  const char* message() const { return s_.c_str(); }
35protected:
36  std::string s_;
37};
38
39SC_MODULE(sctop)
40{
41public:
42   SC_CTOR(sctop)
43   {
44        SC_THREAD(run);
45        SC_METHOD(m1); dont_initialize();
46        method_handle = sc_get_current_process_handle();
47        SC_THREAD(throwee1);
48        throwee1_h = sc_get_current_process_handle();
49   }
50
51   void run() {
52      wait (5, SC_NS);
53      cout <<  sc_time_stamp() << ": reset method m1" << endl;
54      method_handle.reset();
55      cout <<  sc_time_stamp() << ": after reset of method m1" << endl;
56   }
57
58   void m1()
59   {
60      cout << sc_time_stamp() << ": in m1" << endl;
61      cout << sc_time_stamp() << ": in m1() "
62           << "throwing exception in throwee1" << endl;
63
64      throwee1_h.throw_it(
65         my_exception("thrown in throwee1 from m1()")
66      );
67
68      cout << sc_time_stamp() << ": in m1() "
69           << "after throwing exception in throwee1" << endl;
70  }
71
72  void throwee1()
73  {
74    // catch exception and exit
75    while (1) {
76       try {
77         wait(50, SC_NS);
78         cerr << sc_time_stamp() << ": in throwee1, normal flow" << endl;
79       }
80       catch (my_exception const & x) {
81         cerr << sc_time_stamp() << ": in throwee1, caught exception "
82              << endl;
83         return;
84       }
85    }
86  }
87
88protected:
89  sc_process_handle method_handle;
90  sc_process_handle throwee1_h;
91};
92
93
94int sc_main (int, char*[])
95{
96  sc_report_handler::set_actions( SC_ID_DISABLE_WILL_ORPHAN_PROCESS_, SC_DO_NOTHING );
97  sctop top1("Top1");
98  sc_start(10, SC_NS);
99  return 0;
100}
101