111988Sandreas.sandberg@arm.com# Copyright (c) 2017 ARM Limited
211988Sandreas.sandberg@arm.com# All rights reserved.
311988Sandreas.sandberg@arm.com#
411988Sandreas.sandberg@arm.com# The license below extends only to copyright in the software and shall
511988Sandreas.sandberg@arm.com# not be construed as granting a license to any other intellectual
611988Sandreas.sandberg@arm.com# property including but not limited to intellectual property relating
711988Sandreas.sandberg@arm.com# to a hardware implementation of the functionality of the software
811988Sandreas.sandberg@arm.com# licensed hereunder.  You may use the software subject to the license
911988Sandreas.sandberg@arm.com# terms below provided that you ensure that this notice is replicated
1011988Sandreas.sandberg@arm.com# unmodified and in its entirety in all distributions of the software,
1111988Sandreas.sandberg@arm.com# modified or unmodified, in source code or in binary form.
1211988Sandreas.sandberg@arm.com#
134167Sbinkertn@umich.edu# Copyright (c) 2006 The Regents of The University of Michigan
149983Sstever@gmail.com# Copyright (c) 2013 Advanced Micro Devices, Inc.
159983Sstever@gmail.com# Copyright (c) 2013 Mark D. Hill and David A. Wood
164167Sbinkertn@umich.edu# All rights reserved.
174167Sbinkertn@umich.edu#
184167Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without
194167Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are
204167Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright
214167Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer;
224167Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright
234167Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the
244167Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution;
254167Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
264167Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
274167Sbinkertn@umich.edu# this software without specific prior written permission.
284167Sbinkertn@umich.edu#
294167Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
304167Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
314167Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
324167Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
334167Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
344167Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
354167Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
364167Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
374167Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
384167Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
394167Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
404167Sbinkertn@umich.edu#
414167Sbinkertn@umich.edu# Authors: Nathan Binkert
424167Sbinkertn@umich.edu
4312563Sgabeblack@google.comfrom __future__ import print_function
4412563Sgabeblack@google.com
455696Snate@binkert.orgimport m5
4611802Sandreas.sandberg@arm.comimport _m5.event
474167Sbinkertn@umich.edu
4811988Sandreas.sandberg@arm.comfrom _m5.event import GlobalSimLoopExitEvent as SimExit
4912041Sandreas.sandberg@arm.comfrom _m5.event import PyEvent as Event
5012041Sandreas.sandberg@arm.comfrom _m5.event import getEventQueue, setEventQueue
515605Snate@binkert.org
529983Sstever@gmail.commainq = None
535605Snate@binkert.org
5412024Sandreas.sandberg@arm.comclass EventWrapper(Event):
5512024Sandreas.sandberg@arm.com    """Helper class to wrap callable objects in an Event base class"""
5612024Sandreas.sandberg@arm.com
5712024Sandreas.sandberg@arm.com    def __init__(self, func, **kwargs):
5812024Sandreas.sandberg@arm.com        super(EventWrapper, self).__init__(**kwargs)
5912024Sandreas.sandberg@arm.com
6012024Sandreas.sandberg@arm.com        if not callable(func):
6112024Sandreas.sandberg@arm.com            raise RuntimeError("Can't wrap '%s', object is not callable" % \
6212024Sandreas.sandberg@arm.com                               str(func))
6312024Sandreas.sandberg@arm.com
6412024Sandreas.sandberg@arm.com        self._func = func
6512024Sandreas.sandberg@arm.com
6612024Sandreas.sandberg@arm.com    def __call__(self):
6712024Sandreas.sandberg@arm.com        self._func()
6812024Sandreas.sandberg@arm.com
6912024Sandreas.sandberg@arm.com    def __str__(self):
7012024Sandreas.sandberg@arm.com        return "EventWrapper(%s)" % (str(self._func), )
7112024Sandreas.sandberg@arm.com
7212024Sandreas.sandberg@arm.com
735605Snate@binkert.orgclass ProgressEvent(Event):
745605Snate@binkert.org    def __init__(self, eventq, period):
755605Snate@binkert.org        super(ProgressEvent, self).__init__()
764167Sbinkertn@umich.edu        self.period = int(period)
775605Snate@binkert.org        self.eventq = eventq
785605Snate@binkert.org        self.eventq.schedule(self, m5.curTick() + self.period)
794167Sbinkertn@umich.edu
804167Sbinkertn@umich.edu    def __call__(self):
8112563Sgabeblack@google.com        print("Progress! Time now %fs" % (m5.curTick()/1e12))
825605Snate@binkert.org        self.eventq.schedule(self, m5.curTick() + self.period)
835605Snate@binkert.org
8412024Sandreas.sandberg@arm.com
8512024Sandreas.sandberg@arm.comdef create(func, priority=Event.Default_Pri):
8612024Sandreas.sandberg@arm.com    """Create an Event from a function"""
8712024Sandreas.sandberg@arm.com
8812024Sandreas.sandberg@arm.com    return EventWrapper(func, priority=priority)
8912024Sandreas.sandberg@arm.com
9012024Sandreas.sandberg@arm.com__all__ = [ 'Event', 'EventWrapper', 'ProgressEvent', 'SimExit',
9112024Sandreas.sandberg@arm.com            'mainq', 'create' ]
92