TEST(ParallelUtils,ReadWriteLock) { const int N = 1000000; const int K = 10; ReadWriteLock lock; volatile int c =0; #pragma omp parallel for num_threads(4) for(int i=0; i<N; i++) { if (i % K == 0) { // 10% write probability lock.start_write(); c++; lock.end_write(); } else { lock.start_read(); // nothing to do here .. lock.end_read(); } } EXPECT_EQ(N/K, c); }
///////////////////// // Initializes network bool InitNetworkSystem() { curl_global_init(CURL_GLOBAL_ALL); nlSystemUseChangeLock.startWriteAccess(); bNetworkInited = false; if(!nlInit()) { SystemError("nlInit failed"); nlSystemUseChangeLock.endWriteAccess(); return false; } if(!nlSelectNetwork(NL_IP)) { SystemError("could not select IP-based network"); nlSystemUseChangeLock.endWriteAccess(); return false; } bNetworkInited = true; dnsCache = new ThreadVar<dnsCacheT>(); #ifndef WIN32 //sigignore(SIGPIPE); signal(SIGPIPE, sigpipe_handler); #endif nlSystemUseChangeLock.endWriteAccess(); return true; }
////////////////// // Shutdowns the network system bool QuitNetworkSystem() { nlSystemUseChangeLock.startWriteAccess(); nlShutdown(); bNetworkInited = false; delete dnsCache; dnsCache = NULL; nlSystemUseChangeLock.endWriteAccess(); curl_global_cleanup(); return true; }
int handle() { nlSystemUseChangeLock.startReadAccess(); int ret = -1; if(bNetworkInited) { // only do that if we have the network system still up // this should already close the socket but not lock other parts in HawkNL nlPrepareClose(sock); // hopefully this does not block anymore ret = (nlClose(sock) != NL_FALSE) ? 0 : -1; } nlSystemUseChangeLock.endReadAccess(); return ret; }
lazy_string::char_reference &lazy_string::char_reference::operator=(char value) { ReadWriteLock ¤t = (*this->ls->lock); current.writeLock(); bool updating = false; if(this->ls->present.use_count() > 1) { this->ls->present = make_shared<std::string>(this->ls->present->substr(this->ls->start, this->ls->sizevar)); this->ls->start = 0; ReadWriteLock newLock; newLock.writeLock(); this->ls->lock = make_shared<ReadWriteLock>(newLock); updating = true; } const string svalue(1, value); (*this->ls->present).replace(this->ls->start + this->index, 1, svalue); current.writeUnlock(); if(updating) this->ls->lock->writeUnlock(); return *this; }
WriteLocker::WriteLocker(ReadWriteLock& rwLock) : _rwLock(rwLock) { rwLock.writeLock(); }
ReadLocker::ReadLocker(ReadWriteLock& rwLock) : _rwLock(rwLock) { rwLock.readLock(); }