Source code of Windows XP (NT5)
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.

159 lines
2.8 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. intbits.c
  5. Abstract:
  6. This module contains routines to do interlocked bit manipulation
  7. Author:
  8. Neill Clift (NeillC) 12-May-2000
  9. Environment:
  10. User and kernel mode.
  11. Revision History:
  12. Rob Earhart (earhart) October 13, 2000
  13. Moved from Ex to Rtl
  14. --*/
  15. #include "ntrtlp.h"
  16. #pragma hdrstop
  17. #undef RtlInterlockedSetBits
  18. NTKERNELAPI
  19. ULONG
  20. FASTCALL
  21. RtlInterlockedSetBits (
  22. IN OUT PULONG Flags,
  23. IN ULONG Flag
  24. )
  25. /*++
  26. Routine Description:
  27. This function atomically sets the specified flags in the target
  28. Arguments:
  29. Flags - Pointer to variable containing current mask.
  30. Flag - Flags to set in target
  31. Return Value:
  32. ULONG - Old value of mask before modification
  33. --*/
  34. {
  35. ULONG NewFlags, OldFlags;
  36. OldFlags = *Flags;
  37. NewFlags = OldFlags | Flag;
  38. while (NewFlags != OldFlags) {
  39. NewFlags = InterlockedCompareExchange ((PLONG) Flags, (LONG) NewFlags, (LONG) OldFlags);
  40. if (NewFlags == OldFlags) {
  41. break;
  42. }
  43. OldFlags = NewFlags;
  44. NewFlags |= Flag;
  45. }
  46. return OldFlags;
  47. }
  48. #undef RtlInterlockedClearBits
  49. NTKERNELAPI
  50. ULONG
  51. FASTCALL
  52. RtlInterlockedClearBits (
  53. IN OUT PULONG Flags,
  54. IN ULONG Flag
  55. )
  56. /*++
  57. Routine Description:
  58. This function atomically clears the specified flags in the target
  59. Arguments:
  60. Flags - Pointer to variable containing current mask.
  61. Flag - Flags to clear in target
  62. Return Value:
  63. ULONG - Old value of mask before modification
  64. --*/
  65. {
  66. ULONG NewFlags, OldFlags;
  67. OldFlags = *Flags;
  68. NewFlags = OldFlags & ~Flag;
  69. while (NewFlags != OldFlags) {
  70. NewFlags = InterlockedCompareExchange ((PLONG) Flags, (LONG) NewFlags, (LONG) OldFlags);
  71. if (NewFlags == OldFlags) {
  72. break;
  73. }
  74. OldFlags = NewFlags;
  75. NewFlags &= ~Flag;
  76. }
  77. return OldFlags;
  78. }
  79. #undef RtlInterlockedSetClearBits
  80. NTKERNELAPI
  81. ULONG
  82. FASTCALL
  83. RtlInterlockedSetClearBits (
  84. IN OUT PULONG Flags,
  85. IN ULONG sFlag,
  86. IN ULONG cFlag
  87. )
  88. /*++
  89. Routine Description:
  90. This function atomically sets and clears the specified flags in the target
  91. Arguments:
  92. Flags - Pointer to variable containing current mask.
  93. sFlag - Flags to set in target
  94. CFlag - Flags to clear in target
  95. Return Value:
  96. ULONG - Old value of mask before modification
  97. --*/
  98. {
  99. ULONG NewFlags, OldFlags;
  100. OldFlags = *Flags;
  101. NewFlags = (OldFlags | sFlag) & ~cFlag;
  102. while (NewFlags != OldFlags) {
  103. NewFlags = InterlockedCompareExchange ((PLONG) Flags, (LONG) NewFlags, (LONG) OldFlags);
  104. if (NewFlags == OldFlags) {
  105. break;
  106. }
  107. OldFlags = NewFlags;
  108. NewFlags = (NewFlags | sFlag) & ~cFlag;
  109. }
  110. return OldFlags;
  111. }