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+ using System ;
6+ using System . Runtime . InteropServices ;
7+
8+ static class GCPollNative
9+ {
10+ // Simple function that can be marked as SuppressGCTransition which will
11+ // result in a GCPoll insertion.
12+ [ DllImport ( nameof ( GCPollNative ) ) ]
13+ [ SuppressGCTransition ]
14+ public static extern uint NextUInt32 ( uint n ) ;
15+
16+ // Simple function that can be marked as SuppressGCTransition which will
17+ // result in a GCPoll insertion.
18+ [ DllImport ( nameof ( GCPollNative ) ) ]
19+ [ SuppressGCTransition ]
20+ public static extern ulong NextUInt64 ( ulong n ) ;
21+ }
22+
23+ class InsertGCPoll
24+ {
25+ private static int PropNextInt32 => ( int ) GCPollNative . NextUInt32 ( 0 ) ;
26+ private static long PropNextInt64 => ( long ) GCPollNative . NextUInt64 ( 0 ) ;
27+
28+ private static void AccessAsProperty32 ( )
29+ {
30+ int a = PropNextInt32 ;
31+ int b = PropNextInt32 ;
32+ DisplayValues ( a , b ) ;
33+ }
34+
35+ private static void AccessAsProperty64 ( )
36+ {
37+ long a = PropNextInt64 ;
38+ long b = PropNextInt64 ;
39+ DisplayValues ( a , b ) ;
40+ }
41+
42+ private static void DisplayValues < T > ( T a , T b )
43+ {
44+ Console . WriteLine ( $ "{ a } { b } ") ;
45+ }
46+
47+ private static void BranchOnProperty32 ( )
48+ {
49+ if ( - 1 == PropNextInt32 )
50+ {
51+ Console . WriteLine ( "" ) ;
52+ }
53+ }
54+
55+ private static void BranchOnProperty64 ( )
56+ {
57+ if ( - 1 == PropNextInt64 )
58+ {
59+ Console . WriteLine ( "" ) ;
60+ }
61+ }
62+
63+ private static void CompoundStatementBranchOnProperty ( )
64+ {
65+ if ( - 1 == ( PropNextInt64 + PropNextInt32 - PropNextInt64 + PropNextInt64 - PropNextInt32 ) )
66+ {
67+ Console . WriteLine ( "" ) ;
68+ }
69+ }
70+
71+ private static void LoopOn32 ( )
72+ {
73+ uint i = 0 ;
74+ for ( int j = 0 ; j < 10 || i < 32 ; ++ j )
75+ {
76+ i += GCPollNative . NextUInt32 ( 1 ) ;
77+ }
78+ }
79+
80+ private static void LoopOn64 ( )
81+ {
82+ ulong i = 0 ;
83+ for ( int j = 0 ; j < 10 || i < 32 ; ++ j )
84+ {
85+ i += GCPollNative . NextUInt64 ( 1 ) ;
86+ }
87+ }
88+
89+ public static int Main ( )
90+ {
91+ try
92+ {
93+ AccessAsProperty32 ( ) ;
94+ AccessAsProperty64 ( ) ;
95+ BranchOnProperty32 ( ) ;
96+ BranchOnProperty64 ( ) ;
97+ CompoundStatementBranchOnProperty ( ) ;
98+ LoopOn32 ( ) ;
99+ LoopOn64 ( ) ;
100+ }
101+ catch ( Exception e )
102+ {
103+ Console . WriteLine ( e . ToString ( ) ) ;
104+ return 101 ;
105+ }
106+ return 100 ;
107+ }
108+ }
0 commit comments