/* This function can be used during initialization to remove a previously held mutex that has not been released. * This should only be used when there is no other process using the mutex */ void resetMutex() { std::string mutex_name; irods::error ret = getMutexName( mutex_name ); if ( !ret.ok() ) { rodsLog( LOG_ERROR, "resetMutex: call to getMutexName failed" ); } boost::interprocess::named_mutex::remove( mutex_name.c_str() ); }
int lockMutex( mutex_type **mutex ) { std::string mutex_name; irods::error ret = getMutexName( mutex_name ); if ( !ret.ok() ) { rodsLog( LOG_ERROR, "lockMutex: call to getMutexName failed" ); return -1; } try { *mutex = new boost::interprocess::named_mutex( boost::interprocess::open_or_create, mutex_name.c_str() ); } catch ( const boost::interprocess::interprocess_exception& ) { rodsLog( LOG_ERROR, "boost::interprocess::named_mutex threw a boost::interprocess::interprocess_exception." ); return -1; } try { ( *mutex )->lock(); } catch ( const boost::interprocess::interprocess_exception& ) { rodsLog( LOG_ERROR, "lock threw a boost::interprocess::interprocess_exception." ); return -1; } return 0; }
/* create a new file mapping and open the shared buffer */ static int sharedBuffer_create (SharedBuffer* sb,char* bufferName){ int size = BUFFER_SIZE; int res; #ifndef WIN32 key_t kt; int shmid, key_id; struct shmid_ds shmds; static int pid_ext = 0; #endif assert(sb != NULL); assert(bufferName != NULL); sb->data->name = bufferName; sb->data->write_pointer = sb->data->read_pointer = 0; sb->data->mutex = CreateMutex(NULL,FALSE, (getMutexName(sb->data->name))); if (sb->data->mutex == NULL){ error("SUBLIMEIO: Could not create mutex "); exit(1); } #ifdef WIN32 sb->data->hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // max. object size size + (int)FIELDS_LENGTH(sb), // buffer size bufferName); // name of mapping object if ((sb->data->hMapFile == NULL)|| (sb->data->hMapFile == INVALID_HANDLE_VALUE)) { error("SUBLIMEIO: Could not create file mapping object "); exit(1); } #else /* !WIN32 */ if (getenv("SUBLIME_PROC_ID") == NULL){ key_id = getpid() + pid_ext++; } else { key_id = atoi(getenv("SUBLIME_PROC_ID")) + pid_ext++; } kt = ftok(getTempDirLocation(),key_id); /* malloc for file descriptor */ sb->data->hMapFile = (int *)malloc(sizeof(int)); if ( sb->data->hMapFile == NULL){ error("SUBLIMEIO: malloc failed"); } /* 511 - all permissions */ if ((shmid = shmget(kt, BUFFER_SIZE, IPC_CREAT | 511 )) == -1){ error("CreateNewSharedBuffer: shmget failed"); } //fprintf(stderr,"shared buffer number is %d and key is %d\n",shmid, key_id); //fflush(stderr); if ((shmctl(shmid, IPC_STAT, &shmds)) == -1){ error("CreateNewSharedBuffer: shmctl failed"); } shmds.shm_perm.mode = shmds.shm_perm.mode | O_RDWR ; *(sb->data->hMapFile) = shmid; #endif /* WIN32 */ if ((res = sharedBuffer_open(sb, size)) != 0){ return res; } sb->data->dataBuffer->size = size; return res; }