kill_reset.cpp revision 12855:588919e0e4aa
1// Reset and kill a thread process, including nested kills
2
3#include <systemc>
4
5using namespace sc_core;
6using std::cout;
7using std::endl;
8
9struct M3: sc_module
10{
11  M3(sc_module_name _name)
12  {
13    SC_THREAD(ticker);
14      k = sc_get_current_process_handle();
15
16    SC_THREAD(calling);
17
18    SC_THREAD(target);
19      t = sc_get_current_process_handle();
20
21    killing_over = false;
22  }
23
24  sc_process_handle t, k;
25  sc_event ev;
26  int count;
27  bool killing_over;
28
29  void ticker()
30  {
31    for (;;)
32    {
33      try {
34        wait(10, SC_NS);
35        ev.notify();
36      }
37      catch (const sc_unwind_exception& ex) {
38        // ticker process killed by target
39        cout << "sc_unwind_exception caught by ticker" << endl;
40        sc_assert( !ex.is_reset() );
41        sc_assert( count == 1 );
42        sc_assert( !killing_over );
43        throw ex;
44      }
45    }
46  }
47
48  void calling()
49  {
50    wait(15, SC_NS);
51    // Target runs at time 10 NS due to notification
52    sc_assert( count == 1 );
53
54    wait(10, SC_NS);
55    // Target runs again at time 20 NS due to notification
56    sc_assert( count == 2 );
57
58    t.reset();
59    // Target reset immediately at time 25 NS
60    sc_assert( count == 0 );
61
62    wait(10, SC_NS);
63    // Target runs again at time 30 NS due to notification
64    sc_assert( count == 1 );
65
66    t.kill();
67    sc_assert( !killing_over );
68    killing_over = true;
69
70    // Target killed immediately at time 35 NS
71    sc_assert( t.terminated() ); // FAILS IN PRESENCE OF k.kill(); on line 96
72    sc_assert( k.terminated() );
73
74    sc_stop();
75  }
76
77  void target()
78  {
79    cout << "Target called/reset at " << sc_time_stamp() << endl;
80    count = 0;
81    for (;;)
82    {
83      try {
84        wait(ev);
85        cout << "Target awoke at " << sc_time_stamp() << endl;
86        ++count;
87      }
88      catch (const sc_unwind_exception& ex) {
89        cout << "sc_unwind_exception caught by target" << endl;
90        if (count == 2)
91          sc_assert( ex.is_reset() );
92        else if (count == 1)
93        {
94          sc_assert( !ex.is_reset() );
95          sc_assert( !killing_over );
96          k.kill();
97        }
98        else
99          sc_assert( false );
100        throw ex;
101      }
102    }
103  }
104
105  SC_HAS_PROCESS(M3);
106};
107
108int sc_main(int argc, char* argv[])
109{
110  M3 m("m");
111
112  sc_start();
113
114  return 0;
115}
116
117