📄 mutex.c
字号:
} /* Initialize the global shared memory mutex */ if ( (err = pthread_mutex_init(pmtx,&mtxattr)) != 0 ) { DbgLog(DL0,"InitializeMutexes: pthread_mutex_init() failed. returned %#x\n", err); return FALSE; }#elif (POSIXSEM)#error "this won't work since these are really the AIX calls.."#elif (SPINXPL) xplfd = open (XPL_FILE,O_CREAT|O_RDWR,S_IRWXU|S_IRWXG|S_IRWXO);#elif (SYSVSEM)#error "Caveat Emptor... this does not work"//#error "Define XPL fcns for SYSTEM V Semaphores" key_t tok; // This really needs some work... since we need to differentiate between // the various Xprocess locks which may exist... However at this time // we know there is only one so we will just instantiate it as a global... // The other calls will ingnore thei9r parameters for SysV sems tok = ftok(TOK_PATH ,'b');DbgLog(DL0,"creating semaphore %x \n",tok); pthread_mutex_lock (&semmtx); if ( (Shm_Sem = semget(tok,1,IPC_CREAT | 0666)) < 0 ) { DbgLog(DL0,"creating semaphore check for existing \n"); if (errno == EEXIST) { if ((Shm_Sem = semget(tok,0,0)) < 0) { DbgLog(DL0,"Failed to get semaphore for Xprocess locking \n "); pthread_mutex_unlock (&semmtx); return FALSE; } } else { DbgLog(DL0,"Failed to get semaphore for Xprocess locking error not eexist \n "); pthread_mutex_unlock(&semmtx); return FALSE; } } pthread_mutex_unlock(&semmtx); DbgLog(DL0,"Semid = %d \n",Shm_Sem); return TRUE; #elif NOXPROCLOCKreturn TRUE;#else#error "Define XPL fcns"#endif return TRUE;}intDestroyXProcLock(void *xpl){#if AIX#if defined(PKCS64) return msem_remove((xpl));#else /* Destroy the global shared memory mutex */ pthread_mutex_destroy((xpl)); /* Destroy the attribute object used to create all the mutexes */ pthread_mutexattr_destroy( &mtxattr ); return TRUE;#endif#elif (PTHREADXPL) /* Destroy the global shared memory mutex */ pthread_mutex_destroy((xpl)); /* Destroy the attribute object used to create all the mutexes */ pthread_mutexattr_destroy( &mtxattr ); return TRUE;#elif (POSIXSEM)#error "this won't work since these are really the AIX calls.."#elif SYSVSEM#error "Caveat Emptor... this does not work"//error "Define XPL fcns" pthread_mutex_lock(&semmtx); semctl(Shm_Sem,1,IPC_RMID,0); pthread_mutex_unlock(&semmtx); return TRUE;#elif NOXPROCLOCKreturn TRUE;#elif SPINXPLreturn TRUE;#else#error "Define XPL fcns"#endif}intXProcLock(void *xpl){#if AIX#if defined(PKCS64) return msem_lock(xpl, 0 );#else return pthread_mutex_lock((xpl));#endif#elif (PTHREADXPL) return pthread_mutex_lock((xpl));#elif (POSIXSEM)#error "this won't work since these are really the AIX calls.."#elif SYSVSEM#error "Caveat Emptor... this does not work"//#error "Define XPL fcns" pthread_mutex_lock(&semmtx); semop(Shm_Sem,&xlock_lock[0],2); pthread_mutex_unlock(&semmtx); return TRUE;#elif NOXPROCLOCKreturn TRUE;#elif SPINXPL flock(xplfd,LOCK_EX); return TRUE;#else#error "Define XPL fcns"#endif}intXProcUnLock(void *xpl){#if AIX#if defined(PKCS64) return msem_unlock((xpl),0);#else return pthread_mutex_unlock((xpl));#endif#elif (PTHREADXPL) return pthread_mutex_unlock((xpl));#elif (POSIXSEM)#error "this won't work since these are really the AIX calls.."#elif SYSVSEM#error "Caveat Emptor... this does not work"//#error "Define XPL fcns" pthread_mutex_lock(&semmtx); semop(Shm_Sem,&xlock_unlock[0],1); pthread_mutex_unlock(&semmtx); return TRUE;#elif NOXPROCLOCKreturn TRUE;#elif SPINXPL flock(xplfd,LOCK_UN); return TRUE;#else#error "Define XPL fcns"#endif}/********************************************************************************* * * InitializeMutexes - * * Initializes the global shared memory mutex, and sets up mtxattr, * the attribute identifier used to create all the per-process mutexes * *********************************************************************************/int InitializeMutexes ( void ) { int err;#if 1 if ( (err = CreateXProcLock(&(shmp->slt_mutex))) != TRUE){ DbgLog(DL0,"InitializeMutexes: CreateXProcLock() failed - returned %#x\n", err); return FALSE; }#else#if !defined(PKCS64) /* Initialize the attributes object */ if ( (err = pthread_mutexattr_init(&mtxattr)) != 0 ) { DbgLog(DL0,"InitializeMutexes: pthread_mutexattr_init() failed - returned %#x\n", err); return FALSE; } /* Set the attribute variable so that mutexes created with it can be shared across processes */ if ( (err = pthread_mutexattr_setpshared( &mtxattr, PTHREAD_PROCESS_SHARED )) != 0 ) { DbgLog(DL0,"InitializeMutexes: pthread_mutexattr_setpshared() failed - returned %#x\n", err); return FALSE; } /* Initialize the global shared memory mutex */ if ( (err = pthread_mutex_init(&(shmp->slt_mutex),&mtxattr)) != 0 ) { DbgLog(DL0,"InitializeMutexes: pthread_mutex_init() failed. returned %#x\n", err); return FALSE; }#elif AIX /* Initialize the global shared memory mutex */ if ( msem_init(&(shmp->slt_mutex),0) == NULL ) { DbgLog(DL0,"InitializeMutexes: pthread_mutex_init() failed. returned NULL\n"); return FALSE; }#elif LINUX #error " Linux Needs the XPROC lock stuff defined"#endif#endif #if TEST_COND_VARS if ( ! InitializeConditionVariables() ) { return FALSE; } #endif /* TEST_COND_VARS */ return TRUE;}#if TEST_COND_VARS BOOL InitializeConditionVariables ( void ) { int err; if ( (err = pthread_condattr_init( &(shmp->shmem_cv_attr) ) ) != 0 ) { DbgLog(DL0,"InitializeConditionVariables: pthread_condattr_init returned %s (%d; %#x)\n", SysConst(err), err, err); return FALSE; }#if !defined(LINUX) if ( (err = pthread_condattr_setpshared ( &(shmp->shmem_cv_attr), PTHREAD_PROCESS_SHARED ) ) != 0 ) { /* if ( (err = pthread_condattr_setpshared ( &(shmp->shmem_cv_attr), PTHREAD_PROCESS_PRIVATE ) ) != 0 ) { */ DbgLog(DL0,"InitializeConditionVariables: pthread_condattr_setpshared returned %s (%d; %#x)\n", SysConst(err), err, err); return FALSE; }#endif if ( (err = pthread_cond_init( &(shmp->shmem_cv), &(shmp->shmem_cv_attr) ) ) != 0 ) { DbgLog(DL0,"InitializeConditionVariables: pthread_cond_init returned %s (%d; %#x)\n", SysConst(err), err, err); return FALSE; } if ( (err = pthread_mutex_init( &(shmp->shmem_cv_mutex), &mtxattr ) ) != 0 ) { DbgLog(DL0,"InitializeConditionVariables: pthread_mutex_init returned %s (%d; %#x)\n", SysConst(err), err, err); return FALSE; } return TRUE; } BOOL DestroyConditionVariables ( void ) { int err; if ( (err = pthread_mutex_destroy ( &(shmp->shmem_cv_mutex) ) ) != 0 ) { DbgLog(DL0,"DestroyConditionVariables: pthread_mutex_destroy returned %s (%d; %#x)\n", SysConst(err), err, err); return FALSE; } if ( (err = pthread_cond_destroy( &(shmp->shmem_cv) ) ) != 0 ) { DbgLog(DL0,"DestroyConditionVariables: pthread_cond_destroy returned %s (%d; %#x)\n", SysConst(err), err, err); return FALSE; } if ( (err = pthread_condattr_destroy( &(shmp->shmem_cv_attr) ) ) != 0 ) { DbgLog(DL0,"DestroyConditionVariables: pthread_condattr_destroy returned %s (%d; %#x)\n", SysConst(err), err, err); return FALSE; } return TRUE; }#endif /* TEST_COND_VARS *//*********************************************************************** * DestroyMutexes - * * Destroys all the mutexes used by the program * ***********************************************************************/int DestroyMutexes ( void ) { int i; /* Get the global shared memory mutex */#if 1 XProcLock(&(shmp->slt_mutex));#else#ifdef PKCS64 msem_lock(&(shmp->slt_mutex), 0 );#else pthread_mutex_lock(&(shmp->slt_mutex));#endif#endif #if TEST_COND_VARS if ( ! DestroyConditionVariables() ) { return FALSE; } #endif /* TEST_COND_VARS */#ifdef FIXME // SAB FIXME... This is really useless as we don't use the // per process mutexes on the shared memory... thank goodness, since // this would have complicated the linux port as it does not support // Process shared mutexes // of course when we want to use these mutexes we need to figure out // how to handle it in linux... /* Destroy the per-process mutexes */ for ( i = 0; i < NUMBER_PROCESSES_ALLOWED; i++ ) { /* Should I get and release the per-process mutexes here? */ /* No. The only way this'll get called is if no processes are currently attached to the slotmgr So, in theory, noone should be holding a mutex - if they are, it's in error */ /* FIXME: Should make sure that they were successfully created before destroying them */#ifdef PKCS64 msem_remove(&(shmp->proc_table[i].proc_mutex));#else pthread_mutex_destroy( &(shmp->proc_table[i].proc_mutex) );#endif }#endif /* Give up the global shared memory mutex */ /* (we have to release it before we destroy it, otherwise the behavior's undefined) */#if 1 XProcUnLock(&(shmp->slt_mutex)); DestroyXProcLock(&(shmp->slt_mutex));#else#ifdef PKCS64 msem_unlock(&(shmp->slt_mutex),0); /* Destroy the global shared memory mutex */ msem_remove(&(shmp->slt_mutex));#else pthread_mutex_unlock(&(shmp->slt_mutex)); /* Destroy the global shared memory mutex */ pthread_mutex_destroy(&(shmp->slt_mutex)); /* Destroy the attribute object used to create all the mutexes */ pthread_mutexattr_destroy( &mtxattr );#endif#endif return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -