Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
7.9 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #ifndef _THREAD_H_
  13. #define _THREAD_H_
  14. #define THREAD_STARED 100
  15. #define WAIT_EVENT_0 WAIT_OBJECT_0
  16. #define WAIT_EVENT_FAILED (MAXIMUM_WAIT_OBJECTS+1)
  17. #define WAIT_EVENT_ABANDONED (MAXIMUM_WAIT_OBJECTS+2)
  18. #define WAIT_EVENT_TERMINATED (MAXIMUM_WAIT_OBJECTS+3)
  19. typedef void (*PVOIDTHREADPROC)(void *);
  20. /*
  21. * Each connection is saved so that we can enumerate, delete, and trigger
  22. * This template provides the container for the connections and cookies
  23. */
  24. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  25. class EventMap : public CObject
  26. {
  27. private:
  28. CMap <KEY, ARG_KEY, VALUE, ARG_VALUE> m_cmap ;
  29. protected:
  30. public:
  31. EventMap () ;
  32. virtual ~EventMap () ;
  33. int GetCount () const ;
  34. BOOL IsEmpty () const ;
  35. BOOL Lookup(ARG_KEY key, VALUE& rValue) const ;
  36. VALUE& operator[](ARG_KEY key) ;
  37. void SetAt(ARG_KEY key, ARG_VALUE newValue) ;
  38. BOOL RemoveKey(ARG_KEY key) ;
  39. void RemoveAll () ;
  40. POSITION GetStartPosition() const ;
  41. void GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const ;
  42. } ;
  43. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  44. EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: EventMap ()
  45. {
  46. }
  47. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  48. EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: ~EventMap ()
  49. {
  50. }
  51. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  52. int EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: GetCount() const
  53. {
  54. int count = m_cmap.GetCount () ;
  55. return count ;
  56. }
  57. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  58. BOOL EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: IsEmpty() const
  59. {
  60. return m_cmap.IsEmpty () ;
  61. }
  62. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  63. BOOL EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: Lookup(ARG_KEY key, VALUE& rValue) const
  64. {
  65. return m_cmap.Lookup ( key , rValue ) ;
  66. }
  67. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  68. VALUE& EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: operator[](ARG_KEY key)
  69. {
  70. return m_cmap.operator [] ( key ) ;
  71. }
  72. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  73. void EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: SetAt(ARG_KEY key, ARG_VALUE newValue)
  74. {
  75. m_cmap.SetAt ( key , newValue ) ;
  76. }
  77. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  78. BOOL EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: RemoveKey(ARG_KEY key)
  79. {
  80. return m_cmap.RemoveKey ( key ) ;
  81. }
  82. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  83. void EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: RemoveAll()
  84. {
  85. m_cmap.RemoveAll () ;
  86. }
  87. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  88. POSITION EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: GetStartPosition() const
  89. {
  90. return m_cmap.GetStartPosition () ;
  91. }
  92. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  93. void EventMap <KEY, ARG_KEY, VALUE, ARG_VALUE>:: GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const
  94. {
  95. m_cmap.GetNextAssoc ( rNextPosition , rKey , rValue ) ;
  96. }
  97. /*InterfaceGarbageCollector makes it easier to use the interface
  98. *by remembering to release them when you fall out of scope. The is
  99. *useful when you are using an IMosProvider and have lots of points
  100. *of failure; you can just give up and let the wrapper clean up for
  101. *you.
  102. */
  103. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  104. class ThreadMap : public CObject
  105. {
  106. private:
  107. CCriticalSection * criticalSection ;
  108. CMap <KEY, ARG_KEY, VALUE, ARG_VALUE> cmap ;
  109. protected:
  110. public:
  111. ThreadMap ();
  112. virtual ~ThreadMap () ;
  113. int GetCount () const ;
  114. BOOL IsEmpty () const ;
  115. BOOL Lookup(ARG_KEY key, VALUE& rValue) const ;
  116. VALUE& operator[](ARG_KEY key) ;
  117. void SetAt(ARG_KEY key, ARG_VALUE newValue) ;
  118. BOOL RemoveKey(ARG_KEY key) ;
  119. void RemoveAll () ;
  120. POSITION GetStartPosition() const ;
  121. void GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const ;
  122. } ;
  123. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  124. ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: ThreadMap () :
  125. criticalSection(NULL)
  126. {
  127. criticalSection = new CCriticalSection ;
  128. }
  129. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  130. ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: ~ThreadMap ()
  131. {
  132. //this may be null
  133. delete criticalSection ;
  134. }
  135. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  136. int ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: GetCount() const
  137. {
  138. criticalSection->Lock () ;
  139. int count = cmap.GetCount () ;
  140. criticalSection->Unlock () ;
  141. return count ;
  142. }
  143. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  144. BOOL ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: IsEmpty() const
  145. {
  146. if (TRUE == m_bMakeThreadSafe)
  147. {
  148. criticalSection->Lock () ;
  149. BOOL isEmpty = cmap.IsEmpty () ;
  150. criticalSection->Unlock () ;
  151. return isEmpty ;
  152. }
  153. else
  154. {
  155. return cmap.IsEmpty () ;
  156. }
  157. }
  158. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  159. BOOL ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: Lookup(ARG_KEY key, VALUE& rValue) const
  160. {
  161. criticalSection->Lock () ;
  162. BOOL lookup = cmap.Lookup ( key , rValue ) ;
  163. criticalSection->Unlock () ;
  164. return lookup ;
  165. }
  166. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  167. VALUE& ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: operator[](ARG_KEY key)
  168. {
  169. criticalSection->Lock () ;
  170. VALUE &value = cmap.operator [] ( key ) ;
  171. criticalSection->Unlock () ;
  172. return value ;
  173. }
  174. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  175. void ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: SetAt(ARG_KEY key, ARG_VALUE newValue)
  176. {
  177. criticalSection->Lock () ;
  178. cmap.SetAt ( key , newValue ) ;
  179. criticalSection->Unlock () ;
  180. }
  181. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  182. BOOL ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: RemoveKey(ARG_KEY key)
  183. {
  184. criticalSection->Lock () ;
  185. BOOL removeKey = cmap.RemoveKey ( key ) ;
  186. criticalSection->Unlock () ;
  187. return removeKey ;
  188. }
  189. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  190. void ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: RemoveAll()
  191. {
  192. criticalSection->Lock () ;
  193. cmap.RemoveAll () ;
  194. criticalSection->Unlock () ;
  195. }
  196. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  197. POSITION ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE> :: GetStartPosition() const
  198. {
  199. criticalSection->Lock () ;
  200. POSITION position = cmap.GetStartPosition () ;
  201. criticalSection->Unlock () ;
  202. return position ;
  203. }
  204. template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
  205. void ThreadMap <KEY, ARG_KEY, VALUE, ARG_VALUE>:: GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const
  206. {
  207. criticalSection->Lock () ;
  208. cmap.GetNextAssoc ( rNextPosition , rKey , rValue ) ;
  209. criticalSection->Unlock () ;
  210. }
  211. class CThread
  212. {
  213. private:
  214. BOOL bWaiting;
  215. HANDLE m_hThreadStopEvent;
  216. HANDLE m_hThreadSyncEvent;
  217. DWORD m_cRef;
  218. EventMap <HANDLE, HANDLE, HANDLE,HANDLE> m_UserEvents ;
  219. static ThreadMap <CThread*, CThread*, CThread*,CThread*> m_ThreadMap ;
  220. protected:
  221. uintptr_t m_ulThreadHandle;
  222. public:
  223. CThread();
  224. virtual ~CThread();
  225. SCODE Start();
  226. SCODE Stop();
  227. SCODE Wait(DWORD timeout = INFINITE);
  228. virtual SCODE Process(){return S_OK;};
  229. void AddEvent(HANDLE userEvent){m_UserEvents.SetAt(userEvent,userEvent);}
  230. void DeleteEvent(HANDLE userEvent){m_UserEvents.RemoveKey(userEvent);}
  231. void Exit(){SetEvent(m_hThreadSyncEvent);}
  232. ULONG AddRef(void){return ++m_cRef;}
  233. ULONG Release(void);
  234. operator void*();
  235. static void ProcessDetach();
  236. };
  237. #endif