Files
rocm-systems/projects/rocprofiler/src/utils/access_control.h
T
Ammar ELWazir de4abd0d0f Adding rocprofilerv2
Change-Id: Ic0cc280ba207d2b8f6ccae1cd4ac3184152fc1ad


[ROCm/rocprofiler commit: 8032adb64f]
2023-03-09 13:20:33 +00:00

117 строки
4.1 KiB
C++

/* Copyright (c) 2022 Advanced Micro Devices, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#ifndef SRC_UTILS_ACCESS_CONTROL_H_
#define SRC_UTILS_ACCESS_CONTROL_H_
#include <atomic>
#include <iostream>
#include <mutex>
// TODO(aelwazir): To be implemented
// type for reference counter
typedef std::atomic<uint64_t> RocmToolsRefCount;
// type for access/release control flag
typedef std::atomic<bool> RocmToolsARControl;
namespace rocmtools {
// the access/release control flag can be shared or exclusive
static RocmToolsARControl kARShared = ATOMIC_VAR_INIT(0);
static RocmToolsARControl kARExclusive = ATOMIC_VAR_INIT(1);
/*-------------------------------------------------------------------------*/
/**
* @Synopsis A class for defining access mode for any resource and
* maintaining the refernce count for accesses to the resource.
* All the resources must include this class object for corectness
*/
/*--------------------------------------------------------------------------*/
class AccessControl {
public:
//!< constructor that defaults the RefCount and ARControl
AccessControl(RocmToolsRefCount rrc = ATOMIC_VAR_INIT(0),
RocmToolsARControl rac = ATOMIC_VAR_INIT(kARShared.load()));
//!< naive destructor
~AccessControl();
bool Acquire(RocmToolsARControl access_mode);
void Release(RocmToolsARControl access_mode);
private:
RocmToolsRefCount rcount_; //!< refernce count, every successful access
//! increments it
RocmToolsARControl arcontrol_; //!< shared access or exclusive?
std::mutex accessmutex_;
};
//<!
// AccessControl::AccessControl(RocmToolsRefCount rrc, RocmToolsARControl rac)
// {
// rcount_ = rrc.load();
// arcontrol_ = rac.load();
// }
//<!
AccessControl::~AccessControl() {
// we cannot decrement reference count here, we can only hope it is 0
#ifdef ROCMTOOL_DEBUG_PRINT
int zero = 0;
if (rcount_.compare_exchange_strong(zero&, 0, std::memory_order_acq_rel))
cout << endl
<< __FILE__ << __FUNC__ << __LINE__
<< ": reference count not"
" zero"
<< endl;
#endif
}
#if 0
//!< three steps, 1) get scoped lock, 2) check if it is shared to shared
//!< acquire, and 3) if it is exclusive, check if refcount is 0
bool AccessControl::Acquire(RocmToolsARControl rac) {
// first, ensure we have a lock_guard
std::lock_guard<std::mutex> lg(accessmutex_);
// we should always expect the current value to be shared
RocmToolsARControl rac_expected = kARShared.load();
// if what we have is what we expect then we are done
if (rac.load(std::memory_order_relaxed) == rac_expected.load()) {
// rcount_.store(std::memory_order_acq_rel,
// rcount_load(std::memory_order_acq_rel)+1));
return true;
}
// now, we have to switch from shared to exclusive, but we need the refcount
// to be 0. If it is 0, return true, otherwise false
// int zero = 0;
// return rcount_.compare_exchange_strong(zero&, 0,
// std::memory_order_acq_rel);
}
#endif
//!< always exchange to kARShared and reduce refcount
// void AccessControl::Release(
} // namespace rocmtools
#endif // SRC_UTILS_ACCESS_CONTROL_H_