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 | |
9 | Module Name: |
10 | |
11 | palobjbase.hpp |
12 | |
13 | Abstract: |
14 | PAL object base class |
15 | |
16 | |
17 | |
18 | --*/ |
19 | |
20 | #ifndef _PALOBJBASE_HPP_ |
21 | #define _PALOBJBASE_HPP_ |
22 | |
23 | #include "pal/corunix.hpp" |
24 | #include "pal/cs.hpp" |
25 | #include "pal/thread.hpp" |
26 | |
27 | namespace CorUnix |
28 | { |
29 | class CSimpleDataLock : IDataLock |
30 | { |
31 | private: |
32 | |
33 | CRITICAL_SECTION m_cs; |
34 | bool m_fInitialized; |
35 | |
36 | public: |
37 | |
38 | CSimpleDataLock() |
39 | : |
40 | m_fInitialized(FALSE) |
41 | { |
42 | }; |
43 | |
44 | virtual ~CSimpleDataLock() |
45 | { |
46 | if (m_fInitialized) |
47 | { |
48 | InternalDeleteCriticalSection(&m_cs); |
49 | } |
50 | }; |
51 | |
52 | PAL_ERROR |
53 | Initialize( |
54 | void |
55 | ) |
56 | { |
57 | PAL_ERROR palError = NO_ERROR; |
58 | |
59 | InternalInitializeCriticalSection(&m_cs); |
60 | m_fInitialized = TRUE; |
61 | |
62 | return palError; |
63 | }; |
64 | |
65 | void |
66 | AcquireLock( |
67 | CPalThread *pthr, |
68 | IDataLock **pDataLock |
69 | ) |
70 | { |
71 | InternalEnterCriticalSection(pthr, &m_cs); |
72 | *pDataLock = static_cast<IDataLock*>(this); |
73 | }; |
74 | |
75 | virtual |
76 | void |
77 | ReleaseLock( |
78 | CPalThread *pthr, |
79 | bool fDataChanged |
80 | ) |
81 | { |
82 | InternalLeaveCriticalSection(pthr, &m_cs); |
83 | }; |
84 | |
85 | }; |
86 | |
87 | class CPalObjectBase : public IPalObject |
88 | { |
89 | template <class T> friend void InternalDelete(T *p); |
90 | |
91 | protected: |
92 | |
93 | LONG m_lRefCount; |
94 | |
95 | VOID *m_pvImmutableData; |
96 | VOID *m_pvLocalData; |
97 | |
98 | CObjectType *m_pot; |
99 | CObjectAttributes m_oa; |
100 | |
101 | CSimpleDataLock m_sdlLocalData; |
102 | |
103 | // |
104 | // The thread that initiated object cleanup |
105 | // |
106 | |
107 | CPalThread *m_pthrCleanup; |
108 | |
109 | virtual ~CPalObjectBase(); |
110 | |
111 | virtual |
112 | void |
113 | AcquireObjectDestructionLock( |
114 | CPalThread *pthr |
115 | ) = 0; |
116 | |
117 | virtual |
118 | bool |
119 | ReleaseObjectDestructionLock( |
120 | CPalThread *pthr, |
121 | bool fDestructionPending |
122 | ) = 0; |
123 | |
124 | public: |
125 | |
126 | CPalObjectBase( |
127 | CObjectType *pot |
128 | ) |
129 | : |
130 | m_lRefCount(1), |
131 | m_pvImmutableData(NULL), |
132 | m_pvLocalData(NULL), |
133 | m_pot(pot), |
134 | m_pthrCleanup(NULL) |
135 | { |
136 | }; |
137 | |
138 | virtual |
139 | PAL_ERROR |
140 | Initialize( |
141 | CPalThread *pthr, |
142 | CObjectAttributes *poa |
143 | ); |
144 | |
145 | // |
146 | // IPalObject routines |
147 | // |
148 | |
149 | virtual |
150 | CObjectType * |
151 | GetObjectType( |
152 | VOID |
153 | ); |
154 | |
155 | virtual |
156 | CObjectAttributes * |
157 | GetObjectAttributes( |
158 | VOID |
159 | ); |
160 | |
161 | virtual |
162 | PAL_ERROR |
163 | GetImmutableData( |
164 | void **ppvImmutableData // OUT |
165 | ); |
166 | |
167 | virtual |
168 | PAL_ERROR |
169 | GetProcessLocalData( |
170 | CPalThread *pthr, // IN, OPTIONAL |
171 | LockType eLockRequest, |
172 | IDataLock **ppDataLock, // OUT |
173 | void **ppvProcessLocalData // OUT |
174 | ); |
175 | |
176 | virtual |
177 | DWORD |
178 | AddReference( |
179 | void |
180 | ); |
181 | |
182 | virtual |
183 | DWORD |
184 | ReleaseReference( |
185 | CPalThread *pthr |
186 | ); |
187 | }; |
188 | } |
189 | |
190 | #endif // _PALOBJBASE_HPP_ |
191 | |
192 | |