lock.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. lock.c
  9. Abstract:
  10. This module implements "lock" services for the EFI core. Now don't get
  11. excited, UEFI really is single threaded. This is more of a validation than
  12. any real synchronization.
  13. Author:
  14. Evan Green 28-Feb-2014
  15. Environment:
  16. Firmware
  17. --*/
  18. //
  19. // ------------------------------------------------------------------- Includes
  20. //
  21. #include "ueficore.h"
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. //
  29. // ----------------------------------------------- Internal Function Prototypes
  30. //
  31. //
  32. // -------------------------------------------------------------------- Globals
  33. //
  34. //
  35. // ------------------------------------------------------------------ Functions
  36. //
  37. VOID
  38. EfiCoreInitializeLock (
  39. PEFI_LOCK Lock,
  40. EFI_TPL Tpl
  41. )
  42. /*++
  43. Routine Description:
  44. This routine initializes an EFI lock.
  45. Arguments:
  46. Lock - Supplies a pointer to the lock to initialize.
  47. Tpl - Supplies the Task Prioriry Level the lock is acquired at.
  48. Return Value:
  49. None.
  50. --*/
  51. {
  52. Lock->Tpl = Tpl;
  53. Lock->OwnerTpl = TPL_APPLICATION;
  54. Lock->State = EfiLockReleased;
  55. return;
  56. }
  57. EFI_STATUS
  58. EfiCoreAcquireLockOrFail (
  59. PEFI_LOCK Lock
  60. )
  61. /*++
  62. Routine Description:
  63. This routine attempts to acquire the given lock, and fails if it is already
  64. held.
  65. Arguments:
  66. Lock - Supplies a pointer to the lock to try to acquire.
  67. Return Value:
  68. EFI_SUCCESS if the lock was successfully acquired.
  69. EFI_ACCESS_DENIED if the lock was already held and could not be acquired.
  70. --*/
  71. {
  72. ASSERT((Lock != NULL) && (Lock->State != EfiLockUninitialized));
  73. if (Lock->State == EfiLockAcquired) {
  74. return EFI_ACCESS_DENIED;
  75. }
  76. Lock->OwnerTpl = EfiCoreRaiseTpl(Lock->Tpl);
  77. Lock->State = EfiLockAcquired;
  78. return EFI_SUCCESS;
  79. }
  80. VOID
  81. EfiCoreAcquireLock (
  82. PEFI_LOCK Lock
  83. )
  84. /*++
  85. Routine Description:
  86. This routine raises to the task priority level of the given lock and
  87. acquires the lock.
  88. Arguments:
  89. Lock - Supplies a pointer to the lock to acquire.
  90. Return Value:
  91. None.
  92. --*/
  93. {
  94. ASSERT((Lock != NULL) && (Lock->State == EfiLockReleased));
  95. Lock->OwnerTpl = EfiCoreRaiseTpl(Lock->Tpl);
  96. Lock->State = EfiLockAcquired;
  97. return;
  98. }
  99. VOID
  100. EfiCoreReleaseLock (
  101. PEFI_LOCK Lock
  102. )
  103. /*++
  104. Routine Description:
  105. This routine releases ownership of the given lock, and lowers back down
  106. to the original TPL.
  107. Arguments:
  108. Lock - Supplies a pointer to the lock to release.
  109. Return Value:
  110. None.
  111. --*/
  112. {
  113. EFI_TPL Tpl;
  114. ASSERT((Lock != NULL) && (Lock->State == EfiLockAcquired));
  115. Tpl = Lock->OwnerTpl;
  116. Lock->State = EfiLockReleased;
  117. EfiCoreRestoreTpl(Tpl);
  118. return;
  119. }
  120. BOOLEAN
  121. EfiCoreIsLockHeld (
  122. PEFI_LOCK Lock
  123. )
  124. /*++
  125. Routine Description:
  126. This routine determines if the given lock is held.
  127. Arguments:
  128. Lock - Supplies a pointer to the lock to query.
  129. Return Value:
  130. TRUE if the lock is held.
  131. FALSE if the lock is not held.
  132. --*/
  133. {
  134. ASSERT((Lock != NULL) && (Lock->State != EfiLockUninitialized));
  135. if (Lock->State == EfiLockAcquired) {
  136. return TRUE;
  137. }
  138. return FALSE;
  139. }
  140. //
  141. // --------------------------------------------------------- Internal Functions
  142. //