mutex.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * mutex.c
  6. *
  7. * This file implements a mutual-exclusion locking facility for Modules
  8. * using the NSS Cryptoki Framework.
  9. */
  10. #ifndef CK_T
  11. #include "ck.h"
  12. #endif /* CK_T */
  13. /*
  14. * NSSCKFWMutex
  15. *
  16. * NSSCKFWMutex_Destroy
  17. * NSSCKFWMutex_Lock
  18. * NSSCKFWMutex_Unlock
  19. *
  20. * nssCKFWMutex_Create
  21. * nssCKFWMutex_Destroy
  22. * nssCKFWMutex_Lock
  23. * nssCKFWMutex_Unlock
  24. *
  25. * -- debugging versions only --
  26. * nssCKFWMutex_verifyPointer
  27. *
  28. */
  29. struct NSSCKFWMutexStr {
  30. PRLock *lock;
  31. };
  32. #ifdef DEBUG
  33. /*
  34. * But first, the pointer-tracking stuff.
  35. *
  36. * NOTE: the pointer-tracking support in NSS/base currently relies
  37. * upon NSPR's CallOnce support. That, however, relies upon NSPR's
  38. * locking, which is tied into the runtime. We need a pointer-tracker
  39. * implementation that uses the locks supplied through C_Initialize.
  40. * That support, however, can be filled in later. So for now, I'll
  41. * just do this routines as no-ops.
  42. */
  43. static CK_RV
  44. mutex_add_pointer(
  45. const NSSCKFWMutex *fwMutex)
  46. {
  47. return CKR_OK;
  48. }
  49. static CK_RV
  50. mutex_remove_pointer(
  51. const NSSCKFWMutex *fwMutex)
  52. {
  53. return CKR_OK;
  54. }
  55. NSS_IMPLEMENT CK_RV
  56. nssCKFWMutex_verifyPointer(
  57. const NSSCKFWMutex *fwMutex)
  58. {
  59. return CKR_OK;
  60. }
  61. #endif /* DEBUG */
  62. /*
  63. * nssCKFWMutex_Create
  64. *
  65. */
  66. NSS_EXTERN NSSCKFWMutex *
  67. nssCKFWMutex_Create(
  68. CK_C_INITIALIZE_ARGS_PTR pInitArgs,
  69. CryptokiLockingState LockingState,
  70. NSSArena *arena,
  71. CK_RV *pError)
  72. {
  73. NSSCKFWMutex *mutex;
  74. mutex = nss_ZNEW(arena, NSSCKFWMutex);
  75. if (!mutex) {
  76. *pError = CKR_HOST_MEMORY;
  77. return (NSSCKFWMutex *)NULL;
  78. }
  79. *pError = CKR_OK;
  80. mutex->lock = NULL;
  81. if (LockingState == MultiThreaded) {
  82. mutex->lock = PR_NewLock();
  83. if (!mutex->lock) {
  84. *pError = CKR_HOST_MEMORY; /* we couldn't get the resource */
  85. }
  86. }
  87. if (CKR_OK != *pError) {
  88. (void)nss_ZFreeIf(mutex);
  89. return (NSSCKFWMutex *)NULL;
  90. }
  91. #ifdef DEBUG
  92. *pError = mutex_add_pointer(mutex);
  93. if (CKR_OK != *pError) {
  94. if (mutex->lock) {
  95. PR_DestroyLock(mutex->lock);
  96. }
  97. (void)nss_ZFreeIf(mutex);
  98. return (NSSCKFWMutex *)NULL;
  99. }
  100. #endif /* DEBUG */
  101. return mutex;
  102. }
  103. /*
  104. * nssCKFWMutex_Destroy
  105. *
  106. */
  107. NSS_EXTERN CK_RV
  108. nssCKFWMutex_Destroy(
  109. NSSCKFWMutex *mutex)
  110. {
  111. CK_RV rv = CKR_OK;
  112. #ifdef NSSDEBUG
  113. rv = nssCKFWMutex_verifyPointer(mutex);
  114. if (CKR_OK != rv) {
  115. return rv;
  116. }
  117. #endif /* NSSDEBUG */
  118. if (mutex->lock) {
  119. PR_DestroyLock(mutex->lock);
  120. }
  121. #ifdef DEBUG
  122. (void)mutex_remove_pointer(mutex);
  123. #endif /* DEBUG */
  124. (void)nss_ZFreeIf(mutex);
  125. return rv;
  126. }
  127. /*
  128. * nssCKFWMutex_Lock
  129. *
  130. */
  131. NSS_EXTERN CK_RV
  132. nssCKFWMutex_Lock(
  133. NSSCKFWMutex *mutex)
  134. {
  135. #ifdef NSSDEBUG
  136. CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
  137. if (CKR_OK != rv) {
  138. return rv;
  139. }
  140. #endif /* NSSDEBUG */
  141. if (mutex->lock) {
  142. PR_Lock(mutex->lock);
  143. }
  144. return CKR_OK;
  145. }
  146. /*
  147. * nssCKFWMutex_Unlock
  148. *
  149. */
  150. NSS_EXTERN CK_RV
  151. nssCKFWMutex_Unlock(
  152. NSSCKFWMutex *mutex)
  153. {
  154. PRStatus nrv;
  155. #ifdef NSSDEBUG
  156. CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
  157. if (CKR_OK != rv) {
  158. return rv;
  159. }
  160. #endif /* NSSDEBUG */
  161. if (!mutex->lock)
  162. return CKR_OK;
  163. nrv = PR_Unlock(mutex->lock);
  164. /* if unlock fails, either we have a programming error, or we have
  165. * some sort of hardware failure... in either case return CKR_DEVICE_ERROR.
  166. */
  167. return nrv == PR_SUCCESS ? CKR_OK : CKR_DEVICE_ERROR;
  168. }
  169. /*
  170. * NSSCKFWMutex_Destroy
  171. *
  172. */
  173. NSS_EXTERN CK_RV
  174. NSSCKFWMutex_Destroy(
  175. NSSCKFWMutex *mutex)
  176. {
  177. #ifdef DEBUG
  178. CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
  179. if (CKR_OK != rv) {
  180. return rv;
  181. }
  182. #endif /* DEBUG */
  183. return nssCKFWMutex_Destroy(mutex);
  184. }
  185. /*
  186. * NSSCKFWMutex_Lock
  187. *
  188. */
  189. NSS_EXTERN CK_RV
  190. NSSCKFWMutex_Lock(
  191. NSSCKFWMutex *mutex)
  192. {
  193. #ifdef DEBUG
  194. CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
  195. if (CKR_OK != rv) {
  196. return rv;
  197. }
  198. #endif /* DEBUG */
  199. return nssCKFWMutex_Lock(mutex);
  200. }
  201. /*
  202. * NSSCKFWMutex_Unlock
  203. *
  204. */
  205. NSS_EXTERN CK_RV
  206. NSSCKFWMutex_Unlock(
  207. NSSCKFWMutex *mutex)
  208. {
  209. #ifdef DEBUG
  210. CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
  211. if (CKR_OK != rv) {
  212. return rv;
  213. }
  214. #endif /* DEBUG */
  215. return nssCKFWMutex_Unlock(mutex);
  216. }