qlockh_t qlock_alloc( ix_t ord ) { qlock_t *qlockp; /* sanity checks */ ASSERT( qlock_inited ); /* verify the ordinal is not already taken, and mark as taken */ ASSERT( ! QLOCK_ORDMAP_GET( qlock_ordalloced, ord )); QLOCK_ORDMAP_SET( qlock_ordalloced, ord ); /* allocate lock memory */ qlockp = ( qlock_t * )calloc( 1, sizeof( qlock_t )); ASSERT( qlockp ); #ifdef HIDDEN /* allocate a us lock: bypass if miniroot */ if ( qlock_usp ) { qlockp->ql_uslockh = usnewlock( qlock_usp ); ASSERT( qlockp->ql_uslockh ); } #endif /* HIDDEN */ /* assign the ordinal position */ qlockp->ql_ord = ord; return ( qlockh_t )qlockp; }
bool vrpn_Semaphore::init() { if (vrpn_Semaphore::ppaArena == NULL) { vrpn_Semaphore::allocArena(); } if (cResources == 1) { fUsingLock = true; ps = NULL; // use lock instead of semaphore if ((l = usnewlock(vrpn_Semaphore::ppaArena)) == NULL) { fprintf(stderr, "vrpn_Semaphore::vrpn_Semaphore: error allocating " "lock from arena.\n"); return false; } } else { fUsingLock = false; l = NULL; if ((ps = usnewsema(vrpn_Semaphore::ppaArena, cResources)) == NULL) { fprintf(stderr, "vrpn_Semaphore::vrpn_Semaphore: error allocating " "semaphore from arena.\n"); return false; } } return true; }
void accept_mutex_init(void) { ptrdiff_t old; /* default is 8 */ #define CONF_INITUSERS_MAX 15 if ((old = usconfig(CONF_INITUSERS, CONF_INITUSERS_MAX)) == -1) { perror("usconfig"); exit(-1); } if ((old = usconfig(CONF_LOCKTYPE, US_NODEBUG)) == -1) { perror("usconfig"); exit(-1); } if ((old = usconfig(CONF_ARENATYPE, US_SHAREDONLY)) == -1) { perror("usconfig"); exit(-1); } if ((us = usinit("/dev/zero")) == NULL) { perror("usinit"); exit(-1); } if ((uslock = usnewlock(us)) == NULL) { perror("usnewlock"); exit(-1); } }
bool vrpn_Semaphore::init() { #ifdef sgi if (vrpn_Semaphore::ppaArena==NULL) { vrpn_Semaphore::allocArena(); } if (cResources==1) { fUsingLock=true; ps=NULL; // use lock instead of semaphore if ((l = usnewlock(vrpn_Semaphore::ppaArena)) == NULL) { fprintf(stderr,"vrpn_Semaphore::vrpn_Semaphore: error allocating lock from arena.\n"); return false; } } else { fUsingLock=false; l=NULL; if ((ps = usnewsema(vrpn_Semaphore::ppaArena, cResources)) == NULL) { fprintf(stderr,"vrpn_Semaphore::vrpn_Semaphore: error allocating semaphore from arena.\n"); return false; } } #elif defined(_WIN32) // args are security, initial count, max count, and name // TCH 20 Feb 2001 - Make the PC behavior closer to the SGI behavior. int numMax = cResources; if (numMax < 1) { numMax = 1; } hSemaphore = CreateSemaphore(NULL, cResources, numMax, NULL); if (!hSemaphore) { // get error info from windows (from FormatMessage help page) LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); fprintf(stderr,"vrpn_Semaphore::vrpn_Semaphore: error creating semaphore, " "WIN32 CreateSemaphore call caused the following error: %s\n", (LPTSTR) lpMsgBuf); // Free the buffer. LocalFree( lpMsgBuf ); return false; } #elif defined(__APPLE__) // We need to use sem_open on the mac because sem_init is not implemented int numMax = cResources; if (numMax < 1) { numMax = 1; } char *tempname = new char[100]; sprintf(tempname, "/tmp/vrpn_sem.XXXXXXX"); semaphore = sem_open(mktemp(tempname), O_CREAT, 0600, numMax); if (semaphore == SEM_FAILED) { perror("vrpn_Semaphore::vrpn_Semaphore: error opening semaphore"); delete [] tempname; return false; } delete [] tempname; #else // Posix threads are the default. // We use sem_init on linux (instead of sem_open). int numMax = cResources; if (numMax < 1) { numMax = 1; } semaphore = new sem_t; if (sem_init(semaphore, 0, numMax) != 0) { perror("vrpn_Semaphore::vrpn_Semaphore: error initializing semaphore"); return false; } #endif return true; }