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.

132 lines
2.4 KiB

  1. #include "brian.h"
  2. VOID
  3. InitEvents (
  4. )
  5. {
  6. NtCreateEvent( &EventEvent, SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,
  7. NULL, SynchronizationEvent, TRUE );
  8. RtlZeroMemory( Events, sizeof( Events ));
  9. }
  10. VOID
  11. UninitEvents (
  12. )
  13. {
  14. USHORT Index;
  15. //
  16. // Release any current events.
  17. //
  18. for (Index = 0; Index < MAX_EVENTS; Index++) {
  19. if (Events[Index].Used) {
  20. NtSetEvent( Events[Index].Handle, NULL );
  21. }
  22. }
  23. }
  24. NTSTATUS
  25. ObtainEvent (
  26. OUT PUSHORT NewIndex
  27. )
  28. {
  29. NTSTATUS Status;
  30. USHORT Index;
  31. //
  32. // Wait for the handle event
  33. //
  34. if ((Status = NtWaitForSingleObject( EventEvent,
  35. FALSE,
  36. NULL )) != STATUS_SUCCESS) {
  37. return Status;
  38. }
  39. //
  40. // Find an available index. Return STATUS_INSUFFICIENT_RESOURCES
  41. // if not found.
  42. //
  43. for (Index = 0; Index < MAX_EVENTS; Index++) {
  44. if (!Events[Index].Used) {
  45. break;
  46. }
  47. }
  48. if (Index >= MAX_EVENTS) {
  49. Status = STATUS_INSUFFICIENT_RESOURCES;
  50. //
  51. // Otherwise reserve this event index.
  52. //
  53. } else {
  54. Status = NtCreateEvent( &Events[Index].Handle,
  55. SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,
  56. NULL,
  57. SynchronizationEvent,
  58. FALSE );
  59. if (NT_SUCCESS( Status )) {
  60. Events[Index].Used = TRUE;
  61. *NewIndex = Index;
  62. }
  63. }
  64. NtSetEvent( EventEvent, NULL );
  65. return Status;
  66. }
  67. VOID
  68. FreeEvent (
  69. IN USHORT Index
  70. )
  71. {
  72. //
  73. // Return immediately if beyond the end of the valid events.
  74. //
  75. if (Index >= MAX_EVENTS) {
  76. return;
  77. }
  78. //
  79. // Grab the event for the events.
  80. //
  81. if (NtWaitForSingleObject( EventEvent, FALSE, NULL ) != STATUS_SUCCESS) {
  82. return;
  83. }
  84. //
  85. // Mark the index as unused and release the event if held.
  86. //
  87. if (Events[Index].Used) {
  88. Events[Index].Used = FALSE;
  89. NtSetEvent( Events[Index].Handle, NULL );
  90. }
  91. NtSetEvent( EventEvent, NULL );
  92. return;
  93. }