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
6//
7
8#ifndef _HANDLETABLE_INL
9#define _HANDLETABLE_INL
10
11inline void HndAssignHandle(OBJECTHANDLE handle, OBJECTREF objref)
12{
13 CONTRACTL
14 {
15 NOTHROW;
16 GC_NOTRIGGER;
17 SO_TOLERANT;
18 MODE_COOPERATIVE;
19 }
20 CONTRACTL_END;
21
22 // sanity
23 _ASSERTE(handle);
24
25 // unwrap the objectref we were given
26 _UNCHECKED_OBJECTREF value = OBJECTREF_TO_UNCHECKED_OBJECTREF(objref);
27
28 HndLogSetEvent(handle, value);
29
30 // if we are doing a non-NULL pointer store then invoke the write-barrier
31 if (value)
32 HndWriteBarrier(handle, objref);
33
34 // store the pointer
35 *(_UNCHECKED_OBJECTREF *)handle = value;
36}
37
38inline void* HndInterlockedCompareExchangeHandle(OBJECTHANDLE handle, OBJECTREF objref, OBJECTREF oldObjref)
39{
40 WRAPPER_NO_CONTRACT;
41
42 // sanity
43 _ASSERTE(handle);
44
45 // unwrap the objectref we were given
46 _UNCHECKED_OBJECTREF value = OBJECTREF_TO_UNCHECKED_OBJECTREF(objref);
47 _UNCHECKED_OBJECTREF oldValue = OBJECTREF_TO_UNCHECKED_OBJECTREF(oldObjref);
48
49 // if we are doing a non-NULL pointer store then invoke the write-barrier
50 if (value)
51 HndWriteBarrier(handle, objref);
52
53 // store the pointer
54
55 void* ret = Interlocked::CompareExchangePointer(reinterpret_cast<_UNCHECKED_OBJECTREF volatile*>(handle), value, oldValue);
56
57 if (ret == oldValue)
58 HndLogSetEvent(handle, value);
59
60 return ret;
61}
62
63inline BOOL HndFirstAssignHandle(OBJECTHANDLE handle, OBJECTREF objref)
64{
65 CONTRACTL
66 {
67 NOTHROW;
68 GC_NOTRIGGER;
69 SO_TOLERANT;
70 MODE_COOPERATIVE;
71 }
72 CONTRACTL_END;
73
74 // sanity
75 _ASSERTE(handle);
76
77 // unwrap the objectref we were given
78 _UNCHECKED_OBJECTREF value = OBJECTREF_TO_UNCHECKED_OBJECTREF(objref);
79 _UNCHECKED_OBJECTREF null = NULL;
80
81 // store the pointer if we are the first ones here
82 BOOL success = (NULL == Interlocked::CompareExchangePointer(reinterpret_cast<_UNCHECKED_OBJECTREF volatile*>(handle),
83 value,
84 null));
85
86 // if we successfully did a non-NULL pointer store then invoke the write-barrier
87 if (success)
88 {
89 if (value)
90 HndWriteBarrier(handle, objref);
91
92 HndLogSetEvent(handle, value);
93 }
94
95 // return our result
96 return success;
97}
98
99#endif // _HANDLETABLE_INL
100