/security/nss/lib/libpkix/pkix_pl_nss/system/pkix_pl_mutex.c

http://github.com/zpao/v8monkey · C · 196 lines · 92 code · 34 blank · 70 comment · 4 complexity · 087e3eef9fd75b58bf886ca2ff16955c MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the PKIX-C library.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Sun Microsystems, Inc.
  18. * Portions created by the Initial Developer are
  19. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Sun Microsystems, Inc.
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /*
  38. * pkix_pl_mutex.c
  39. *
  40. * Mutual Exclusion (Lock) Object Functions
  41. *
  42. */
  43. #include "pkix_pl_mutex.h"
  44. /* --Private-Functions-------------------------------------------- */
  45. /*
  46. * FUNCTION: pkix_pl_Mutex_Destroy
  47. * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
  48. */
  49. static PKIX_Error *
  50. pkix_pl_Mutex_Destroy(
  51. PKIX_PL_Object *object,
  52. void *plContext)
  53. {
  54. PKIX_PL_Mutex *mutex = NULL;
  55. PKIX_ENTER(MUTEX, "pkix_pl_Mutex_Destroy");
  56. PKIX_NULLCHECK_ONE(object);
  57. /* Sanity check: Test that "object" is a mutex */
  58. PKIX_CHECK(pkix_CheckType(object, PKIX_MUTEX_TYPE, plContext),
  59. PKIX_OBJECTNOTMUTEX);
  60. mutex = (PKIX_PL_Mutex*) object;
  61. PKIX_MUTEX_DEBUG("\tCalling PR_DestroyLock).\n");
  62. PR_DestroyLock(mutex->lock);
  63. mutex->lock = NULL;
  64. cleanup:
  65. PKIX_RETURN(MUTEX);
  66. }
  67. /*
  68. * FUNCTION: pkix_pl_Mutex_RegisterSelf
  69. * DESCRIPTION:
  70. * Registers PKIX_MUTEX_TYPE and its related functions with systemClasses[]
  71. * THREAD SAFETY:
  72. * Not Thread Safe - for performance and complexity reasons
  73. *
  74. * Since this function is only called by PKIX_PL_Initialize, which should
  75. * only be called once, it is acceptable that this function is not
  76. * thread-safe.
  77. */
  78. PKIX_Error *
  79. pkix_pl_Mutex_RegisterSelf(
  80. /* ARGSUSED */ void *plContext)
  81. {
  82. extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
  83. pkix_ClassTable_Entry entry;
  84. PKIX_ENTER(MUTEX, "pkix_pl_Mutex_RegisterSelf");
  85. entry.description = "Mutex";
  86. entry.objCounter = 0;
  87. entry.typeObjectSize = sizeof(PKIX_PL_Mutex);
  88. entry.destructor = pkix_pl_Mutex_Destroy;
  89. entry.equalsFunction = NULL;
  90. entry.hashcodeFunction = NULL;
  91. entry.toStringFunction = NULL;
  92. entry.comparator = NULL;
  93. entry.duplicateFunction = NULL;
  94. systemClasses[PKIX_MUTEX_TYPE] = entry;
  95. PKIX_RETURN(MUTEX);
  96. }
  97. /* --Public-Functions--------------------------------------------- */
  98. /*
  99. * FUNCTION: PKIX_PL_Mutex_Create (see comments in pkix_pl_system.h)
  100. */
  101. PKIX_Error *
  102. PKIX_PL_Mutex_Create(
  103. PKIX_PL_Mutex **pNewLock,
  104. void *plContext)
  105. {
  106. PKIX_PL_Mutex *mutex = NULL;
  107. PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Create");
  108. PKIX_NULLCHECK_ONE(pNewLock);
  109. PKIX_CHECK(PKIX_PL_Object_Alloc
  110. (PKIX_MUTEX_TYPE,
  111. sizeof (PKIX_PL_Mutex),
  112. (PKIX_PL_Object **)&mutex,
  113. plContext),
  114. PKIX_COULDNOTCREATELOCKOBJECT);
  115. PKIX_MUTEX_DEBUG("\tCalling PR_NewLock).\n");
  116. mutex->lock = PR_NewLock();
  117. /* If an error occurred in NSPR, report it here */
  118. if (mutex->lock == NULL) {
  119. PKIX_DECREF(mutex);
  120. PKIX_ERROR_ALLOC_ERROR();
  121. }
  122. *pNewLock = mutex;
  123. cleanup:
  124. PKIX_RETURN(MUTEX);
  125. }
  126. /*
  127. * FUNCTION: PKIX_PL_Mutex_Lock (see comments in pkix_pl_system.h)
  128. */
  129. PKIX_Error *
  130. PKIX_PL_Mutex_Lock(
  131. PKIX_PL_Mutex *mutex,
  132. void *plContext)
  133. {
  134. PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Lock");
  135. PKIX_NULLCHECK_ONE(mutex);
  136. PKIX_MUTEX_DEBUG("\tCalling PR_Lock).\n");
  137. PR_Lock(mutex->lock);
  138. PKIX_MUTEX_DEBUG_ARG("(Thread %u just acquired the lock)\n",
  139. (PKIX_UInt32)PR_GetCurrentThread());
  140. PKIX_RETURN(MUTEX);
  141. }
  142. /*
  143. * FUNCTION: PKIX_PL_Mutex_Unlock (see comments in pkix_pl_system.h)
  144. */
  145. PKIX_Error *
  146. PKIX_PL_Mutex_Unlock(
  147. PKIX_PL_Mutex *mutex,
  148. void *plContext)
  149. {
  150. PRStatus result;
  151. PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Unlock");
  152. PKIX_NULLCHECK_ONE(mutex);
  153. PKIX_MUTEX_DEBUG("\tCalling PR_Unlock).\n");
  154. result = PR_Unlock(mutex->lock);
  155. PKIX_MUTEX_DEBUG_ARG("(Thread %u just released the lock)\n",
  156. (PKIX_UInt32)PR_GetCurrentThread());
  157. if (result == PR_FAILURE) {
  158. PKIX_ERROR_FATAL(PKIX_ERRORUNLOCKINGMUTEX);
  159. }
  160. cleanup:
  161. PKIX_RETURN(MUTEX);
  162. }