Blame nss/lib/ssl/sslmutex.h

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