gem5  v20.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
global_event.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2013 Advanced Micro Devices, Inc.
3  * Copyright (c) 2013 Mark D. Hill and David A. Wood
4  * All rights reserved.
5  *
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.
16  *
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.
28  */
29 
30 #include "sim/global_event.hh"
31 
33 
35  : barrier(numMainEventQueues),
36  barrierEvent(numMainEventQueues, NULL)
37 {
38 }
39 
40 
42 {
43  // see GlobalEvent::BarrierEvent::~BarrierEvent() comments
44  if (barrierEvent[0] != NULL) {
45  for (int i = 0; i < numMainEventQueues; ++i)
46  delete barrierEvent[i];
47  }
48 }
49 
50 
52 {
53  // This function is scheduling a global event, which actually is a
54  // set of local events, one event on each eventq. Global events need
55  // to have a total order. A thread cannot start executing events that
56  // follow a global event till all other threads have executed that global
57  // event as well. If global events were not in a total order, a deadlock
58  // would occur for there will be two threads who would be waiting for
59  // each other to execute the global events they themselves have executed.
60  //
61  // To ensure this total order, we do two things.
62  // First, before scheduling any global event, a thread needs to acquire
63  // the lock globalQMutex. This ensures that only one thread can schedule
64  // global events at any given time.
65  // Second, the local events corresponding to a global event are always
66  // first inserted in to the asyncq, irrespective of whether or not the
67  // thread scheduling the event owns the eventq on which the event is
68  // being scheduled. Thus global events have the same order in the asyncq
69  // of each thread. When they are inserted in the actual eventq, the
70  // comparators in the Event class ensure that the total order is
71  // maintained.
72 
73  globalQMutex.lock();
74 
75  for (int i = 0; i < numMainEventQueues; ++i) {
76  mainEventQueue[i]->schedule(barrierEvent[i], when, true);
77  }
78 
79  globalQMutex.unlock();
80 }
81 
83 {
85  for (uint32_t i = 0; i < numMainEventQueues; ++i) {
86  if (barrierEvent[i]->scheduled()) {
88  mainEventQueue[i]->deschedule(barrierEvent[i]);
89  }
90  }
91 
92  curEventQueue(q);
93 }
94 
96 {
97  // Read the comment in the schedule() function above.
98  globalQMutex.lock();
99 
100  for (uint32_t i = 0; i < numMainEventQueues; ++i) {
101  if (barrierEvent[i]->scheduled())
102  mainEventQueue[i]->reschedule(barrierEvent[i], when);
103  else
104  mainEventQueue[i]->schedule(barrierEvent[i], when, true);
105  }
106 
107  globalQMutex.unlock();
108 }
109 
111 {
112  // if AutoDelete is set, local events will get deleted in event
113  // loop, but we need to delete GlobalEvent object too... so let
114  // the local event in slot 0 do it
115  if (isFlagSet(AutoDelete) && _globalEvent->barrierEvent[0] == this) {
116  // set backpointer to NULL so that global event knows not to
117  // turn around and recursively delete local events
118  _globalEvent->barrierEvent[0] = NULL;
119  delete _globalEvent;
120  }
121 }
122 
123 
124 void
126 {
127  // wait for all queues to arrive at barrier, then process event
128  if (globalBarrier()) {
129  _globalEvent->process();
130  }
131 
132  // second barrier to force all queues to wait for event processing
133  // to finish before continuing
134  globalBarrier();
135 }
136 
137 
138 void
140 {
141  // wait for all queues to arrive at barrier, then process event
142  if (globalBarrier()) {
143  _globalEvent->process();
144  }
145 
146  // second barrier to force all queues to wait for event processing
147  // to finish before continuing
148  globalBarrier();
150 }
151 
152 void
154 {
155  if (repeat) {
156  schedule(curTick() + repeat);
157  }
158 }
159 
160 const char *
162 {
163  return "GlobalSyncEvent";
164 }
Global events and related declarations.
Bitfield< 7 > i
int8_t Priority
Definition: eventq.hh:117
vector< EventQueue * > mainEventQueue
Array for main event queues.
Definition: eventq.cc:55
Tick when() const
static const FlagsType AutoDelete
Definition: eventq.hh:101
Bitfield< 6 > f
Tick curTick()
The current simulated tick.
Definition: core.hh:44
bool scheduled() const
Queue of events sorted in time order.
Definition: eventq.hh:613
uint64_t Tick
Tick count type.
Definition: types.hh:61
Bitfield< 27 > q
EventQueue * curEventQueue()
Definition: eventq.hh:82
const char * description() const
static std::mutex globalQMutex
Mutex variable for providing exculsive right to schedule global events.
Definition: global_event.hh:68
void handleAsyncInsertions()
Function for moving events from the async_queue to the main queue.
Definition: eventq.cc:434
void schedule(Tick when)
Definition: global_event.cc:51
void reschedule(Tick when)
Definition: global_event.cc:95
uint32_t numMainEventQueues
Current number of allocated main event queues.
Definition: eventq.cc:54
BaseGlobalEvent(Priority p, Flags f)
Definition: global_event.cc:34
virtual ~BaseGlobalEvent()
Definition: global_event.cc:41
Bitfield< 0 > p
std::vector< BarrierEvent * > barrierEvent
The individual local event instances (one per thread/event queue).

Generated on Thu May 28 2020 16:21:35 for gem5 by doxygen 1.8.13