From 29eb5bed4dcac092f1393fab68df6ac5cdbdd290 Mon Sep 17 00:00:00 2001 From: "Oleg.Korshul" Date: Mon, 5 Feb 2024 10:54:54 +0300 Subject: [PATCH] Add new class --- DesktopEditor/graphics/BaseThread.h | 2 + DesktopEditor/graphics/BaseThreadMonitor.cpp | 111 +++++++++++++++++++ DesktopEditor/graphics/BaseThreadMonitor.h | 86 ++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 DesktopEditor/graphics/BaseThreadMonitor.cpp create mode 100644 DesktopEditor/graphics/BaseThreadMonitor.h diff --git a/DesktopEditor/graphics/BaseThread.h b/DesktopEditor/graphics/BaseThread.h index 2a5fa9aec9..820b6bf417 100644 --- a/DesktopEditor/graphics/BaseThread.h +++ b/DesktopEditor/graphics/BaseThread.h @@ -100,6 +100,8 @@ namespace NSThreads #else static void* __ThreadProc(void* pv); #endif + + friend class CBaseThreadMonitor; }; } diff --git a/DesktopEditor/graphics/BaseThreadMonitor.cpp b/DesktopEditor/graphics/BaseThreadMonitor.cpp new file mode 100644 index 0000000000..db88cbb471 --- /dev/null +++ b/DesktopEditor/graphics/BaseThreadMonitor.cpp @@ -0,0 +1,111 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2023 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ + +#include "./BaseThreadMonitor.h" + +namespace NSThreads +{ + CBaseThreadMonitor::CBaseThreadMonitor() + { + m_bIsInit = false; + m_pReceiver = NULL; + m_funcRelease = nullptr; + + m_oCS.InitializeCriticalSection(); + } + + CBaseThreadMonitor::~CBaseThreadMonitor() + { + m_oCS.DeleteCriticalSection(); + } + + bool CBaseThreadMonitor::Init(void* receiver) + { + CTemporaryCS oCS(&m_oCS); + + if (m_bIsInit) + return false; + + m_pReceiver = receiver; + } + + bool CBaseThreadMonitor::Destroy() + { + CTemporaryCS oCS(&m_oCS); + + m_bIsInit = false; + m_pReceiver = NULL; + } + + bool CBaseThreadMonitor::IsInit() + { + CTemporaryCS oCS(&m_oCS); + return m_bIsInit; + } + + void CBaseThreadMonitor::EnterCS() + { + m_oCS.Enter(); + } + void CBaseThreadMonitor::LeaveCS() + { + m_oCS.Leave(); + } + + CBaseThread* CBaseThreadMonitor::GetBaseThread(const ASC_THREAD_ID& nThreadId) + { + return NULL; + } + + void CBaseThreadMonitor::SetReleaseHandler(std::function func) + { + CTemporaryCS oCS(&m_oCS); + m_funcRelease = func; + } + + void CBaseThreadMonitor::Register(CBaseThread* pInstance) + { + m_listThreads.push_back({NSThreads::GetCurrentThreadId(), pInstance}); + } + + void CBaseThreadMonitor::Unregister(CBaseThread* pInstance) + { + for (std::list::iterator i = m_listThreads.begin(); i != m_listThreads.end(); i++) + { + if (i->Instance == pInstance) + { + m_listThreads.erase(i); + return; + } + } + } +} diff --git a/DesktopEditor/graphics/BaseThreadMonitor.h b/DesktopEditor/graphics/BaseThreadMonitor.h new file mode 100644 index 0000000000..a021473a72 --- /dev/null +++ b/DesktopEditor/graphics/BaseThreadMonitor.h @@ -0,0 +1,86 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2023 + * + * This program is a free software product. You can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License (AGPL) + * version 3 as published by the Free Software Foundation. In accordance with + * Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect + * that Ascensio System SIA expressly excludes the warranty of non-infringement + * of any third-party rights. + * + * This program is distributed WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For + * details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html + * + * You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish + * street, Riga, Latvia, EU, LV-1050. + * + * The interactive user interfaces in modified source and object code versions + * of the Program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU AGPL version 3. + * + * Pursuant to Section 7(b) of the License you must retain the original Product + * logo when distributing the program. Pursuant to Section 7(e) we decline to + * grant you any rights under trademark law for use of our trademarks. + * + * All the Product's GUI elements, including illustrations and icon sets, as + * well as technical writing content are licensed under the terms of the + * Creative Commons Attribution-ShareAlike 4.0 International. See the License + * terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + * + */ +#ifndef _BUILD_BASETHREAD_MONITOR_H_ +#define _BUILD_BASETHREAD_MONITOR_H_ + +#include "BaseThread.h" +#include "TemporaryCS.h" +#include +#include + +namespace NSThreads +{ + class CBaseThreadInfo + { + public: + ASC_THREAD_ID ID; + CBaseThread* Instance; + }; + + class CBaseThreadMonitor + { + private: + NSCriticalSection::CRITICAL_SECTION m_oCS; + + bool m_bIsInit; + + void* m_pReceiver; + std::function m_funcRelease; + + std::list m_listThreads; + + private: + CBaseThreadMonitor(); + + public: + static CBaseThreadMonitor& Get(); + ~CBaseThreadMonitor(); + + public: + bool IsInit(); + + bool Init(void* receiver); + bool Destroy(); + + CBaseThread* GetBaseThread(const ASC_THREAD_ID& nThreadId); + void SetReleaseHandler(std::function func); + + void EnterCS(); + void LeaveCS(); + + private: + void Register(CBaseThread* pInstance); + void Unregister(CBaseThread* pInstance); + }; +} + +#endif // _BUILD_BASETHREAD_MONITOR_H_