sc_spawn.cc revision 13288:f1c04129f709
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "base/logging.hh"
31#include "systemc/core/process.hh"
32#include "systemc/core/process_types.hh"
33#include "systemc/core/scheduler.hh"
34#include "systemc/ext/channel/sc_in.hh"
35#include "systemc/ext/channel/sc_inout.hh"
36#include "systemc/ext/channel/sc_out.hh"
37#include "systemc/ext/channel/sc_signal_in_if.hh"
38#include "systemc/ext/core/sc_main.hh"
39#include "systemc/ext/core/sc_module.hh"
40#include "systemc/ext/core/sc_spawn.hh"
41
42namespace sc_gem5
43{
44
45Process *
46spawnWork(ProcessFuncWrapper *func, const char *name,
47          const ::sc_core::sc_spawn_options *opts)
48{
49    bool method = false;
50    bool dontInitialize = false;
51    if (opts) {
52        if (opts->_spawnMethod)
53            method = true;
54        if (opts->_dontInitialize)
55            dontInitialize = true;
56        if (opts->_stackSize != -1)
57            warn("Ignoring request to set stack size.\n");
58    }
59
60    if (!name || name[0] == '\0') {
61        if (method)
62            name = ::sc_core::sc_gen_unique_name("method_p");
63        else
64            name = ::sc_core::sc_gen_unique_name("thread_p");
65    }
66
67    Process *proc;
68    if (method)
69        proc = new Method(name, func);
70    else
71        proc = new Thread(name, func);
72
73    proc->dontInitialize(dontInitialize);
74
75    if (opts) {
76        for (auto e: opts->_events)
77            newStaticSensitivityEvent(proc, e);
78
79        for (auto p: opts->_ports)
80            newStaticSensitivityPort(proc, p);
81
82        for (auto e: opts->_exports)
83            newStaticSensitivityExport(proc, e);
84
85        for (auto i: opts->_interfaces)
86            newStaticSensitivityInterface(proc, i);
87
88        for (auto f: opts->_finders)
89            newStaticSensitivityFinder(proc, f);
90
91        for (auto p: opts->_in_resets)
92            newReset(p.target, proc, p.sync, p.value);
93
94        for (auto p: opts->_inout_resets)
95            newReset(p.target, proc, p.sync, p.value);
96
97        for (auto p: opts->_out_resets)
98            newReset(p.target, proc, p.sync, p.value);
99
100        for (auto i: opts->_if_resets)
101            newReset(i.target, proc, i.sync, i.value);
102    }
103
104    if (opts && opts->_dontInitialize &&
105            opts->_events.empty() && opts->_ports.empty() &&
106            opts->_exports.empty() && opts->_interfaces.empty() &&
107            opts->_finders.empty()) {
108        SC_REPORT_WARNING(
109                "(W558) disable() or dont_initialize() called on process "
110                "with no static sensitivity, it will be orphaned",
111                proc->name());
112    }
113
114    scheduler.reg(proc);
115
116    return proc;
117}
118
119} // namespace sc_gem5
120
121namespace sc_core
122{
123
124sc_spawn_options::sc_spawn_options() :
125    _spawnMethod(false), _dontInitialize(false), _stackSize(-1)
126{}
127
128
129void
130sc_spawn_options::spawn_method()
131{
132    _spawnMethod = true;
133}
134
135void
136sc_spawn_options::dont_initialize()
137{
138    _dontInitialize = true;
139}
140
141void
142sc_spawn_options::set_stack_size(int ss)
143{
144    _stackSize = ss;
145}
146
147
148void
149sc_spawn_options::set_sensitivity(const sc_event *e)
150{
151    _events.push_back(e);
152}
153
154void
155sc_spawn_options::set_sensitivity(sc_port_base *p)
156{
157    _ports.push_back(p);
158}
159
160void
161sc_spawn_options::set_sensitivity(sc_export_base *e)
162{
163    _exports.push_back(e);
164}
165
166void
167sc_spawn_options::set_sensitivity(sc_interface *i)
168{
169    _interfaces.push_back(i);
170}
171
172void
173sc_spawn_options::set_sensitivity(sc_event_finder *f)
174{
175    _finders.push_back(f);
176}
177
178
179void
180sc_spawn_options::reset_signal_is(const sc_in<bool> &port, bool value)
181{
182    _in_resets.emplace_back(&port, value, true);
183}
184
185void
186sc_spawn_options::reset_signal_is(const sc_inout<bool> &port, bool value)
187{
188    _inout_resets.emplace_back(&port, value, true);
189}
190
191void
192sc_spawn_options::reset_signal_is(const sc_out<bool> &port, bool value)
193{
194    _out_resets.emplace_back(&port, value, true);
195}
196
197void
198sc_spawn_options::reset_signal_is(
199        const sc_signal_in_if<bool> &iface, bool value)
200{
201    _if_resets.emplace_back(&iface, value, true);
202}
203
204
205void
206sc_spawn_options::async_reset_signal_is(const sc_in<bool> &port, bool value)
207{
208    _in_resets.emplace_back(&port, value, false);
209}
210
211void
212sc_spawn_options::async_reset_signal_is(const sc_inout<bool> &port, bool value)
213{
214    _inout_resets.emplace_back(&port, value, false);
215}
216
217void
218sc_spawn_options::async_reset_signal_is(const sc_out<bool> &port, bool value)
219{
220    _out_resets.emplace_back(&port, value, false);
221}
222
223void
224sc_spawn_options::async_reset_signal_is(
225        const sc_signal_in_if<bool> &iface, bool value)
226{
227    _if_resets.emplace_back(&iface, value, false);
228}
229
230} // namespace sc_core
231