Index: openmp/libomptarget/src/device.h =================================================================== --- openmp/libomptarget/src/device.h +++ openmp/libomptarget/src/device.h @@ -42,60 +42,76 @@ /// Map between host data and target data. struct HostDataToTargetTy { - uintptr_t HstPtrBase; // host info. - uintptr_t HstPtrBegin; - uintptr_t HstPtrEnd; // non-inclusive. - map_var_info_t HstPtrName; // Optional source name of mapped variable. + const uintptr_t HstPtrBase; // host info. + const uintptr_t HstPtrBegin; + const uintptr_t HstPtrEnd; // non-inclusive. + const map_var_info_t HstPtrName; // Optional source name of mapped variable. - uintptr_t TgtPtrBegin; // target info. + const uintptr_t TgtPtrBegin; // target info. private: - /// use mutable to allow modification via std::set iterator which is const. - mutable uint64_t RefCount; static const uint64_t INFRefCount = ~(uint64_t)0; - /// This mutex will be locked when data movement is issued. For targets that - /// doesn't support async data movement, this mutex can guarantee that after - /// it is released, memory region on the target is update to date. For targets - /// that support async data movement, this can guarantee that data movement - /// has been issued. This mutex *must* be locked right before releasing the - /// mapping table lock. - std::shared_ptr UpdateMtx; + + struct StatesTy { + StatesTy(uint64_t RC) : RefCount(RC) {} + /// this copy constructor is added to make HostDataToTargetTy copiable + /// when it is used by std::set copy constructor + StatesTy(const StatesTy &S) : RefCount(S.RefCount) {} + /// use mutable to allow modification via std::set iterator which is const. + uint64_t RefCount; + /// This mutex will be locked when data movement is issued. For targets that + /// doesn't support async data movement, this mutex can guarantee that after + /// it is released, memory region on the target is update to date. For + /// targets that support async data movement, this can guarantee that data + /// movement has been issued. This mutex *must* be locked right before + /// releasing the mapping table lock. + std::mutex UpdateMtx; + }; + // When HostDataToTargetTy is used by std::set, std::set::iterator is const + // use unique_ptr to make States mutable. + const std::unique_ptr States; public: HostDataToTargetTy(uintptr_t BP, uintptr_t B, uintptr_t E, uintptr_t TB, map_var_info_t Name = nullptr, bool IsINF = false) : HstPtrBase(BP), HstPtrBegin(B), HstPtrEnd(E), HstPtrName(Name), - TgtPtrBegin(TB), RefCount(IsINF ? INFRefCount : 1), - UpdateMtx(std::make_shared()) {} + TgtPtrBegin(TB), + States(std::make_unique(IsINF ? INFRefCount : 1)) {} + + HostDataToTargetTy(const HostDataToTargetTy &Entry) + : HstPtrBase(Entry.HstPtrBase), HstPtrBegin(Entry.HstPtrBegin), + HstPtrEnd(Entry.HstPtrEnd), HstPtrName(Entry.HstPtrName), + TgtPtrBegin(Entry.TgtPtrBegin), + States(std::make_unique(*Entry.States)) {} - uint64_t getRefCount() const { return RefCount; } + uint64_t getRefCount() const { return States->RefCount; } uint64_t resetRefCount() const { - if (RefCount != INFRefCount) - RefCount = 1; + if (States->RefCount != INFRefCount) + States->RefCount = 1; - return RefCount; + return States->RefCount; } uint64_t incRefCount() const { - if (RefCount != INFRefCount) { - ++RefCount; - assert(RefCount < INFRefCount && "refcount overflow"); + if (States->RefCount != INFRefCount) { + ++States->RefCount; + assert(States->RefCount < INFRefCount && "refcount overflow"); } - return RefCount; + return States->RefCount; } uint64_t decRefCount() const { - if (RefCount != INFRefCount) { - assert(RefCount > 0 && "refcount underflow"); - --RefCount; + if (States->RefCount != INFRefCount) { + assert(States->RefCount > 0 && "refcount underflow"); + --States->RefCount; } - return RefCount; + return States->RefCount; } - bool isRefCountInf() const { return RefCount == INFRefCount; } + bool isRefCountInf() const { return States->RefCount == INFRefCount; } std::string refCountToStr() const { return isRefCountInf() ? "INF" : std::to_string(getRefCount()); @@ -109,9 +125,9 @@ return getRefCount() == 1; } - void lock() const { UpdateMtx->lock(); } + void lock() const { States->UpdateMtx.lock(); } - void unlock() const { UpdateMtx->unlock(); } + void unlock() const { States->UpdateMtx.unlock(); } }; typedef uintptr_t HstPtrBeginTy;