sc_event.hh revision 13294
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_EVENT_HH__
31#define __SYSTEMC_EXT_CORE_SC_EVENT_HH__
32
33#include <cassert>
34#include <set>
35#include <sstream>
36#include <vector>
37
38#include "../utils/sc_report_handler.hh"
39#include "sc_port.hh"
40#include "sc_time.hh"
41
42namespace sc_gem5
43{
44
45class Event;
46class DynamicSensitivityEventAndList;
47class DynamicSensitivityEventOrList;
48
49}
50
51namespace sc_core
52{
53
54class sc_event;
55class sc_event_and_expr;
56class sc_event_or_expr;
57class sc_interface;
58class sc_object;
59class sc_port_base;
60
61class sc_event_and_list
62{
63  public:
64    sc_event_and_list();
65    sc_event_and_list(const sc_event_and_list &);
66    sc_event_and_list(const sc_event &);
67    sc_event_and_list &operator = (const sc_event_and_list &);
68    ~sc_event_and_list();
69
70    int size() const;
71    void swap(sc_event_and_list &);
72
73    sc_event_and_list &operator &= (const sc_event &);
74    sc_event_and_list &operator &= (const sc_event_and_list &);
75
76    sc_event_and_expr operator & (const sc_event &) const;
77    sc_event_and_expr operator & (const sc_event_and_list &);
78
79  private:
80    friend class sc_event_and_expr;
81    friend class sc_gem5::DynamicSensitivityEventAndList;
82
83    explicit sc_event_and_list(bool auto_delete);
84
85    void insert(sc_event const &e);
86    void insert(sc_event_and_list const &eal);
87
88    std::set<const sc_event *> events;
89    bool autoDelete;
90    mutable unsigned busy;
91};
92
93class sc_event_or_list
94{
95  public:
96    sc_event_or_list();
97    sc_event_or_list(const sc_event_or_list &);
98    sc_event_or_list(const sc_event &);
99    sc_event_or_list& operator = (const sc_event_or_list &);
100    ~sc_event_or_list();
101
102    int size() const;
103    void swap(sc_event_or_list &);
104
105    sc_event_or_list &operator |= (const sc_event &);
106    sc_event_or_list &operator |= (const sc_event_or_list &);
107
108    sc_event_or_expr operator | (const sc_event &) const;
109    sc_event_or_expr operator | (const sc_event_or_list &) const;
110
111  private:
112    friend class sc_event_or_expr;
113    friend class sc_gem5::DynamicSensitivityEventOrList;
114
115    explicit sc_event_or_list(bool auto_delete);
116
117    void insert(sc_event const &e);
118    void insert(sc_event_or_list const &eol);
119
120    std::set<const sc_event *> events;
121    bool autoDelete;
122    mutable unsigned busy;
123};
124
125class sc_event_and_expr
126{
127  public:
128    sc_event_and_expr(sc_event_and_expr const &e);
129    operator const sc_event_and_list &() const;
130
131    void insert(sc_event const &e) const;
132    void insert(sc_event_and_list const &eal) const;
133
134    ~sc_event_and_expr();
135
136  private:
137    friend class sc_event_and_list;
138    friend class sc_event;
139
140    sc_event_and_expr();
141    mutable sc_event_and_list *list;
142};
143
144sc_event_and_expr operator & (sc_event_and_expr, sc_event const &);
145sc_event_and_expr operator & (sc_event_and_expr, sc_event_and_list const &);
146
147class sc_event_or_expr
148{
149  public:
150    sc_event_or_expr(sc_event_or_expr const &e);
151    operator const sc_event_or_list &() const;
152
153    void insert(sc_event const &e) const;
154    void insert(sc_event_or_list const &eol) const;
155
156    ~sc_event_or_expr();
157
158  private:
159    friend class sc_event_or_list;
160    friend class sc_event;
161
162    sc_event_or_expr();
163    mutable sc_event_or_list *list;
164};
165
166sc_event_or_expr operator | (sc_event_or_expr, sc_event const &);
167sc_event_or_expr operator | (sc_event_or_expr, sc_event_or_list const &);
168
169class sc_event
170{
171  public:
172    sc_event();
173    explicit sc_event(const char *);
174    ~sc_event();
175
176    const char *name() const;
177    const char *basename() const;
178    bool in_hierarchy() const;
179    sc_object *get_parent_object() const;
180
181    void notify();
182    void notify(const sc_time &);
183    void notify(double, sc_time_unit);
184    void cancel();
185
186    // Nonstandard
187    // Returns whether this event is currently triggered.
188    bool triggered() const;
189
190    // Deprecated
191    void notify_delayed();
192    void notify_delayed(const sc_time &);
193
194    sc_event_and_expr operator & (const sc_event &) const;
195    sc_event_and_expr operator & (const sc_event_and_list &) const;
196    sc_event_or_expr operator | (const sc_event &) const;
197    sc_event_or_expr operator | (const sc_event_or_list &) const;
198
199  private:
200    // Disabled
201    sc_event(const sc_event &) {}
202    sc_event &operator = (const sc_event &) { return *this; }
203
204    friend class ::sc_gem5::Event;
205    ::sc_gem5::Event *_gem5_event;
206};
207
208class sc_event_finder
209{
210  protected:
211    virtual ~sc_event_finder() {}
212
213  public:
214    // Should be "implementation defined" but used in the tests.
215    virtual const sc_event &find_event(sc_interface *if_p=NULL) const = 0;
216    virtual const sc_port_base *port() const = 0;
217};
218
219template <class IF>
220class sc_event_finder_t : public sc_event_finder
221{
222  public:
223    sc_event_finder_t(const sc_port_base &p,
224                      const sc_event & (IF::*_method)() const) :
225        _method(_method)
226    {
227        _port = dynamic_cast<const sc_port_b<IF> *>(&p);
228        assert(_port);
229    }
230
231    virtual ~sc_event_finder_t() {}
232
233    const sc_port_base *port() const { return _port; }
234
235    const sc_event &
236    find_event(sc_interface *if_p=NULL) const override
237    {
238        static const sc_event none;
239        const IF *iface = if_p ? dynamic_cast<const IF *>(if_p) :
240            dynamic_cast<const IF *>(_port->get_interface());
241        if (!iface) {
242            std::ostringstream ss;
243            ss << "port is not bound: port '" << _port->name() << "' (" <<
244                _port->kind() << ")";
245            SC_REPORT_ERROR("(E118) find event failed", ss.str().c_str());
246            return none;
247        }
248        return (const_cast<IF *>(iface)->*_method)();
249    }
250
251  private:
252    const sc_port_b<IF> *_port;
253    const sc_event &(IF::*_method)() const;
254};
255
256const std::vector<sc_event *> &sc_get_top_level_events();
257sc_event *sc_find_event(const char *);
258
259} // namespace sc_core
260
261#endif  //__SYSTEMC_EXT_CORE_SC_INTERFACE_HH__
262