diff --git a/Common/kernel.pro b/Common/kernel.pro index d58bf72f9f..3aa2c5eca2 100644 --- a/Common/kernel.pro +++ b/Common/kernel.pro @@ -23,8 +23,9 @@ include(../OfficeUtils/OfficeUtils.pri) include(../DesktopEditor/xml/build/qt/libxml2.pri) # DOWNLOADER -HEADERS += ./FileDownloader/FileDownloader.h -HEADERS += ./FileDownloader/FileDownloader_private.h +HEADERS += \ + ./FileDownloader/FileDownloader.h \ + ./FileDownloader/FileDownloader_private.h SOURCES += ./FileDownloader/FileDownloader.cpp @@ -47,3 +48,27 @@ core_mac { LIBS += -framework AppKit } + +# BLOCKER +DEFINES += CRITICALSECTION_USE_DYNAMIC_LIBRARY +HEADERS += \ + ./../DesktopEditor/graphics/TemporaryCS.h + +SOURCES += \ + ./../DesktopEditor/graphics/TemporaryCS.cpp + +# THREAD +DEFINES += BASETHREAD_USE_DYNAMIC_LIBRARY +HEADERS += \ + ./../DesktopEditor/graphics/BaseThread.h + +SOURCES += \ + ./../DesktopEditor/graphics/BaseThread.cpp + +# TIMER +DEFINES += TIMER_USE_DYNAMIC_LIBRARY +HEADERS += \ + ./../DesktopEditor/graphics/Timer.h + +SOURCES += \ + ./../DesktopEditor/graphics/Timer.cpp diff --git a/DesktopEditor/common/StringBuilder.h b/DesktopEditor/common/StringBuilder.h index 879cad0ddd..5130a1d6a6 100644 --- a/DesktopEditor/common/StringBuilder.h +++ b/DesktopEditor/common/StringBuilder.h @@ -33,6 +33,7 @@ #define _BUILD_STRING_BUILDER_CROSSPLATFORM_H_ #include +#include #include #include "Types.h" diff --git a/DesktopEditor/graphics/BaseThread.cpp b/DesktopEditor/graphics/BaseThread.cpp new file mode 100644 index 0000000000..4fdf68d4eb --- /dev/null +++ b/DesktopEditor/graphics/BaseThread.cpp @@ -0,0 +1,208 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2018 + * + * 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 Lubanas st. 125a-25, Riga, Latvia, + * EU, LV-1021. + * + * 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 "./BaseThread.h" + +#if defined(_WIN32) || defined(_WIN64) ||defined(_WIN32_WCE) + +#include + +#else + +#include +#include "time.h" + +#endif + +namespace NSThreads +{ + class CThreadDescriptor + { + public: + CThreadDescriptor() + { + } + virtual ~CThreadDescriptor() + { + } + }; + + ASC_THREAD_ID GetCurrentThreadId() + { +#if defined(_WIN32) || defined (_WIN64) + return ::GetCurrentThreadId(); +#else + return pthread_self(); +#endif + } + + void Sleep(int nMilliseconds) + { +#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) + ::Sleep((DWORD)nMilliseconds); +#else + struct timespec tim, tim2; + tim.tv_sec = nMilliseconds / 1000; + tim.tv_nsec = (nMilliseconds % 1000) * 1000000; + + ::nanosleep(&tim , &tim2); +#endif + } + +#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) + DWORD WINAPI CBaseThread::__ThreadProc(void* pv) + { + CBaseThread* pThis = (CBaseThread*)pv; + return pThis->ThreadProc(); + } + + class __native_thread : public CThreadDescriptor + { + friend class CBaseThread; + private: + HANDLE m_thread; + + public: + __native_thread() : CThreadDescriptor() + { + m_thread = NULL; + } + virtual ~__native_thread() + { + if (m_thread != NULL) + { + CloseHandle(m_thread); + m_thread = NULL; + } + } + }; +#else + void* CBaseThread::__ThreadProc(void* pv) + { + int old_thread_type; + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_thread_type); + + CBaseThread* pThis = (CBaseThread*)pv; + pThis->ThreadProc(); + return NULL; + } + class __native_thread : public CThreadDescriptor + { + friend class CBaseThread; + private: + pthread_t m_thread; + + public: + __native_thread() : CThreadDescriptor() + { + m_thread = NULL; + } + virtual ~__native_thread() + { + } + }; +#endif + + CBaseThread::CBaseThread() + { + m_hThread = NULL; + m_bRunThread = FALSE; + m_bSuspend = FALSE; + + m_lError = 0; + m_lThreadPriority = 0; + } + CBaseThread::~CBaseThread() + { + Stop(); + } + void CBaseThread::Start(int lPriority) + { + if (m_bRunThread) + return; + m_lError = 0; + m_bSuspend = FALSE; + + m_hThread = new __native_thread(); + + m_bRunThread = TRUE; +#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) + DWORD dwTemp; + ((__native_thread*)m_hThread)->m_thread = CreateThread(NULL, 0, &__ThreadProc, (void*)this, 0, &dwTemp); + SetThreadPriority(((__native_thread*)m_hThread)->m_thread, lPriority); +#else + pthread_create(&((__native_thread*)m_hThread)->m_thread, 0, &__ThreadProc, (void*)this); +#endif + m_lThreadPriority = lPriority; + } + void CBaseThread::Suspend() + { + m_bSuspend = TRUE; + } + void CBaseThread::Resume() + { + m_bSuspend = FALSE; + } + void CBaseThread::Stop() + { + if (!m_bRunThread) + return; + m_bRunThread = FALSE; + + Join(); + RELEASEOBJECT(m_hThread); + } + + INT CBaseThread::IsSuspended() { return m_bSuspend; } + INT CBaseThread::IsRunned() { return m_bRunThread; } + int CBaseThread::GetError() { return m_lError; } + + CThreadDescriptor* CBaseThread::GetDescriptor() { return m_hThread; } + int CBaseThread::GetPriority() { return m_lThreadPriority; } + + void CBaseThread::CheckSuspend() + { + while (m_bSuspend && m_bRunThread) + NSThreads::Sleep(10); + } + + void CBaseThread::Join() + { + if (NULL == m_hThread) + return; + +#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) + WaitForSingleObject(((__native_thread*)m_hThread)->m_thread, INFINITE); +#else + pthread_join(((__native_thread*)m_hThread)->m_thread, 0); +#endif + } +} diff --git a/DesktopEditor/graphics/BaseThread.h b/DesktopEditor/graphics/BaseThread.h index 76f69bb98a..ae597e7b7c 100644 --- a/DesktopEditor/graphics/BaseThread.h +++ b/DesktopEditor/graphics/BaseThread.h @@ -34,186 +34,70 @@ #include "../common/Types.h" -#if defined(_WIN32) || defined(_WIN64) ||defined(_WIN32_WCE) - -#include "windows.h" -#include "winbase.h" - +#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) +#include #else - #include -#include -#include "time.h" +#endif +#if defined(_WIN32) || defined (_WIN64) +typedef DWORD ASC_THREAD_ID; +#else +typedef pthread_t ASC_THREAD_ID; +#endif + +#ifndef BASETHREAD_USE_DYNAMIC_LIBRARY +#define BASETHREAD_DECL_EXPORT +#else +#include "../common/base_export.h" +#define BASETHREAD_DECL_EXPORT Q_DECL_EXPORT #endif namespace NSThreads { - class CThreadDescriptor - { - public: - CThreadDescriptor(){} - virtual ~CThreadDescriptor(){} - }; + BASETHREAD_DECL_EXPORT ASC_THREAD_ID GetCurrentThreadId(); - static void Sleep(int nMilliseconds) - { -#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) - ::Sleep((DWORD)nMilliseconds); -#else - struct timespec tim, tim2; - tim.tv_sec = nMilliseconds / 1000; - tim.tv_nsec = (nMilliseconds % 1000) * 1000000; + BASETHREAD_DECL_EXPORT void Sleep(int nMilliseconds); - ::nanosleep(&tim , &tim2); -#endif - } - - class CBaseThread + class CThreadDescriptor; + class BASETHREAD_DECL_EXPORT CBaseThread { protected: CThreadDescriptor* m_hThread; - INT m_bRunThread; - INT m_bSuspend; + INT m_bRunThread; + INT m_bSuspend; - int m_lError; - int m_lThreadPriority; + int m_lError; + int m_lThreadPriority; public: - CBaseThread() - { - m_hThread = NULL; - m_bRunThread = FALSE; - m_bSuspend = FALSE; - - m_lError = 0; - m_lThreadPriority = 0; - } - virtual ~CBaseThread() - { - Stop(); - } + CBaseThread(); + virtual ~CBaseThread(); public: - virtual void Start(int lPriority) - { - if (m_bRunThread) - return; - m_lError = 0; - m_bSuspend = FALSE; + virtual void Start(int lPriority); + virtual void Suspend(); + virtual void Resume(); + virtual void Stop(); - m_hThread = new __native_thread(); + INT IsSuspended(); + INT IsRunned(); + int GetError(); - m_bRunThread = TRUE; -#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) - DWORD dwTemp; - ((__native_thread*)m_hThread)->m_thread = CreateThread(NULL, 0, &__ThreadProc, (void*)this, 0, &dwTemp); - SetThreadPriority(((__native_thread*)m_hThread)->m_thread, lPriority); -#else - pthread_create(&((__native_thread*)m_hThread)->m_thread, 0, &__ThreadProc, (void*)this); -#endif - m_lThreadPriority = lPriority; - } - virtual void Suspend() - { - m_bSuspend = TRUE; - } - virtual void Resume() - { - m_bSuspend = FALSE; - } - virtual void Stop() - { - if (!m_bRunThread) - return; - m_bRunThread = FALSE; - - Join(); - RELEASEOBJECT(m_hThread); - } + CThreadDescriptor* GetDescriptor(); + int GetPriority(); - inline INT IsSuspended() { return m_bSuspend; } - inline INT IsRunned() { return m_bRunThread; } - inline int GetError() { return m_lError; } - - inline CThreadDescriptor* GetDescriptor() { return m_hThread; } - inline int GetPriority() { return m_lThreadPriority; } - - virtual void CheckSuspend() - { - while (m_bSuspend && m_bRunThread) - NSThreads::Sleep(10); - } + virtual void CheckSuspend(); protected: - virtual void Join() - { - if (NULL == m_hThread) - return; - -#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) - WaitForSingleObject(((__native_thread*)m_hThread)->m_thread, INFINITE); -#else - pthread_join(((__native_thread*)m_hThread)->m_thread, 0); -#endif - } + virtual void Join(); virtual DWORD ThreadProc() = 0; - private: - #if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) - static DWORD WINAPI __ThreadProc(void* pv) - { - CBaseThread* pThis = (CBaseThread*)pv; - return pThis->ThreadProc(); - } - - class __native_thread : public NSThreads::CThreadDescriptor - { - friend class CBaseThread; - private: - HANDLE m_thread; - - public: - __native_thread() : CThreadDescriptor() - { - m_thread = NULL; - } - virtual ~__native_thread() - { - if (m_thread != NULL) - { - CloseHandle(m_thread); - m_thread = NULL; - } - } - }; + static DWORD WINAPI __ThreadProc(void* pv); #else - static void* __ThreadProc(void* pv) - { - int old_thread_type; - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_thread_type); - - CBaseThread* pThis = (CBaseThread*)pv; - pThis->ThreadProc(); - return NULL; - } - class __native_thread : public NSThreads::CThreadDescriptor - { - friend class CBaseThread; - private: - pthread_t m_thread; - - public: - __native_thread() : CThreadDescriptor() - { - m_thread = NULL; - } - virtual ~__native_thread() - { - } - }; + static void* __ThreadProc(void* pv); #endif - }; + }; } #endif // _BUILD_BASETHREAD_H_ diff --git a/DesktopEditor/graphics/TemporaryCS.cpp b/DesktopEditor/graphics/TemporaryCS.cpp index 55fc899ca6..8929ad9523 100644 --- a/DesktopEditor/graphics/TemporaryCS.cpp +++ b/DesktopEditor/graphics/TemporaryCS.cpp @@ -32,6 +32,23 @@ #include "TemporaryCS.h" #include "../common/Types.h" +namespace NSCriticalSection +{ + class CRITICAL_SECTION_NATIVE + { + public: + CRITICAL_SECTION_NATIVE() + { + } + virtual ~CRITICAL_SECTION_NATIVE() + { + } + + virtual void Enter() = 0; + virtual void Leave() = 0; + }; +} + #if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) #include "windows.h" diff --git a/DesktopEditor/graphics/TemporaryCS.h b/DesktopEditor/graphics/TemporaryCS.h index 7512e611f3..b786f0c451 100644 --- a/DesktopEditor/graphics/TemporaryCS.h +++ b/DesktopEditor/graphics/TemporaryCS.h @@ -32,19 +32,17 @@ #ifndef _BUILD_TEMPORARY_CS_H_ #define _BUILD_TEMPORARY_CS_H_ +#ifndef CRITICALSECTION_USE_DYNAMIC_LIBRARY +#define CRITICALSECTION_DECL_EXPORT +#else +#include "../common/base_export.h" +#define CRITICALSECTION_DECL_EXPORT Q_DECL_EXPORT +#endif + namespace NSCriticalSection { - class CRITICAL_SECTION_NATIVE - { - public: - CRITICAL_SECTION_NATIVE() {}; - virtual ~CRITICAL_SECTION_NATIVE() {}; - - virtual void Enter() = 0; - virtual void Leave() = 0; - }; - - class CRITICAL_SECTION + class CRITICAL_SECTION_NATIVE; + class CRITICALSECTION_DECL_EXPORT CRITICAL_SECTION { private: CRITICAL_SECTION_NATIVE* m_pCS; @@ -61,7 +59,7 @@ namespace NSCriticalSection }; } -class CTemporaryCS +class CRITICALSECTION_DECL_EXPORT CTemporaryCS { public: CTemporaryCS(NSCriticalSection::CRITICAL_SECTION* cs); @@ -72,4 +70,4 @@ protected: NSCriticalSection::CRITICAL_SECTION* m_cs; }; -#endif // _BUILD_TEMPORARY_CS_H_ \ No newline at end of file +#endif // _BUILD_TEMPORARY_CS_H_ diff --git a/DesktopEditor/graphics/Timer.cpp b/DesktopEditor/graphics/Timer.cpp new file mode 100644 index 0000000000..b332b6ea34 --- /dev/null +++ b/DesktopEditor/graphics/Timer.cpp @@ -0,0 +1,124 @@ +/* + * (c) Copyright Ascensio System SIA 2010-2018 + * + * 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 Lubanas st. 125a-25, Riga, Latvia, + * EU, LV-1021. + * + * 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 "Timer.h" +#include "time.h" + +#ifdef _MAC +#include "mach/mach.h" +#include "mach/mach_time.h" +#endif + +#if defined(_WIN32) || defined (_WIN64) +//#define _CAN_USE_COM_THREADS +#endif + +namespace NSTimers +{ +#ifdef _MAC + static DWORD getUptimeInMilliseconds() + { + const int64_t kOneMillion = 1000 * 1000; + static mach_timebase_info_data_t s_timebase_info; + + if (s_timebase_info.denom == 0) { + (void) mach_timebase_info(&s_timebase_info); + } + + // mach_absolute_time() returns billionth of seconds, + // so divide by one million to get milliseconds + return (DWORD)((mach_absolute_time() * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom)); + } +#endif + + // CLOCK_MONOTONIC defined ONLY since macOS 10.12!!! (crash on earlier version) + DWORD GetTickCount() + { +#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) + return ::GetTickCount(); +#else +#if defined(CLOCK_MONOTONIC) && !defined(_MAC) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + + return (ts.tv_sec * 1000 + (DWORD)(ts.tv_nsec / 1000000)); +#else + //uint64_t nano = mach_absolute_time(); + //return nano / 1000000; + return getUptimeInMilliseconds(); +#endif +#endif + } + + CTimer::CTimer() : NSThreads::CBaseThread() + { + m_dwInterval = 40; + m_bIsCOMNeed = FALSE; + } + CTimer::~CTimer() + { + } + + void CTimer::SetInterval(const DWORD& dwInterval) { m_dwInterval = dwInterval; } + void CTimer::SetCOMNeed(const INT& bIsCOM) { m_bIsCOMNeed = bIsCOM; } + + DWORD CTimer::ThreadProc() + { +#ifdef _CAN_USE_COM_THREADS + if (m_bIsCOMNeed) + CoInitialize(NULL); +#endif + DWORD m_startTime, m_curTime; + m_startTime = NSTimers::GetTickCount(); + + while (m_bRunThread) + { + m_curTime = NSTimers::GetTickCount(); + while (m_curTime - m_startTime < m_dwInterval) + { + NSThreads::Sleep(10); + if (!m_bRunThread) + break; + m_curTime = NSTimers::GetTickCount(); + } + + m_startTime = NSTimers::GetTickCount(); + OnTimer(); + } + +#ifdef _CAN_USE_COM_THREADS + if (m_bIsCOMNeed) + CoUninitialize(); +#endif + return 0; + } +} diff --git a/DesktopEditor/graphics/Timer.h b/DesktopEditor/graphics/Timer.h index 562b2625d2..72b62e1c04 100644 --- a/DesktopEditor/graphics/Timer.h +++ b/DesktopEditor/graphics/Timer.h @@ -33,122 +33,33 @@ #define _BUILD_TIMER_H_ #include "BaseThread.h" -#include "time.h" -#ifdef _MAC -#include "mach/mach.h" -#include "mach/mach_time.h" -#endif - -#if defined(_WIN32) || defined (_WIN64) -//#define _CAN_USE_COM_THREADS -#endif - -#if defined(_WIN32) || defined (_WIN64) -typedef DWORD ASC_THREAD_ID; +#ifndef TIMER_USE_DYNAMIC_LIBRARY +#define TIMER_DECL_EXPORT #else -typedef pthread_t ASC_THREAD_ID; +#include "../common/base_export.h" +#define TIMER_DECL_EXPORT Q_DECL_EXPORT #endif -namespace NSThreads -{ - static ASC_THREAD_ID GetCurrentThreadId() - { -#if defined(_WIN32) || defined (_WIN64) - return ::GetCurrentThreadId(); -#else - return pthread_self(); -#endif - } -} - namespace NSTimers { -#ifdef _MAC - static DWORD getUptimeInMilliseconds() - { - const int64_t kOneMillion = 1000 * 1000; - static mach_timebase_info_data_t s_timebase_info; - - if (s_timebase_info.denom == 0) { - (void) mach_timebase_info(&s_timebase_info); - } - - // mach_absolute_time() returns billionth of seconds, - // so divide by one million to get milliseconds - return (DWORD)((mach_absolute_time() * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom)); - } -#endif - - // CLOCK_MONOTONIC defined ONLY since macOS 10.12!!! (crash on earlier version) - static DWORD GetTickCount() - { -#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) - return ::GetTickCount(); -#else -#if defined(CLOCK_MONOTONIC) && !defined(_MAC) - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + TIMER_DECL_EXPORT DWORD GetTickCount(); - return (ts.tv_sec * 1000 + (DWORD)(ts.tv_nsec / 1000000)); -#else - //uint64_t nano = mach_absolute_time(); - //return nano / 1000000; - return getUptimeInMilliseconds(); -#endif -#endif - } - - class CTimer : public NSThreads::CBaseThread + class TIMER_DECL_EXPORT CTimer : public NSThreads::CBaseThread { private: DWORD m_dwInterval; - INT m_bIsCOMNeed; + INT m_bIsCOMNeed; public: - CTimer() : NSThreads::CBaseThread() - { - m_dwInterval = 40; - m_bIsCOMNeed = FALSE; - } - virtual ~CTimer() - { - } + CTimer(); + virtual ~CTimer(); - inline void SetInterval(const DWORD& dwInterval) { m_dwInterval = dwInterval; } - inline void SetCOMNeed(const INT& bIsCOM) { m_bIsCOMNeed = bIsCOM; } + void SetInterval(const DWORD& dwInterval); + void SetCOMNeed(const INT& bIsCOM); protected: - virtual DWORD ThreadProc() - { -#ifdef _CAN_USE_COM_THREADS - if (m_bIsCOMNeed) - CoInitialize(NULL); -#endif - DWORD m_startTime, m_curTime; - m_startTime = NSTimers::GetTickCount(); - - while (m_bRunThread) - { - m_curTime = NSTimers::GetTickCount(); - while (m_curTime - m_startTime < m_dwInterval) - { - NSThreads::Sleep(10); - if (!m_bRunThread) - break; - m_curTime = NSTimers::GetTickCount(); - } - - m_startTime = NSTimers::GetTickCount(); - OnTimer(); - } - -#ifdef _CAN_USE_COM_THREADS - if (m_bIsCOMNeed) - CoUninitialize(); -#endif - return 0; - } + virtual DWORD ThreadProc(); virtual void OnTimer() = 0; };