sc_spawn.hh revision 13196
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 3513196Sgabeblack@google.com#include "sc_join.hh" 3612839Sgabeblack@google.com#include "sc_process_handle.hh" 3712839Sgabeblack@google.com 3812839Sgabeblack@google.comnamespace sc_core 3912839Sgabeblack@google.com{ 4012839Sgabeblack@google.com 4112993Sgabeblack@google.comclass sc_spawn_options; 4212993Sgabeblack@google.com 4312993Sgabeblack@google.com} // namespace sc_core 4412993Sgabeblack@google.com 4512993Sgabeblack@google.comnamespace sc_gem5 4612993Sgabeblack@google.com{ 4712993Sgabeblack@google.com 4812993Sgabeblack@google.comclass Process; 4912993Sgabeblack@google.com 5012993Sgabeblack@google.comtemplate <typename T> 5112993Sgabeblack@google.comstruct ProcessObjFuncWrapper : public ProcessFuncWrapper 5212993Sgabeblack@google.com{ 5312993Sgabeblack@google.com T t; 5412993Sgabeblack@google.com 5512993Sgabeblack@google.com ProcessObjFuncWrapper(T t) : t(t) {} 5612993Sgabeblack@google.com 5712993Sgabeblack@google.com void call() override { t(); } 5812993Sgabeblack@google.com}; 5912993Sgabeblack@google.com 6012993Sgabeblack@google.comtemplate <typename T, typename R> 6112993Sgabeblack@google.comstruct ProcessObjRetFuncWrapper : public ProcessFuncWrapper 6212993Sgabeblack@google.com{ 6312993Sgabeblack@google.com T t; 6412993Sgabeblack@google.com R *r; 6512993Sgabeblack@google.com 6612993Sgabeblack@google.com ProcessObjRetFuncWrapper(T t, R *r) : t(t), r(r) {} 6712993Sgabeblack@google.com 6812993Sgabeblack@google.com void call() override { *r = t(); } 6912993Sgabeblack@google.com}; 7012993Sgabeblack@google.com 7112993Sgabeblack@google.comProcess *spawnWork(ProcessFuncWrapper *func, const char *name, 7212993Sgabeblack@google.com const ::sc_core::sc_spawn_options *); 7312993Sgabeblack@google.com 7412993Sgabeblack@google.com} // namespace sc_gem5 7512993Sgabeblack@google.com 7612993Sgabeblack@google.comnamespace sc_core 7712993Sgabeblack@google.com{ 7812993Sgabeblack@google.com 7912839Sgabeblack@google.comtemplate <class T> 8012839Sgabeblack@google.comclass sc_in; 8112839Sgabeblack@google.comtemplate <class T> 8212839Sgabeblack@google.comclass sc_inout; 8312839Sgabeblack@google.comtemplate <class T> 8412839Sgabeblack@google.comclass sc_out; 8512839Sgabeblack@google.comtemplate <class T> 8612839Sgabeblack@google.comclass sc_signal_in_if; 8712839Sgabeblack@google.com 8812839Sgabeblack@google.comclass sc_event; 8912839Sgabeblack@google.comclass sc_event_finder; 9012839Sgabeblack@google.comclass sc_export_base; 9112839Sgabeblack@google.comclass sc_interface; 9212839Sgabeblack@google.comclass sc_port_base; 9312839Sgabeblack@google.com 9412839Sgabeblack@google.comclass sc_spawn_options 9512839Sgabeblack@google.com{ 9612839Sgabeblack@google.com public: 9712993Sgabeblack@google.com friend ::sc_gem5::Process *::sc_gem5::spawnWork( 9812993Sgabeblack@google.com ::sc_gem5::ProcessFuncWrapper *, const char *, 9912993Sgabeblack@google.com const sc_spawn_options *); 10012993Sgabeblack@google.com 10112839Sgabeblack@google.com sc_spawn_options(); 10212839Sgabeblack@google.com 10312839Sgabeblack@google.com void spawn_method(); 10412839Sgabeblack@google.com void dont_initialize(); 10512839Sgabeblack@google.com void set_stack_size(int); 10612839Sgabeblack@google.com 10712839Sgabeblack@google.com void set_sensitivity(const sc_event *); 10812839Sgabeblack@google.com void set_sensitivity(sc_port_base *); 10912839Sgabeblack@google.com void set_sensitivity(sc_export_base *); 11012839Sgabeblack@google.com void set_sensitivity(sc_interface *); 11112839Sgabeblack@google.com void set_sensitivity(sc_event_finder *); 11212839Sgabeblack@google.com 11312839Sgabeblack@google.com void reset_signal_is(const sc_in<bool> &, bool); 11412839Sgabeblack@google.com void reset_signal_is(const sc_inout<bool> &, bool); 11512839Sgabeblack@google.com void reset_signal_is(const sc_out<bool> &, bool); 11612839Sgabeblack@google.com void reset_signal_is(const sc_signal_in_if<bool> &, bool); 11712839Sgabeblack@google.com 11812839Sgabeblack@google.com void async_reset_signal_is(const sc_in<bool> &, bool); 11912839Sgabeblack@google.com void async_reset_signal_is(const sc_inout<bool> &, bool); 12012839Sgabeblack@google.com void async_reset_signal_is(const sc_out<bool> &, bool); 12112839Sgabeblack@google.com void async_reset_signal_is(const sc_signal_in_if<bool> &, bool); 12212839Sgabeblack@google.com 12312839Sgabeblack@google.com private: 12412993Sgabeblack@google.com bool _spawnMethod; 12512993Sgabeblack@google.com bool _dontInitialize; 12612993Sgabeblack@google.com int _stackSize; 12712993Sgabeblack@google.com std::vector<const sc_event *> _events; 12812993Sgabeblack@google.com std::vector<sc_port_base *> _ports; 12912993Sgabeblack@google.com std::vector<sc_export_base *> _exports; 13012993Sgabeblack@google.com std::vector<sc_interface *> _interfaces; 13112993Sgabeblack@google.com std::vector<sc_event_finder *> _finders; 13212993Sgabeblack@google.com 13312839Sgabeblack@google.com // Disabled 13412839Sgabeblack@google.com sc_spawn_options(const sc_spawn_options &) {} 13512839Sgabeblack@google.com sc_spawn_options &operator = (const sc_spawn_options &) { return *this; } 13612839Sgabeblack@google.com}; 13712839Sgabeblack@google.com 13812839Sgabeblack@google.comvoid sc_spawn_warn_unimpl(const char *func); 13912839Sgabeblack@google.com 14012839Sgabeblack@google.comtemplate <typename T> 14112839Sgabeblack@google.comsc_process_handle 14212839Sgabeblack@google.comsc_spawn(T object, const char *name_p=nullptr, 14312839Sgabeblack@google.com const sc_spawn_options *opt_p=nullptr) 14412839Sgabeblack@google.com{ 14512993Sgabeblack@google.com auto func = new ::sc_gem5::ProcessObjFuncWrapper<T>(object); 14612993Sgabeblack@google.com ::sc_gem5::Process *p = spawnWork(func, name_p, opt_p); 14712993Sgabeblack@google.com return sc_process_handle() = p; 14812839Sgabeblack@google.com} 14912839Sgabeblack@google.com 15012839Sgabeblack@google.comtemplate <typename T> 15112839Sgabeblack@google.comsc_process_handle 15212839Sgabeblack@google.comsc_spawn(typename T::result_type *r_p, T object, const char *name_p=nullptr, 15312839Sgabeblack@google.com const sc_spawn_options *opt_p=nullptr) 15412839Sgabeblack@google.com{ 15512993Sgabeblack@google.com auto func = new ::sc_gem5::ProcessObjRetFuncWrapper< 15612993Sgabeblack@google.com T, typename T::result_type>(object, r_p); 15712993Sgabeblack@google.com ::sc_gem5::Process *p = spawnWork(func, name_p, opt_p); 15812993Sgabeblack@google.com return sc_process_handle() = p; 15912839Sgabeblack@google.com} 16012839Sgabeblack@google.com 16112839Sgabeblack@google.com#define sc_bind boost::bind 16212839Sgabeblack@google.com#define sc_ref(r) boost::ref(r) 16312839Sgabeblack@google.com#define sc_cref(r) boost::cref(r) 16412839Sgabeblack@google.com 16512879Sgabeblack@google.com#define SC_FORK \ 16612879Sgabeblack@google.com{ \ 16712879Sgabeblack@google.com ::sc_core::sc_process_handle forkees[] = { 16812879Sgabeblack@google.com 16912879Sgabeblack@google.com#define SC_JOIN \ 17013196Sgabeblack@google.com }; \ 17113196Sgabeblack@google.com ::sc_core::sc_join join; \ 17213196Sgabeblack@google.com for (int i = 0; i < sizeof(forkees) / sizeof(forkees[0]); i++) \ 17313196Sgabeblack@google.com join.add_process(forkees[i]); \ 17413196Sgabeblack@google.com join.wait(); \ 17512879Sgabeblack@google.com} 17612879Sgabeblack@google.com 17712879Sgabeblack@google.com// Non-standard 17813196Sgabeblack@google.com#define SC_CJOIN \ 17913196Sgabeblack@google.com }; \ 18013196Sgabeblack@google.com ::sc_core::sc_join join; \ 18113196Sgabeblack@google.com for (int i = 0; i < sizeof(forkees) / sizeof(forkees[0]); i++) \ 18213196Sgabeblack@google.com join.add_process(forkees[i]); \ 18313196Sgabeblack@google.com join.wait_clocked(); \ 18413196Sgabeblack@google.com} 18513196Sgabeblack@google.com 18612839Sgabeblack@google.com 18712839Sgabeblack@google.com} // namespace sc_core 18812839Sgabeblack@google.com 18912839Sgabeblack@google.comnamespace sc_unnamed 19012839Sgabeblack@google.com{ 19112839Sgabeblack@google.com 19212839Sgabeblack@google.comtypedef int ImplementationDefined; 19312839Sgabeblack@google.comextern ImplementationDefined _1; 19412839Sgabeblack@google.comextern ImplementationDefined _2; 19512839Sgabeblack@google.comextern ImplementationDefined _3; 19612839Sgabeblack@google.comextern ImplementationDefined _4; 19712839Sgabeblack@google.comextern ImplementationDefined _5; 19812839Sgabeblack@google.comextern ImplementationDefined _6; 19912839Sgabeblack@google.comextern ImplementationDefined _7; 20012839Sgabeblack@google.comextern ImplementationDefined _8; 20112839Sgabeblack@google.comextern ImplementationDefined _9; 20212839Sgabeblack@google.com 20312839Sgabeblack@google.com} // namespace sc_unnamed 20412839Sgabeblack@google.com 20512839Sgabeblack@google.com#endif //__SYSTEMC_EXT_CORE_SC_SPAWN_HH__ 206