1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 #include "common.h"
5 #include "CommonTypes.h"
6 #include "CommonMacros.h"
7 #include "daccess.h"
8 #include "event.h"
9 #include "PalRedhawkCommon.h"
10 #include "PalRedhawk.h"
11 #include "rhassert.h"
12 #include "slist.h"
13 #include "gcrhinterface.h"
14 #include "varint.h"
15 #include "regdisplay.h"
16 #include "StackFrameIterator.h"
17 #include "thread.h"
18 #include "holder.h"
19 #include "Crst.h"
20 #include "RWLock.h"
21 #include "threadstore.h"
22 #include "threadstore.inl"
23 
24 //
25 // -----------------------------------------------------------------------------------------------------------
26 //
27 // CLR wrapper around events. This version directly uses Win32 events (there's no support for host
28 // interception).
29 //
30 
CreateManualEventNoThrow(bool bInitialState)31 bool CLREventStatic::CreateManualEventNoThrow(bool bInitialState)
32 {
33     m_hEvent = PalCreateEventW(NULL, TRUE, bInitialState, NULL);
34     m_fInitialized = true;
35     return IsValid();
36 }
37 
CreateAutoEventNoThrow(bool bInitialState)38 bool CLREventStatic::CreateAutoEventNoThrow(bool bInitialState)
39 {
40     m_hEvent = PalCreateEventW(NULL, FALSE, bInitialState, NULL);
41     m_fInitialized = true;
42     return IsValid();
43 }
44 
CreateOSManualEventNoThrow(bool bInitialState)45 bool CLREventStatic::CreateOSManualEventNoThrow(bool bInitialState)
46 {
47     m_hEvent = PalCreateEventW(NULL, TRUE, bInitialState, NULL);
48     m_fInitialized = true;
49     return IsValid();
50 }
51 
CreateOSAutoEventNoThrow(bool bInitialState)52 bool CLREventStatic::CreateOSAutoEventNoThrow(bool bInitialState)
53 {
54     m_hEvent = PalCreateEventW(NULL, FALSE, bInitialState, NULL);
55     m_fInitialized = true;
56     return IsValid();
57 }
58 
CloseEvent()59 void CLREventStatic::CloseEvent()
60 {
61     if (m_fInitialized && m_hEvent != INVALID_HANDLE_VALUE)
62     {
63         PalCloseHandle(m_hEvent);
64         m_hEvent = INVALID_HANDLE_VALUE;
65     }
66 }
67 
IsValid() const68 bool CLREventStatic::IsValid() const
69 {
70     return m_fInitialized && m_hEvent != INVALID_HANDLE_VALUE;
71 }
72 
Set()73 bool CLREventStatic::Set()
74 {
75     if (!m_fInitialized)
76         return false;
77     return PalSetEvent(m_hEvent);
78 }
79 
Reset()80 bool CLREventStatic::Reset()
81 {
82     if (!m_fInitialized)
83         return false;
84     return PalResetEvent(m_hEvent);
85 }
86 
Wait(uint32_t dwMilliseconds,bool bAlertable,bool bAllowReentrantWait)87 uint32_t CLREventStatic::Wait(uint32_t dwMilliseconds, bool bAlertable, bool bAllowReentrantWait)
88 {
89     UInt32 result = WAIT_FAILED;
90 
91     if (m_fInitialized)
92     {
93         bool        disablePreemptive = false;
94         Thread *    pCurThread  = ThreadStore::GetCurrentThreadIfAvailable();
95 
96         if (NULL != pCurThread)
97         {
98             if (pCurThread->IsCurrentThreadInCooperativeMode())
99             {
100                 pCurThread->EnablePreemptiveMode();
101                 disablePreemptive = true;
102             }
103         }
104 
105         result = PalCompatibleWaitAny(bAlertable, dwMilliseconds, 1, &m_hEvent, bAllowReentrantWait);
106 
107         if (disablePreemptive)
108         {
109             pCurThread->DisablePreemptiveMode();
110         }
111     }
112 
113     return result;
114 }
115 
GetOSEvent()116 HANDLE CLREventStatic::GetOSEvent()
117 {
118     if (!m_fInitialized)
119         return INVALID_HANDLE_VALUE;
120     return m_hEvent;
121 }
122