security/nss/lib/ckfw/mutex.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/ckfw/mutex.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,269 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * mutex.c
    1.10 + *
    1.11 + * This file implements a mutual-exclusion locking facility for Modules
    1.12 + * using the NSS Cryptoki Framework.
    1.13 + */
    1.14 +
    1.15 +#ifndef CK_T
    1.16 +#include "ck.h"
    1.17 +#endif /* CK_T */
    1.18 +
    1.19 +/*
    1.20 + * NSSCKFWMutex
    1.21 + *
    1.22 + *  NSSCKFWMutex_Destroy
    1.23 + *  NSSCKFWMutex_Lock
    1.24 + *  NSSCKFWMutex_Unlock
    1.25 + *
    1.26 + *  nssCKFWMutex_Create
    1.27 + *  nssCKFWMutex_Destroy
    1.28 + *  nssCKFWMutex_Lock
    1.29 + *  nssCKFWMutex_Unlock
    1.30 + *
    1.31 + *  -- debugging versions only --
    1.32 + *  nssCKFWMutex_verifyPointer
    1.33 + *
    1.34 + */
    1.35 +
    1.36 +struct NSSCKFWMutexStr {
    1.37 +  PRLock *lock;
    1.38 +};
    1.39 +
    1.40 +#ifdef DEBUG
    1.41 +/*
    1.42 + * But first, the pointer-tracking stuff.
    1.43 + *
    1.44 + * NOTE: the pointer-tracking support in NSS/base currently relies
    1.45 + * upon NSPR's CallOnce support.  That, however, relies upon NSPR's
    1.46 + * locking, which is tied into the runtime.  We need a pointer-tracker
    1.47 + * implementation that uses the locks supplied through C_Initialize.
    1.48 + * That support, however, can be filled in later.  So for now, I'll
    1.49 + * just do this routines as no-ops.
    1.50 + */
    1.51 +
    1.52 +static CK_RV
    1.53 +mutex_add_pointer
    1.54 +(
    1.55 +  const NSSCKFWMutex *fwMutex
    1.56 +)
    1.57 +{
    1.58 +  return CKR_OK;
    1.59 +}
    1.60 +
    1.61 +static CK_RV
    1.62 +mutex_remove_pointer
    1.63 +(
    1.64 +  const NSSCKFWMutex *fwMutex
    1.65 +)
    1.66 +{
    1.67 +  return CKR_OK;
    1.68 +}
    1.69 +
    1.70 +NSS_IMPLEMENT CK_RV
    1.71 +nssCKFWMutex_verifyPointer
    1.72 +(
    1.73 +  const NSSCKFWMutex *fwMutex
    1.74 +)
    1.75 +{
    1.76 +  return CKR_OK;
    1.77 +}
    1.78 +
    1.79 +#endif /* DEBUG */
    1.80 +
    1.81 +/*
    1.82 + * nssCKFWMutex_Create
    1.83 + *
    1.84 + */
    1.85 +NSS_EXTERN NSSCKFWMutex *
    1.86 +nssCKFWMutex_Create
    1.87 +(
    1.88 +  CK_C_INITIALIZE_ARGS_PTR pInitArgs,
    1.89 +  CryptokiLockingState LockingState,
    1.90 +  NSSArena *arena,
    1.91 +  CK_RV *pError
    1.92 +)
    1.93 +{
    1.94 +  NSSCKFWMutex *mutex;
    1.95 +  
    1.96 +  mutex = nss_ZNEW(arena, NSSCKFWMutex);
    1.97 +  if (!mutex) {
    1.98 +    *pError = CKR_HOST_MEMORY;
    1.99 +    return (NSSCKFWMutex *)NULL;
   1.100 +  }
   1.101 +  *pError = CKR_OK;
   1.102 +  mutex->lock = NULL;
   1.103 +  if (LockingState == MultiThreaded) {
   1.104 +    mutex->lock = PR_NewLock();
   1.105 +    if (!mutex->lock) {
   1.106 +      *pError = CKR_HOST_MEMORY; /* we couldn't get the resource */
   1.107 +    }
   1.108 +  }
   1.109 +    
   1.110 +  if( CKR_OK != *pError ) {
   1.111 +    (void)nss_ZFreeIf(mutex);
   1.112 +    return (NSSCKFWMutex *)NULL;
   1.113 +  }
   1.114 +
   1.115 +#ifdef DEBUG
   1.116 +  *pError = mutex_add_pointer(mutex);
   1.117 +  if( CKR_OK != *pError ) {
   1.118 +    if (mutex->lock) {
   1.119 +      PR_DestroyLock(mutex->lock);
   1.120 +    }
   1.121 +    (void)nss_ZFreeIf(mutex);
   1.122 +    return (NSSCKFWMutex *)NULL;
   1.123 +  }
   1.124 +#endif /* DEBUG */
   1.125 +
   1.126 +  return mutex;
   1.127 +}  
   1.128 +
   1.129 +/*
   1.130 + * nssCKFWMutex_Destroy
   1.131 + *
   1.132 + */
   1.133 +NSS_EXTERN CK_RV
   1.134 +nssCKFWMutex_Destroy
   1.135 +(
   1.136 +  NSSCKFWMutex *mutex
   1.137 +)
   1.138 +{
   1.139 +  CK_RV rv = CKR_OK;
   1.140 +
   1.141 +#ifdef NSSDEBUG
   1.142 +  rv = nssCKFWMutex_verifyPointer(mutex);
   1.143 +  if( CKR_OK != rv ) {
   1.144 +    return rv;
   1.145 +  }
   1.146 +#endif /* NSSDEBUG */
   1.147 + 
   1.148 +  if (mutex->lock) {
   1.149 +    PR_DestroyLock(mutex->lock);
   1.150 +  } 
   1.151 +
   1.152 +#ifdef DEBUG
   1.153 +  (void)mutex_remove_pointer(mutex);
   1.154 +#endif /* DEBUG */
   1.155 +
   1.156 +  (void)nss_ZFreeIf(mutex);
   1.157 +  return rv;
   1.158 +}
   1.159 +
   1.160 +/*
   1.161 + * nssCKFWMutex_Lock
   1.162 + *
   1.163 + */
   1.164 +NSS_EXTERN CK_RV
   1.165 +nssCKFWMutex_Lock
   1.166 +(
   1.167 +  NSSCKFWMutex *mutex
   1.168 +)
   1.169 +{
   1.170 +#ifdef NSSDEBUG
   1.171 +  CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   1.172 +  if( CKR_OK != rv ) {
   1.173 +    return rv;
   1.174 +  }
   1.175 +#endif /* NSSDEBUG */
   1.176 +  if (mutex->lock) {
   1.177 +    PR_Lock(mutex->lock);
   1.178 +  }
   1.179 +  
   1.180 +  return CKR_OK;
   1.181 +}
   1.182 +
   1.183 +/*
   1.184 + * nssCKFWMutex_Unlock
   1.185 + *
   1.186 + */
   1.187 +NSS_EXTERN CK_RV
   1.188 +nssCKFWMutex_Unlock
   1.189 +(
   1.190 +  NSSCKFWMutex *mutex
   1.191 +)
   1.192 +{
   1.193 +  PRStatus nrv;
   1.194 +#ifdef NSSDEBUG
   1.195 +  CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   1.196 +
   1.197 +  if( CKR_OK != rv ) {
   1.198 +    return rv;
   1.199 +  }
   1.200 +#endif /* NSSDEBUG */
   1.201 +
   1.202 +  if (!mutex->lock) 
   1.203 +    return CKR_OK;
   1.204 +
   1.205 +  nrv =  PR_Unlock(mutex->lock);
   1.206 +
   1.207 +  /* if unlock fails, either we have a programming error, or we have
   1.208 +   * some sort of hardware failure... in either case return CKR_DEVICE_ERROR.
   1.209 +   */
   1.210 +  return nrv == PR_SUCCESS ? CKR_OK : CKR_DEVICE_ERROR;
   1.211 +}
   1.212 +
   1.213 +/*
   1.214 + * NSSCKFWMutex_Destroy
   1.215 + *
   1.216 + */
   1.217 +NSS_EXTERN CK_RV
   1.218 +NSSCKFWMutex_Destroy
   1.219 +(
   1.220 +  NSSCKFWMutex *mutex
   1.221 +)
   1.222 +{
   1.223 +#ifdef DEBUG
   1.224 +  CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   1.225 +  if( CKR_OK != rv ) {
   1.226 +    return rv;
   1.227 +  }
   1.228 +#endif /* DEBUG */
   1.229 +  
   1.230 +  return nssCKFWMutex_Destroy(mutex);
   1.231 +}
   1.232 +
   1.233 +/*
   1.234 + * NSSCKFWMutex_Lock
   1.235 + *
   1.236 + */
   1.237 +NSS_EXTERN CK_RV
   1.238 +NSSCKFWMutex_Lock
   1.239 +(
   1.240 +  NSSCKFWMutex *mutex
   1.241 +)
   1.242 +{
   1.243 +#ifdef DEBUG
   1.244 +  CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   1.245 +  if( CKR_OK != rv ) {
   1.246 +    return rv;
   1.247 +  }
   1.248 +#endif /* DEBUG */
   1.249 +  
   1.250 +  return nssCKFWMutex_Lock(mutex);
   1.251 +}
   1.252 +
   1.253 +/*
   1.254 + * NSSCKFWMutex_Unlock
   1.255 + *
   1.256 + */
   1.257 +NSS_EXTERN CK_RV
   1.258 +NSSCKFWMutex_Unlock
   1.259 +(
   1.260 +  NSSCKFWMutex *mutex
   1.261 +)
   1.262 +{
   1.263 +#ifdef DEBUG
   1.264 +  CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
   1.265 +  if( CKR_OK != rv ) {
   1.266 +    return rv;
   1.267 +  }
   1.268 +#endif /* DEBUG */
   1.269 +
   1.270 +  return nssCKFWMutex_Unlock(mutex);
   1.271 +}
   1.272 +

mercurial