Changeset 482 for trunk/Common/alax.info/roatlcom.h
- Timestamp:
- Jul 2, 2015, 1:00:57 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/alax.info/roatlcom.h
r321 r482 1 1 //////////////////////////////////////////////////////////// 2 // Copyright (C) Roman Ryltsov, 2006-2014 2 // Template class library; extends Widnows SDK, ATL, WTL 3 // 4 // Copyright (C) Roman Ryltsov, 2006-2015 3 5 // Created by Roman Ryltsov roman@alax.info 4 6 // 5 // A permission to use the source code is granted as long as reference to6 // source website http://alax.info is retained.7 // A permission to re-use this source code is granted as long as copyright notice and 8 // reference to source website http://alax.info is retained. 7 9 8 10 #pragma once … … 15 17 #include "roatlcollections.h" 16 18 19 //////////////////////////////////////////////////////////// 20 // Version 21 17 22 #if !defined(_WIN32_WINNT_WIN7) 18 23 #define _WIN32_WINNT_WIN7 0x0601 19 24 #endif // !defined(_WIN32_WINNT_WIN7) 25 26 #if !defined(_versionhelpers_H_INCLUDED_) 27 28 inline bool IsWindowsVistaOrGreater() 29 { 30 OSVERSIONINFOEX VersionInformation = { sizeof VersionInformation }; 31 GetVersionEx((OSVERSIONINFO*) &VersionInformation); 32 return VersionInformation.dwMajorVersion >= 6; // Windows Vista, Windows Server 2008 33 } 34 35 #endif // !defined(_versionhelpers_H_INCLUDED_) 20 36 21 37 //////////////////////////////////////////////////////////// … … 367 383 public: 368 384 // CObjectPtrT 369 CLocalObjectPtr() throw()385 CLocalObjectPtr() 370 386 { 371 387 m_Instance.SetVoid(NULL); 372 388 m_Instance.InternalFinalConstructAddRef(); 373 #if _MSC_VER >= 1400374 ATLVERIFY(SUCCEEDED(m_Instance._AtlInitialConstruct()));375 #endif // _MSC_VER >= 1400389 #if _MSC_VER >= 1400 390 ATLVERIFY(SUCCEEDED(m_Instance._AtlInitialConstruct())); 391 #endif // _MSC_VER >= 1400 376 392 ATLVERIFY(SUCCEEDED(m_Instance.FinalConstruct())); 377 393 ATLVERIFY(SUCCEEDED(m_Instance._AtlFinalConstruct())); … … 379 395 m_Instance.InternalAddRef(); 380 396 } 381 ~CLocalObjectPtr() throw()382 { 383 } 384 operator const _Object* () const throw()397 ~CLocalObjectPtr() 398 { 399 } 400 operator const _Object* () const 385 401 { 386 402 return &m_Instance; 387 403 } 388 operator _Object* () throw()404 operator _Object* () 389 405 { 390 406 return &m_Instance; 391 407 } 392 _Object* operator -> () throw()408 _Object* operator -> () 393 409 { 394 410 return &m_Instance; … … 654 670 // CThreadT 655 671 656 template <typename _Owner>672 template <typename COwner> 657 673 class ATL_NO_VTABLE CThreadT : 658 674 public CComObjectRootEx<CComMultiThreadModelNoCS>, … … 660 676 { 661 677 protected: 662 typedef CThreadT< _Owner> CThread;678 typedef CThreadT<COwner> CThread; 663 679 664 680 public: … … 668 684 669 685 public: 670 typedef DWORD (_Owner::*OWNERTHREADPROC)(CThread* pThread, CEvent& InitializationEvent, CEvent& TerminationEvent); 686 typedef DWORD (COwner::*OWNERTHREADPROC)(CThread* pThread, CEvent& InitializationEvent, CEvent& TerminationEvent); 687 688 //////////////////////////////////////////////////////// 689 // CMessageQueue 690 691 class CMessageQueue 692 { 693 public: 694 // CMessageQueue 695 CMessageQueue() 696 { 697 MSG Message; 698 PeekMessage(&Message, NULL, WM_USER, WM_USER, PM_NOREMOVE); 699 } 700 BOOL Wait(DWORD nTimeoutTime = INFINITE) 701 { 702 const DWORD nWaitResult = MsgWaitForMultipleObjects(0, NULL, FALSE, nTimeoutTime, QS_POSTMESSAGE); 703 _Z5_WAITRESULT(nWaitResult); 704 _A(nWaitResult == WAIT_OBJECT_0 || nWaitResult == WAIT_TIMEOUT && nTimeoutTime != INFINITE); 705 return nWaitResult == WAIT_OBJECT_0; 706 } 707 BOOL Peek(UINT& nMessage) 708 { 709 MSG Message; 710 if(!PeekMessage(&Message, NULL, 0, 0, PM_REMOVE)) 711 return FALSE; 712 nMessage = Message.message; 713 return TRUE; 714 } 715 static VOID Post(DWORD nIdentifier, UINT nMessage, WPARAM wParam = 0, LPARAM lParam = 0) 716 { 717 _A(nIdentifier); 718 _W(PostThreadMessage(nIdentifier, nMessage, wParam, lParam)); 719 } 720 }; 671 721 672 722 private: 673 _Owner* m_pOwner;723 COwner* m_pOwner; 674 724 OWNERTHREADPROC m_pOwnerThreadProc; 675 725 CEvent m_InitializationEvent; 676 726 CEvent m_TerminationEvent; 677 CHandle m_ThreadHandle; 678 DWORD m_nThreadIdentifier; 679 DWORD m_nThreadExitCode; 680 681 DWORD ThreadProc() throw() 727 CHandle m_Handle; 728 DWORD m_nIdentifier; 729 730 DWORD ThreadProc() 682 731 { 683 732 DWORD nResult; 684 733 _ATLTRY 685 734 { 686 ATLTRACE(atlTraceGeneral, 3, _T(" Thread %d (0x%x) has been started\n"), GetCurrentThreadId(), GetCurrentThreadId());735 ATLTRACE(atlTraceGeneral, 3, _T("Starting thread %d (0x%X)\n"), GetCurrentThreadId(), GetCurrentThreadId()); 687 736 ATLASSERT(m_pOwner && m_pOwnerThreadProc); 688 737 nResult = (m_pOwner->*m_pOwnerThreadProc)(this, m_InitializationEvent, m_TerminationEvent); 689 ATLTRACE(atlTraceGeneral, 3, _T(" Thread %d (0x%x) is being completed with result %d\n"), GetCurrentThreadId(), GetCurrentThreadId(), nResult);738 ATLTRACE(atlTraceGeneral, 3, _T("Stopping thread %d (0x%X), result %d (0x%X)\n"), GetCurrentThreadId(), GetCurrentThreadId(), nResult, nResult); 690 739 } 691 740 _ATLCATCH(Exception) 692 741 { 693 742 //_Z_EXCEPTION(); 694 ATLTRACE(atlTraceException, 1, _T("An exception 0x%08X has been caught in thread %d \n"), Exception.m_hr, GetCurrentThreadId());695 nResult = Exception.m_hr;743 ATLTRACE(atlTraceException, 1, _T("An exception 0x%08X has been caught in thread %d (0x%X)\n"), (HRESULT) Exception, GetCurrentThreadId(), GetCurrentThreadId()); 744 nResult = (HRESULT) Exception; 696 745 } 697 746 _ATLCATCHALL() 698 747 { 699 748 //_Z_EXCEPTION(); 700 ATLTRACE(atlTraceException, 1, _T("An exception has been caught in thread %d \n"), GetCurrentThreadId());749 ATLTRACE(atlTraceException, 1, _T("An exception has been caught in thread %d (0x%X)\n"), GetCurrentThreadId(), GetCurrentThreadId()); 701 750 nResult = E_FAIL; 702 751 } 703 752 return nResult; 704 753 } 705 static DWORD WINAPI ThreadProc(CThreadT* pThread) throw()754 static DWORD WINAPI ThreadProc(CThreadT* pThread) 706 755 { 707 756 return pThread->ThreadProc(); … … 710 759 public: 711 760 // CThreadT 712 CThreadT() throw():761 CThreadT() : 713 762 m_pOwner(NULL), 714 763 m_pOwnerThreadProc(NULL), 715 764 m_InitializationEvent(NULL, TRUE, FALSE, NULL), 716 765 m_TerminationEvent(NULL, TRUE, FALSE, NULL), 717 m_nThreadIdentifier(0), 718 m_nThreadExitCode(0) 766 m_nIdentifier(0) 719 767 { 720 768 ATLTRACE(atlTraceRefcount, 4, _T("this 0x%p\n"), this); 721 769 } 722 ~CThreadT() throw()770 ~CThreadT() 723 771 { 724 772 ATLTRACE(atlTraceRefcount, 4, _T("this 0x%p\n"), this); 725 773 Terminate(); 726 774 } 727 const CHandle& GetHandle() const throw() 728 { 729 return m_ThreadHandle; 730 } 731 DWORD GetIdentifier() const throw() 732 { 733 return m_nThreadIdentifier; 734 } 735 DWORD GetExitCode() const throw() 736 { 737 // TODO: An option to wait for exit code 738 return m_nThreadExitCode; 739 } 740 VOID Initialize(_Owner* pOwner, OWNERTHREADPROC pOwnerThreadProc, CEvent** ppInitializationEvent) 775 const CHandle& GetHandle() const 776 { 777 return m_Handle; 778 } 779 DWORD GetIdentifier() const 780 { 781 return m_nIdentifier; 782 } 783 BOOL GetExitCode(DWORD& nExitCode) const 784 { 785 return GetExitCodeThread(m_Handle, &nExitCode); 786 } 787 BOOL IsStillActive() const 788 { 789 if(!m_Handle) 790 return FALSE; 791 DWORD nExitCode; 792 __E(GetExitCode(nExitCode)); 793 return nExitCode == STILL_ACTIVE; 794 } 795 VOID Initialize(COwner* pOwner, OWNERTHREADPROC pOwnerThreadProc, CEvent** ppInitializationEvent) 741 796 { 742 797 ATLASSERT(!m_pOwner && pOwner); … … 744 799 m_pOwner = pOwner; 745 800 m_pOwnerThreadProc = pOwnerThreadProc; 746 m_nThreadExitCode = 0;747 801 ATLASSERT(WaitForSingleObject(m_TerminationEvent, 0) == WAIT_TIMEOUT); 748 m_ThreadHandle.Attach(CreateThreadT<CThreadT>(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, &m_nThreadIdentifier)); 749 ATLWINCHECK(m_ThreadHandle); 750 ATLASSERT(m_nThreadIdentifier); 751 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d (0x%x) has been started from thread %d\n"), m_nThreadIdentifier, m_nThreadIdentifier, GetCurrentThreadId()); 752 ResumeThread(m_ThreadHandle); 802 static const DWORD g_nFlags = 0; //CREATE_SUSPENDED; 803 m_Handle.Attach(CreateThreadT<CThreadT>(NULL, 0, ThreadProc, this, g_nFlags, &m_nIdentifier)); 804 ATLWINCHECK(m_Handle); 805 ATLASSERT(m_nIdentifier); 806 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d (0x%X) started from thread %d (0x%X)\n"), m_nIdentifier, m_nIdentifier, GetCurrentThreadId(), GetCurrentThreadId()); 807 //ResumeThread(m_Handle); 753 808 *ppInitializationEvent = &m_InitializationEvent; 754 809 } 755 BOOL Initialize( _Owner* pOwner, OWNERTHREADPROC pOwnerThreadProc)810 BOOL Initialize(COwner* pOwner, OWNERTHREADPROC pOwnerThreadProc) 756 811 { 757 812 CEvent* pInitializationEvent; 758 813 Initialize(pOwner, pOwnerThreadProc, &pInitializationEvent); 759 814 ATLASSERT(pInitializationEvent == &m_InitializationEvent); 760 HANDLE phObjects[] = { m_ThreadHandle, m_InitializationEvent };761 DWORD nWaitResult = WaitForMultipleObjects(_countof(phObjects), phObjects, FALSE, INFINITE);815 const HANDLE phObjects[] = { m_Handle, m_InitializationEvent }; 816 const DWORD nWaitResult = WaitForMultipleObjects(_countof(phObjects), phObjects, FALSE, INFINITE); 762 817 ATLASSERT((nWaitResult - WAIT_OBJECT_0) < _countof(phObjects)); 763 818 if(nWaitResult == WAIT_OBJECT_0 + 0) 764 819 { 765 ATLVERIFY(GetExitCodeThread(m_ThreadHandle, &m_nThreadExitCode)); 766 m_ThreadHandle.Close(); 767 SetLastError(m_nThreadExitCode); 820 DWORD nExitCode; 821 ATLVERIFY(GetExitCodeThread(m_Handle, &nExitCode)); 822 m_Handle.Close(); 823 SetLastError(nExitCode); 768 824 return FALSE; 769 825 } 770 826 return TRUE; 771 827 } 772 BOOL Terminate(ULONG nTimeout = INFINITE, BOOL bForce Termination = TRUE) throw()773 { 774 if(!m_ ThreadHandle)828 BOOL Terminate(ULONG nTimeout = INFINITE, BOOL bForce = TRUE) 829 { 830 if(!m_Handle) 775 831 return FALSE; 776 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d (0x% x) is being terminated from thread %d, nTimeout %d, bForceTermination %d\n"), m_nThreadIdentifier, m_nThreadIdentifier, GetCurrentThreadId(), nTimeout, bForceTermination);832 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d (0x%X) is being terminated from thread %d (0x%X), nTimeout %d, bForce %d\n"), m_nIdentifier, m_nIdentifier, GetCurrentThreadId(), GetCurrentThreadId(), nTimeout, bForce); 777 833 ATLVERIFY(m_TerminationEvent.Set()); 778 if(GetCurrentThreadId() != m_n ThreadIdentifier)779 { 780 const DWORD nWaitResult = WaitForSingleObject(m_ ThreadHandle, nTimeout);781 ATLTRACE(atlTraceGeneral, 4, _T("m_n ThreadIdentifier %d, nWaitResult %d\n"), m_nThreadIdentifier, nWaitResult);834 if(GetCurrentThreadId() != m_nIdentifier) 835 { 836 const DWORD nWaitResult = WaitForSingleObject(m_Handle, nTimeout); 837 ATLTRACE(atlTraceGeneral, 4, _T("m_nIdentifier %d (0x%X), nWaitResult 0x%X\n"), m_nIdentifier, m_nIdentifier, nWaitResult); 782 838 ATLASSERT(nWaitResult == WAIT_OBJECT_0 || nWaitResult == WAIT_TIMEOUT && nTimeout != INFINITE); 783 839 if(nWaitResult != WAIT_OBJECT_0) 784 840 { 785 if(!bForce Termination)841 if(!bForce) 786 842 return FALSE; 787 ATLTRACE(atlTraceGeneral, 2, _T("Thread %d (0x% x) is being forcefully terminated from thread %d\n"), m_nThreadIdentifier, m_nThreadIdentifier, GetCurrentThreadId());788 ATLVERIFY(TerminateThread(m_ ThreadHandle, (DWORD) E_FAIL));843 ATLTRACE(atlTraceGeneral, 2, _T("Thread %d (0x%X) is being forcefully terminated from thread %d (0x%X)\n"), m_nIdentifier, m_nIdentifier, GetCurrentThreadId(), GetCurrentThreadId()); 844 ATLVERIFY(TerminateThread(m_Handle, (DWORD) HRESULT_FROM_WIN32(ERROR_TIMEOUT))); 789 845 } 790 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d (0x%x) has been terminated from thread %d\n"), m_n ThreadIdentifier, m_nThreadIdentifier, GetCurrentThreadId());846 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d (0x%x) has been terminated from thread %d\n"), m_nIdentifier, m_nIdentifier, GetCurrentThreadId()); 791 847 } else 792 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d is self-terminated, no need to synchronize\n"), m_nThreadIdentifier);793 m_ ThreadHandle.Close();848 ATLTRACE(atlTraceGeneral, 3, _T("Thread %d (0x%X) is self-terminated\n"), m_nIdentifier, m_nIdentifier); 849 m_Handle.Close(); 794 850 return TRUE; 795 851 } 796 VOID AsynchronousTerminate() throw()852 VOID AsynchronousTerminate() 797 853 { 798 854 ATLVERIFY(m_TerminationEvent.Set()); 855 } 856 VOID PostMessage(UINT nMessage, WPARAM wParam = 0, LPARAM lParam = 0) 857 { 858 _A(m_Handle); 859 _W(PostThreadMessage(m_nIdentifier, nMessage, wParam, lParam)); 799 860 } 800 861 }; … … 802 863 //////////////////////////////////////////////////////// 803 864 // CTimedEventDaemon 865 866 // TODO: Clean up tracing 867 #if _MSC_VER >= 1800 // Visual Studio 2013 868 #if defined(_DEBUG) 869 #define atlTraceTimedEvent 0 // Disabled for stock ATLTRACE 870 #else 871 #define atlTraceTimedEvent ((INT) -1) // Disabled for my ATLTRACE 872 #endif 873 #else 874 #define atlTraceTimedEvent atlTraceCore 875 #endif 804 876 805 877 class ATL_NO_VTABLE CTimedEventDaemon : … … 834 906 public: 835 907 // CTimedEvent 836 CTimedEvent() throw()837 { 838 } 839 CTimedEvent(ULONG nDelay, ULONG nResolution, TIMECALLBACK* pTimeProc, DWORD_PTR nUser, UINT nFlags) throw():908 CTimedEvent() 909 { 910 } 911 CTimedEvent(ULONG nDelay, ULONG nResolution, TIMECALLBACK* pTimeProc, DWORD_PTR nUser, UINT nFlags) : 840 912 m_nDelay(nDelay), 841 913 m_nResolution(nResolution), … … 858 930 public: 859 931 // CTimedEventList 860 BOOL IsValidPosition(POSITION Position) const throw()932 BOOL IsValidPosition(POSITION Position) const 861 933 { 862 934 for(POSITION CurrentPosition = GetHeadPosition(); CurrentPosition; GetNext(CurrentPosition)) … … 865 937 return FALSE; 866 938 } 867 POSITION GetEarliestActivePosition() const throw()939 POSITION GetEarliestActivePosition() const 868 940 { 869 941 POSITION BestPosition = NULL; … … 888 960 static BOOL g_bDefaultTimedEventDaemonTerminated; 889 961 static CTimedEventDaemon* g_pDefaultTimedEventDaemon; 890 #if _DEVELOPMENT891 DWORD m_nSignature;892 #endif // _DEVELOPMENT962 #if _DEVELOPMENT 963 DWORD m_nSignature; 964 #endif // _DEVELOPMENT 893 965 mutable CComAutoCriticalSection m_DataCriticalSection; 894 966 CTimedEventList m_TimedEventList; 895 967 CEvent m_ListChangeEvent; 896 #if defined(_WINDLL)897 CEvent m_ThreadPreTerminationEvent;898 #endif // defined(_WINDLL)968 #if defined(_WINDLL) 969 CEvent m_ThreadPreTerminationEvent; 970 #endif // defined(_WINDLL) 899 971 CObjectPtr<CThread> m_pThread; 900 972 CEvent m_IdlenessEvent; 901 973 902 static VOID STDMETHODCALLTYPE DestroyDefaultTimedEventDaemon(DWORD_PTR nParameter) throw()974 static VOID STDMETHODCALLTYPE DestroyDefaultTimedEventDaemon(DWORD_PTR nParameter) 903 975 { 904 976 _ATLTRY … … 911 983 if(g_pDefaultTimedEventDaemon) 912 984 { 913 #if defined(_DEBUG) || _TRACE914 // NOTE: Add a lock removed by timeSetEvent915 _pAtlModule->Lock();916 #endif // defined(_DEBUG) || _TRACE985 #if defined(_DEBUG) || _TRACE 986 // NOTE: Add a lock removed by timeSetEvent 987 _pAtlModule->Lock(); 988 #endif // defined(_DEBUG) || _TRACE 917 989 pTimedEventDaemon.Swap(&g_pDefaultTimedEventDaemon); 918 990 } … … 941 1013 { 942 1014 const DWORD nWaitResult = WaitForMultipleObjects(DIM(phObjects), phObjects, FALSE, INFINITE); 943 ATLTRACE(atlTraceSync, 5, _T("nWaitResult 0x%x\n"),nWaitResult);1015 _Z5_WAITRESULT(nWaitResult); 944 1016 _A((nWaitResult - WAIT_OBJECT_0) < DIM(phObjects)); 945 1017 if(nWaitResult != WAIT_OBJECT_0 + 1) // m_ListChangeEvent … … 953 1025 CurrentPosition = m_TimedEventList.GetEarliestActivePosition(); 954 1026 _W(m_ListChangeEvent.Reset()); 955 ATLTRACE(atlTraceCore, 4, _T("%d timed events on the list\n"), m_TimedEventList.GetCount());1027 _Z4(atlTraceTimedEvent, 4, _T("%d timed events on the list\n"), m_TimedEventList.GetCount()); 956 1028 if(!CurrentPosition) 957 1029 { 958 ATLTRACE(atlTraceCore, 4, _T("No more active timed events found\n"));1030 _Z4(atlTraceTimedEvent, 4, _T("No more active timed events found\n")); 959 1031 break; 960 1032 } … … 963 1035 } 964 1036 const DWORD nWaitResult = (nDelay > 0) ? WaitForMultipleObjects(DIM(phObjects), phObjects, FALSE, nDelay) : WAIT_TIMEOUT; 965 ATLTRACE(atlTraceSync, 5, _T("nWaitResult 0x%x\n"),nWaitResult);1037 _Z5_WAITRESULT(nWaitResult); 966 1038 _A((nWaitResult - WAIT_OBJECT_0) < DIM(phObjects) || nWaitResult == WAIT_TIMEOUT); 967 1039 if(nWaitResult == WAIT_OBJECT_0 + 0) // Termination … … 969 1041 if(nWaitResult == WAIT_OBJECT_0 + 1) // m_ListChangeEvent 970 1042 { 971 ATLTRACE(atlTraceCore, 4, _T("Timed event list changed\n"));1043 _Z4(atlTraceTimedEvent, 4, _T("Timed event list changed\n")); 972 1044 continue; 973 1045 } … … 978 1050 if(!m_TimedEventList.IsValidPosition(CurrentPosition)) 979 1051 continue; 980 ATLTRACE(atlTraceCore, 4, _T("Firing an event, CurrentPosition 0x%08X\n"), CurrentPosition);1052 _Z4(atlTraceTimedEvent, 4, _T("Firing an event, CurrentPosition 0x%p\n"), CurrentPosition); 981 1053 CTimedEvent& CurrentTimedEvent = m_TimedEventList.GetAt(CurrentPosition); 982 1054 CurrentTimedEvent.m_nTime = GetTickCount(); … … 1014 1086 public: 1015 1087 // CTimedEventDaemon 1016 static CObjectPtr<CTimedEventDaemon> GetDefaultTimedEventDaemon() throw()1088 static CObjectPtr<CTimedEventDaemon> GetDefaultTimedEventDaemon() 1017 1089 { 1018 1090 CObjectPtr<CTimedEventDaemon> pTimedEventDaemon; … … 1028 1100 _pAtlModule->AddTermFunc(DestroyDefaultTimedEventDaemon, (DWORD_PTR) (CTimedEventDaemon*) pTimedEventDaemon); 1029 1101 g_pDefaultTimedEventDaemon = pTimedEventDaemon.Detach(); 1030 #if defined(_DEBUG) || _TRACE1031 // NOTE: Exclude object's module lock so that it does not affect termination lock count report1032 _pAtlModule->Unlock();1033 #endif // defined(_DEBUG) || _TRACE1102 #if defined(_DEBUG) || _TRACE 1103 // NOTE: Exclude object's module lock so that it does not affect termination lock count report 1104 _pAtlModule->Unlock(); 1105 #endif // defined(_DEBUG) || _TRACE 1034 1106 } 1035 1107 _ATLCATCHALL() … … 1042 1114 return pTimedEventDaemon; 1043 1115 } 1044 CTimedEventDaemon() throw():1116 CTimedEventDaemon() : 1045 1117 m_ListChangeEvent(TRUE, FALSE), 1046 1118 m_IdlenessEvent(TRUE, TRUE) 1047 1119 { 1048 ATLTRACE(atlTraceRefcount, 4, _T("this 0x%p\n"), this);1049 #if _DEVELOPMENT1050 // WARN: CTimedEventDaemon has unresolved problems with late termination, so1051 // we mark used memory block to distinguish in head trace1052 m_nSignature = 0x89674523;1053 #endif // _DEVELOPMENT1054 } 1055 ~CTimedEventDaemon() throw()1056 { 1057 ATLTRACE(atlTraceRefcount, 4, _T("this 0x%p\n"), this);1120 _Z4_THIS(); 1121 #if _DEVELOPMENT 1122 // WARN: CTimedEventDaemon has unresolved problems with late termination, so 1123 // we mark used memory block to distinguish in head trace 1124 m_nSignature = 0x89674523; 1125 #endif // _DEVELOPMENT 1126 } 1127 ~CTimedEventDaemon() 1128 { 1129 _Z4_THIS(); 1058 1130 _A(!m_pThread); 1059 1131 } … … 1067 1139 __E(m_pThread.Construct()->Initialize(this, &CTimedEventDaemon::ThreadProc)); 1068 1140 } 1069 VOID Terminate() throw()1141 VOID Terminate() 1070 1142 { 1071 1143 CObjectPtr<CThread> pThread; … … 1087 1159 const HANDLE phObjects[] = { ThreadPreTerminationEvent, Thread }; 1088 1160 const DWORD nWaitResult = WaitForMultipleObjects(DIM(phObjects), phObjects, FALSE, INFINITE); 1089 _Z5 (atlTraceSync, 5, _T("nWaitResult 0x%x\n"),nWaitResult);1161 _Z5_WAITRESULT(nWaitResult); 1090 1162 _A(nWaitResult - WAIT_OBJECT_0 < DIM(phObjects)); 1091 1163 return; … … 1095 1167 } 1096 1168 } 1097 HRESULT SetEvent(ULONG nDelay, ULONG nResolution, TIMECALLBACK* pTimeProc, DWORD_PTR nUser, UINT nFlags, UINT_PTR* pnEvent) throw()1169 HRESULT SetEvent(ULONG nDelay, ULONG nResolution, TIMECALLBACK* pTimeProc, DWORD_PTR nUser, UINT nFlags, UINT_PTR* pnEvent) 1098 1170 { 1099 1171 _ATLTRY … … 1105 1177 _A(pnEvent); 1106 1178 *pnEvent = (UINT_PTR) Position; 1107 ATLTRACE(atlTraceCore, 4, _T("An event has been set, Position 0x%08X, nDelay %d, nFlags 0x%x, %d events on the list\n"), Position, nDelay, nFlags, m_TimedEventList.GetCount());1179 _Z4(atlTraceTimedEvent, 4, _T("An event has been set, Position 0x%p, nDelay %d, nFlags 0x%X, %d events on the list\n"), Position, nDelay, nFlags, m_TimedEventList.GetCount()); 1108 1180 } 1109 1181 _ATLCATCH(Exception) … … 1113 1185 return S_OK; 1114 1186 } 1115 HRESULT KillEvent(UINT_PTR nEvent) throw()1187 HRESULT KillEvent(UINT_PTR nEvent) 1116 1188 { 1117 1189 BOOL bConcurrentlyActive = FALSE; … … 1119 1191 CComCritSecLock<CComAutoCriticalSection> DataLock(m_DataCriticalSection); 1120 1192 const POSITION Position = (POSITION) nEvent; 1121 ATLTRACE(atlTraceCore, 4, _T("An event is being killed, Position 0x%08X\n"), Position);1193 _Z4(atlTraceTimedEvent, 4, _T("An event is being killed, Position 0x%p\n"), Position); 1122 1194 _D(m_TimedEventList.IsValidPosition(Position), E_INVALIDARG); 1123 1195 if(m_TimedEventList.GetAt(Position).m_bActive) … … 1126 1198 m_TimedEventList.RemoveAt(Position); 1127 1199 _W(m_ListChangeEvent.Set()); 1128 ATLTRACE(atlTraceCore, 4, _T("An event has been killed, Position 0x%08X, %d more events on the list\n"), Position, m_TimedEventList.GetCount());1200 _Z4(atlTraceTimedEvent, 4, _T("An event has been killed, Position 0x%p, %d more events on the list\n"), Position, m_TimedEventList.GetCount()); 1129 1201 } 1130 1202 if(bConcurrentlyActive) 1131 1203 { 1132 static const DWORD g_nTimeout = 5000; 1133 DWORD nWaitResult = WaitForSingleObject(m_IdlenessEvent, g_nTimeout); 1204 static const DWORD g_nTimeout = 5 * 1000; // 5 seconds 1205 const DWORD nWaitResult = WaitForSingleObject(m_IdlenessEvent, g_nTimeout); 1206 //_Z5_WAITRESULT(nWaitResult); 1134 1207 _A(nWaitResult == WAIT_OBJECT_0 || nWaitResult == WAIT_TIMEOUT); 1135 1208 if(nWaitResult != WAIT_OBJECT_0) 1136 ATLTRACE(atlTraceGeneral, 2, _T("Worker thread failed to get idle within reasonable time, nWaitResult %d\n"), nWaitResult);1209 _Z2(atlTraceTimedEvent, 2, _T("Worker thread failed to get idle within reasonable time, nWaitResult 0x%X\n"), nWaitResult); 1137 1210 } 1138 1211 return S_OK; … … 1140 1213 1141 1214 // Standard API substitution 1142 static MMRESULT timeSetEvent(UINT nDelay, UINT nResolution, TIMECALLBACK* pTimeProc, DWORD_PTR nUser, UINT nFlags) throw()1215 static MMRESULT timeSetEvent(UINT nDelay, UINT nResolution, TIMECALLBACK* pTimeProc, DWORD_PTR nUser, UINT nFlags) 1143 1216 { 1144 1217 _A(nFlags == (TIME_CALLBACK_FUNCTION | TIME_PERIODIC) || nFlags == (TIME_CALLBACK_FUNCTION | TIME_ONESHOT)); … … 1152 1225 return (MMRESULT) nEvent; 1153 1226 } 1154 static MMRESULT timeKillEvent(UINT uEvent) throw()1227 static MMRESULT timeKillEvent(UINT uEvent) 1155 1228 { 1156 1229 MMRESULT nResult = MMSYSERR_INVALPARAM; … … 1217 1290 } 1218 1291 } 1219 static VOID CALLBACK TimedEventProc(UINT_PTR nEvent, UINT, DWORD_PTR nUser, DWORD_PTR, DWORD_PTR) throw()1292 static VOID CALLBACK TimedEventProc(UINT_PTR nEvent, UINT, DWORD_PTR nUser, DWORD_PTR, DWORD_PTR) 1220 1293 { 1221 1294 _ATLTRY … … 1231 1304 public: 1232 1305 // CTimedEventT 1233 CTimedEventT() throw():1306 CTimedEventT() : 1234 1307 m_nEvent(0), 1235 1308 m_IdlenessEvent(TRUE, TRUE) … … 1240 1313 #endif // defined(_DEBUG) 1241 1314 } 1242 ~CTimedEventT() throw()1315 ~CTimedEventT() 1243 1316 { 1244 1317 Destroy(); 1245 1318 static const DWORD g_nTimeout = 5 * 1000; 1246 1319 const DWORD nWaitResult = WaitForSingleObject(m_IdlenessEvent, g_nTimeout); 1247 ATLTRACE(atlTraceSync, 5, _T("nWaitResult 0x%x\n"),nWaitResult);1320 _Z5_WAITRESULT(nWaitResult); 1248 1321 _A(nWaitResult == WAIT_OBJECT_0 || nWaitResult == WAIT_TIMEOUT); 1249 1322 if(nWaitResult != WAIT_OBJECT_0) 1250 1323 ATLTRACE(atlTraceGeneral, 2, _T("Worker thread failed to get idle within reasonable time, nWaitResult %d\n"), nWaitResult); 1251 1324 } 1252 VOID Initialize(CTimedEventDaemon* pTimedEventDaemon) throw()1325 VOID Initialize(CTimedEventDaemon* pTimedEventDaemon) 1253 1326 { 1254 1327 _A(pTimedEventDaemon); … … 1257 1330 m_pTimedEventDaemon = pTimedEventDaemon; 1258 1331 } 1259 const _Parameter& GetParameter() const throw()1332 const _Parameter& GetParameter() const 1260 1333 { 1261 1334 return m_Parameter; 1262 1335 } 1263 VOID SetParameter(_Parameter Parameter) throw()1336 VOID SetParameter(_Parameter Parameter) 1264 1337 { 1265 1338 m_Parameter = Parameter; 1266 1339 } 1267 UINT_PTR GetEvent() const throw()1340 UINT_PTR GetEvent() const 1268 1341 { 1269 1342 CComCritSecLock<CComAutoCriticalSection> DataLock(m_DataCriticalSection); 1270 1343 return m_nEvent; 1271 1344 } 1272 BOOL IsActive() const throw()1345 BOOL IsActive() const 1273 1346 { 1274 1347 CComCritSecLock<CComAutoCriticalSection> DataLock(m_DataCriticalSection); … … 1304 1377 return Create(pOwner, pHandler, nDelay, nResolution, TRUE); 1305 1378 } 1306 VOID Destroy() throw()1379 VOID Destroy() 1307 1380 { 1308 1381 CObjectPtr<CTimedEventDaemon> pTimedEventDaemon; … … 1331 1404 public: 1332 1405 // CSimpleTimedEventT 1333 CSimpleTimedEventT() throw()1406 CSimpleTimedEventT() 1334 1407 { 1335 1408 __super::Initialize(CTimedEventDaemon::GetDefaultTimedEventDaemon()); … … 1391 1464 public: 1392 1465 // CCheck 1393 CCheck() throw():1466 CCheck() : 1394 1467 m_nThreadIdentifier(0) 1395 1468 { 1396 ATLTRACE(atlTraceRefcount, 5, _T("this 0x%p\n"), this);1397 } 1398 ~CCheck() throw()1399 { 1400 ATLTRACE(atlTraceRefcount, 5, _T("this 0x%p\n"), this);1469 _Z5_THIS(); 1470 } 1471 ~CCheck() 1472 { 1473 _Z5_THIS(); 1401 1474 _A(!m_TimeoutEvent.IsActive()); 1402 1475 } … … 1425 1498 public: 1426 1499 // CDebugTimeCheck 1427 static ULONG GetDefaultTimeout() throw()1500 static ULONG GetDefaultTimeout() 1428 1501 { 1429 1502 static const ULONG g_nTimeout = 5000; … … 1438 1511 m_pCheck.Construct()->Initialize(CString(pszDescription), nTimeout); 1439 1512 } 1440 ~CDebugTimeCheck() throw()1513 ~CDebugTimeCheck() 1441 1514 { 1442 1515 if(m_pCheck) … … 1535 1608 // CRoCriticalSection, CRoCriticalSectionLock 1536 1609 1537 #if defined(_ M_IX86) && (defined(_DEBUG) || _TRACE)1610 #if defined(_DEBUG) || _TRACE 1538 1611 1539 1612 #pragma warning(disable: 4748) // warning C4748: /GS can not protect parameters and local variables from local buffer overrun because optimizations are disabled in function … … 1559 1632 public: 1560 1633 // CLockContext 1561 CLockContext(CRoCriticalSection* pCriticalSection, CONTEXT& ThreadContext, ULONG nTimeout) throw():1634 CLockContext(CRoCriticalSection* pCriticalSection, CONTEXT& ThreadContext, ULONG nTimeout) : 1562 1635 m_CriticalSection(*pCriticalSection), 1563 1636 m_nThreadIdentifier(GetCurrentThreadId()), … … 1587 1660 CONTEXT LockingThreadContext = LockContext.m_ThreadContext; 1588 1661 CComCritSecLock<CComAutoCriticalSection> Lock(CDebugSymbols::GetGlobalCriticalSection()); 1589 ATLCORETRACE3(LockedThreadContext)(atlTraceGeneral, 2, _T("Thread %d locked the critical section 0x%08X, lock time call stack follows:\n"), nLockThreadIdentifier, this); 1590 CDebugTraceCallStack::TraceCallStack(LockedThreadContext, 15, NULL); 1662 #if defined(_DEBUG) 1663 // NOTE: Debug builds don't have functions inlined so lock time stack is likely to be damaged anyway, let us just skip it 1664 LockedThreadContext.ContextFlags = 0; 1665 #endif // defined(_DEBUG) 1666 if(LockedThreadContext.ContextFlags) 1667 { 1668 ATLCORETRACE3(LockedThreadContext)(atlTraceGeneral, 2, _T("Thread %d locked the critical section 0x%p, lock time call stack follows:\n"), nLockThreadIdentifier, this); 1669 CDebugTraceCallStack::TraceCallStack(nLockThreadIdentifier, LockedThreadContext); 1670 } else 1671 ATLCORETRACE3(LockedThreadContext)(atlTraceGeneral, 2, _T("Thread %d locked the critical section 0x%p\n"), nLockThreadIdentifier, this); 1591 1672 ATLCORETRACE1(atlTraceGeneral, 2, _T("Thread %d current stack follows:\n"), nLockThreadIdentifier, this); 1592 CDebugTraceCallStack::TraceCallStack(nLockThreadIdentifier , 15);1593 ATLCORETRACE3(LockingThreadContext)(atlTraceGeneral, 2, _T("Thread %d is trying to lock the critical section 0x% 08X, call stack follows:\n"), LockContext.m_nThreadIdentifier, this);1594 CDebugTraceCallStack::TraceCallStack(Lock ingThreadContext, 15, NULL);1673 CDebugTraceCallStack::TraceCallStack(nLockThreadIdentifier); 1674 ATLCORETRACE3(LockingThreadContext)(atlTraceGeneral, 2, _T("Thread %d is trying to lock the critical section 0x%p, call stack follows:\n"), LockContext.m_nThreadIdentifier, this); 1675 CDebugTraceCallStack::TraceCallStack(LockContext.m_nThreadIdentifier, LockingThreadContext); 1595 1676 #pragma region MiniDump 1596 #if !defined(_WIN64)1597 #if !defined(_DEBUG) && defined(_TRACE) &&_TRACE1677 #if defined(_WINDLL) && !defined(_WIN64) 1678 #if !defined(_DEBUG) && _TRACE 1598 1679 if(nTimeoutCount == 3) 1599 1680 { … … 1627 1708 } 1628 1709 } 1629 #endif // !defined(_DEBUG) && defined(_TRACE) &&_TRACE1630 #endif // defined(_WIN 64)1710 #endif // !defined(_DEBUG) && _TRACE 1711 #endif // defined(_WINDLL) && !defined(_WIN64) 1631 1712 #pragma endregion 1632 1713 #if defined(_DEBUG) … … 1635 1716 } 1636 1717 } 1637 static VOID CALLBACK LockTimeout(UINT_PTR nEvent, UINT, DWORD_PTR nParameter, DWORD_PTR, DWORD_PTR) throw()1718 static VOID CALLBACK LockTimeout(UINT_PTR nEvent, UINT, DWORD_PTR nParameter, DWORD_PTR, DWORD_PTR) 1638 1719 { 1639 1720 CLockContext* pLockContext = (CLockContext*) nParameter; … … 1653 1734 public: 1654 1735 // CRoCriticalSection 1655 CRoCriticalSection() throw()1736 CRoCriticalSection() 1656 1737 { 1657 1738 m_nLockThreadIdentifier = 0; 1658 1739 ZeroMemory(&m_LockThreadContext, sizeof m_LockThreadContext); 1659 1740 } 1660 ~CRoCriticalSection() throw()1741 ~CRoCriticalSection() 1661 1742 { 1662 1743 _A(m_sec.LockCount < 0); 1663 1744 } 1664 HRESULT Lock() throw()1745 HRESULT Lock() 1665 1746 { 1666 1747 _ATLTRY 1667 1748 { 1668 CONTEXT ThreadContext = { CONTEXT_FULL };1749 CONTEXT ThreadContext; 1669 1750 if(!GetCurrentThreadContext(&ThreadContext)) 1670 1751 ThreadContext.ContextFlags = 0; … … 1684 1765 if(nRecursionCount == (LONG) 1) // First lock 1685 1766 { 1686 // NOTE: Stack walk out to step out of this function and lock helper class method1687 // Number of required step out steps depends on compiler settings and1688 // is 3 for debug builds to step out of this function, lock helper Lock and constructor1689 // and is 1 for release builds with inline any suitable option1690 CDebugSymbols::GlobalStackWalk(NULL, ThreadContext);1691 #if defined(_DEBUG)1692 CDebugSymbols::GlobalStackWalk(NULL, ThreadContext);1693 CDebugSymbols::GlobalStackWalk(NULL, ThreadContext);1694 #endif // defined(_DEBUG)1695 1767 CComCritSecLock<CComAutoCriticalSection> GlobalLock(g_CriticalSection); 1696 1768 m_nLockThreadIdentifier = GetCurrentThreadId(); … … 1707 1779 return S_OK; 1708 1780 } 1709 HRESULT Unlock() throw()1781 HRESULT Unlock() 1710 1782 { 1711 1783 { … … 1716 1788 { 1717 1789 m_nLockThreadIdentifier = 0; 1718 ZeroMemory(&m_LockThreadContext, sizeof m_LockThreadContext);1790 m_LockThreadContext.ContextFlags = 0; //ZeroMemory(&m_LockThreadContext, sizeof m_LockThreadContext); 1719 1791 } 1720 1792 } … … 1730 1802 1731 1803 #if !defined(_WIN64) && defined(_WINDLL) 1732 #if !defined(_DEBUG) && defined(_TRACE) &&_TRACE1804 #if !defined(_DEBUG) && _TRACE 1733 1805 1734 1806 #define AVAILABILITY_INTERNALWRITEMINIDUMP … … 1737 1809 1738 1810 //extern "C" // __declspec(dllexport) 1739 ATLINLINE HRESULT STDMETHODCALLTYPE InternalWriteMiniDump(HWND hParentWindow, HINSTANCE hInstance, LPSTR pszCommandLine, INT nShowCommand) throw()1811 ATLINLINE HRESULT STDMETHODCALLTYPE InternalWriteMiniDump(HWND hParentWindow, HINSTANCE hInstance, LPSTR pszCommandLine, INT nShowCommand) 1740 1812 { 1741 1813 _ATLTRY … … 1752 1824 __E(Process); 1753 1825 TCHAR pszDirectory[MAX_PATH] = { 0 }; 1754 if( GetOsVersion() >= 0x00060000)1826 if(IsWindowsVistaOrGreater()) 1755 1827 { 1756 1828 _W(SHGetSpecialFolderPath(NULL, pszDirectory, CSIDL_COMMON_APPDATA, TRUE)); … … 1786 1858 } 1787 1859 1788 #endif // !defined(_DEBUG) && defined(_TRACE) &&_TRACE1860 #endif // !defined(_DEBUG) && _TRACE 1789 1861 #endif // !defined(_WIN64) && defined(_WINDLL) 1790 1862 1863 #pragma optimize("", on) 1864 #pragma warning(default: 4748) 1865 1866 #else 1867 1868 typedef CComAutoCriticalSection CRoCriticalSection; 1869 1870 #endif // defined(_DEBUG) || _TRACE 1871 1872 typedef CComAutoCriticalSection CRoLightCriticalSection; 1873 1874 #if TRUE 1875 1876 template <typename CCriticalSection> 1877 inline BOOL TryLock(CCriticalSection& CriticalSection); 1878 1879 #if _WIN32_WINNT >= 0x0400 1880 template <> 1881 inline BOOL TryLock<CComCriticalSection>(CComCriticalSection& CriticalSection) 1882 { 1883 return TryEnterCriticalSection(&CriticalSection.m_sec); 1884 } 1885 template <> 1886 inline BOOL TryLock<CRoCriticalSection>(CRoCriticalSection& CriticalSection) 1887 { 1888 return TryEnterCriticalSection(&CriticalSection.m_sec); 1889 } 1890 #endif // _WIN32_WINNT >= 0x0400 1891 1892 template <typename CCriticalSection> 1893 class CRoCriticalSectionLockT 1894 { 1895 private: 1896 CCriticalSection& m_CriticalSection; 1897 BOOL m_bLocked; 1898 1899 CRoCriticalSectionLockT(const CRoCriticalSectionLockT&); 1900 CRoCriticalSectionLockT& operator = (const CRoCriticalSectionLockT&); 1901 1902 public: 1903 // CRoCriticalSectionLockT 1904 CRoCriticalSectionLockT(CCriticalSection& CriticalSection, BOOL bInitialLock = TRUE) : 1905 m_CriticalSection(CriticalSection), 1906 m_bLocked(FALSE) 1907 { 1908 if(bInitialLock) 1909 Lock(); 1910 } 1911 ~CRoCriticalSectionLockT() 1912 { 1913 if(m_bLocked) 1914 Unlock(); 1915 } 1916 VOID Lock() 1917 { 1918 _A(!m_bLocked); 1919 _V(m_CriticalSection.Lock()); 1920 m_bLocked = TRUE; 1921 } 1922 BOOL TryLock() 1923 { 1924 _A(!m_bLocked); 1925 if(!::TryLock(m_CriticalSection)) 1926 return FALSE; 1927 m_bLocked = TRUE; 1928 return TRUE; 1929 } 1930 VOID Unlock() 1931 { 1932 ATLASSUME(m_bLocked); 1933 m_CriticalSection.Unlock(); 1934 m_bLocked = FALSE; 1935 } 1936 BOOL IsLocked() const 1937 { 1938 return m_bLocked; 1939 } 1940 }; 1941 1942 typedef CRoCriticalSectionLockT<CRoCriticalSection> CRoCriticalSectionLock; 1943 1944 #else 1945 1946 typedef CComCritSecLock<CRoCriticalSection> CRoCriticalSectionLock; 1947 1948 #endif 1949 1950 typedef CComCritSecLock<CRoLightCriticalSection> CRoLightCriticalSectionLock; 1951 1952 ///////////////////////////////////////////////////////////////////////////// 1953 // DeadlockCriticalSectionT 1954 1791 1955 template <typename _CriticalSection> 1792 inline DWORD WINAPI DeadlockCriticalSectionThreadProcT(VOID* pvParameter) throw() 1793 { 1794 _CriticalSection& CriticalSection = *(reinterpret_cast<_CriticalSection*>(pvParameter)); 1795 CComCritSecLock<_CriticalSection> Lock(CriticalSection); 1956 inline DWORD WINAPI DeadlockCriticalSectionThreadProcT(_CriticalSection* pCriticalSection) 1957 { 1958 CComCritSecLock<_CriticalSection> Lock(*pCriticalSection); 1796 1959 _A(FALSE); 1797 1960 return 0; … … 1799 1962 1800 1963 template <typename _CriticalSection> 1801 inline VOID DeadlockCriticalSectionT(_CriticalSection& CriticalSection) throw()1964 inline VOID DeadlockCriticalSectionT(_CriticalSection& CriticalSection) 1802 1965 { 1803 1966 CComCritSecLock<_CriticalSection> Lock(CriticalSection); 1804 DWORD nThreadIdentifier = 0; 1805 CHandle ThreadHandle(CreateThreadT<_CriticalSection>(NULL, 0, DeadlockCriticalSectionThreadProcT<_CriticalSection>, &CriticalSection, 0, &nThreadIdentifier)); 1967 // WARN: GetThreadId is WinVista+, so we have to use CreateThreadT instead of AtlCreateThread 1968 DWORD nThreadIdentifier; 1969 CHandle ThreadHandle; 1970 ThreadHandle.Attach(CreateThreadT<_CriticalSection>(NULL, 0, DeadlockCriticalSectionThreadProcT<_CriticalSection>, &CriticalSection, 0, &nThreadIdentifier)); 1806 1971 ATLTRACE(atlTraceGeneral, 1, _T("Deadlocked in threads %d and %d (spawned)\n"), GetCurrentThreadId(), nThreadIdentifier); 1807 1972 WaitForSingleObject(ThreadHandle, INFINITE); … … 1809 1974 } 1810 1975 1811 #pragma optimize("", on)1812 #pragma warning(default: 4748)1813 1814 #else1815 1816 typedef CComAutoCriticalSection CRoCriticalSection;1817 1818 #endif // defined(_M_IX86) && (defined(_DEBUG) || _TRACE)1819 1820 typedef CComAutoCriticalSection CRoLightCriticalSection;1821 1822 #if TRUE1823 1824 template <typename _CriticalSection>1825 class CRoCriticalSectionLockT1826 {1827 private:1828 _CriticalSection& m_CriticalSection;1829 BOOL m_bLocked;1830 1831 CRoCriticalSectionLockT(const CRoCriticalSectionLockT&) throw();1832 CRoCriticalSectionLockT& operator = (const CRoCriticalSectionLockT&) throw();1833 1834 public:1835 // CRoCriticalSectionLockT1836 CRoCriticalSectionLockT(_CriticalSection& CriticalSection, BOOL bInitialLock = TRUE) throw() :1837 m_CriticalSection(CriticalSection),1838 m_bLocked(FALSE)1839 {1840 if(bInitialLock)1841 Lock();1842 }1843 ~CRoCriticalSectionLockT() throw()1844 {1845 if(m_bLocked)1846 Unlock();1847 }1848 VOID Lock() throw()1849 {1850 _A(!m_bLocked);1851 _V(m_CriticalSection.Lock());1852 m_bLocked = TRUE;1853 }1854 #if _WIN32_WINNT >= 0x04001855 BOOL TryLock() throw()1856 {1857 _A(!m_bLocked);1858 // SUGG: Take advantage of m_CriticalSection's TryLock if available1859 _A(sizeof m_CriticalSection >= sizeof (CRITICAL_SECTION));1860 CRITICAL_SECTION* pInternalCriticalSection = reinterpret_cast<CRITICAL_SECTION*>(&m_CriticalSection);1861 if(!TryEnterCriticalSection(pInternalCriticalSection))1862 return FALSE;1863 m_bLocked = TRUE;1864 return TRUE;1865 }1866 #endif // _WIN32_WINNT >= 0x04001867 VOID Unlock() throw()1868 {1869 ATLASSUME(m_bLocked);1870 m_CriticalSection.Unlock();1871 m_bLocked = FALSE;1872 }1873 };1874 1875 typedef CRoCriticalSectionLockT<CRoCriticalSection> CRoCriticalSectionLock;1876 1877 #else1878 1879 typedef CComCritSecLock<CRoCriticalSection> CRoCriticalSectionLock;1880 1881 #endif1882 1883 typedef CComCritSecLock<CRoLightCriticalSection> CRoLightCriticalSectionLock;1884 1885 1976 ///////////////////////////////////////////////////////////////////////////// 1886 1977 // CComCritSecUnlock 1887 1978 1888 template <typename _CriticalSection>1979 template <typename CCriticalSection> 1889 1980 class CComCritSecUnlock 1890 1981 { 1891 1982 private: 1892 _CriticalSection& m_CriticalSection;1983 CCriticalSection& m_CriticalSection; 1893 1984 BOOL m_bUnlocked; 1894 1985 1895 CComCritSecUnlock(const CComCritSecUnlock&) throw();1896 CComCritSecUnlock& operator = (const CComCritSecUnlock&) throw();1986 CComCritSecUnlock(const CComCritSecUnlock&); 1987 CComCritSecUnlock& operator = (const CComCritSecUnlock&); 1897 1988 1898 1989 public: 1899 1990 // CComCritSecUnlock 1900 CComCritSecUnlock( _CriticalSection& CriticalSection, BOOL bInitialUnlock = TRUE) throw() :1991 CComCritSecUnlock(CCriticalSection& CriticalSection, BOOL bInitialUnlock = TRUE) : 1901 1992 m_CriticalSection(CriticalSection), 1902 1993 m_bUnlocked(FALSE) … … 1905 1996 Unlock(); 1906 1997 } 1907 ~CComCritSecUnlock() throw()1998 ~CComCritSecUnlock() 1908 1999 { 1909 2000 if(m_bUnlocked) 1910 2001 Lock(); 1911 2002 } 1912 VOID Unlock() throw()2003 VOID Unlock() 1913 2004 { 1914 2005 _A(!m_bUnlocked); … … 1916 2007 m_bUnlocked = TRUE; 1917 2008 } 1918 VOID Lock() throw()2009 VOID Lock() 1919 2010 { 1920 2011 _A(m_bUnlocked); … … 1922 2013 m_bUnlocked = FALSE; 1923 2014 } 1924 #if _WIN32_WINNT >= 0x0400 1925 BOOL TryLock() throw() 2015 BOOL TryLock() 1926 2016 { 1927 2017 _A(m_bUnlocked); 1928 // SUGG: Take advantage of m_CriticalSection's TryLock if available 1929 _A(sizeof m_CriticalSection == sizeof (CRITICAL_SECTION)); 1930 CRITICAL_SECTION* pInternalCriticalSection = reinterpret_cast<CRITICAL_SECTION*>(&m_CriticalSection); 1931 if(!TryEnterCriticalSection(pInternalCriticalSection)) 2018 if(!::TryLock(m_CriticalSection)) 1932 2019 return FALSE; 1933 2020 m_bUnlocked = FALSE; 1934 2021 return TRUE; 1935 2022 } 1936 #endif // _WIN32_WINNT >= 0x0400 2023 BOOL IsLocked() const 2024 { 2025 return m_bLocked; 2026 } 1937 2027 }; 1938 2028 … … 1946 2036 // NOTE: Requires Windows Vista or Windows Server 2008 1947 2037 2038 #if _WIN32_WINNT >= 0x0600 2039 1948 2040 class CApiComReleaseReaderWriterLock 1949 2041 { … … 1953 2045 public: 1954 2046 // CApiComReleaseReaderWriterLock 1955 CApiComReleaseReaderWriterLock() throw()2047 CApiComReleaseReaderWriterLock() 1956 2048 { 1957 2049 memset(&m_Lock, 0, sizeof m_Lock); 1958 2050 } 1959 ~CApiComReleaseReaderWriterLock() throw()1960 { 1961 } 1962 VOID Initialize() throw()2051 ~CApiComReleaseReaderWriterLock() 2052 { 2053 } 2054 VOID Initialize() 1963 2055 { 1964 2056 InitializeSRWLock(&m_Lock); 1965 2057 } 1966 VOID Terminate() throw()1967 { 1968 } 1969 BOOL VerifyAcquiredExclusive() const throw()2058 VOID Terminate() 2059 { 2060 } 2061 BOOL VerifyAcquiredExclusive() const 1970 2062 { 1971 2063 return TRUE; // No way to verify 1972 2064 } 1973 VOID AcquireExclusive() throw()2065 VOID AcquireExclusive() 1974 2066 { 1975 2067 AcquireSRWLockExclusive(&m_Lock); 1976 2068 } 1977 #if _WIN32_WINNT >= _WIN32_WINNT_WIN71978 BOOLEAN TryAcquireExclusive() throw()2069 #if _WIN32_WINNT >= _WIN32_WINNT_WIN7 2070 BOOLEAN TryAcquireExclusive() 1979 2071 { 1980 2072 return TryAcquireSRWLockExclusive(&m_Lock); 1981 2073 } 1982 #endif // _WIN32_WINNT >= _WIN32_WINNT_WIN71983 VOID ReleaseExclusive() throw()2074 #endif // _WIN32_WINNT >= _WIN32_WINNT_WIN7 2075 VOID ReleaseExclusive() 1984 2076 { 1985 2077 ReleaseSRWLockExclusive(&m_Lock); 1986 2078 } 1987 BOOL VerifyAcquiredShared() const throw()2079 BOOL VerifyAcquiredShared() const 1988 2080 { 1989 2081 return TRUE; // No way to verify 1990 2082 } 1991 VOID AcquireShared() throw()2083 VOID AcquireShared() 1992 2084 { 1993 2085 AcquireSRWLockShared(&m_Lock); 1994 2086 } 1995 #if _WIN32_WINNT >= _WIN32_WINNT_WIN71996 BOOLEAN TryAcquireShared() throw()2087 #if _WIN32_WINNT >= _WIN32_WINNT_WIN7 2088 BOOLEAN TryAcquireShared() 1997 2089 { 1998 2090 return TryAcquireSRWLockShared(&m_Lock); 1999 2091 } 2000 #endif // _WIN32_WINNT >= _WIN32_WINNT_WIN72001 VOID ReleaseShared() throw()2092 #endif // _WIN32_WINNT >= _WIN32_WINNT_WIN7 2093 VOID ReleaseShared() 2002 2094 { 2003 2095 ReleaseSRWLockShared(&m_Lock); 2004 2096 } 2005 2097 }; 2098 2099 #endif // _WIN32_WINNT >= 0x0600 2006 2100 2007 2101 //////////////////////////////////////////////////////////// … … 2016 2110 public: 2017 2111 // CNativeComReleaseReaderWriterLockT 2018 CNativeComReleaseReaderWriterLockT() throw():2112 CNativeComReleaseReaderWriterLockT() : 2019 2113 m_nLock(0) 2020 2114 { 2021 2115 } 2022 ~CNativeComReleaseReaderWriterLockT() throw()2023 { 2024 } 2025 VOID Initialize() throw()2116 ~CNativeComReleaseReaderWriterLockT() 2117 { 2118 } 2119 VOID Initialize() 2026 2120 { 2027 2121 m_nLock = 0; 2028 2122 } 2029 VOID Terminate() throw()2030 { 2031 } 2032 BOOL VerifyAcquiredExclusive() const throw()2123 VOID Terminate() 2124 { 2125 } 2126 BOOL VerifyAcquiredExclusive() const 2033 2127 { 2034 2128 return TRUE; // No way to reliably verify 2035 2129 } 2036 VOID AcquireExclusiveNoSpin() throw()2130 VOID AcquireExclusiveNoSpin() 2037 2131 { 2038 2132 for(; ; ) … … 2044 2138 } 2045 2139 } 2046 VOID AcquireExclusiveSpin(UINT nSpinCount) throw()2140 VOID AcquireExclusiveSpin(UINT nSpinCount) 2047 2141 { 2048 2142 for(UINT nSpinIndex = 0; ; nSpinIndex++) … … 2057 2151 } 2058 2152 } 2059 VOID AcquireExclusive() throw()2153 VOID AcquireExclusive() 2060 2154 { 2061 2155 (t_nDefaultSpinCount > 0) ? AcquireExclusiveSpin(t_nDefaultSpinCount) : AcquireExclusiveNoSpin(); 2062 2156 } 2063 BOOLEAN TryAcquireExclusive() throw()2157 BOOLEAN TryAcquireExclusive() 2064 2158 { 2065 2159 if(m_nLock.ExchangeAdd(-0x10000) == 0) … … 2068 2162 return FALSE; 2069 2163 } 2070 VOID ReleaseExclusive() throw()2164 VOID ReleaseExclusive() 2071 2165 { 2072 2166 _A(m_nLock < -0x10000 / 2); 2073 2167 m_nLock.ExchangeAdd(+0x10000); 2074 2168 } 2075 BOOL VerifyAcquiredShared() const throw()2169 BOOL VerifyAcquiredShared() const 2076 2170 { 2077 2171 return TRUE; // No way to reliably verify 2078 2172 } 2079 VOID AcquireSharedNoSpin() throw()2173 VOID AcquireSharedNoSpin() 2080 2174 { 2081 2175 for(; ; ) … … 2087 2181 } 2088 2182 } 2089 VOID AcquireSharedSpin(UINT nSpinCount) throw()2183 VOID AcquireSharedSpin(UINT nSpinCount) 2090 2184 { 2091 2185 for(UINT nSpinIndex = 0; ; nSpinIndex++) … … 2100 2194 } 2101 2195 } 2102 VOID AcquireShared() throw()2196 VOID AcquireShared() 2103 2197 { 2104 2198 (t_nDefaultSpinCount > 0) ? AcquireSharedSpin(t_nDefaultSpinCount) : AcquireSharedNoSpin(); 2105 2199 } 2106 BOOLEAN TryAcquireShared() throw()2200 BOOLEAN TryAcquireShared() 2107 2201 { 2108 2202 if(m_nLock.Increment() > 0) … … 2111 2205 return FALSE; 2112 2206 } 2113 VOID ReleaseShared() throw()2207 VOID ReleaseShared() 2114 2208 { 2115 2209 m_nLock.Decrement(); … … 2143 2237 public: 2144 2238 // CAcquirement 2145 static BOOL IsEqualThreadIdentifier(const CAcquirement& Acquirement, DWORD nThreadIdentifier) throw()2239 static BOOL IsEqualThreadIdentifier(const CAcquirement& Acquirement, DWORD nThreadIdentifier) 2146 2240 { 2147 2241 return Acquirement.m_nThreadIdentifier == nThreadIdentifier; 2148 2242 } 2149 CAcquirement() throw()2150 { 2151 } 2152 CAcquirement(DWORD nThreadIdentifier, BOOL bExclusive) throw():2243 CAcquirement() 2244 { 2245 } 2246 CAcquirement(DWORD nThreadIdentifier, BOOL bExclusive) : 2153 2247 m_nThreadIdentifier(nThreadIdentifier), 2154 2248 m_bExclusive(bExclusive) … … 2168 2262 public: 2169 2263 // CAcquirementList 2170 BOOL IsEmpty() const throw()2264 BOOL IsEmpty() const 2171 2265 { 2172 2266 m_DataReaderWriterLock.AcquireShared(); … … 2175 2269 return bResult; 2176 2270 } 2177 BOOL Find(DWORD nThreadIdentifier = GetCurrentThreadId()) const throw()2271 BOOL Find(DWORD nThreadIdentifier = GetCurrentThreadId()) const 2178 2272 { 2179 2273 POSITION Position = NULL; … … 2183 2277 return Position != NULL; 2184 2278 } 2185 BOOL Find(BOOL& bExclusive, DWORD nThreadIdentifier = GetCurrentThreadId()) const throw()2279 BOOL Find(BOOL& bExclusive, DWORD nThreadIdentifier = GetCurrentThreadId()) const 2186 2280 { 2187 2281 POSITION Position = NULL; … … 2193 2287 return Position != NULL; 2194 2288 } 2195 VOID Add(BOOL bExclusive, DWORD nThreadIdentifier = GetCurrentThreadId()) throw()2289 VOID Add(BOOL bExclusive, DWORD nThreadIdentifier = GetCurrentThreadId()) 2196 2290 { 2197 2291 m_DataReaderWriterLock.AcquireExclusive(); … … 2199 2293 m_DataReaderWriterLock.ReleaseExclusive(); 2200 2294 } 2201 BOOL RemoveLast(DWORD nThreadIdentifier = GetCurrentThreadId()) throw()2295 BOOL RemoveLast(DWORD nThreadIdentifier = GetCurrentThreadId()) 2202 2296 { 2203 2297 POSITION Position; … … 2215 2309 public: 2216 2310 // CComDebugReaderWriterLock 2217 CComDebugReaderWriterLock() throw()2218 { 2219 } 2220 ~CComDebugReaderWriterLock() throw()2221 { 2222 } 2223 VOID Initialize() throw()2311 CComDebugReaderWriterLock() 2312 { 2313 } 2314 ~CComDebugReaderWriterLock() 2315 { 2316 } 2317 VOID Initialize() 2224 2318 { 2225 2319 __super::Initialize(); 2226 2320 _A(m_AcquirementList.IsEmpty()); 2227 2321 } 2228 VOID Terminate() throw()2322 VOID Terminate() 2229 2323 { 2230 2324 _A(m_AcquirementList.IsEmpty()); 2231 2325 } 2232 BOOL VerifyAcquiredExclusive() const throw()2326 BOOL VerifyAcquiredExclusive() const 2233 2327 { 2234 2328 BOOL bExclusive; … … 2237 2331 return bExclusive; 2238 2332 } 2239 VOID AcquireExclusive() throw()2333 VOID AcquireExclusive() 2240 2334 { 2241 2335 _A(!m_AcquirementList.Find()); … … 2243 2337 m_AcquirementList.Add(TRUE); 2244 2338 } 2245 BOOLEAN TryAcquireExclusive() throw()2339 BOOLEAN TryAcquireExclusive() 2246 2340 { 2247 2341 _A(!m_AcquirementList.Find()); … … 2251 2345 return TRUE; 2252 2346 } 2253 VOID ReleaseExclusive() throw()2347 VOID ReleaseExclusive() 2254 2348 { 2255 2349 _W(m_AcquirementList.RemoveLast()); … … 2257 2351 _A(!m_AcquirementList.Find()); 2258 2352 } 2259 BOOL VerifyAcquiredShared() const throw()2353 BOOL VerifyAcquiredShared() const 2260 2354 { 2261 2355 BOOL bExclusive; … … 2264 2358 return !bExclusive; 2265 2359 } 2266 VOID AcquireShared() throw()2360 VOID AcquireShared() 2267 2361 { 2268 2362 _A(!m_AcquirementList.Find()); … … 2270 2364 m_AcquirementList.Add(FALSE); 2271 2365 } 2272 BOOLEAN TryAcquireShared() throw()2366 BOOLEAN TryAcquireShared() 2273 2367 { 2274 2368 _A(!m_AcquirementList.Find()); … … 2278 2372 return TRUE; 2279 2373 } 2280 VOID ReleaseShared() throw()2374 VOID ReleaseShared() 2281 2375 { 2282 2376 _W(m_AcquirementList.RemoveLast()); … … 2303 2397 public: 2304 2398 // CComAutoReaderWriterLock 2305 CComAutoReaderWriterLock() throw()2399 CComAutoReaderWriterLock() 2306 2400 { 2307 2401 __super::Initialize(); 2308 2402 } 2309 ~CComAutoReaderWriterLock() throw()2403 ~CComAutoReaderWriterLock() 2310 2404 { 2311 2405 __super::Terminate(); … … 2322 2416 BOOL m_bLocked; 2323 2417 2324 CComReaderWriterLockExclusiveLockT(const CComReaderWriterLockExclusiveLockT&) throw();2325 CComReaderWriterLockExclusiveLockT& operator = (const CComReaderWriterLockExclusiveLockT&) throw();2418 CComReaderWriterLockExclusiveLockT(const CComReaderWriterLockExclusiveLockT&); 2419 CComReaderWriterLockExclusiveLockT& operator = (const CComReaderWriterLockExclusiveLockT&); 2326 2420 2327 2421 public: 2328 2422 // CComReaderWriterLockExclusiveLockT 2329 CComReaderWriterLockExclusiveLockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialLock = TRUE) throw():2423 CComReaderWriterLockExclusiveLockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialLock = TRUE) : 2330 2424 m_Lock(ReaderWriterLock), 2331 2425 m_bLocked(FALSE) … … 2334 2428 Lock(); 2335 2429 } 2336 ~CComReaderWriterLockExclusiveLockT() throw()2430 ~CComReaderWriterLockExclusiveLockT() 2337 2431 { 2338 2432 if(m_bLocked) 2339 2433 Unlock(); 2340 2434 } 2341 VOID Lock() throw()2435 VOID Lock() 2342 2436 { 2343 2437 _A(!m_bLocked); … … 2346 2440 } 2347 2441 //#if _WIN32_WINNT >= _WIN32_WINNT_WIN7 2348 BOOL TryLock() throw()2442 BOOL TryLock() 2349 2443 { 2350 2444 _A(!m_bLocked); … … 2355 2449 } 2356 2450 //#endif // _WIN32_WINNT >= _WIN32_WINNT_WIN7 2357 VOID Unlock() throw()2451 VOID Unlock() 2358 2452 { 2359 2453 ATLASSUME(m_bLocked); … … 2370 2464 BOOL m_bUnlocked; 2371 2465 2372 CComReaderWriterLockExclusiveUnlockT(const CComReaderWriterLockExclusiveUnlockT&) throw();2373 CComReaderWriterLockExclusiveUnlockT& operator = (const CComReaderWriterLockExclusiveUnlockT&) throw();2466 CComReaderWriterLockExclusiveUnlockT(const CComReaderWriterLockExclusiveUnlockT&); 2467 CComReaderWriterLockExclusiveUnlockT& operator = (const CComReaderWriterLockExclusiveUnlockT&); 2374 2468 2375 2469 public: 2376 2470 // CComReaderWriterLockExclusiveUnlockT 2377 CComReaderWriterLockExclusiveUnlockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialUnlock = TRUE) throw():2471 CComReaderWriterLockExclusiveUnlockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialUnlock = TRUE) : 2378 2472 m_Lock(ReaderWriterLock), 2379 2473 m_bUnlocked(FALSE) … … 2382 2476 Unlock(); 2383 2477 } 2384 ~CComReaderWriterLockExclusiveUnlockT() throw()2478 ~CComReaderWriterLockExclusiveUnlockT() 2385 2479 { 2386 2480 if(m_bUnlocked) 2387 2481 Lock(); 2388 2482 } 2389 VOID Unlock() throw()2483 VOID Unlock() 2390 2484 { 2391 2485 _A(!m_bUnlocked); … … 2393 2487 m_bUnlocked = TRUE; 2394 2488 } 2395 VOID Lock() throw()2489 VOID Lock() 2396 2490 { 2397 2491 ATLASSUME(m_bUnlocked); … … 2400 2494 } 2401 2495 //#if _WIN32_WINNT >= _WIN32_WINNT_WIN7 2402 BOOL TryLock() throw()2496 BOOL TryLock() 2403 2497 { 2404 2498 _A(!m_bLocked); … … 2418 2512 BOOL m_bLocked; 2419 2513 2420 CComReaderWriterLockSharedLockT(const CComReaderWriterLockSharedLockT&) throw();2421 CComReaderWriterLockSharedLockT& operator = (const CComReaderWriterLockSharedLockT&) throw();2514 CComReaderWriterLockSharedLockT(const CComReaderWriterLockSharedLockT&); 2515 CComReaderWriterLockSharedLockT& operator = (const CComReaderWriterLockSharedLockT&); 2422 2516 2423 2517 public: 2424 2518 // CComReaderWriterLockSharedLockT 2425 CComReaderWriterLockSharedLockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialLock = TRUE) throw():2519 CComReaderWriterLockSharedLockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialLock = TRUE) : 2426 2520 m_Lock(ReaderWriterLock), 2427 2521 m_bLocked(FALSE) … … 2430 2524 Lock(); 2431 2525 } 2432 ~CComReaderWriterLockSharedLockT() throw()2526 ~CComReaderWriterLockSharedLockT() 2433 2527 { 2434 2528 if(m_bLocked) 2435 2529 Unlock(); 2436 2530 } 2437 VOID Lock() throw()2531 VOID Lock() 2438 2532 { 2439 2533 _A(!m_bLocked); … … 2442 2536 } 2443 2537 //#if _WIN32_WINNT >= _WIN32_WINNT_WIN7 2444 BOOL TryLock() throw()2538 BOOL TryLock() 2445 2539 { 2446 2540 _A(!m_bLocked); … … 2451 2545 } 2452 2546 //#endif // _WIN32_WINNT >= _WIN32_WINNT_WIN7 2453 VOID Unlock() throw()2547 VOID Unlock() 2454 2548 { 2455 2549 ATLASSUME(m_bLocked); … … 2466 2560 BOOL m_bUnlocked; 2467 2561 2468 CComReaderWriterLockSharedUnlockT(const CComReaderWriterLockSharedUnlockT&) throw();2469 CComReaderWriterLockSharedUnlockT& operator = (const CComReaderWriterLockSharedUnlockT&) throw();2562 CComReaderWriterLockSharedUnlockT(const CComReaderWriterLockSharedUnlockT&); 2563 CComReaderWriterLockSharedUnlockT& operator = (const CComReaderWriterLockSharedUnlockT&); 2470 2564 2471 2565 public: 2472 2566 // CComReaderWriterLockSharedUnlockT 2473 CComReaderWriterLockSharedUnlockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialUnlock = TRUE) throw():2567 CComReaderWriterLockSharedUnlockT(_ReaderWriterLock& ReaderWriterLock, BOOL bInitialUnlock = TRUE) : 2474 2568 m_Lock(ReaderWriterLock), 2475 2569 m_bUnlocked(FALSE) … … 2478 2572 Unlock(); 2479 2573 } 2480 ~CComReaderWriterLockSharedUnlockT() throw()2574 ~CComReaderWriterLockSharedUnlockT() 2481 2575 { 2482 2576 if(m_bUnlocked) 2483 2577 Lock(); 2484 2578 } 2485 VOID Unlock() throw()2579 VOID Unlock() 2486 2580 { 2487 2581 _A(!m_bUnlocked); … … 2489 2583 m_bUnlocked = TRUE; 2490 2584 } 2491 VOID Lock() throw()2585 VOID Lock() 2492 2586 { 2493 2587 ATLASSUME(m_bUnlocked); … … 2496 2590 } 2497 2591 //#if _WIN32_WINNT >= _WIN32_WINNT_WIN7 2498 BOOL TryLock() throw()2592 BOOL TryLock() 2499 2593 { 2500 2594 _A(!m_bLocked);
Note: See TracChangeset
for help on using the changeset viewer.