sc_spawn.cc revision 13267:7997f7b8bede
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/core/sc_main.hh"
35#include "systemc/ext/core/sc_module.hh"
36#include "systemc/ext/core/sc_spawn.hh"
37
38namespace sc_gem5
39{
40
41Process *
42spawnWork(ProcessFuncWrapper *func, const char *name,
43          const ::sc_core::sc_spawn_options *opts)
44{
45    bool method = false;
46    bool dontInitialize = false;
47    if (opts) {
48        if (opts->_spawnMethod)
49            method = true;
50        if (opts->_dontInitialize)
51            dontInitialize = true;
52        if (opts->_stackSize != -1)
53            warn("Ignoring request to set stack size.\n");
54    }
55
56    if (!name || name[0] == '\0') {
57        if (method)
58            name = ::sc_core::sc_gen_unique_name("method_p");
59        else
60            name = ::sc_core::sc_gen_unique_name("thread_p");
61    }
62
63    Process *proc;
64    if (method)
65        proc = new Method(name, func);
66    else
67        proc = new Thread(name, func);
68
69    proc->dontInitialize(dontInitialize);
70
71    if (opts) {
72        for (auto e: opts->_events)
73            newStaticSensitivityEvent(proc, e);
74
75        for (auto p: opts->_ports)
76            newStaticSensitivityPort(proc, p);
77
78        for (auto e: opts->_exports)
79            newStaticSensitivityExport(proc, e);
80
81        for (auto i: opts->_interfaces)
82            newStaticSensitivityInterface(proc, i);
83
84        for (auto f: opts->_finders)
85            newStaticSensitivityFinder(proc, f);
86
87        for (auto p: opts->_in_resets)
88            newResetSensitivityPort(proc, p.target, p.value, p.sync);
89
90        for (auto p: opts->_inout_resets)
91            newResetSensitivityPort(proc, p.target, p.value, p.sync);
92
93        for (auto p: opts->_out_resets)
94            newResetSensitivityPort(proc, p.target, p.value, p.sync);
95
96        for (auto i: opts->_if_resets)
97            newResetSensitivitySignal(proc, i.target, i.value, i.sync);
98    }
99
100    if (opts && opts->_dontInitialize &&
101            opts->_events.empty() && opts->_ports.empty() &&
102            opts->_exports.empty() && opts->_interfaces.empty() &&
103            opts->_finders.empty()) {
104        SC_REPORT_WARNING(
105                "(W558) disable() or dont_initialize() called on process "
106                "with no static sensitivity, it will be orphaned",
107                proc->name());
108    }
109
110    scheduler.reg(proc);
111
112    return proc;
113}
114
115} // namespace sc_gem5
116
117namespace sc_core
118{
119
120sc_spawn_options::sc_spawn_options() :
121    _spawnMethod(false), _dontInitialize(false), _stackSize(-1)
122{}
123
124
125void
126sc_spawn_options::spawn_method()
127{
128    _spawnMethod = true;
129}
130
131void
132sc_spawn_options::dont_initialize()
133{
134    _dontInitialize = true;
135}
136
137void
138sc_spawn_options::set_stack_size(int ss)
139{
140    _stackSize = ss;
141}
142
143
144void
145sc_spawn_options::set_sensitivity(const sc_event *e)
146{
147    _events.push_back(e);
148}
149
150void
151sc_spawn_options::set_sensitivity(sc_port_base *p)
152{
153    _ports.push_back(p);
154}
155
156void
157sc_spawn_options::set_sensitivity(sc_export_base *e)
158{
159    _exports.push_back(e);
160}
161
162void
163sc_spawn_options::set_sensitivity(sc_interface *i)
164{
165    _interfaces.push_back(i);
166}
167
168void
169sc_spawn_options::set_sensitivity(sc_event_finder *f)
170{
171    _finders.push_back(f);
172}
173
174
175void
176sc_spawn_options::reset_signal_is(const sc_in<bool> &port, bool value)
177{
178    _in_resets.emplace_back(&port, value, true);
179}
180
181void
182sc_spawn_options::reset_signal_is(const sc_inout<bool> &port, bool value)
183{
184    _inout_resets.emplace_back(&port, value, true);
185}
186
187void
188sc_spawn_options::reset_signal_is(const sc_out<bool> &port, bool value)
189{
190    _out_resets.emplace_back(&port, value, true);
191}
192
193void
194sc_spawn_options::reset_signal_is(
195        const sc_signal_in_if<bool> &iface, bool value)
196{
197    _if_resets.emplace_back(&iface, value, true);
198}
199
200
201void
202sc_spawn_options::async_reset_signal_is(const sc_in<bool> &port, bool value)
203{
204    _in_resets.emplace_back(&port, value, false);
205}
206
207void
208sc_spawn_options::async_reset_signal_is(const sc_inout<bool> &port, bool value)
209{
210    _inout_resets.emplace_back(&port, value, false);
211}
212
213void
214sc_spawn_options::async_reset_signal_is(const sc_out<bool> &port, bool value)
215{
216    _out_resets.emplace_back(&port, value, false);
217}
218
219void
220sc_spawn_options::async_reset_signal_is(
221        const sc_signal_in_if<bool> &iface, bool value)
222{
223    _if_resets.emplace_back(&iface, value, false);
224}
225
226} // namespace sc_core
227