michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #ifndef __SSLMUTEX_H_ michael@0: #define __SSLMUTEX_H_ 1 michael@0: michael@0: /* What SSL really wants is portable process-shared unnamed mutexes in michael@0: * shared memory, that have the property that if the process that holds michael@0: * them dies, they are released automatically, and that (unlike fcntl michael@0: * record locking) lock to the thread, not to the process. michael@0: * NSPR doesn't provide that. michael@0: * Windows has mutexes that meet that description, but they're not portable. michael@0: * POSIX mutexes are not automatically released when the holder dies, michael@0: * and other processes/threads cannot release the mutex on behalf of the michael@0: * dead holder. michael@0: * POSIX semaphores can be used to accomplish this on systems that implement michael@0: * process-shared unnamed POSIX semaphores, because a watchdog thread can michael@0: * discover and release semaphores that were held by a dead process. michael@0: * On systems that do not support process-shared POSIX unnamed semaphores, michael@0: * they can be emulated using pipes. michael@0: * The performance cost of doing that is not yet measured. michael@0: * michael@0: * So, this API looks a lot like POSIX pthread mutexes. michael@0: */ michael@0: michael@0: #include "prtypes.h" michael@0: #include "prlock.h" michael@0: michael@0: #if defined(NETBSD) michael@0: #include /* for __NetBSD_Version__ */ michael@0: #endif michael@0: michael@0: #if defined(WIN32) michael@0: michael@0: #include michael@0: michael@0: typedef struct michael@0: { michael@0: PRBool isMultiProcess; michael@0: #ifdef WINNT michael@0: /* on WINNT we need both the PRLock and the Win32 mutex for fibers */ michael@0: struct { michael@0: #else michael@0: union { michael@0: #endif michael@0: PRLock* sslLock; michael@0: HANDLE sslMutx; michael@0: } u; michael@0: } sslMutex; michael@0: michael@0: typedef int sslPID; michael@0: michael@0: #elif defined(LINUX) || defined(AIX) || defined(BEOS) || defined(BSDI) || (defined(NETBSD) && __NetBSD_Version__ < 500000000) || defined(OPENBSD) michael@0: michael@0: #include michael@0: #include "prtypes.h" michael@0: michael@0: typedef struct { michael@0: PRBool isMultiProcess; michael@0: union { michael@0: PRLock* sslLock; michael@0: struct { michael@0: int mPipes[3]; michael@0: PRInt32 nWaiters; michael@0: } pipeStr; michael@0: } u; michael@0: } sslMutex; michael@0: typedef pid_t sslPID; michael@0: michael@0: #elif defined(XP_UNIX) /* other types of Unix */ michael@0: michael@0: #include /* for pid_t */ michael@0: #include /* for sem_t, and sem_* functions */ michael@0: michael@0: typedef struct michael@0: { michael@0: PRBool isMultiProcess; michael@0: union { michael@0: PRLock* sslLock; michael@0: sem_t sem; michael@0: } u; michael@0: } sslMutex; michael@0: michael@0: typedef pid_t sslPID; michael@0: michael@0: #else michael@0: michael@0: /* what platform is this ?? */ michael@0: michael@0: typedef struct { michael@0: PRBool isMultiProcess; michael@0: union { michael@0: PRLock* sslLock; michael@0: /* include cross-process locking mechanism here */ michael@0: } u; michael@0: } sslMutex; michael@0: michael@0: typedef int sslPID; michael@0: michael@0: #endif michael@0: michael@0: #include "seccomon.h" michael@0: michael@0: SEC_BEGIN_PROTOS michael@0: michael@0: extern SECStatus sslMutex_Init(sslMutex *sem, int shared); michael@0: michael@0: /* If processLocal is set to true, then just free resources which are *only* associated michael@0: * with the current process. Leave any shared resources (including the state of michael@0: * shared memory) intact. */ michael@0: extern SECStatus sslMutex_Destroy(sslMutex *sem, PRBool processLocal); michael@0: michael@0: extern SECStatus sslMutex_Unlock(sslMutex *sem); michael@0: michael@0: extern SECStatus sslMutex_Lock(sslMutex *sem); michael@0: michael@0: #ifdef WINNT michael@0: michael@0: extern SECStatus sslMutex_2LevelInit(sslMutex *sem); michael@0: michael@0: #endif michael@0: michael@0: SEC_END_PROTOS michael@0: michael@0: #endif