int _mi_readinfo(MI_INFO *info, int lock_type, int check_keybuffer) { DBUG_ENTER("_mi_readinfo"); if (info->lock_type == F_UNLCK) { MYISAM_SHARE *share=info->s; if (!share->tot_locks) { if (my_lock(share->kfile,lock_type,0L,F_TO_EOF, info->lock_wait | MY_SEEK_NOT_DONE)) DBUG_RETURN(1); if (mi_state_info_read_dsk(share->kfile, &share->state, 1)) { int error=my_errno() ? my_errno() : -1; (void) my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF, MYF(MY_SEEK_NOT_DONE)); set_my_errno(error); DBUG_RETURN(1); } } if (check_keybuffer) (void) _mi_test_if_changed(info); info->invalidator=info->s->invalidator; } else if (lock_type == F_WRLCK && info->lock_type == F_RDLCK) { set_my_errno(EACCES); /* Not allowed to change */ DBUG_RETURN(-1); /* when have read_lock() */ } DBUG_RETURN(0); } /* _mi_readinfo */
void up(int me) { my_lock(chopstick[me]); printf("%d가 왼쪽 젓가락을 잡음.\n",me); sleep(10); //여기에 일부러 슬립함수를 이용하여 모든 철학자들이 왼쪽 젓가락을 확보하고 오른쪽젓가락을 기다리게 만들었다. my_lock(chopstick[(me+1)%5]); printf("%d가 오른쪽 젓가락을 잡음.\n",me); }
int StorageHandler::SendEvent(const Event* event) { /* * TODO: Use multimessage instead of creating a separate buffer and copying the MEP data into it */ const EVENT_HDR* data = EventSerializer::SerializeEvent(event); /* * Send the event to the merger with a zero copy message */ zmq::message_t zmqMessage((void*) data, data->length * 4, (zmq::free_fn*) ZMQHandler::freeZmqMessage); while (ZMQHandler::IsRunning()) { tbb::spin_mutex::scoped_lock my_lock(sendMutex_); try { mergerSockets_[event->getBurstID() % mergerSockets_.size()]->send(zmqMessage); break; } catch (const zmq::error_t& ex) { if (ex.num() != EINTR) { // try again if EINTR (signal caught) LOG_ERROR<< ex.what() << ENDL; onShutDown(); return 0; } } } return data->length * 4; }
int main(int argc, char **argv) { int fd; long i= 0; long seqno = 0; pid_t pid; ssize_t n; char line[MAX_LINE+1]; pid =getpid(); fd = open(SWQFILE, O_RDWR, "ab" ); for(i = 0 ; i< 20 ; i++ ) { my_lock(fd); lseek(fd, 0L, SEEK_SET); n = read(fd, line, MAX_LINE); line[n] = '\0'; n = sscanf(line, "%ld\n" , &seqno); printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long)pid, seqno); seqno++; snprintf(line, sizeof(line), "%ld\n",seqno); lseek(fd, 0L, SEEK_SET); write(fd, line, strlen(line)); my_unlock(fd); } exit(0); }
int main(int argc, char **argv) { int fd; long i, seqno; pid_t pid; ssize_t n; char line[MAXLINE + 1]; pid = getpid(); fd = open(SEQFILE, O_RDWR, FILE_MODE); for (i = 0; i < 20; i++) { my_lock(fd); /* lock the file */ lseek(fd, 0L, SEEK_SET); /* rewind before read */ n = read(fd, line, MAXLINE); line[n] = '\0'; /* null terminate for sscanf */ n = sscanf(line, "%ld\n", &seqno); printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long) pid, seqno); seqno++; /* increment sequence number */ snprintf(line, sizeof(line), "%ld\n", seqno); lseek(fd, 0L, SEEK_SET); /* rewind before write */ write(fd, line, strlen(line)); my_unlock(fd); /* unlock the file */ } exit(0); }
int main(int argc, char *argv[]) { int fd; long i, seqno; pid_t pid; ssize_t n; char line[MAXLINE + 1]; pid = getpid(); fd = Open(SEQFILE, O_RDWR, FILE_MODE);/*以读写模式打开文件*/ for(i = 0; i < 20; ++i){ my_lock(fd);/*对指定文件进行加锁*/ Lseek(fd, 0L, SEEK_SET); n = Read(fd, line, MAXLINE); line[n] = '\0'; n = sscanf(line, "%ld\n", &seqno);/*将line中的序列号保存到seqno中*/ printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long)pid, seqno); seqno++;/*序列号加1*/ snprintf(line, sizeof(line), "%ld\n", seqno);/*将序列号保存到line缓冲区中*/ Lseek(fd, 0L, SEEK_SET); Write(fd, line, strlen(line));/*将line缓冲区中的内容写到指定的文件中*/ my_unlock(fd);/*释放锁*/ } n exit(0); }
int main(int argc, char **argv) { int fd; long i, nloop, seqno; ssize_t n; char line[MAXLINE + 1]; if (argc != 2) err_quit("usage: loopmain <loopcount>"); nloop = atol(argv[1]); fd = Open(SEQFILE, O_RDWR, FILE_MODE); for (i = 0; i < nloop; i++) { my_lock(fd); /* lock the file */ Lseek(fd, 0L, SEEK_SET); /* rewind before read */ n = Read(fd, line, MAXLINE); line[n] = '\0'; /* null terminate for sscanf */ n = sscanf(line, "%ld\n", &seqno); seqno++; /* increment sequence number */ snprintf(line, sizeof(line), "%ld\n", seqno); Lseek(fd, 0L, SEEK_SET); /* rewind before write */ Write(fd, line, strlen(line)); my_unlock(fd); /* unlock the file */ } exit(0); }
// //////////////////////////////////////////////////////////////////////////// void LogHandlerXFile::OpenFileImpl(const char *file_name) { boost::shared_ptr<std::ofstream> tmp_file_ptr( new std::ofstream(file_name, (!(my_flags_ & DoNotAppend)) ? (std::ios_base::app | std::ios_base::ate) : (std::ios_base::app | std::ios_base::trunc))); if (tmp_file_ptr->fail()) ThrowErrno("Open attempt failed."); // CODE NOTE: LogFileHandler buffering test code. To be removed. std::streambuf *new_buffer_ptr = tmp_file_ptr->rdbuf()->pubsetbuf(TestFileBuffer, sizeof(TestFileBuffer)); if (new_buffer_ptr == NULL) ThrowErrno("Attempt to set the log file buffer size to " + AnyToString(sizeof(TestFileBuffer)) + " bytes failed."); { std::string tmp_file_name(file_name); boost::mutex::scoped_lock my_lock(the_lock_); if ((out_file_ptr_ != NULL) && out_file_ptr_->is_open()) { out_file_ptr_->flush(); out_file_ptr_->close(); out_file_ptr_.reset(); } out_file_ptr_.swap(tmp_file_ptr); out_file_name_.swap(tmp_file_name); } }
main() { int fd, i, n, pid, seqno; char buff[MAXBUFF + 1]; pid = getpid(); if ( (fd = open(SEQFILE, 2) ) < 0) err_sys("can't open %s", SEQFILE); for (i=0 ; i<20 ; i++) { my_lock(fd); /* lock the file */ lseek(fd, 0L, 0); /* rewind before read */ if ( (n = read(fd, buff, MAXBUFF)) <= 0 ) err_sys("read error"); buff[n] = '\0'; /* null terminate for sscanf */ if ( (n = sscanf(buff, "%d\n", &seqno)) != 1) err_sys("sscanf error"); printf("pid = %d, seq# = %d\n", pid, seqno); seqno++; /* increment the sequence number */ sprintf(buff, "%03d\n", seqno); n = strlen(buff); lseek(fd, 0L, 0); /* rewind before write */ if (write(fd, buff, n) != n) err_sys("write error"); my_unlock(fd); /* unlick the file */ } }
int main(int argc, char*argv[]) { int fd; long i, seqno; pid_t pid; ssize_t n; char line[MAXLINE + 1]; int result; pid = getpid(); fd = open(SEQFILE, O_CREAT | O_RDWR, FILE_MODE); for (i = 0; i < 20; i++) { my_lock(fd); if ((result = lseek(fd, 0, SEEK_SET)) == -1) { printf("lseek error ...\n"); return -1; } n = read(fd, line, MAXLINE); line[n] = '\0'; n = sscanf(line, "%ld\n", &seqno); printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long)pid, seqno); seqno++; snprintf(line, sizeof(line), "%ld\n", seqno); lseek(fd, 0L, SEEK_SET); write(fd, line, strlen(line)); my_unlock(fd); } }
// //////////////////////////////////////////////////////////////////////////// XercesContext::~XercesContext() { try { boost::mutex::scoped_lock my_lock(XercesContext_InitializationLock); xercesc::XMLPlatformUtils::Terminate(); } catch (...) { } }
// //////////////////////////////////////////////////////////////////////////// LogHandlerXFile::~LogHandlerXFile() { boost::mutex::scoped_lock my_lock(the_lock_); if ((out_file_ptr_ != NULL) && out_file_ptr_->is_open()) { out_file_ptr_->flush(); out_file_ptr_->close(); } }
// //////////////////////////////////////////////////////////////////////////// std::string CoreDumperBase::SetFileBaseName(const std::string &file_name_base, bool is_full_name, MLB::Utility::ProcessId process_id, const std::string &host_name, const MLB::Utility::TimeT &time_stamp) { ScopedLock my_lock(GetLockRef()); return(SetFileBaseNameImpl(file_name_base, is_full_name, process_id, host_name, time_stamp)); }
// //////////////////////////////////////////////////////////////////////////// LogHandlerFileBase::LogHandlerFileBaseFlag LogHandlerFileBase::SetFlags( LogHandlerFileBaseFlag new_flags) { boost::mutex::scoped_lock my_lock(the_lock_); LogHandlerFileBaseFlag old_flags = my_flags_; my_flags_ = new_flags; return(old_flags); }
void _myisam_log_record(enum myisam_log_commands command, MI_INFO *info, const uchar *record, my_off_t filepos, int result) { uchar buff[21],*pos; int error,old_errno; uint length; ulong pid=(ulong) GETPID(); old_errno=my_errno; if (!info->s->base.blobs) length=info->s->base.reclength; else length=info->s->base.reclength+ _my_calc_total_blob_length(info,record); buff[0]=(uchar) command; mi_int2store(buff+1,info->dfile); mi_int4store(buff+3,pid); mi_int2store(buff+7,result); mi_sizestore(buff+9,filepos); mi_int4store(buff+17,length); mysql_mutex_lock(&THR_LOCK_myisam); error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); (void) mysql_file_write(myisam_log_file, buff, sizeof(buff), MYF(0)); (void) mysql_file_write(myisam_log_file, record, info->s->base.reclength, MYF(0)); if (info->s->base.blobs) { MI_BLOB *blob,*end; for (end=info->blobs+info->s->base.blobs, blob= info->blobs; blob != end ; blob++) { memcpy(&pos, record+blob->offset+blob->pack_length, sizeof(char*)); (void) mysql_file_write(myisam_log_file, pos, blob->length, MYF(0)); } } if (!error) error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); mysql_mutex_unlock(&THR_LOCK_myisam); my_errno=old_errno; }
void _nisam_log_command(enum nisam_log_commands command, N_INFO *info, const byte *buffert, uint length, int result) { char buff[9]; int error,old_errno; ulong pid=(ulong) GETPID(); old_errno=my_errno; buff[0]=(char) command; int2store(buff+1,info->dfile); int4store(buff+3,pid); int2store(buff+7,result); pthread_mutex_lock(&THR_LOCK_isam); error=my_lock(nisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); VOID(my_write(nisam_log_file,buff,sizeof(buff),MYF(0))); if (buffert) VOID(my_write(nisam_log_file,buffert,length,MYF(0))); if (!error) error=my_lock(nisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); pthread_mutex_unlock(&THR_LOCK_isam); my_errno=old_errno; }
void _myisam_log_command(enum myisam_log_commands command, MI_INFO *info, const uchar *buffert, uint length, int result) { uchar buff[9]; int error,old_errno; ulong pid=(ulong) GETPID(); old_errno=my_errno; buff[0]=(char) command; mi_int2store(buff+1,info->dfile); mi_int4store(buff+3,pid); mi_int2store(buff+7,result); mysql_mutex_lock(&THR_LOCK_myisam); error=my_lock(myisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); (void) mysql_file_write(myisam_log_file, buff, sizeof(buff), MYF(0)); if (buffert) (void) mysql_file_write(myisam_log_file, buffert, length, MYF(0)); if (!error) error=my_lock(myisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); mysql_mutex_unlock(&THR_LOCK_myisam); my_errno=old_errno; }
void operator()(const int /* threadID */ ) const { for (int i=0; i<50; i++) { const bool is_reader = i%5==0; // 1 writer for every 4 readers if (is_reader) { tbb::reader_writer_lock::scoped_lock_read my_lock(the_mutex); active_readers++; if (active_readers > 1) sim_readers = true; ASSERT(active_writers==0, "Active writers in read-locked region."); Harness::Sleep(10); active_readers--; } else { // is writer tbb::reader_writer_lock::scoped_lock my_lock(the_mutex); active_writers++; ASSERT(active_readers==0, "Active readers in write-locked region."); ASSERT(active_writers<=1, "More than one active writer in write-locked region."); Harness::Sleep(10); active_writers--; } } }
void StorageHandler::setMergers(std::string mergerList) { tbb::spin_mutex::scoped_lock my_lock(sendMutex_); for (auto socket : mergerSockets_) { ZMQHandler::DestroySocket(socket); } mergerSockets_.clear(); for (std::string address : GetMergerAddresses(mergerList)) { zmq::socket_t* socket = ZMQHandler::GenerateSocket("StorageHandler", ZMQ_PUSH); socket->connect(address.c_str()); mergerSockets_.push_back(socket); } }
// //////////////////////////////////////////////////////////////////////////// void LogHandlerFileBase::EmitLiteral(unsigned int literal_length, const char *literal_string) { boost::mutex::scoped_lock my_lock(the_lock_); EmitLiteralImpl(literal_length, literal_string); if (!(my_flags_ & NoConsoleOutput)) { std::cout.write(literal_string, static_cast<std::streamsize>(literal_length)); std::cout << std::endl; } }
void _nisam_log_record(enum nisam_log_commands command, N_INFO *info, const byte *record, ulong filepos, int result) { char buff[17],*pos; int error,old_errno; uint length; ulong pid=(ulong) GETPID(); old_errno=my_errno; if (!info->s->base.blobs) length=info->s->base.reclength; else length=info->s->base.reclength+ _calc_total_blob_length(info,record); buff[0]=(char) command; int2store(buff+1,info->dfile); int4store(buff+3,pid); int2store(buff+7,result); int4store(buff+9,filepos); int4store(buff+13,length); pthread_mutex_lock(&THR_LOCK_isam); error=my_lock(nisam_log_file,F_WRLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); VOID(my_write(nisam_log_file,buff,sizeof(buff),MYF(0))); VOID(my_write(nisam_log_file,(byte*) record,info->s->base.reclength,MYF(0))); if (info->s->base.blobs) { N_BLOB *blob,*end; for (end=info->blobs+info->s->base.blobs, blob= info->blobs; blob != end ; blob++) { bmove(&pos,record+blob->offset+blob->pack_length,sizeof(char*)); VOID(my_write(nisam_log_file,pos,blob->length,MYF(0))); } } if (!error) error=my_lock(nisam_log_file,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)); pthread_mutex_unlock(&THR_LOCK_isam); my_errno=old_errno; }
// //////////////////////////////////////////////////////////////////////////// void LogHandlerFileBase::EmitLiteral(const LogEmitControl &emit_control, unsigned int literal_length, const char *literal_string) { if (emit_control.ShouldLogPersistent() || emit_control.ShouldLogScreen()) { boost::mutex::scoped_lock my_lock(the_lock_); if (emit_control.ShouldLogPersistent()) EmitLiteralImpl(literal_length, literal_string); if ((!(my_flags_ & NoConsoleOutput)) && emit_control.ShouldLogScreen()) { std::cout.write(literal_string, static_cast<std::streamsize>(literal_length)); std::cout << std::endl; } } }
int main(int argc, char **argv) { int fd; long i, nloop, seqno; ssize_t n; char line[MAXLINE + 1]; if (argc != 2) { fprintf(stderr, "usage: loopmain <loopcount>\n"); exit(1); } nloop = atol(argv[1]); if ((fd = open(SEQFILE, O_RDWR | O_NONBLOCK, FILE_MODE)) == -1) { fprintf(stderr, "open error for %s: %s\n", SEQFILE, strerror(errno)); exit(1); } for (i = 0; i < nloop; i++) { my_lock(fd); /* lock the file */ if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) { /* rewind before read */ perror("lseek error"); exit(1); } if ((n = read(fd, line, MAXLINE)) == -1) { perror("read error"); exit(1); } line[n] = '\0'; /* null terminate for sscanf */ n = sscanf(line, "%ld\n", &seqno); seqno++; /* increment sequence number */ snprintf(line, sizeof(line), "%ld\n", seqno); if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) { /* rewind before write */ perror("lseek error"); exit(1); } if (write(fd, line, strlen(line)) != strlen(line)) { perror("write error"); exit(1); } my_unlock(fd); /* unlock the file */ } exit(0); }
int main(int argc, char **argv) { int fd, i, seqno, c; pid_t pid; ssize_t n; char line[MAXLINE + 1], *ptr; pid = getpid(); if ((fd = open(SEQFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { printf("open error for %s: %s\n", SEQFILE, strerror(errno)); exit(1); } setvbuf(stdout, NULL, _IONBF, 0); /* unbuffered */ for (i = 0; i < 20; i++) { my_lock(fd); /* lock the file */ if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) { /* rewind before read */ perror("lseek error"); exit(1); } if ((n = read(fd, line, MAXLINE)) < 0) { perror("read error"); exit(1); } line[n] = '\0'; /* null terminate for sscanf */ n = sscanf(line, "%d\n", &seqno); snprintf(line, sizeof(line), "pid = %d, seq# = %d\n", pid, seqno); for (ptr = line; (c = *ptr++) != 0; ) putchar(c); seqno++; /* increment sequence number */ sprintf(line, "%03d\n", seqno); if (lseek(fd, 0, SEEK_SET) == (off_t) -1) { /* rewind before write */ perror("lseek error"); exit(1); } if (write(fd, line, strlen(line)) != strlen(line)) { perror("write error"); exit(1); } my_unlock(fd); /* unlock the file */ } exit(0); }
static void counter_func(struct work_struct *work) { unsigned int cpu = smp_processor_id(); int i; pr_info("Started job #%u\n", cpu); for (i = 0; i < TIMES_INC; i++) { my_lock(&count_lock); count++; my_unlock(&count_lock); } pr_info("finished job #%u\n", cpu); }
/* unique_lock 을 이용하여 lock 의 granularity 를 세분화 하여 lock 경합을 줄였다. */ void get_and_process_data() { std::mutex m; some_class get_next_data_chunk; std::unique_lock<std::mutex> my_lock(m); some_class data_to_process = get_next_data_chunk; my_lock.unlock(); /* (1) */ /* 이 시점에 get_and_process_data 를 다른 스레드에서 호출할 경우 invariant 가 파괴될 가능성이 매우 크다. */ int result = process(data_to_process); my_lock.lock(); /* (2) */ write_result(data_to_process, result); }
// //////////////////////////////////////////////////////////////////////////// void LogHandlerFileBase::EmitLine(const LogEmitControl &emit_control) { if (emit_control.ShouldLogPersistent() || emit_control.ShouldLogScreen()) { boost::mutex::scoped_lock my_lock(the_lock_); emit_control.UpdateTime(); if (emit_control.ShouldLogPersistent()) EmitLineImpl(emit_control); if ((!(my_flags_ & NoConsoleOutput)) && emit_control.ShouldLogScreen()) { std::cout.write(emit_control.GetLeaderPtr(), static_cast<std::streamsize>(emit_control.GetLeaderLength())); std::cout.write(emit_control.line_buffer_.c_str(), static_cast<std::streamsize>(emit_control.line_buffer_.size())); std::cout << std::endl; } } }
void operator()(const int /* threadID */ ) const { int nIters = MAX_WORK/nThread; sBarrier.wait(); tbb::tick_count t0 = tbb::tick_count::now(); for(int j = 0; j < nIters; j++) { for(int i = 0; i < MAX_WORK * (100 - WorkRatiox100); i++) { locals.local() += 1.0; } { tbb::critical_section::scoped_lock my_lock(cs); for(int i = 0; i < MAX_WORK * WorkRatiox100; i++) { locals.local() += 1.0; } unprotected_count++; } } locals.local() = (tbb::tick_count::now() - t0).seconds(); }
int main(int argc, char **argv) { int fd = 0, stat, nconflicts; long i, j, nproc; sem_t *ptr; pid_t pid; ssize_t n; if (argc != 2) err_quit("usage: locksvsemrace1 <#processes>"); nproc = atol(argv[1]); Pipe(pipefd); ptr = My_shm(sizeof(sem_t)); /* create memory-based semaphore */ Sem_init(ptr, 1, 0); for (j = 0; j < nproc; j++) { if (Fork() == 0) { /* 4child */ Sem_wait(ptr); /* wait for parent to start children */ for (i = 0; i < 10; i++) { my_lock(fd); /* lock the file */ my_unlock(fd); /* unlock the file */ } exit(0); } /* parent loops around, creating next child */ } for (j = 0; j < nproc; j++) Sem_post(ptr); /* start all the children */ /* now just wait for all the children to finish */ while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) ; Close(pipefd[1]); nconflicts = 0; while ( (n = Read(pipefd[0], &stat, 1)) > 0) nconflicts += n; printf("%d conflicts\n", nconflicts); exit(0); }
void Event::destroy() { tbb::spin_mutex::scoped_lock my_lock(destroyMutex_); //std::cout << "Event::destroy() for "<< (int) (this->getEventNumber())<< std::endl; #ifdef MEASURE_TIME firstEventPartAddedTime_.stop(); #endif for (uint_fast8_t i = 0; i != SourceIDManager::NUMBER_OF_L0_DATA_SOURCES; i++) { L0Subevents[i]->destroy(); } for (uint_fast8_t i = 0; i != SourceIDManager::NUMBER_OF_L1_DATA_SOURCES; i++) { L1Subevents[i]->destroy(); } for (auto& pair : nonSuppressedLkrFragmentsByCrateCREAMID) { delete pair.second; } nonSuppressedLkrFragmentsByCrateCREAMID.clear(); reset(); }