am_status_t Log::setLevelsFromString(const std::string& logLevels) throw() { am_status_t status = AM_SUCCESS; ScopeLock mylock(*lockPtr); status = pSetLevelsFromString(logLevels); return status; }
am_status_t Log::setDebugFileSize(const long debugFileSize) { am_status_t status = AM_SUCCESS; ScopeLock mylock(*lockPtr); maxLogFileSize = debugFileSize; if (maxLogFileSize < DEBUG_FILE_MIN_SIZE) { maxLogFileSize = DEBUG_FILE_MIN_SIZE; } return status; }
uint64_t session_manage::get_increase( void ) { scopedlock mylock(m_mutex.get()); if (m_increseid == 0xFFFFFFFFFFFFFF0) { m_increseid = 0xFF; } m_increseid++; return m_increseid; }
int main() { std::mutex mymutex; std::unique_lock<std::mutex> mylock(mymutex); if(mylock.owns_lock()) std::cout << "main: mylock is locked\n"; call_locked(mylock); if(mylock.owns_lock()) std::cout << "main: mylock is still locked\n"; }
int do_child(void) { int ret; int tmp; int i; while (1) { ret = mylock(); if (ret != 0) { fprintf(stderr, "mylock fail!\n"); exit(1); } if ((tmp = getnum(PRIMER)) == 0) { ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } continue; } if (tmp == END) { ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } break; } ret = setnum(PRIMER, 0); if (ret != 0) { fprintf(stderr, "setnum() error!\n"); exit(1); } ret = myunlock(); if (ret != 0) { fprintf(stderr, "myunlock fail!\n"); exit(1); } for (i=2;i<tmp/2;i++) { if (tmp%i == 0) { ret = 0; break; } ret = 1; continue; } if (ret == 1) { printf("%d\n", tmp); } } exit(0); }
bool GetTask(FunctionArgs& args) { boost::mutex::scoped_lock mylock(myMutex); if (!tasks.empty()) { args = tasks.front(); tasks.pop_front(); return true; } else { return false; } }
void AddTask(FunctionArgs arg) { { boost::mutex::scoped_lock mylock(myMutex); tasks.push_back(arg); Update(); } if (!myThread) { finished = false; myThread = new boost::thread(boost::bind(&SaverThread::SaveStuff, this)); } };
static void add(int i) { int tmp; mylock(&lock); get_num(&tmp); add_num(&tmp, i); put_num(&tmp); myunlock(&lock); }
int do_child(int proj_id) { int interval; int *shm_p, shm_id; key_t shm_key; // call fork to generate shmkey if ((shm_key = ftok(PATHNAME, proj_id)) == -1) { perror("ftok()"); exit(1); } // use shm_id in child process which was got from parent process shm_id = shmget(shm_key, sizeof(int), 0); if (shm_id < 0) { perror("shmget()"); exit(1); } // shmat function maps shared memroy in local process shm_p = (int *)shmat(shm_id, NULL, 0); if ((void *)shm_p == (void *)-1) { perror("shmat()"); exit(1); } // critical section if (mylock(lockid) == -1) { exit(1); } interval = *shm_p; interval++; usleep(1); *shm_p = interval; if (myunlock(lockid) == -1) { perror("shmdt()"); exit(1); } // critical section if (shmdt(shm_p) < 0) { perror("shmdt()"); exit(1); } exit(0); }
int open_pipe() { paip = fopen(pipename, "r"); if(paip == NULL) { fprintf(stderr, "Error opening pipe : %s\n", strerror(errno)); return 7; } if (mylock(fileno (paip)) == -1) { if(errno == EWOULDBLOCK) { fprintf(stderr, "Error locking pipe : %s\n", "another scrolllog is running"); } else { fprintf(stderr, "Error locking pipe : %s\n", strerror(errno)); } return 8; } return 0; }
void HudManager::TextsUpdateCallback::operator ()(osg::Node *node, osg::NodeVisitor *nv) { //test if the head elements are ready to be removed. if true, ask for remove them HudTexts * texts = _manager->getHudTexts(); //get the timer only once, it can make a little time difference, but the optimization is //better than the precision here osg::Timer_t actual = osg::Timer::instance()->tick(); //First remove texts if(texts->size() > 0) { bool stopRemoving = false; while(!stopRemoving && texts->size() > 0) { //compare actual with text begin time + duration float duration = osg::Timer::instance()->delta_s(texts->front().second, actual); if(duration >= _manager->getDisplaySettings()->getVisibleTime()) _manager->removeText(texts->front().first); else stopRemoving = true; } } //Now add texts { OpenThreads::ScopedLock<OpenThreads::Mutex> mylock(_manager->_TestToAddStackMutex); while(!_manager->_textToAddStack.empty()) { _manager->addText(_manager->_textToAddStack.front()); _manager->_textToAddStack.pop(); } } traverse(node,nv); }
void CMultiTaskHandler::AddTask( pktask_t && task ) { //we have a task to run, so these this to false m_NoTasksYet = false; try { lock_guard<mutex> mylock( m_mutextasks ); m_tasks.push_back( std::move(task) ); } catch( exception e ){SimpleHandleException(e);} try { unique_lock<mutex> mynewtasklock(m_mutexNewTask); m_newTask.notify_one(); //FIXME: This condition variable intermitently make the program crash for no obvious reasons at this line.. // it may have been a VS2012 stdlib bug, because on VS2013, this hasn't happened yet. } catch( exception e ){SimpleHandleException(e);} }
int rlSharedMemory::read(unsigned long offset, void *buf, int len) { void *ptr; if(status != OK) return -1; if(len <= 0) return -1; if(offset+len > _size) return -1; ptr = user_adr + offset; #ifdef RLWIN32 LockFileEx(hSharedFile,LOCKFILE_EXCLUSIVE_LOCK,0,_size,0,&overlapped); // Changed by FMakkinga 18-03-2013 #elif defined(RLUNIX) flock(fdlock,LOCK_EX); #else mylock(mutex,1); #endif memcpy(buf,ptr,len); #ifdef RLWIN32 UnlockFileEx(hSharedFile,0,_size,0,&overlapped); // Changed by FMakkinga 18-03-2013 #elif defined(RLUNIX) flock(fdlock,LOCK_UN); #else myunlock(mutex); #endif return len; }
void DatabasePool::addAction(Json::Value * newVal) { std::unique_lock<std::mutex> mylock(listMut); this->actList.push_back(newVal); }
int wdlock(uint8_t runmode,uint32_t timeout) { pid_t ownerpid; pid_t newownerpid; uint32_t l; lfd = open("." STR(APPNAME) ".lock",O_WRONLY|O_CREAT,0666); if (lfd<0) { mfs_errlog(LOG_ERR,"can't create lockfile in working directory"); return -1; } ownerpid = mylock(lfd); if (ownerpid<0) { mfs_errlog(LOG_ERR,"fcntl error"); return -1; } if (ownerpid>0) { if (runmode==RM_TEST) { fprintf(stderr,STR(APPNAME) " pid: %ld\n",(long)ownerpid); return -1; } if (runmode==RM_START) { fprintf(stderr,"can't start: lockfile is already locked by another process\n"); return -1; } if (runmode==RM_RELOAD) { if (kill(ownerpid,SIGHUP)<0) { mfs_errlog(LOG_WARNING,"can't send reload signal to lock owner"); return -1; } fprintf(stderr,"reload signal has beed sent\n"); return 0; } if (runmode==RM_KILL) { fprintf(stderr,"sending SIGKILL to lock owner (pid:%ld)\n",(long int)ownerpid); if (kill(ownerpid,SIGKILL)<0) { mfs_errlog(LOG_WARNING,"can't kill lock owner"); return -1; } } else { fprintf(stderr,"sending SIGTERM to lock owner (pid:%ld)\n",(long int)ownerpid); if (kill(ownerpid,SIGTERM)<0) { mfs_errlog(LOG_WARNING,"can't kill lock owner"); return -1; } } l=0; fprintf(stderr,"waiting for termination ... "); fflush(stderr); do { newownerpid = mylock(lfd); if (newownerpid<0) { mfs_errlog(LOG_ERR,"fcntl error"); return -1; } if (newownerpid>0) { l++; if (l>=timeout) { syslog(LOG_ERR,"about %"PRIu32" seconds passed and lockfile is still locked - giving up",l); fprintf(stderr,"giving up\n"); return -1; } if (l%10==0) { syslog(LOG_WARNING,"about %"PRIu32" seconds passed and lock still exists",l); fprintf(stderr,"%"PRIu32"s ",l); fflush(stderr); } if (newownerpid!=ownerpid) { fprintf(stderr,"\nnew lock owner detected\n"); if (runmode==RM_KILL) { fprintf(stderr,"sending SIGKILL to lock owner (pid:%ld) ... ",(long int)newownerpid); fflush(stderr); if (kill(newownerpid,SIGKILL)<0) { mfs_errlog(LOG_WARNING,"can't kill lock owner"); return -1; } } else { fprintf(stderr,"sending SIGTERM to lock owner (pid:%ld) ... ",(long int)newownerpid); fflush(stderr); if (kill(newownerpid,SIGTERM)<0) { mfs_errlog(LOG_WARNING,"can't kill lock owner"); return -1; } } ownerpid = newownerpid; } } sleep(1); } while (newownerpid!=0); fprintf(stderr,"terminated\n"); return 0; } if (runmode==RM_START || runmode==RM_RESTART) { fprintf(stderr,"lockfile created and locked\n"); } else if (runmode==RM_STOP || runmode==RM_KILL) { fprintf(stderr,"can't find process to terminate\n"); return -1; } else if (runmode==RM_RELOAD) { fprintf(stderr,"can't find process to send reload signal\n"); return -1; } else if (runmode==RM_TEST) { fprintf(stderr,STR(APPNAME) " is not running\n"); } return 0; }
bool operator==(const MetadataListImpl& list) const { std::unique_lock<std::mutex> mylock(m_mutex, std::defer_lock); // Released when out of scope std::unique_lock<std::mutex> otherlock(list.m_mutex, std::defer_lock); // Released when out of scope std::lock(mylock, otherlock); // Lock both, prevent deadlock return compareVectorsOfPointers(m_data, list.m_data); }