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#include "systemc/core/event.hh"
31#include "systemc/ext/core/sc_event.hh"
32#include "systemc/ext/core/sc_module.hh"
33
34namespace sc_core
35{
36
37
38/*
39 * sc_event_and_list
40 */
41
42sc_event_and_list::sc_event_and_list() : autoDelete(false), busy(0) {}
43
44sc_event_and_list::sc_event_and_list(const sc_event_and_list &eal) :
45    events(eal.events), autoDelete(false), busy(0)
46{}
47
48sc_event_and_list::sc_event_and_list(const sc_event &e) : sc_event_and_list()
49{
50    insert(e);
51}
52
53sc_event_and_list::sc_event_and_list(bool auto_delete) :
54    autoDelete(auto_delete), busy(0)
55{}
56
57sc_event_and_list::~sc_event_and_list() {}
58
59sc_event_and_list &
60sc_event_and_list::operator = (const sc_event_and_list &eal)
61{
62    events = eal.events;
63    return *this;
64}
65
66int
67sc_event_and_list::size() const
68{
69    return events.size();
70}
71
72void
73sc_event_and_list::swap(sc_event_and_list &eal)
74{
75    events.swap(eal.events);
76}
77
78sc_event_and_list &
79sc_event_and_list::operator &= (const sc_event &e)
80{
81    insert(e);
82    return *this;
83}
84
85sc_event_and_list &
86sc_event_and_list::operator &= (const sc_event_and_list &eal)
87{
88    insert(eal);
89    return *this;
90}
91
92sc_event_and_expr
93sc_event_and_list::operator & (const sc_event &e) const
94{
95    sc_event_and_expr expr;
96    expr.insert(*this);
97    expr.insert(e);
98    return expr;
99}
100
101sc_event_and_expr
102sc_event_and_list::operator & (const sc_event_and_list &eal) const
103{
104    sc_event_and_expr expr;
105    expr.insert(*this);
106    expr.insert(eal);
107    return expr;
108}
109
110void
111sc_event_and_list::insert(sc_event const &e)
112{
113    events.insert(&e);
114}
115
116void
117sc_event_and_list::insert(sc_event_and_list const &eal)
118{
119    events.insert(eal.events.begin(), eal.events.end());
120}
121
122
123/*
124 * sc_event_or_list
125 */
126
127sc_event_or_list::sc_event_or_list() : autoDelete(false), busy(0) {}
128
129sc_event_or_list::sc_event_or_list(const sc_event_or_list &eol) :
130    events(eol.events), autoDelete(false), busy(0)
131{}
132
133sc_event_or_list::sc_event_or_list(const sc_event &e) : sc_event_or_list()
134{
135    insert(e);
136}
137
138sc_event_or_list::sc_event_or_list(bool auto_delete) :
139    autoDelete(auto_delete), busy(0)
140{}
141
142sc_event_or_list &
143sc_event_or_list::operator = (const sc_event_or_list &eol)
144{
145    events = eol.events;
146    return *this;
147}
148
149sc_event_or_list::~sc_event_or_list() {}
150
151int
152sc_event_or_list::size() const
153{
154    return events.size();
155}
156
157void
158sc_event_or_list::swap(sc_event_or_list &eol)
159{
160    events.swap(eol.events);
161}
162
163sc_event_or_list &
164sc_event_or_list::operator |= (const sc_event &e)
165{
166    insert(e);
167    return *this;
168}
169
170sc_event_or_list &
171sc_event_or_list::operator |= (const sc_event_or_list &eol)
172{
173    insert(eol);
174    return *this;
175}
176
177sc_event_or_expr
178sc_event_or_list::operator | (const sc_event &e) const
179{
180    sc_event_or_expr expr;
181    expr.insert(*this);
182    expr.insert(e);
183    return expr;
184}
185
186sc_event_or_expr
187sc_event_or_list::operator | (const sc_event_or_list &eol) const
188{
189    sc_event_or_expr expr;
190    expr.insert(*this);
191    expr.insert(eol);
192    return expr;
193}
194
195void
196sc_event_or_list::insert(sc_event const &e)
197{
198    events.insert(&e);
199}
200
201void
202sc_event_or_list::insert(sc_event_or_list const &eol)
203{
204    events.insert(eol.events.begin(), eol.events.end());
205}
206
207
208/*
209 * sc_event_and_expr
210 */
211
212// Move semantics
213sc_event_and_expr::sc_event_and_expr(sc_event_and_expr const &e) :
214    list(e.list)
215{
216    e.list = nullptr;
217}
218
219sc_event_and_expr::operator const sc_event_and_list &() const
220{
221    sc_event_and_list *temp = list;
222    list = nullptr;
223    return *temp;
224}
225
226void
227sc_event_and_expr::insert(sc_event const &e) const
228{
229    assert(list);
230    list->insert(e);
231}
232
233void
234sc_event_and_expr::insert(sc_event_and_list const &eal) const
235{
236    assert(list);
237    list->insert(eal);
238}
239
240sc_event_and_expr::~sc_event_and_expr() { delete list; }
241
242sc_event_and_expr::sc_event_and_expr() : list(new sc_event_and_list(true)) {}
243
244sc_event_and_expr
245operator & (sc_event_and_expr expr, sc_event const &e)
246{
247    expr.insert(e);
248    return expr;
249}
250
251sc_event_and_expr
252operator & (sc_event_and_expr expr, sc_event_and_list const &eal)
253{
254    expr.insert(eal);
255    return expr;
256}
257
258
259/*
260 * sc_event_or_expr
261 */
262
263// Move semantics
264sc_event_or_expr::sc_event_or_expr(sc_event_or_expr const &e) :
265    list(e.list)
266{
267    e.list = nullptr;
268}
269
270sc_event_or_expr::operator const sc_event_or_list &() const
271{
272    sc_event_or_list *temp = list;
273    list = NULL;
274    return *temp;
275}
276
277void
278sc_event_or_expr::insert(sc_event const &e) const
279{
280    assert(list);
281    list->insert(e);
282}
283
284void
285sc_event_or_expr::insert(sc_event_or_list const &eol) const
286{
287    assert(list);
288    list->insert(eol);
289}
290
291sc_event_or_expr::~sc_event_or_expr() { delete list; }
292
293sc_event_or_expr::sc_event_or_expr() : list(new sc_event_or_list(true)) {}
294
295sc_event_or_expr
296operator | (sc_event_or_expr expr, sc_event const &e)
297{
298    expr.insert(e);
299    return expr;
300}
301
302sc_event_or_expr
303operator | (sc_event_or_expr expr, sc_event_or_list const &eol)
304{
305    expr.insert(eol);
306    return expr;
307}
308
309
310/*
311 * sc_event
312 */
313
314sc_event::sc_event() :
315    _gem5_event(new ::sc_gem5::Event(
316                this, sc_core::sc_gen_unique_name("event")))
317{}
318
319sc_event::sc_event(const char *_name) :
320    _gem5_event(new ::sc_gem5::Event(this, _name))
321{}
322
323sc_event::~sc_event() { delete _gem5_event; }
324
325const char *sc_event::name() const { return _gem5_event->name().c_str(); }
326const char *
327sc_event::basename() const
328{
329    return _gem5_event->basename().c_str();
330}
331bool sc_event::in_hierarchy() const { return _gem5_event->inHierarchy(); }
332
333sc_object *
334sc_event::get_parent_object() const
335{
336    return _gem5_event->getParentObject();
337}
338
339void sc_event::notify() { _gem5_event->notify(); }
340void sc_event::notify(const sc_time &t) { _gem5_event->notify(t); }
341void sc_event::notify(double d, sc_time_unit u) { _gem5_event->notify(d, u); }
342void sc_event::cancel() { _gem5_event->cancel(); }
343bool sc_event::triggered() const { return _gem5_event->triggered(); }
344void
345sc_event::notify_delayed()
346{
347    _gem5_event->notifyDelayed(SC_ZERO_TIME);
348}
349void
350sc_event::notify_delayed(const sc_time &t)
351{
352    _gem5_event->notifyDelayed(t);
353}
354
355sc_event_and_expr
356sc_event::operator & (const sc_event &e) const
357{
358    sc_event_and_expr expr;
359    expr.insert(*this);
360    expr.insert(e);
361    return expr;
362}
363
364sc_event_and_expr
365sc_event::operator & (const sc_event_and_list &eal) const
366{
367    sc_event_and_expr expr;
368    expr.insert(*this);
369    expr.insert(eal);
370    return expr;
371}
372
373sc_event_or_expr
374sc_event::operator | (const sc_event &e) const
375{
376    sc_event_or_expr expr;
377    expr.insert(*this);
378    expr.insert(e);
379    return expr;
380}
381
382sc_event_or_expr
383sc_event::operator | (const sc_event_or_list &eol) const
384{
385    sc_event_or_expr expr;
386    expr.insert(*this);
387    expr.insert(eol);
388    return expr;
389}
390
391sc_event::sc_event(bool) :
392    _gem5_event(new ::sc_gem5::Event(
393                this, sc_core::sc_gen_unique_name(
394                    "$$$internal kernel event$$$"), true))
395{}
396
397sc_event::sc_event(bool, const char *_name) :
398    _gem5_event(new ::sc_gem5::Event(
399                this,
400                (std::string("$$$internal kernel event$$$") + _name).c_str(),
401                true))
402{}
403
404const std::vector<sc_event *> &
405sc_get_top_level_events()
406{
407    return ::sc_gem5::topLevelEvents;
408}
409
410sc_event *
411sc_find_event(const char *name)
412{
413    std::string str(name);
414    ::sc_gem5::EventsIt it = ::sc_gem5::findEvent(str);
415    return it == ::sc_gem5::allEvents.end() ? nullptr : *it;
416}
417
418} // namespace sc_core
419
420namespace sc_gem5
421{
422
423InternalScEvent::InternalScEvent() : sc_event(true) {}
424InternalScEvent::InternalScEvent(const char *_name) : sc_event(true, _name) {}
425
426} // namespace sc_gem5
427