2 * Copyright (c) 2011-2013 Advanced Micro Devices, Inc.
3 * Copyright (c) 2013 Mark D. Hill and David A. Wood
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef __SIM_GLOBAL_EVENT_HH__
31 #define __SIM_GLOBAL_EVENT_HH__
36 #include "base/barrier.hh"
37 #include "sim/eventq_impl.hh"
40 * @file sim/global_event.hh
41 * Global events and related declarations.
43 * A global event is an event that occurs across all threads, i.e.,
44 * globally. It consists of a set of "local" (regular) Events, one
45 * per thread/event queue, a barrier object, and common state. The
46 * local events are scheduled for the same tick. The local event
47 * process() method enters the barrier to wait for other threads; once
48 * all threads reach that tick (and enter the associated barrier), the
49 * global event is triggered and its associated activity is performed.
51 * There are two basic global event patterns, GlobalEvent and
52 * GlobalSyncEvent. GlobalEvent is the base class for typical global
53 * events, while GlobalSyncEvent is optimized for global
54 * synchronization operations.
58 * Common base class for GlobalEvent and GlobalSyncEvent.
60 class BaseGlobalEvent : public EventBase
63 //! Mutex variable for providing exculsive right to schedule global
64 //! events. This is necessary so that a total order can be maintained
65 //! amongst the global events. Without ensuring the total order, it is
66 //! possible that threads execute global events in different orders,
67 //! which can result in a deadlock.
68 static std::mutex globalQMutex;
72 /// The base class for the local events that will synchronize
73 /// threads to perform the global event. This class is abstract,
74 /// since it derives from the abstract Event class but still does
75 /// not define the required process() method.
76 class BarrierEvent : public Event
79 BaseGlobalEvent *_globalEvent;
81 BarrierEvent(BaseGlobalEvent *global_event, Priority p, Flags f)
82 : Event(p, f), _globalEvent(global_event)
88 friend class BaseGlobalEvent;
92 // This method will be called from the process() method in
93 // the local barrier events
94 // (GlobalSyncEvent::BarrierEvent). The local event
95 // queues are always locked when servicing events (calling
96 // the process() method), which means that it will be
97 // locked when entering this method. We need to unlock it
98 // while waiting on the barrier to prevent deadlocks if
99 // another thread wants to lock the event queue.
100 EventQueue::ScopedRelease release(curEventQueue());
101 return _globalEvent->barrier.wait();
105 virtual BaseGlobalEvent *globalEvent() { return _globalEvent; }
108 //! The barrier that all threads wait on before performing the
112 //! The individual local event instances (one per thread/event queue).
113 std::vector<BarrierEvent *> barrierEvent;
116 BaseGlobalEvent(Priority p, Flags f);
118 virtual ~BaseGlobalEvent();
120 virtual void process() = 0;
122 virtual const char *description() const = 0;
124 void schedule(Tick when);
126 bool scheduled() const
129 for (uint32_t i = 0; i < numMainEventQueues; ++i) {
130 sched = sched || barrierEvent[i]->scheduled();
138 assert(numMainEventQueues > 0);
139 return barrierEvent[0]->when();
143 void reschedule(Tick when);
148 * Funky intermediate class to support CRTP so that we can have a
149 * common constructor to create the local events, even though the
150 * types of the local events are defined in the derived classes.
152 template <class Derived>
153 class BaseGlobalEventTemplate : public BaseGlobalEvent
156 BaseGlobalEventTemplate(Priority p, Flags f)
157 : BaseGlobalEvent(p, f)
159 for (int i = 0; i < numMainEventQueues; ++i)
160 barrierEvent[i] = new typename Derived::BarrierEvent(this, p, f);
166 * The main global event class. Ordinary global events should derive
167 * from this class, and define process() to specify the action to be
168 * taken when the event is reached. All threads will synchronize at a
169 * barrier, exactly one of the threads will execute the process()
170 * method, then the threads will synchronize again so that none of
171 * them continue until process() is complete.
173 class GlobalEvent : public BaseGlobalEventTemplate<GlobalEvent>
176 typedef BaseGlobalEventTemplate<GlobalEvent> Base;
178 class BarrierEvent : public Base::BarrierEvent
182 BarrierEvent(Base *global_event, Priority p, Flags f)
183 : Base::BarrierEvent(global_event, p, f)
187 GlobalEvent(Priority p, Flags f)
191 GlobalEvent(Tick when, Priority p, Flags f)
197 virtual void process() = 0;
201 * A special global event that synchronizes all threads and forces
202 * them to process asynchronously enqueued events. Useful for
203 * separating quanta in a quantum-based parallel simulation.
205 class GlobalSyncEvent : public BaseGlobalEventTemplate<GlobalSyncEvent>
208 typedef BaseGlobalEventTemplate<GlobalSyncEvent> Base;
210 class BarrierEvent : public Base::BarrierEvent
214 BarrierEvent(Base *global_event, Priority p, Flags f)
215 : Base::BarrierEvent(global_event, p, f)
219 GlobalSyncEvent(Priority p, Flags f)
220 : Base(p, f), repeat(0)
223 GlobalSyncEvent(Tick when, Tick _repeat, Priority p, Flags f)
224 : Base(p, f), repeat(_repeat)
231 const char *description() const;
237 #endif // __SIM_GLOBAL_EVENT_HH__