/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include"seccomon.h" /* This ifdef should match the one in sslsnce.c */ #ifdefined(XP_UNIX) || defined(XP_WIN32)
#ifdefined(LINUX) && defined(i386) /* No memory barrier needed for this platform */
/* nWaiters includes the holder of the lock (if any) and the number **threadswaitingforit.AfterincrementingnWaiters,ifthecount **isexactly1,thenyouhavethelockandmayproceed.Ifthe **countisgreaterthan1,thenyoumustwaitonthepipe.
*/
if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure;
}
newValue = PR_ATOMIC_INCREMENT(&pMutex->u.pipeStr.nWaiters); /* Do Memory Barrier here. */ if (newValue > 1) { int cc; char c; do {
cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1);
} while (cc < 0 && errno == EINTR); if (cc != 1) { if (cc < 0)
nss_MD_unix_map_default_error(errno); else
PORT_SetError(PR_UNKNOWN_ERROR); return SECFailure;
}
} return SECSuccess;
}
#else
/* Using Atomic operations requires the use of a memory barrier instruction **onPowerPC,Sparc,andAlpha.NSPR'sPR_Atomicfunctionsdonotperform **them,andNSPRdoesnotprovideafunctionthatdoesthem(e.g.PR_Barrier). **So,wedon'tusethemonthoseplatforms.
*/
SECStatus
sslMutex_Unlock(sslMutex* pMutex)
{ int cc; char c = 1;
if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Unlock(pMutex);
}
if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure;
} do {
cc = write(pMutex->u.pipeStr.mPipes[1], &c, 1);
} while (cc < 0 && (errno == EINTR || errno == EAGAIN)); if (cc != 1) { if (cc < 0)
nss_MD_unix_map_default_error(errno); else
PORT_SetError(PR_UNKNOWN_ERROR); return SECFailure;
}
return SECSuccess;
}
SECStatus
sslMutex_Lock(sslMutex* pMutex)
{ int cc; char c;
if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Lock(pMutex);
}
if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure;
}
do {
cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1);
} while (cc < 0 && errno == EINTR); if (cc != 1) { if (cc < 0)
nss_MD_unix_map_default_error(errno); else
PORT_SetError(PR_UNKNOWN_ERROR); return SECFailure;
}
return SECSuccess;
}
#endif
#elifdefined(WIN32)
#include"win32err.h"
/* on Windows, we need to find the optimal type of locking mechanism to use forthesslMutex.
SECStatus
sslMutex_2LevelInit(sslMutex *sem)
{ /* the following adds a PRLock to sslMutex . This is done in each processofamulti-processserverandisonlyneededonWINNT,if usingfibers.Wecan'ttellifnativethreadsorfibersareused,so wealwaysdoitonWINNT
*/
PR_ASSERT(sem); if (sem) { /* we need to reset the sslLock in the children or the single_process init
function below will assert */
sem->u.sslLock = NULL;
} return single_process_sslMutex_Init(sem);
}
if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Init(pMutex);
}
#ifdef WINNT /* we need a lock on WINNT for fibers in the parent process */
retvalue = sslMutex_2LevelInit(pMutex); if (SECSuccess != retvalue) return SECFailure; #endif
SECStatus
sslMutex_Destroy(sslMutex *pMutex, PRBool processLocal)
{
HANDLE hMutex; int rv; int retvalue = SECSuccess;
PR_ASSERT(pMutex != 0); if (!pMutex) {
PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure;
}
if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Destroy(pMutex);
}
/* multi-process mode */ #ifdef WINNT /* on NT, get rid of the PRLock used for fibers within a process */
retvalue = sslMutex_2LevelDestroy(pMutex); #endif
PR_ASSERT(pMutex != 0); if (!pMutex) {
PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure;
}
if (PR_FALSE == pMutex->isMultiProcess) { return single_process_sslMutex_Lock(pMutex);
} #ifdef WINNT /* lock first to preserve from other threads/fibers in the same process */
retvalue = single_process_sslMutex_Lock(pMutex); #endif
PR_ASSERT(pMutex->u.sslMutx != 0 &&
pMutex->u.sslMutx != INVALID_HANDLE_VALUE); if ((hMutex = pMutex->u.sslMutx) == 0 || hMutex == INVALID_HANDLE_VALUE) {
PORT_SetError(PR_INVALID_ARGUMENT_ERROR); return SECFailure; /* what else ? */
} /* acquire the mutex to be the only owner accross all other processes */
event = WaitForSingleObject(hMutex, INFINITE); switch (event) { case WAIT_OBJECT_0: case WAIT_ABANDONED:
rv = SECSuccess; break;
case WAIT_TIMEOUT: #ifdefined(WAIT_IO_COMPLETION) case WAIT_IO_COMPLETION: #endif default: /* should never happen. nothing we can do. */
PR_ASSERT(PR_FALSE && "WaitForSingleObject returned invalid value.");
PORT_SetError(PR_UNKNOWN_ERROR);
rv = SECFailure; break;
case WAIT_FAILED: /* failure returns this */
rv = SECFailure;
lastError = GetLastError(); /* for debugging */
nss_MD_win32_map_default_error(lastError); break;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.