summaryrefslogtreecommitdiff
path: root/src/systemc/tests/systemc/kernel/process_control/test06/test06.cpp
blob: 616e3acf71f4499a3bba5d3e70abba80e3faaf71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//----------------------------------------------------------------------
//   Copyright 2009 Cadence Design Systems, Inc.
//   All Rights Reserved Worldwide
//   Copyright 2009 Forte Design Systems, Inc.
//   Copyright 2010 OFFIS Institute for Information technology
// 
// test06: test hierarchical kills
//----------------------------------------------------------------------

#define SC_INCLUDE_DYNAMIC_PROCESSES
#include <systemc.h>

SC_MODULE(top) {
public:
  SC_CTOR(top) {
    SC_THREAD(parent);
      sensitive << clk.pos();
      dont_initialize();
  }

  void proc_tree( unsigned depth, unsigned width, bool as_method, bool spawn_only )
  {
    unsigned w = width;
    if (sc_time_stamp() == SC_ZERO_TIME || spawn_only )
      while( depth && w --> 0 )
    {
      sc_spawn_options sp;
      sp.set_sensitivity( &clk.pos() );

      if(as_method) // we are spawned as method, spawn a thread
      {
        sc_spawn( sc_bind( &top::proc_tree, this, depth-1, width, !as_method, false )
                , sc_gen_unique_name("thread"), &sp );
      }
      else // we are spawned as thread, spawn a method
      {
        sp.spawn_method();
        sc_spawn( sc_bind( &top::proc_tree, this, depth-1, width, !as_method, false )
                , sc_gen_unique_name("method"), &sp );
      }
    }

    if(spawn_only) return;

    std::cout << sc_get_current_process_handle().name()
              << " triggered "
                << "(" << sc_time_stamp() << " @ " << sc_delta_count() << ")"
              << std::endl;

    // start thread
    if( !as_method ) thread_loop();
  }

  void thread_loop()
  {
    struct local_ {
       ~local_(){
          std::cout
            << sc_get_current_process_handle().name()
            << " local deleted "
            << "(" << sc_time_stamp() << " @ " << sc_delta_count() << ")"
            << std::endl;
       }
    } l; l=l;

    unsigned rounds = 5;
    while( rounds --> 0 )
    {
      wait();
      std::cout << sc_get_current_process_handle().name()
                << " triggered "
                << "(" << sc_time_stamp() << " @ " << sc_delta_count() << ")"
                << std::endl;
    }
    std::cout << sc_get_current_process_handle().name()
              << " ended "
              << "(" << sc_time_stamp() << " @ " << sc_delta_count() << ")"
              << std::endl;
  }

  void parent()
  {
    proc_tree( 3, 1, true , true );
    proc_tree( 3, 1, false, true );

    wait();

    // copy children (needed, since children may get reordered)
    std::vector< sc_object* > children =
      sc_get_current_process_handle().get_child_objects();

    std::vector< sc_object* >::const_iterator it = children.begin();

    while( it != children.end() )
    {
      sc_process_handle h( *it++ );
      sc_assert( h.valid() );

      std::cout << h.name() << " "
                << "kill requested "
                << "(" << h.get_process_object()->kind() << ") "
                << "(" << sc_time_stamp() << " @ " << sc_delta_count() << ")"
                << std::endl;

      h.kill( SC_INCLUDE_DESCENDANTS );
    }

    wait();

    std::cout << sc_get_current_process_handle().name()
              << " ended "
              << "(" << sc_time_stamp() << " @ " << sc_delta_count() << ")"
              << std::endl;

    wait();
    sc_stop();
    while(true) wait();
  }

  sc_in<bool> clk;
};

int sc_main (int argc, char *argv[])
{
  sc_clock clk("clk", 10, SC_NS, 0.5);
  top t("top");
  t.clk(clk);
  sc_start();
  return 0;
}