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.

108 lines
1.9 KiB

  1. /*++
  2. Copyright (c) 2001, Microsoft Corporation
  3. Module Name:
  4. tls.h
  5. Abstract:
  6. This file defines the TLS.
  7. Author:
  8. Revision History:
  9. Notes:
  10. --*/
  11. #ifndef _TLS_H_
  12. #define _TLS_H_
  13. class TLS
  14. {
  15. public:
  16. static inline void Initialize()
  17. {
  18. dwTLSIndex = TlsAlloc();
  19. }
  20. static inline void Uninitialize()
  21. {
  22. TlsFree(dwTLSIndex);
  23. }
  24. static inline TLS* GetTLS()
  25. {
  26. //
  27. // Should allocate TLS data if doesn't exist.
  28. //
  29. return InternalAllocateTLS();
  30. }
  31. static inline TLS* ReferenceTLS()
  32. {
  33. //
  34. // Shouldn't allocate TLS data even TLS data doesn't exist.
  35. //
  36. return (TLS*)TlsGetValue(dwTLSIndex);
  37. }
  38. static inline BOOL DestroyTLS()
  39. {
  40. return InternalDestroyTLS();
  41. }
  42. inline int IncrementAIMMRefCnt()
  43. {
  44. return ++_fActivateCnt;
  45. }
  46. inline int DecrementAIMMRefCnt()
  47. {
  48. if (_fActivateCnt)
  49. return --_fActivateCnt;
  50. else
  51. return -1;
  52. }
  53. private:
  54. int _fActivateCnt;
  55. private:
  56. static inline TLS* InternalAllocateTLS()
  57. {
  58. TLS* ptls = (TLS*)TlsGetValue(dwTLSIndex);
  59. if (ptls == NULL)
  60. {
  61. if ((ptls = (TLS*)cicMemAllocClear(sizeof(TLS))) == NULL)
  62. return NULL;
  63. if (! TlsSetValue(dwTLSIndex, ptls))
  64. {
  65. cicMemFree(ptls);
  66. return NULL;
  67. }
  68. }
  69. return ptls;
  70. }
  71. static BOOL InternalDestroyTLS()
  72. {
  73. TLS* ptls = (TLS*)TlsGetValue(dwTLSIndex);
  74. if (ptls != NULL)
  75. {
  76. cicMemFree(ptls);
  77. TlsSetValue(dwTLSIndex, NULL);
  78. return TRUE;
  79. }
  80. return FALSE;
  81. }
  82. private:
  83. static DWORD dwTLSIndex;
  84. };
  85. #endif // _TLS_H_