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.

194 lines
4.2 KiB

  1. //============================================================================
  2. // Copyright (c) 1996, Microsoft Corporation
  3. //
  4. // File: sync.c
  5. //
  6. // History:
  7. // Abolade Gbadegesin Jan-12-1996 Created.
  8. //
  9. // Synchronization routines used by IPBOOTP.
  10. //============================================================================
  11. #include "pchbootp.h"
  12. //----------------------------------------------------------------------------
  13. // Function: QueueBootpWorker
  14. //
  15. // This function is called to queue a BOOTP function in a safe fashion;
  16. // if cleanup is in progress or if RIP has stopped, this function
  17. // discards the work-item.
  18. //----------------------------------------------------------------------------
  19. DWORD
  20. QueueBootpWorker(
  21. WORKERFUNCTION pFunction,
  22. PVOID pContext
  23. ) {
  24. DWORD dwErr = NO_ERROR;
  25. EnterCriticalSection(&ig.IG_CS);
  26. if (ig.IG_Status != IPBOOTP_STATUS_RUNNING) {
  27. //
  28. // cannot queue a work function when RIP has quit or is quitting
  29. //
  30. dwErr = ERROR_CAN_NOT_COMPLETE;
  31. }
  32. else {
  33. BOOL bSuccess;
  34. ++ig.IG_ActivityCount;
  35. bSuccess = QueueUserWorkItem(
  36. (LPTHREAD_START_ROUTINE)pFunction,
  37. pContext, 0
  38. );
  39. if (!bSuccess) {
  40. dwErr = GetLastError();
  41. --ig.IG_ActivityCount;
  42. }
  43. }
  44. LeaveCriticalSection(&ig.IG_CS);
  45. return dwErr;
  46. }
  47. //----------------------------------------------------------------------------
  48. // Function: EnterBootpAPI
  49. //
  50. // This function is called to when entering a BOOTP api, as well as
  51. // when entering the input thread and timer thread.
  52. // It checks to see if BOOTP has stopped, and if so it quits; otherwise
  53. // it increments the count of active threads.
  54. //----------------------------------------------------------------------------
  55. BOOL
  56. EnterBootpAPI(
  57. ) {
  58. BOOL bEntered;
  59. EnterCriticalSection(&ig.IG_CS);
  60. if (ig.IG_Status == IPBOOTP_STATUS_RUNNING) {
  61. //
  62. // BOOTP is running, so the API may continue
  63. //
  64. ++ig.IG_ActivityCount;
  65. bEntered = TRUE;
  66. }
  67. else {
  68. //
  69. // BOOTP is not running, so the API exits quietly
  70. //
  71. bEntered = FALSE;
  72. }
  73. LeaveCriticalSection(&ig.IG_CS);
  74. return bEntered;
  75. }
  76. //----------------------------------------------------------------------------
  77. // Function: EnterBootpWorker
  78. //
  79. // This function is called when entering a BOOTP worker-function.
  80. // Since there is a lapse between the time a worker-function is queued
  81. // and the time the function is actually invoked by a worker thread,
  82. // this function must check to see if BOOTP has stopped or is stopping;
  83. // if this is the case, then it decrements the activity count,
  84. // releases the activity semaphore, and quits.
  85. //----------------------------------------------------------------------------
  86. BOOL
  87. EnterBootpWorker(
  88. ) {
  89. BOOL bEntered;
  90. EnterCriticalSection(&ig.IG_CS);
  91. if (ig.IG_Status == IPBOOTP_STATUS_RUNNING) {
  92. //
  93. // BOOTP is running, so the function may continue
  94. //
  95. bEntered = TRUE;
  96. }
  97. else
  98. if (ig.IG_Status == IPBOOTP_STATUS_STOPPING) {
  99. //
  100. // BOOTP is not running, but it was, so the function must stop.
  101. //
  102. --ig.IG_ActivityCount;
  103. ReleaseSemaphore(ig.IG_ActivitySemaphore, 1, NULL);
  104. bEntered = FALSE;
  105. }
  106. else {
  107. //
  108. // BOOTP probably never started. quit quietly
  109. //
  110. bEntered = FALSE;
  111. }
  112. LeaveCriticalSection(&ig.IG_CS);
  113. return bEntered;
  114. }
  115. //----------------------------------------------------------------------------
  116. // Function: LeaveBootpWorker
  117. //
  118. // This function is called when leaving a BOOTP API or worker function.
  119. // It decrements the activity count, and if it detects that BOOTP has stopped
  120. // or is stopping, it releases the activity semaphore.
  121. //----------------------------------------------------------------------------
  122. VOID
  123. LeaveBootpWorker(
  124. ) {
  125. EnterCriticalSection(&ig.IG_CS);
  126. --ig.IG_ActivityCount;
  127. if (ig.IG_Status == IPBOOTP_STATUS_STOPPING) {
  128. ReleaseSemaphore(ig.IG_ActivitySemaphore, 1, NULL);
  129. }
  130. LeaveCriticalSection(&ig.IG_CS);
  131. }