Remove always defined FEATURE_CORECLR
[platform/upstream/coreclr.git] / src / inc / palclr_win.h
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 // ===========================================================================
5 // File: palclr.h
6 //
7 // Various macros and constants that are necessary to make the CLR portable.
8 //
9
10 // ===========================================================================
11
12 #ifndef __PALCLR_WIN_H__
13 #define __PALCLR_WIN_H__
14
15 // PAL SEH
16 // Macros for portable exception handling. The Win32 SEH is emulated using
17 // these macros and setjmp/longjmp on Unix
18 //
19 // Usage notes:
20 //
21 // - The filter has to be a function taking two parameters:
22 // LONG MyFilter(PEXCEPTION_POINTERS *pExceptionInfo, PVOID pv)
23 //
24 // - It is not possible to directly use the local variables in the filter.
25 // All the local information that the filter has to need to know about should
26 // be passed through pv parameter
27 //  
28 // - Do not use goto to jump out of the PAL_TRY block
29 // (jumping out of the try block is not a good idea even on Win32, because of
30 // it causes stack unwind)
31 //
32 //
33 // Simple examples:
34 //
35 // PAL_TRY {
36 //   ....
37 // } WIN_PAL_FINALLY {
38 //   ....
39 // }
40 // WIN_PAL_ENDTRY
41 //
42 //
43 // PAL_TRY {
44 //   ....
45 // } WIN_PAL_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
46 //   ....
47 // }
48 // WIN_PAL_ENDTRY
49 // 
50 //
51 // LONG MyFilter(PEXCEPTION_POINTERS *pExceptionInfo, PVOID pv)
52 // {
53 // ...
54 // }
55 // PAL_TRY {
56 //   ....
57 // } WIN_PAL_EXCEPT_FILTER(MyFilter, NULL) {
58 //   ....
59 // }
60 // WIN_PAL_ENDTRY
61 //
62 //
63 // Complex example:
64 //
65 // struct MyParams
66 // {
67 //     ...
68 // } params;
69 //
70 // PAL_TRY {
71 //   PAL_TRY {
72 //       ...
73 //       if (error) goto Done;
74 //       ...
75 //   Done: ;
76 //   } WIN_PAL_EXCEPT_FILTER(OtherFilter, &params) {
77 //   ...
78 //   }
79 //   WIN_PAL_ENDTRY
80 // }
81 // WIN_PAL_FINALLY {
82 // }
83 // WIN_PAL_ENDTRY
84 //
85
86
87
88 #if defined(_DEBUG_IMPL) && !defined(JIT_BUILD) && !defined(JIT64_BUILD) && !defined(_ARM_) // @ARMTODO
89 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN                                           \
90     BOOL ___oldOkayToThrowValue = FALSE;                                        \
91     BOOL ___oldSOTolerantState = FALSE;                                         \
92     ClrDebugState *___pState = GetClrDebugState();                              \
93     __try                                                                       \
94     {                                                                           \
95         ___oldOkayToThrowValue = ___pState->IsOkToThrow();                      \
96         ___oldSOTolerantState = ___pState->IsSOTolerant();                      \
97         ___pState->SetOkToThrow(TRUE);                                          \
98         ANNOTATION_TRY_BEGIN;
99
100 // Special version that avoids touching the debug state after doing work in a DllMain for process or thread detach.
101 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN_DLLMAIN(_reason)                          \
102     BOOL ___oldOkayToThrowValue = FALSE;                                        \
103     BOOL ___oldSOTolerantState = FALSE;                                         \
104     ClrDebugState *___pState = CheckClrDebugState();                            \
105     __try                                                                       \
106     {                                                                           \
107         if (___pState)                                                          \
108         {                                                                       \
109             ___oldOkayToThrowValue = ___pState->IsOkToThrow();                  \
110             ___oldSOTolerantState = ___pState->IsSOTolerant();                  \
111             ___pState->SetOkToThrow(TRUE);                                      \
112         }                                                                       \
113         if ((_reason == DLL_PROCESS_DETACH) || (_reason == DLL_THREAD_DETACH))  \
114         {                                                                       \
115             ___pState = NULL;                                                   \
116         }                                                                       \
117         ANNOTATION_TRY_BEGIN;
118
119 #define WIN_PAL_TRY_HANDLER_DBG_END                                             \
120         ANNOTATION_TRY_END;                                                     \
121     }                                                                           \
122     __finally                                                                   \
123     {                                                                           \
124         if (___pState != NULL)                                                  \
125         {                                                                       \
126             _ASSERTE(___pState == CheckClrDebugState());                        \
127             ___pState->SetOkToThrow(___oldOkayToThrowValue);                    \
128             ___pState->SetSOTolerance(___oldSOTolerantState);                   \
129         }                                                                       \
130     }
131
132 #define WIN_PAL_ENDTRY_NAKED_DBG                                                \
133     if (__exHandled)                                                            \
134     {                                                                           \
135         RESTORE_SO_TOLERANCE_STATE;                                             \
136     }                                                                           \
137     
138 #else
139 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN                   ANNOTATION_TRY_BEGIN;
140 #define WIN_PAL_TRY_HANDLER_DBG_BEGIN_DLLMAIN(_reason)  ANNOTATION_TRY_BEGIN;
141 #define WIN_PAL_TRY_HANDLER_DBG_END                     ANNOTATION_TRY_END;
142 #define WIN_PAL_ENDTRY_NAKED_DBG                                                          
143 #endif // defined(ENABLE_CONTRACTS_IMPL) && !defined(JIT64_BUILD)
144
145 #endif  // __PALCLR_WIN_H__