sc_process_handle.hh revision 13087:1df34ed84a4b
112337Sjason@lowepower.com/*
212337Sjason@lowepower.com * Copyright 2018 Google, Inc.
312337Sjason@lowepower.com *
412337Sjason@lowepower.com * Redistribution and use in source and binary forms, with or without
512337Sjason@lowepower.com * modification, are permitted provided that the following conditions are
612337Sjason@lowepower.com * met: redistributions of source code must retain the above copyright
712337Sjason@lowepower.com * notice, this list of conditions and the following disclaimer;
812337Sjason@lowepower.com * redistributions in binary form must reproduce the above copyright
912337Sjason@lowepower.com * notice, this list of conditions and the following disclaimer in the
1012337Sjason@lowepower.com * documentation and/or other materials provided with the distribution;
1112337Sjason@lowepower.com * neither the name of the copyright holders nor the names of its
1212337Sjason@lowepower.com * contributors may be used to endorse or promote products derived from
1312337Sjason@lowepower.com * this software without specific prior written permission.
1412337Sjason@lowepower.com *
1512337Sjason@lowepower.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612337Sjason@lowepower.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712337Sjason@lowepower.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812337Sjason@lowepower.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912337Sjason@lowepower.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012337Sjason@lowepower.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112337Sjason@lowepower.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212337Sjason@lowepower.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312337Sjason@lowepower.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412337Sjason@lowepower.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512337Sjason@lowepower.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612337Sjason@lowepower.com *
2712337Sjason@lowepower.com * Authors: Gabe Black
2812337Sjason@lowepower.com */
2912337Sjason@lowepower.com
3012337Sjason@lowepower.com#ifndef __SYSTEMC_EXT_CORE_SC_PROCESS_HANDLE_HH__
3112337Sjason@lowepower.com#define __SYSTEMC_EXT_CORE_SC_PROCESS_HANDLE_HH__
3212337Sjason@lowepower.com
3312337Sjason@lowepower.com#include <exception>
3412337Sjason@lowepower.com#include <vector>
3512337Sjason@lowepower.com
3612337Sjason@lowepower.com#include "systemc/ext/core/sc_object.hh"
3712337Sjason@lowepower.com
3812337Sjason@lowepower.comnamespace sc_gem5
3912337Sjason@lowepower.com{
4012337Sjason@lowepower.com
4112337Sjason@lowepower.comclass Process;
4212337Sjason@lowepower.com
4312337Sjason@lowepower.comstruct ProcessFuncWrapper
4412337Sjason@lowepower.com{
4512337Sjason@lowepower.com    virtual void call() = 0;
4612337Sjason@lowepower.com    virtual ~ProcessFuncWrapper() {}
4712337Sjason@lowepower.com};
4812337Sjason@lowepower.com
4912337Sjason@lowepower.comtemplate <typename T>
5012337Sjason@lowepower.comstruct ProcessMemberFuncWrapper : public ProcessFuncWrapper
5112337Sjason@lowepower.com{
5212337Sjason@lowepower.com    typedef void (T::*TFunc)();
5312337Sjason@lowepower.com    T *t;
5412337Sjason@lowepower.com    TFunc func;
5512337Sjason@lowepower.com
5612337Sjason@lowepower.com    ProcessMemberFuncWrapper(T *t, TFunc func) : t(t), func(func) {}
5712337Sjason@lowepower.com
5812337Sjason@lowepower.com    void call() override { (t->*func)(); }
5912337Sjason@lowepower.com};
6012337Sjason@lowepower.com
6112337Sjason@lowepower.comstruct ExceptionWrapperBase
6212337Sjason@lowepower.com{
6312337Sjason@lowepower.com    virtual void throw_it() = 0;
6412337Sjason@lowepower.com};
6512337Sjason@lowepower.com
6612337Sjason@lowepower.comtemplate <typename T>
6712337Sjason@lowepower.comstruct ExceptionWrapper : public ExceptionWrapperBase
6812337Sjason@lowepower.com{
6912337Sjason@lowepower.com    const T &t;
7012337Sjason@lowepower.com    ExceptionWrapper(const T &t) : t(t) {}
7112337Sjason@lowepower.com
7212337Sjason@lowepower.com    void throw_it() { throw t; }
7312337Sjason@lowepower.com};
7412337Sjason@lowepower.com
7512337Sjason@lowepower.comvoid throw_it_wrapper(Process *p, ExceptionWrapperBase &exc, bool inc_kids);
7612337Sjason@lowepower.com
7712337Sjason@lowepower.com} // namespace sc_gem5
7812337Sjason@lowepower.com
7912337Sjason@lowepower.comnamespace sc_core
8012337Sjason@lowepower.com{
8112337Sjason@lowepower.com
8212337Sjason@lowepower.comclass sc_event;
8312337Sjason@lowepower.com
8412337Sjason@lowepower.comenum sc_curr_proc_kind
85{
86    SC_NO_PROC_,
87    SC_METHOD_PROC_,
88    SC_THREAD_PROC_,
89    SC_CTHREAD_PROC_
90};
91
92enum sc_descendent_inclusion_info
93{
94    SC_NO_DESCENDANTS,
95    SC_INCLUDE_DESCENDANTS
96};
97
98class sc_unwind_exception : public std::exception
99{
100  public:
101    virtual const char *what() const throw();
102    virtual bool is_reset() const;
103
104    // Nonstandard.
105    // These should be protected, but I think this is to enable catch by
106    // value.
107  public:
108    sc_unwind_exception(const sc_unwind_exception &);
109    virtual ~sc_unwind_exception() throw();
110
111  protected:
112    bool _isReset;
113    sc_unwind_exception();
114};
115
116// Deprecated
117// An incomplete version of sc_process_b to satisfy the tests.
118class sc_process_b : public sc_object
119{
120  public:
121    sc_process_b(const char *name) : sc_object(name), file(nullptr), lineno(0)
122    {}
123    sc_process_b() : sc_object(), file(nullptr), lineno(0) {}
124
125    const char *file;
126    int lineno;
127};
128
129// Nonstandard
130void sc_set_location(const char *file, int lineno);
131
132// Deprecated
133sc_process_b *sc_get_curr_process_handle();
134static inline sc_process_b *
135sc_get_current_process_b()
136{
137    return sc_get_curr_process_handle();
138}
139
140// Deprecated/nonstandard
141struct sc_curr_proc_info
142{
143    sc_process_b *process_handle;
144    sc_curr_proc_kind kind;
145    sc_curr_proc_info() : process_handle(NULL), kind(SC_NO_PROC_) {}
146};
147typedef const sc_curr_proc_info *sc_curr_proc_handle;
148
149class sc_process_handle
150{
151  private:
152    ::sc_gem5::Process *_gem5_process;
153
154  public:
155    sc_process_handle();
156    sc_process_handle(const sc_process_handle &);
157    explicit sc_process_handle(sc_object *);
158    ~sc_process_handle();
159
160    // These non-standard operators provide access to the data structure which
161    // actually tracks the process within gem5. By making them operators, we
162    // can minimize the symbols added to the class namespace.
163    operator ::sc_gem5::Process * () const { return _gem5_process; }
164    sc_process_handle &
165    operator = (::sc_gem5::Process *p)
166    {
167        _gem5_process = p;
168        return *this;
169    }
170
171    bool valid() const;
172
173    sc_process_handle &operator = (const sc_process_handle &);
174    bool operator == (const sc_process_handle &) const;
175    bool operator != (const sc_process_handle &) const;
176    bool operator < (const sc_process_handle &) const;
177    void swap(sc_process_handle &);
178
179    const char *name() const;
180    sc_curr_proc_kind proc_kind() const;
181    const std::vector<sc_object *> &get_child_objects() const;
182    const std::vector<sc_event *> &get_child_events() const;
183    sc_object *get_parent_object() const;
184    sc_object *get_process_object() const;
185    bool dynamic() const;
186    bool terminated() const;
187    const sc_event &terminated_event() const;
188
189    void suspend(sc_descendent_inclusion_info include_descendants=
190                 SC_NO_DESCENDANTS);
191    void resume(sc_descendent_inclusion_info include_descendants=
192                SC_NO_DESCENDANTS);
193    void disable(sc_descendent_inclusion_info include_descendants=
194                 SC_NO_DESCENDANTS);
195    void enable(sc_descendent_inclusion_info include_descendants=
196                SC_NO_DESCENDANTS);
197    void kill(sc_descendent_inclusion_info include_descendants=
198              SC_NO_DESCENDANTS);
199    void reset(sc_descendent_inclusion_info include_descendants=
200               SC_NO_DESCENDANTS);
201    bool is_unwinding();
202    const sc_event &reset_event() const;
203
204    void sync_reset_on(sc_descendent_inclusion_info include_descendants=
205                       SC_NO_DESCENDANTS);
206    void sync_reset_off(sc_descendent_inclusion_info include_descendants=
207                        SC_NO_DESCENDANTS);
208
209    template <typename T>
210    void
211    throw_it(const T &user_defined_exception,
212             sc_descendent_inclusion_info include_descendants=
213             SC_NO_DESCENDANTS)
214    {
215        ::sc_gem5::ExceptionWrapper<T> exc(user_defined_exception);
216        ::sc_gem5::throw_it_wrapper(_gem5_process, exc,
217                include_descendants == SC_INCLUDE_DESCENDANTS);
218    }
219};
220
221sc_process_handle sc_get_current_process_handle();
222bool sc_is_unwinding();
223
224// Nonstandard
225// See Accellera's kernel/sim_context.cpp for an explanation of what this is
226// supposed to do. It essentially selects what happens during certain
227// undefined situations.
228extern bool sc_allow_process_control_corners;
229
230} // namespace sc_core
231
232#endif  //__SYSTEMC_EXT_CORE_SC_PROCESS_HANDLE_HH__
233