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.
29 * Authors: Steve Reinhardt
32 #ifndef __SIM_GLOBAL_EVENT_HH__
33 #define __SIM_GLOBAL_EVENT_HH__
38 #include "base/barrier.hh"
39 #include "sim/eventq_impl.hh"
42 * @file sim/global_event.hh
43 * Global events and related declarations.
45 * A global event is an event that occurs across all threads, i.e.,
46 * globally. It consists of a set of "local" (regular) Events, one
47 * per thread/event queue, a barrier object, and common state. The
48 * local events are scheduled for the same tick. The local event
49 * process() method enters the barrier to wait for other threads; once
50 * all threads reach that tick (and enter the associated barrier), the
51 * global event is triggered and its associated activity is performed.
53 * There are two basic global event patterns, GlobalEvent and
54 * GlobalSyncEvent. GlobalEvent is the base class for typical global
55 * events, while GlobalSyncEvent is optimized for global
56 * synchronization operations.
60 * Common base class for GlobalEvent and GlobalSyncEvent.
62 class BaseGlobalEvent : public EventBase
65 //! Mutex variable for providing exculsive right to schedule global
66 //! events. This is necessary so that a total order can be maintained
67 //! amongst the global events. Without ensuring the total order, it is
68 //! possible that threads execute global events in different orders,
69 //! which can result in a deadlock.
70 static std::mutex globalQMutex;
74 /// The base class for the local events that will synchronize
75 /// threads to perform the global event. This class is abstract,
76 /// since it derives from the abstract Event class but still does
77 /// not define the required process() method.
78 class BarrierEvent : public Event
81 BaseGlobalEvent *_globalEvent;
83 BarrierEvent(BaseGlobalEvent *global_event, Priority p, Flags f)
84 : Event(p, f), _globalEvent(global_event)
90 friend class BaseGlobalEvent;
94 // This method will be called from the process() method in
95 // the local barrier events
96 // (GlobalSyncEvent::BarrierEvent). The local event
97 // queues are always locked when servicing events (calling
98 // the process() method), which means that it will be
99 // locked when entering this method. We need to unlock it
100 // while waiting on the barrier to prevent deadlocks if
101 // another thread wants to lock the event queue.
102 EventQueue::ScopedRelease release(curEventQueue());
103 return _globalEvent->barrier.wait();
107 virtual BaseGlobalEvent *globalEvent() { return _globalEvent; }
110 //! The barrier that all threads wait on before performing the
114 //! The individual local event instances (one per thread/event queue).
115 std::vector<BarrierEvent *> barrierEvent;
118 BaseGlobalEvent(Priority p, Flags f);
120 virtual ~BaseGlobalEvent();
122 virtual void process() = 0;
124 virtual const char *description() const = 0;
126 void schedule(Tick when);
128 bool scheduled() const
131 for (uint32_t i = 0; i < numMainEventQueues; ++i) {
132 sched = sched || barrierEvent[i]->scheduled();
140 assert(numMainEventQueues > 0);
141 return barrierEvent[0]->when();
145 void reschedule(Tick when);
150 * Funky intermediate class to support CRTP so that we can have a
151 * common constructor to create the local events, even though the
152 * types of the local events are defined in the derived classes.
154 template <class Derived>
155 class BaseGlobalEventTemplate : public BaseGlobalEvent
158 BaseGlobalEventTemplate(Priority p, Flags f)
159 : BaseGlobalEvent(p, f)
161 for (int i = 0; i < numMainEventQueues; ++i)
162 barrierEvent[i] = new typename Derived::BarrierEvent(this, p, f);
168 * The main global event class. Ordinary global events should derive
169 * from this class, and define process() to specify the action to be
170 * taken when the event is reached. All threads will synchronize at a
171 * barrier, exactly one of the threads will execute the process()
172 * method, then the threads will synchronize again so that none of
173 * them continue until process() is complete.
175 class GlobalEvent : public BaseGlobalEventTemplate<GlobalEvent>
178 typedef BaseGlobalEventTemplate<GlobalEvent> Base;
180 class BarrierEvent : public Base::BarrierEvent
184 BarrierEvent(Base *global_event, Priority p, Flags f)
185 : Base::BarrierEvent(global_event, p, f)
189 GlobalEvent(Priority p, Flags f)
193 GlobalEvent(Tick when, Priority p, Flags f)
199 virtual void process() = 0;
203 * A special global event that synchronizes all threads and forces
204 * them to process asynchronously enqueued events. Useful for
205 * separating quanta in a quantum-based parallel simulation.
207 class GlobalSyncEvent : public BaseGlobalEventTemplate<GlobalSyncEvent>
210 typedef BaseGlobalEventTemplate<GlobalSyncEvent> Base;
212 class BarrierEvent : public Base::BarrierEvent
216 BarrierEvent(Base *global_event, Priority p, Flags f)
217 : Base::BarrierEvent(global_event, p, f)
221 GlobalSyncEvent(Priority p, Flags f)
225 GlobalSyncEvent(Tick when, Tick _repeat, Priority p, Flags f)
226 : Base(p, f), repeat(_repeat)
233 const char *description() const;
239 #endif // __SIM_GLOBAL_EVENT_HH__