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
4.2 KiB

  1. #ifndef _THREAD_LOCAL_STORE_HPP_
  2. #define _THREAD_LOCAL_STORE_HPP_
  3. // Ruler
  4. // 1 2 3 4 5 6 7 8
  5. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  6. /********************************************************************/
  7. /* */
  8. /* The standard layout. */
  9. /* */
  10. /* The standard layout for 'hpp' files for this code is as */
  11. /* follows: */
  12. /* */
  13. /* 1. Include files. */
  14. /* 2. Constants exported from the class. */
  15. /* 3. Data structures exported from the class. */
  16. /* 4. Forward references to other data structures. */
  17. /* 5. Class specifications (including inline functions). */
  18. /* 6. Additional large inline functions. */
  19. /* */
  20. /* Any portion that is not required is simply omitted. */
  21. /* */
  22. /********************************************************************/
  23. #include "Global.hpp"
  24. #include "Assembly.hpp"
  25. /********************************************************************/
  26. /* */
  27. /* Constants exported from the class. */
  28. /* */
  29. /* The environment constants indicate the state of thread */
  30. /* local store. */
  31. /* */
  32. /********************************************************************/
  33. CONST SBIT32 NoTLSMemory = -1;
  34. CONST SBIT32 NoTLSStructure = 0;
  35. /********************************************************************/
  36. /* */
  37. /* Thread local store. */
  38. /* */
  39. /* A wide range of applications use threads. It is often very */
  40. /* valuable to be able to have some private per thread data. */
  41. /* This functionality is supported by the following class. */
  42. /* */
  43. /********************************************************************/
  44. class THREAD_LOCAL_STORE : public ASSEMBLY
  45. {
  46. //
  47. // Private data.
  48. //
  49. BOOLEAN Active;
  50. SBIT32 TlsIndex;
  51. SBIT32 TlsOffset;
  52. public:
  53. //
  54. // Public functions.
  55. //
  56. THREAD_LOCAL_STORE( VOID )
  57. {
  58. if ( Active = ((TlsIndex = TlsAlloc()) != NoTLSMemory) )
  59. { TlsOffset = ((TlsIndex * sizeof(void*)) + TebSlot); }
  60. else
  61. { Failure( "No TLS available" ); }
  62. }
  63. INLINE BOOLEAN Available( VOID )
  64. { return Active; }
  65. #ifdef ASSEMBLY_X86
  66. #ifdef ENABLE_NON_STANDARD_ASSEMBLY
  67. INLINE VOID *GetAddress( VOID )
  68. { return (GetTlsAddress( TlsOffset )); }
  69. #endif
  70. #endif
  71. INLINE VOID *GetPointer( VOID )
  72. { return (GetTlsValue( TlsIndex,TlsOffset )); }
  73. INLINE VOID SetPointer( VOID *NewPointer )
  74. { SetTlsValue( TlsIndex,TlsOffset,NewPointer ); }
  75. ~THREAD_LOCAL_STORE( VOID )
  76. {
  77. Active = False;
  78. if ( TlsIndex != NoTLSMemory )
  79. {
  80. if ( ! TlsFree( TlsIndex ) )
  81. { Failure( "Unable to free TLS memory" ); }
  82. }
  83. }
  84. private:
  85. //
  86. // Disabled operations.
  87. //
  88. THREAD_LOCAL_STORE( CONST THREAD_LOCAL_STORE & Copy );
  89. VOID operator=( CONST THREAD_LOCAL_STORE & Copy );
  90. };
  91. #endif