112855Sgabeblack@google.com/*****************************************************************************
212855Sgabeblack@google.com
312855Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412855Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512855Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612855Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712855Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812855Sgabeblack@google.com  License.  You may obtain a copy of the License at
912855Sgabeblack@google.com
1012855Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112855Sgabeblack@google.com
1212855Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312855Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412855Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512855Sgabeblack@google.com  implied.  See the License for the specific language governing
1612855Sgabeblack@google.com  permissions and limitations under the License.
1712855Sgabeblack@google.com
1812855Sgabeblack@google.com *****************************************************************************/
1912855Sgabeblack@google.com
2012855Sgabeblack@google.com// kill_reset.cpp -- test for
2112855Sgabeblack@google.com//
2212855Sgabeblack@google.com//  Original Author: John Aynsley, Doulos, Inc.
2312855Sgabeblack@google.com//
2412855Sgabeblack@google.com// MODIFICATION LOG - modifiers, enter your name, affiliation, date and
2512855Sgabeblack@google.com//
2612855Sgabeblack@google.com// $Log: kill_reset.cpp,v $
2712855Sgabeblack@google.com// Revision 1.2  2011/05/08 19:18:46  acg
2812855Sgabeblack@google.com//  Andy Goodrich: remove extraneous + prefixes from git diff.
2912855Sgabeblack@google.com//
3012855Sgabeblack@google.com
3112855Sgabeblack@google.com// Reset and kill a thread process, including nested kills and resets
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com#include <systemc>
3412855Sgabeblack@google.com
3512855Sgabeblack@google.comusing namespace sc_core;
3612855Sgabeblack@google.comusing std::cout;
3712855Sgabeblack@google.comusing std::endl;
3812855Sgabeblack@google.com
3912855Sgabeblack@google.comstruct M3: sc_module
4012855Sgabeblack@google.com{
4112855Sgabeblack@google.com  M3(sc_module_name _name)
4212855Sgabeblack@google.com  {
4312855Sgabeblack@google.com    SC_THREAD(ticker);
4412855Sgabeblack@google.com      k = sc_get_current_process_handle();
4512855Sgabeblack@google.com
4612855Sgabeblack@google.com    SC_THREAD(calling);
4712855Sgabeblack@google.com
4812855Sgabeblack@google.com    SC_THREAD(target);
4912855Sgabeblack@google.com      t = sc_get_current_process_handle();
5012855Sgabeblack@google.com
5112855Sgabeblack@google.com    SC_THREAD(victim);
5212855Sgabeblack@google.com      v = sc_get_current_process_handle();
5312855Sgabeblack@google.com
5412855Sgabeblack@google.com    SC_THREAD(bystander);
5512855Sgabeblack@google.com      b = sc_get_current_process_handle();
5612855Sgabeblack@google.com
5712855Sgabeblack@google.com    SC_THREAD(second_bystander);
5812855Sgabeblack@google.com      b2 = sc_get_current_process_handle();
5912855Sgabeblack@google.com
6012855Sgabeblack@google.com    SC_THREAD(third_bystander);
6112855Sgabeblack@google.com      b3 = sc_get_current_process_handle();
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com    killing_over = false;
6412855Sgabeblack@google.com    third_bystander_knocked_over = false;
6512855Sgabeblack@google.com  }
6612855Sgabeblack@google.com
6712855Sgabeblack@google.com  sc_process_handle t, k, v, b, b2, b3;
6812855Sgabeblack@google.com  sc_event ev;
6912855Sgabeblack@google.com  int count;
7012855Sgabeblack@google.com  bool killing_over;
7112855Sgabeblack@google.com  bool third_bystander_knocked_over;
7212855Sgabeblack@google.com
7312855Sgabeblack@google.com  void ticker()
7412855Sgabeblack@google.com  {
7512855Sgabeblack@google.com    for (;;)
7612855Sgabeblack@google.com    {
7712855Sgabeblack@google.com      try {
7812855Sgabeblack@google.com        wait(10, SC_NS);
7912855Sgabeblack@google.com        ev.notify();
8012855Sgabeblack@google.com        sc_assert( !sc_is_unwinding() );
8112855Sgabeblack@google.com      }
8212855Sgabeblack@google.com      catch (const sc_unwind_exception& ex)
8312855Sgabeblack@google.com      {
8412855Sgabeblack@google.com        // ticker process killed by target
8512855Sgabeblack@google.com        cout << "sc_unwind_exception caught by ticker" << endl;
8612855Sgabeblack@google.com        sc_assert( !ex.is_reset() );
8712855Sgabeblack@google.com        sc_assert( count == 1 );
8812855Sgabeblack@google.com        sc_assert( !killing_over );
8912855Sgabeblack@google.com        sc_assert( t.is_unwinding() );
9012855Sgabeblack@google.com        sc_assert( sc_is_unwinding() );
9112855Sgabeblack@google.com
9212855Sgabeblack@google.com        v.kill();
9312855Sgabeblack@google.com        throw ex;
9412855Sgabeblack@google.com      }
9512855Sgabeblack@google.com    }
9612855Sgabeblack@google.com  }
9712855Sgabeblack@google.com
9812855Sgabeblack@google.com  void calling()
9912855Sgabeblack@google.com  {
10012855Sgabeblack@google.com    wait(15, SC_NS);
10112855Sgabeblack@google.com    // Target runs at time 10 NS due to notification
10212855Sgabeblack@google.com    sc_assert( count == 1 );
10312855Sgabeblack@google.com    // The victim awakes every 1ns
10412855Sgabeblack@google.com    sc_assert( sc_time_to_pending_activity() <= sc_time(1, SC_NS) );
10512855Sgabeblack@google.com
10612855Sgabeblack@google.com    wait(10, SC_NS);
10712855Sgabeblack@google.com    // Target runs again at time 20 NS due to notification
10812855Sgabeblack@google.com    sc_assert( count == 2 );
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com    t.reset();
11112855Sgabeblack@google.com    // Target reset immediately at time 25 NS
11212855Sgabeblack@google.com    sc_assert( count == 0 );
11312855Sgabeblack@google.com
11412855Sgabeblack@google.com    wait(10, SC_NS);
11512855Sgabeblack@google.com    // Target runs again at time 30 NS due to notification
11612855Sgabeblack@google.com    sc_assert( count == 1 );
11712855Sgabeblack@google.com
11812855Sgabeblack@google.com    t.kill();
11912855Sgabeblack@google.com    sc_assert( !killing_over );
12012855Sgabeblack@google.com    killing_over = true;
12112855Sgabeblack@google.com
12212855Sgabeblack@google.com    // Target killed immediately at time 35 NS
12312855Sgabeblack@google.com    if (t.valid())
12412855Sgabeblack@google.com      sc_assert( t.terminated() );
12512855Sgabeblack@google.com    if (k.valid())
12612855Sgabeblack@google.com      sc_assert( k.terminated() );
12712855Sgabeblack@google.com    if (v.valid())
12812855Sgabeblack@google.com      sc_assert( v.terminated() );
12912855Sgabeblack@google.com    sc_assert( b.valid() && !b.terminated() );
13012855Sgabeblack@google.com    sc_assert( b2.valid() && !b2.terminated() );
13112855Sgabeblack@google.com    if (b3.valid())
13212855Sgabeblack@google.com      sc_assert( b3.terminated() );
13312855Sgabeblack@google.com
13412855Sgabeblack@google.com    sc_stop();
13512855Sgabeblack@google.com  }
13612855Sgabeblack@google.com
13712855Sgabeblack@google.com  void target()
13812855Sgabeblack@google.com  {
13912855Sgabeblack@google.com    cout << "Target called/reset at " << sc_time_stamp() << endl;
14012855Sgabeblack@google.com    count = 0;
14112855Sgabeblack@google.com    for (;;)
14212855Sgabeblack@google.com    {
14312855Sgabeblack@google.com      try {
14412855Sgabeblack@google.com        wait(ev);
14512855Sgabeblack@google.com        cout << "Target awoke at " << sc_time_stamp() << endl;
14612855Sgabeblack@google.com        ++count;
14712855Sgabeblack@google.com      }
14812855Sgabeblack@google.com      catch (const sc_unwind_exception& ex)
14912855Sgabeblack@google.com      {
15012855Sgabeblack@google.com        cout << "sc_unwind_exception caught by target" << endl;
15112855Sgabeblack@google.com        if (count == 2)
15212855Sgabeblack@google.com          sc_assert( ex.is_reset() );
15312855Sgabeblack@google.com        else if (count == 1)
15412855Sgabeblack@google.com        {
15512855Sgabeblack@google.com          sc_assert( !ex.is_reset() );
15612855Sgabeblack@google.com          sc_assert( !killing_over );
15712855Sgabeblack@google.com          k.kill();
15812855Sgabeblack@google.com        }
15912855Sgabeblack@google.com        else
16012855Sgabeblack@google.com          sc_assert( false );
16112855Sgabeblack@google.com        throw ex;
16212855Sgabeblack@google.com      }
16312855Sgabeblack@google.com    }
16412855Sgabeblack@google.com  }
16512855Sgabeblack@google.com
16612855Sgabeblack@google.com  void victim()
16712855Sgabeblack@google.com  {
16812855Sgabeblack@google.com    try {
16912855Sgabeblack@google.com      while (true)
17012855Sgabeblack@google.com      {
17112855Sgabeblack@google.com        wait(1, SC_NS);
17212855Sgabeblack@google.com        sc_assert( !sc_is_unwinding() );
17312855Sgabeblack@google.com      }
17412855Sgabeblack@google.com    }
17512855Sgabeblack@google.com    catch (const sc_unwind_exception& ex)
17612855Sgabeblack@google.com    {
17712855Sgabeblack@google.com      cout << "sc_unwind_exception caught by victim" << endl;
17812855Sgabeblack@google.com      sc_assert( sc_time_stamp() == sc_time(35, SC_NS) );
17912855Sgabeblack@google.com      sc_assert( ex.is_reset() == false );
18012855Sgabeblack@google.com      sc_assert( !killing_over );
18112855Sgabeblack@google.com      sc_assert( v.is_unwinding() );
18212855Sgabeblack@google.com      sc_assert( sc_is_unwinding() );
18312855Sgabeblack@google.com
18412855Sgabeblack@google.com      b.reset();
18512855Sgabeblack@google.com      throw ex;
18612855Sgabeblack@google.com    }
18712855Sgabeblack@google.com  }
18812855Sgabeblack@google.com
18912855Sgabeblack@google.com  void bystander() // Gets reset by victim
19012855Sgabeblack@google.com  {
19112855Sgabeblack@google.com    for (;;)
19212855Sgabeblack@google.com    {
19312855Sgabeblack@google.com      try {
19412855Sgabeblack@google.com        wait(ev);
19512855Sgabeblack@google.com      }
19612855Sgabeblack@google.com      catch (const sc_unwind_exception& ex) {
19712855Sgabeblack@google.com        cout << "sc_unwind_exception caught by bystander" << endl;
19812855Sgabeblack@google.com        sc_assert( sc_time_stamp() == sc_time(35, SC_NS) );
19912855Sgabeblack@google.com        sc_assert( ex.is_reset() == true );
20012855Sgabeblack@google.com        sc_assert( !killing_over );
20112855Sgabeblack@google.com        sc_assert( v.is_unwinding() ); // sic
20212855Sgabeblack@google.com        sc_assert( sc_is_unwinding() );
20312855Sgabeblack@google.com
20412855Sgabeblack@google.com        b2.reset();
20512855Sgabeblack@google.com        throw ex;
20612855Sgabeblack@google.com      }
20712855Sgabeblack@google.com    }
20812855Sgabeblack@google.com  }
20912855Sgabeblack@google.com
21012855Sgabeblack@google.com  void second_bystander() // Gets reset by bystander
21112855Sgabeblack@google.com  {
21212855Sgabeblack@google.com    for (;;)
21312855Sgabeblack@google.com    {
21412855Sgabeblack@google.com      try {
21512855Sgabeblack@google.com        wait(ev);
21612855Sgabeblack@google.com      }
21712855Sgabeblack@google.com      catch (const sc_unwind_exception& ex) {
21812855Sgabeblack@google.com        cout << "sc_unwind_exception caught by second_bystander" << endl;
21912855Sgabeblack@google.com        sc_assert( sc_time_stamp() == sc_time(35, SC_NS) );
22012855Sgabeblack@google.com        sc_assert( ex.is_reset() == true );
22112855Sgabeblack@google.com        sc_assert( !killing_over );
22212855Sgabeblack@google.com        sc_assert( v.is_unwinding() ); // sic
22312855Sgabeblack@google.com        sc_assert( b.is_unwinding() ); // sic
22412855Sgabeblack@google.com        sc_assert( sc_is_unwinding() );
22512855Sgabeblack@google.com
22612855Sgabeblack@google.com        b3.kill();
22712855Sgabeblack@google.com        throw ex;
22812855Sgabeblack@google.com      }
22912855Sgabeblack@google.com    }
23012855Sgabeblack@google.com  }
23112855Sgabeblack@google.com
23212855Sgabeblack@google.com  void third_bystander() // Gets killed by second bystander
23312855Sgabeblack@google.com  {
23412855Sgabeblack@google.com    for (;;)
23512855Sgabeblack@google.com    {
23612855Sgabeblack@google.com      try {
23712855Sgabeblack@google.com        wait(ev);
23812855Sgabeblack@google.com      }
23912855Sgabeblack@google.com      catch (const sc_unwind_exception& ex) {
24012855Sgabeblack@google.com        cout << "sc_unwind_exception caught by third_bystander" << endl;
24112855Sgabeblack@google.com        sc_assert( sc_time_stamp() == sc_time(35, SC_NS) );
24212855Sgabeblack@google.com        sc_assert( !ex.is_reset() == true );
24312855Sgabeblack@google.com        sc_assert( !killing_over );
24412855Sgabeblack@google.com        sc_assert( v.is_unwinding() ); // sic
24512855Sgabeblack@google.com        sc_assert( b.is_unwinding() ); // sic
24612855Sgabeblack@google.com        sc_assert( b2.is_unwinding() ); // sic
24712855Sgabeblack@google.com        sc_assert( sc_is_unwinding() );
24812855Sgabeblack@google.com
24912855Sgabeblack@google.com        third_bystander_knocked_over = true;
25012855Sgabeblack@google.com        throw ex;
25112855Sgabeblack@google.com      }
25212855Sgabeblack@google.com    }
25312855Sgabeblack@google.com  }
25412855Sgabeblack@google.com
25512855Sgabeblack@google.com  SC_HAS_PROCESS(M3);
25612855Sgabeblack@google.com};
25712855Sgabeblack@google.com
25812855Sgabeblack@google.comint sc_main(int argc, char* argv[])
25912855Sgabeblack@google.com{
26012855Sgabeblack@google.com  M3 m("m");
26112855Sgabeblack@google.com  sc_assert( sc_pending_activity() == false );
26212855Sgabeblack@google.com  sc_assert( sc_time_to_pending_activity() == sc_max_time() );
26312855Sgabeblack@google.com
26412855Sgabeblack@google.com  sc_start();
26512855Sgabeblack@google.com  sc_assert( m.third_bystander_knocked_over );
26612855Sgabeblack@google.com
26712855Sgabeblack@google.com  cout << endl << "Success" << endl;
26812855Sgabeblack@google.com  return 0;
26912855Sgabeblack@google.com}
27012855Sgabeblack@google.com
271