112841Sgabeblack@google.com/*
212841Sgabeblack@google.com * Copyright 2018 Google, Inc.
312841Sgabeblack@google.com *
412841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
512841Sgabeblack@google.com * modification, are permitted provided that the following conditions are
612841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
712841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
812841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
912841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1012841Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1112841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1212841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1312841Sgabeblack@google.com * this software without specific prior written permission.
1412841Sgabeblack@google.com *
1512841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012841Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112841Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312841Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512841Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612841Sgabeblack@google.com *
2712841Sgabeblack@google.com * Authors: Gabe Black
2812841Sgabeblack@google.com */
2912841Sgabeblack@google.com
3013199Sgabeblack@google.com#include "systemc/core/scheduler.hh"
3112841Sgabeblack@google.com#include "systemc/ext/channel/sc_mutex.hh"
3212841Sgabeblack@google.com#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
3312841Sgabeblack@google.com
3412841Sgabeblack@google.comnamespace sc_core
3512841Sgabeblack@google.com{
3612841Sgabeblack@google.com
3712841Sgabeblack@google.comsc_mutex::sc_mutex() : sc_interface(), sc_mutex_if(),
3812841Sgabeblack@google.com        sc_object(sc_gen_unique_name("mutex"))
3912841Sgabeblack@google.com{}
4012841Sgabeblack@google.com
4112841Sgabeblack@google.comsc_mutex::sc_mutex(const char *name) :
4212841Sgabeblack@google.com        sc_interface(), sc_mutex_if(), sc_object(name)
4312841Sgabeblack@google.com{}
4412841Sgabeblack@google.com
4512841Sgabeblack@google.comint
4612841Sgabeblack@google.comsc_mutex::lock()
4712841Sgabeblack@google.com{
4813199Sgabeblack@google.com    while (trylock() == -1)
4913199Sgabeblack@google.com        wait(unlockEvent);
5012841Sgabeblack@google.com    return 0;
5112841Sgabeblack@google.com}
5212841Sgabeblack@google.com
5312841Sgabeblack@google.comint
5412841Sgabeblack@google.comsc_mutex::trylock()
5512841Sgabeblack@google.com{
5613199Sgabeblack@google.com    if (holder.valid())
5713199Sgabeblack@google.com        return -1;
5813199Sgabeblack@google.com    holder = ::sc_gem5::scheduler.current();
5912841Sgabeblack@google.com    return 0;
6012841Sgabeblack@google.com}
6112841Sgabeblack@google.com
6212841Sgabeblack@google.comint
6312841Sgabeblack@google.comsc_mutex::unlock()
6412841Sgabeblack@google.com{
6513199Sgabeblack@google.com    if (holder != ::sc_gem5::scheduler.current())
6613199Sgabeblack@google.com        return -1;
6713199Sgabeblack@google.com
6813199Sgabeblack@google.com    holder = nullptr;
6913199Sgabeblack@google.com    unlockEvent.notify();
7012841Sgabeblack@google.com    return 0;
7112841Sgabeblack@google.com}
7212841Sgabeblack@google.com
7312841Sgabeblack@google.com} // namespace sc_core
74