sc_spawn.hh 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#ifndef __SYSTEMC_EXT_CORE_SC_SPAWN_HH__
31#define __SYSTEMC_EXT_CORE_SC_SPAWN_HH__
32
33#include <functional>
34#include <vector>
35
36#include "sc_join.hh"
37#include "sc_process_handle.hh"
38
39namespace sc_core
40{
41
42class sc_spawn_options;
43
44} // namespace sc_core
45
46namespace sc_gem5
47{
48
49class Process;
50
51template <typename T>
52struct ProcessObjFuncWrapper : public ProcessFuncWrapper
53{
54    T t;
55
56    ProcessObjFuncWrapper(T t) : t(t) {}
57
58    void call() override { t(); }
59};
60
61template <typename T, typename R>
62struct ProcessObjRetFuncWrapper : public ProcessFuncWrapper
63{
64    T t;
65    R *r;
66
67    ProcessObjRetFuncWrapper(T t, R *r) : t(t), r(r) {}
68
69    void call() override { *r = t(); }
70};
71
72Process *spawnWork(ProcessFuncWrapper *func, const char *name,
73                   const ::sc_core::sc_spawn_options *);
74
75} // namespace sc_gem5
76
77namespace sc_core
78{
79
80template <class T>
81class sc_in;
82template <class T>
83class sc_inout;
84template <class T>
85class sc_out;
86template <class T>
87class sc_signal_in_if;
88
89class sc_event;
90class sc_event_finder;
91class sc_export_base;
92class sc_interface;
93class sc_port_base;
94
95class sc_spawn_options
96{
97  public:
98    friend ::sc_gem5::Process *::sc_gem5::spawnWork(
99            ::sc_gem5::ProcessFuncWrapper *, const char *,
100            const sc_spawn_options *);
101
102    sc_spawn_options();
103
104    void spawn_method();
105    void dont_initialize();
106    void set_stack_size(int);
107
108    void set_sensitivity(const sc_event *);
109    void set_sensitivity(sc_port_base *);
110    void set_sensitivity(sc_export_base *);
111    void set_sensitivity(sc_interface *);
112    void set_sensitivity(sc_event_finder *);
113
114    void reset_signal_is(const sc_in<bool> &, bool);
115    void reset_signal_is(const sc_inout<bool> &, bool);
116    void reset_signal_is(const sc_out<bool> &, bool);
117    void reset_signal_is(const sc_signal_in_if<bool> &, bool);
118
119    void async_reset_signal_is(const sc_in<bool> &, bool);
120    void async_reset_signal_is(const sc_inout<bool> &, bool);
121    void async_reset_signal_is(const sc_out<bool> &, bool);
122    void async_reset_signal_is(const sc_signal_in_if<bool> &, bool);
123
124  private:
125    bool _spawnMethod;
126    bool _dontInitialize;
127    int _stackSize;
128    std::vector<const sc_event *> _events;
129    std::vector<sc_port_base *> _ports;
130    std::vector<sc_export_base *> _exports;
131    std::vector<sc_interface *> _interfaces;
132    std::vector<sc_event_finder *> _finders;
133
134    template <typename T>
135    struct Reset
136    {
137        Reset(T *t, bool v, bool s) : target(t), value(v), sync(s) {}
138
139        T *target;
140        bool value;
141        bool sync;
142    };
143
144    std::vector<Reset<const sc_in<bool> > > _in_resets;
145    std::vector<Reset<const sc_inout<bool> > > _inout_resets;
146    std::vector<Reset<const sc_out<bool> > > _out_resets;
147    std::vector<Reset<const sc_signal_in_if<bool> > > _if_resets;
148
149    // Disabled
150    sc_spawn_options(const sc_spawn_options &) {}
151    sc_spawn_options &operator = (const sc_spawn_options &) { return *this; }
152};
153
154void sc_spawn_warn_unimpl(const char *func);
155
156template <typename T>
157sc_process_handle
158sc_spawn(T object, const char *name_p=nullptr,
159         const sc_spawn_options *opt_p=nullptr)
160{
161    auto func = new ::sc_gem5::ProcessObjFuncWrapper<T>(object);
162    ::sc_gem5::Process *p = spawnWork(func, name_p, opt_p);
163    return sc_process_handle() = p;
164}
165
166template <typename T>
167sc_process_handle
168sc_spawn(typename T::result_type *r_p, T object, const char *name_p=nullptr,
169         const sc_spawn_options *opt_p=nullptr)
170{
171    auto func = new ::sc_gem5::ProcessObjRetFuncWrapper<
172        T, typename T::result_type>(object, r_p);
173    ::sc_gem5::Process *p = spawnWork(func, name_p, opt_p);
174    return sc_process_handle() = p;
175}
176
177#define SC_FORK \
178{ \
179    ::sc_core::sc_process_handle forkees[] = {
180
181#define SC_JOIN \
182    }; \
183    ::sc_core::sc_join join; \
184    for (int i = 0; i < sizeof(forkees) / sizeof(forkees[0]); i++) \
185        join.add_process(forkees[i]); \
186    join.wait(); \
187}
188
189// Non-standard
190#define SC_CJOIN \
191    }; \
192    ::sc_core::sc_join join; \
193    for (int i = 0; i < sizeof(forkees) / sizeof(forkees[0]); i++) \
194        join.add_process(forkees[i]); \
195    join.wait_clocked(); \
196}
197
198// This avoids boost introduces a dependency on c++11. If that's a problem,
199// we could imitate Accellera and pick which one to use on the fly.
200
201template <typename F, typename... Args>
202auto sc_bind(F &&f, Args && ...args) ->
203    decltype(std::bind(std::forward<F>(f), std::forward<Args>(args)...))
204{
205    return std::bind(std::forward<F>(f), std::forward<Args>(args)...);
206}
207
208template <typename T>
209auto sc_ref(T &&v) -> decltype(std::ref(std::forward<T>(v)))
210{
211    return std::ref(std::forward<T>(v));
212}
213
214template <typename T>
215auto sc_cref(T &&v) -> decltype(std::cref(std::forward<T>(v)))
216{
217    return std::cref(std::forward<T>(v));
218}
219
220} // namespace sc_core
221
222using sc_core::sc_bind;
223using sc_core::sc_ref;
224using sc_core::sc_cref;
225
226namespace sc_unnamed
227{
228
229using namespace std::placeholders;
230
231} // namespace sc_unnamed
232
233#endif  //__SYSTEMC_EXT_CORE_SC_SPAWN_HH__
234