sc_semaphore.cc revision 13198
11689SN/A/*
28707Sandreas.hansson@arm.com * Copyright 2018 Google, Inc.
38707Sandreas.hansson@arm.com *
48707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
58707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
68707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
78707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
88707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
98707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
108707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
118707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
128707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
138707Sandreas.hansson@arm.com * this software without specific prior written permission.
142325SN/A *
157897Shestness@cs.utexas.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
181689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
191689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
201689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
211689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
251689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261689SN/A *
271689SN/A * Authors: Gabe Black
281689SN/A */
291689SN/A
301689SN/A#include "base/logging.hh"
311689SN/A#include "systemc/ext/channel/sc_semaphore.hh"
321689SN/A#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
331689SN/A
341689SN/Anamespace sc_core
351689SN/A{
361689SN/A
371689SN/Asc_semaphore::sc_semaphore(int value) :
381689SN/A        sc_interface(), sc_semaphore_if(),
391689SN/A        sc_object(sc_gen_unique_name("semaphore")), _value(value)
402665Ssaidi@eecs.umich.edu{}
412665Ssaidi@eecs.umich.edu
422756Sksewell@umich.edusc_semaphore::sc_semaphore(const char *name, int value) :
437897Shestness@cs.utexas.edu        sc_interface(), sc_semaphore_if(), sc_object(name), _value(value)
441689SN/A{}
451689SN/A
461858SN/Aint
476658Snate@binkert.orgsc_semaphore::wait()
482733Sktlim@umich.edu{
498229Snate@binkert.org    while (trywait() == -1)
508229Snate@binkert.org        ::sc_core::wait(posted);
518229Snate@binkert.org    return 0;
524762Snate@binkert.org}
534762Snate@binkert.org
544762Snate@binkert.orgint
558232Snate@binkert.orgsc_semaphore::trywait()
568232Snate@binkert.org{
578232Snate@binkert.org    if (!_value)
584762Snate@binkert.org        return -1;
594762Snate@binkert.org
604762Snate@binkert.org    _value--;
618460SAli.Saidi@ARM.com    return 0;
624762Snate@binkert.org}
631858SN/A
642356SN/Aint
651060SN/Asc_semaphore::post()
661060SN/A{
671060SN/A    if (_value++ == 0)
681060SN/A        posted.notify();
692794Sktlim@umich.edu    return 0;
702794Sktlim@umich.edu}
712794Sktlim@umich.edu
722794Sktlim@umich.eduint sc_semaphore::get_value() const { return _value; }
735702Ssaidi@eecs.umich.edu
745702Ssaidi@eecs.umich.edu} // namespace sc_core
758232Snate@binkert.org