sc_spawn.hh revision 12993
112839Sgabeblack@google.com/*
212839Sgabeblack@google.com * Copyright 2018 Google, Inc.
312839Sgabeblack@google.com *
412839Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512839Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612839Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712839Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812839Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912839Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012839Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112839Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212839Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312839Sgabeblack@google.com * this software without specific prior written permission.
1412839Sgabeblack@google.com *
1512839Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612839Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712839Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812839Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912839Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012839Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112839Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212839Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312839Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412839Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512839Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612839Sgabeblack@google.com *
2712839Sgabeblack@google.com * Authors: Gabe Black
2812839Sgabeblack@google.com */
2912839Sgabeblack@google.com
3012839Sgabeblack@google.com#ifndef __SYSTEMC_EXT_CORE_SC_SPAWN_HH__
3112839Sgabeblack@google.com#define __SYSTEMC_EXT_CORE_SC_SPAWN_HH__
3212839Sgabeblack@google.com
3312993Sgabeblack@google.com#include <vector>
3412993Sgabeblack@google.com
3512839Sgabeblack@google.com#include "sc_process_handle.hh"
3612839Sgabeblack@google.com
3712839Sgabeblack@google.comnamespace sc_core
3812839Sgabeblack@google.com{
3912839Sgabeblack@google.com
4012993Sgabeblack@google.comclass sc_spawn_options;
4112993Sgabeblack@google.com
4212993Sgabeblack@google.com} // namespace sc_core
4312993Sgabeblack@google.com
4412993Sgabeblack@google.comnamespace sc_gem5
4512993Sgabeblack@google.com{
4612993Sgabeblack@google.com
4712993Sgabeblack@google.comclass Process;
4812993Sgabeblack@google.com
4912993Sgabeblack@google.comtemplate <typename T>
5012993Sgabeblack@google.comstruct ProcessObjFuncWrapper : public ProcessFuncWrapper
5112993Sgabeblack@google.com{
5212993Sgabeblack@google.com    T t;
5312993Sgabeblack@google.com
5412993Sgabeblack@google.com    ProcessObjFuncWrapper(T t) : t(t) {}
5512993Sgabeblack@google.com
5612993Sgabeblack@google.com    void call() override { t(); }
5712993Sgabeblack@google.com};
5812993Sgabeblack@google.com
5912993Sgabeblack@google.comtemplate <typename T, typename R>
6012993Sgabeblack@google.comstruct ProcessObjRetFuncWrapper : public ProcessFuncWrapper
6112993Sgabeblack@google.com{
6212993Sgabeblack@google.com    T t;
6312993Sgabeblack@google.com    R *r;
6412993Sgabeblack@google.com
6512993Sgabeblack@google.com    ProcessObjRetFuncWrapper(T t, R *r) : t(t), r(r) {}
6612993Sgabeblack@google.com
6712993Sgabeblack@google.com    void call() override { *r = t(); }
6812993Sgabeblack@google.com};
6912993Sgabeblack@google.com
7012993Sgabeblack@google.comProcess *spawnWork(ProcessFuncWrapper *func, const char *name,
7112993Sgabeblack@google.com                   const ::sc_core::sc_spawn_options *);
7212993Sgabeblack@google.com
7312993Sgabeblack@google.com} // namespace sc_gem5
7412993Sgabeblack@google.com
7512993Sgabeblack@google.comnamespace sc_core
7612993Sgabeblack@google.com{
7712993Sgabeblack@google.com
7812839Sgabeblack@google.comtemplate <class T>
7912839Sgabeblack@google.comclass sc_in;
8012839Sgabeblack@google.comtemplate <class T>
8112839Sgabeblack@google.comclass sc_inout;
8212839Sgabeblack@google.comtemplate <class T>
8312839Sgabeblack@google.comclass sc_out;
8412839Sgabeblack@google.comtemplate <class T>
8512839Sgabeblack@google.comclass sc_signal_in_if;
8612839Sgabeblack@google.com
8712839Sgabeblack@google.comclass sc_event;
8812839Sgabeblack@google.comclass sc_event_finder;
8912839Sgabeblack@google.comclass sc_export_base;
9012839Sgabeblack@google.comclass sc_interface;
9112839Sgabeblack@google.comclass sc_port_base;
9212839Sgabeblack@google.com
9312839Sgabeblack@google.comclass sc_spawn_options
9412839Sgabeblack@google.com{
9512839Sgabeblack@google.com  public:
9612993Sgabeblack@google.com    friend ::sc_gem5::Process *::sc_gem5::spawnWork(
9712993Sgabeblack@google.com            ::sc_gem5::ProcessFuncWrapper *, const char *,
9812993Sgabeblack@google.com            const sc_spawn_options *);
9912993Sgabeblack@google.com
10012839Sgabeblack@google.com    sc_spawn_options();
10112839Sgabeblack@google.com
10212839Sgabeblack@google.com    void spawn_method();
10312839Sgabeblack@google.com    void dont_initialize();
10412839Sgabeblack@google.com    void set_stack_size(int);
10512839Sgabeblack@google.com
10612839Sgabeblack@google.com    void set_sensitivity(const sc_event *);
10712839Sgabeblack@google.com    void set_sensitivity(sc_port_base *);
10812839Sgabeblack@google.com    void set_sensitivity(sc_export_base *);
10912839Sgabeblack@google.com    void set_sensitivity(sc_interface *);
11012839Sgabeblack@google.com    void set_sensitivity(sc_event_finder *);
11112839Sgabeblack@google.com
11212839Sgabeblack@google.com    void reset_signal_is(const sc_in<bool> &, bool);
11312839Sgabeblack@google.com    void reset_signal_is(const sc_inout<bool> &, bool);
11412839Sgabeblack@google.com    void reset_signal_is(const sc_out<bool> &, bool);
11512839Sgabeblack@google.com    void reset_signal_is(const sc_signal_in_if<bool> &, bool);
11612839Sgabeblack@google.com
11712839Sgabeblack@google.com    void async_reset_signal_is(const sc_in<bool> &, bool);
11812839Sgabeblack@google.com    void async_reset_signal_is(const sc_inout<bool> &, bool);
11912839Sgabeblack@google.com    void async_reset_signal_is(const sc_out<bool> &, bool);
12012839Sgabeblack@google.com    void async_reset_signal_is(const sc_signal_in_if<bool> &, bool);
12112839Sgabeblack@google.com
12212839Sgabeblack@google.com  private:
12312993Sgabeblack@google.com    bool _spawnMethod;
12412993Sgabeblack@google.com    bool _dontInitialize;
12512993Sgabeblack@google.com    int _stackSize;
12612993Sgabeblack@google.com    std::vector<const sc_event *> _events;
12712993Sgabeblack@google.com    std::vector<sc_port_base *> _ports;
12812993Sgabeblack@google.com    std::vector<sc_export_base *> _exports;
12912993Sgabeblack@google.com    std::vector<sc_interface *> _interfaces;
13012993Sgabeblack@google.com    std::vector<sc_event_finder *> _finders;
13112993Sgabeblack@google.com
13212839Sgabeblack@google.com    // Disabled
13312839Sgabeblack@google.com    sc_spawn_options(const sc_spawn_options &) {}
13412839Sgabeblack@google.com    sc_spawn_options &operator = (const sc_spawn_options &) { return *this; }
13512839Sgabeblack@google.com};
13612839Sgabeblack@google.com
13712839Sgabeblack@google.comvoid sc_spawn_warn_unimpl(const char *func);
13812839Sgabeblack@google.com
13912839Sgabeblack@google.comtemplate <typename T>
14012839Sgabeblack@google.comsc_process_handle
14112839Sgabeblack@google.comsc_spawn(T object, const char *name_p=nullptr,
14212839Sgabeblack@google.com         const sc_spawn_options *opt_p=nullptr)
14312839Sgabeblack@google.com{
14412993Sgabeblack@google.com    auto func = new ::sc_gem5::ProcessObjFuncWrapper<T>(object);
14512993Sgabeblack@google.com    ::sc_gem5::Process *p = spawnWork(func, name_p, opt_p);
14612993Sgabeblack@google.com    return sc_process_handle() = p;
14712839Sgabeblack@google.com}
14812839Sgabeblack@google.com
14912839Sgabeblack@google.comtemplate <typename T>
15012839Sgabeblack@google.comsc_process_handle
15112839Sgabeblack@google.comsc_spawn(typename T::result_type *r_p, T object, const char *name_p=nullptr,
15212839Sgabeblack@google.com         const sc_spawn_options *opt_p=nullptr)
15312839Sgabeblack@google.com{
15412993Sgabeblack@google.com    auto func = new ::sc_gem5::ProcessObjRetFuncWrapper<
15512993Sgabeblack@google.com        T, typename T::result_type>(object, r_p);
15612993Sgabeblack@google.com    ::sc_gem5::Process *p = spawnWork(func, name_p, opt_p);
15712993Sgabeblack@google.com    return sc_process_handle() = p;
15812839Sgabeblack@google.com}
15912839Sgabeblack@google.com
16012839Sgabeblack@google.com#define sc_bind boost::bind
16112839Sgabeblack@google.com#define sc_ref(r) boost::ref(r)
16212839Sgabeblack@google.com#define sc_cref(r) boost::cref(r)
16312839Sgabeblack@google.com
16412879Sgabeblack@google.com#define SC_FORK \
16512879Sgabeblack@google.com{ \
16612879Sgabeblack@google.com    ::sc_core::sc_process_handle forkees[] = {
16712879Sgabeblack@google.com
16812879Sgabeblack@google.com#define SC_JOIN \
16912879Sgabeblack@google.com    }; /* TODO wait for the forkees. */ \
17012879Sgabeblack@google.com}
17112879Sgabeblack@google.com
17212879Sgabeblack@google.com// Non-standard
17312879Sgabeblack@google.com#define SC_CJOIN SC_JOIN
17412839Sgabeblack@google.com
17512839Sgabeblack@google.com} // namespace sc_core
17612839Sgabeblack@google.com
17712839Sgabeblack@google.comnamespace sc_unnamed
17812839Sgabeblack@google.com{
17912839Sgabeblack@google.com
18012839Sgabeblack@google.comtypedef int ImplementationDefined;
18112839Sgabeblack@google.comextern ImplementationDefined _1;
18212839Sgabeblack@google.comextern ImplementationDefined _2;
18312839Sgabeblack@google.comextern ImplementationDefined _3;
18412839Sgabeblack@google.comextern ImplementationDefined _4;
18512839Sgabeblack@google.comextern ImplementationDefined _5;
18612839Sgabeblack@google.comextern ImplementationDefined _6;
18712839Sgabeblack@google.comextern ImplementationDefined _7;
18812839Sgabeblack@google.comextern ImplementationDefined _8;
18912839Sgabeblack@google.comextern ImplementationDefined _9;
19012839Sgabeblack@google.com
19112839Sgabeblack@google.com} // namespace sc_unnamed
19212839Sgabeblack@google.com
19312839Sgabeblack@google.com#endif  //__SYSTEMC_EXT_CORE_SC_SPAWN_HH__
194