void CScheduler::serviceQueue() { boost::unique_lock<boost::mutex> lock(newTaskMutex); ++nThreadsServicingQueue; // newTaskMutex is locked throughout this loop EXCEPT // when the thread is waiting or when the user's function // is called. while (!shouldStop()) { try { if (!shouldStop() && taskQueue.empty()) { reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock); // Use this chance to get a tiny bit more entropy RandAddSeedSleep(); } while (!shouldStop() && taskQueue.empty()) { // Wait until there is something to do. newTaskScheduled.wait(lock); } // Wait until either there is a new task, or until // the time of the first item on the queue: // wait_until needs boost 1.50 or later; older versions have timed_wait: #if BOOST_VERSION < 105000 while (!shouldStop() && !taskQueue.empty() && newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) { // Keep waiting until timeout } #else // Some boost versions have a conflicting overload of wait_until that returns void. // Explicitly use a template here to avoid hitting that overload. while (!shouldStop() && !taskQueue.empty()) { boost::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first; if (newTaskScheduled.wait_until<>(lock, timeToWaitFor) == boost::cv_status::timeout) break; // Exit loop after timeout, it means we reached the time of the event } #endif // If there are multiple threads, the queue can empty while we're waiting (another // thread may service the task we were waiting on). if (shouldStop() || taskQueue.empty()) continue; Function f = taskQueue.begin()->second; taskQueue.erase(taskQueue.begin()); { // Unlock before calling f, so it can reschedule itself or another task // without deadlocking: reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock); f(); } } catch (...) { --nThreadsServicingQueue; throw; } } --nThreadsServicingQueue; newTaskScheduled.notify_one(); }
static void ipifcregisterproxy(Fs *f, Ipifc *ifc, uint8_t *ip) { Conv **cp, **e; Ipifc *nifc; Iplifc *lifc; Medium *m; uint8_t net[IPaddrlen]; /* register the address on any network that will proxy for us */ e = &f->ipifc->conv[f->ipifc->nc]; if(!isv4(ip)) { /* V6 */ for(cp = f->ipifc->conv; cp < e; cp++){ if(*cp == nil || (nifc = (Ipifc*)(*cp)->ptcl) == ifc) continue; rlock(nifc); m = nifc->medium; if(m == nil || m->addmulti == nil) { runlock(nifc); continue; } for(lifc = nifc->lifc; lifc; lifc = lifc->next){ maskip(ip, lifc->mask, net); if(ipcmp(net, lifc->remote) == 0) { /* add solicited-node multicast addr */ ipv62smcast(net, ip); addselfcache(f, nifc, lifc, net, Rmulti); arpenter(f, V6, ip, nifc->mac, 6, 0); // (*m->addmulti)(nifc, net, ip); break; } } runlock(nifc); } } else { /* V4 */ for(cp = f->ipifc->conv; cp < e; cp++){ if(*cp == nil || (nifc = (Ipifc*)(*cp)->ptcl) == ifc) continue; rlock(nifc); m = nifc->medium; if(m == nil || m->areg == nil){ runlock(nifc); continue; } for(lifc = nifc->lifc; lifc; lifc = lifc->next){ maskip(ip, lifc->mask, net); if(ipcmp(net, lifc->remote) == 0){ (*m->areg)(nifc, ip); break; } } runlock(nifc); } } }
/** * Provides fine-tuned implementation for IDIV/IREM operations on IA32-compatible platforms, * in replacement of common arithmetic helper (see arith_rt.h). */ bool CodeGen::gen_a_platf(JavaByteCodes op, jtype jt) { if (jt != i32) return false; if (op != OPCODE_IDIV && op != OPCODE_IREM) { return false; } // // The method is supposed to be platform-depended, and may not have // Encoder support - leaving as-is, without implementing general // support in Encoder // vpark(eax.reg()); vpark(edx.reg()); rlock(eax); rlock(edx); Val& v1 = vstack(1, vis_imm(1)); Val& v2 = vstack(0, true); alu(alu_cmp, v2.as_opnd(), Opnd(-1)); unsigned br_normal = br(ne, 0, 0); alu(alu_cmp, v1.as_opnd(), Opnd(INT_MIN)); unsigned br_exit = NOTHING; if (op == OPCODE_IREM) { do_mov(edx, Opnd(0)); // prepare exit value for the corner case br_exit = br(eq, 0, 0); } else { do_mov(eax, v1); br_exit = br(eq, 0, 0); } patch(br_normal, ip()); do_mov(eax, v1); // // The method is supposed to be platform-depended, and may not have // Encoder support - leaving as-is, without implementing general // support in Encoder // //CDQ EncoderBase::Operands args0(RegName_EDX, RegName_EAX); ip(EncoderBase::encode(ip(), Mnemonic_CDQ, args0)); //IDIV EncoderBase::Operands args(RegName_EDX, RegName_EAX, devirt(v2.reg(), i32)); ip(EncoderBase::encode(ip(), Mnemonic_IDIV, args)); patch(br_exit, ip()); vpop(); vpop(); vpush(op == OPCODE_IREM ? edx : eax); runlock(eax); runlock(edx); return true; }
static int ipifcstate(struct conv *c, char *state, int n) { struct Ipifc *ifc; struct Iplifc *lifc; int m; ifc = (struct Ipifc *)c->ptcl; m = snprintf(state, n, sfixedformat, ifc->dev, ifc->maxtu, ifc->sendra6, ifc->recvra6, ifc->rp.mflag, ifc->rp.oflag, ifc->rp.maxraint, ifc->rp.minraint, ifc->rp.linkmtu, ifc->rp.reachtime, ifc->rp.rxmitra, ifc->rp.ttl, ifc->rp.routerlt, ifc->in, ifc->out, ifc->inerr, ifc->outerr); rlock(&ifc->rwlock); for (lifc = ifc->lifc; lifc && n > m; lifc = lifc->next) m += snprintf(state + m, n - m, slineformat, lifc->local, lifc->mask, lifc->remote, lifc->validlt, lifc->preflt); if (ifc->lifc == NULL) m += snprintf(state + m, n - m, "\n"); runlock(&ifc->rwlock); return m; }
int nametonum(char *s) { char *p; int i, lo, hi, m, rv; s = estrdup(s); strlower(s); for(p=s; *p; p++) if(*p=='_') *p = ' '; currentmap(0); rlock(&maplock); lo = 0; hi = map->nel; while(hi-lo > 1){ m = (lo+hi)/2; i = strcmp(s, map->el[m].s); if(i < 0) hi = m; else lo = m; } if(hi-lo == 1 && strcmp(s, map->el[lo].s)==0) rv = map->el[lo].n; else rv = -1; runlock(&maplock); free(s); return rv; }
void envcpy(Egrp *to, Egrp *from) { int i; Evalue *ne, *e; rlock(&from->rwl); to->ment = (from->nent+31)&~31; to->ent = smalloc(to->ment*sizeof(to->ent[0])); for(i=0; i<from->nent; i++){ e = from->ent[i]; ne = smalloc(sizeof(Evalue)); ne->name = smalloc(strlen(e->name)+1); strcpy(ne->name, e->name); if(e->value){ ne->value = smalloc(e->len); memmove(ne->value, e->value, e->len); ne->len = e->len; } ne->qid.path = ++to->path; to->ent[i] = ne; } to->nent = from->nent; runlock(&from->rwl); }
void cmd_sync(void) { rlock(&mainlock); syncall(); runlock(&mainlock); }
static File* walkfile1(File *dir, char *elem) { File *fp; Filelist *fl; rlock(&dir->RWLock); if(strcmp(elem, "..") == 0){ fp = dir->parent; incref(&fp->Ref); runlock(&dir->RWLock); closefile(dir); return fp; } fp = nil; for(fl=dir->filelist; fl; fl=fl->link) if(fl->f && strcmp(fl->f->Dir.name, elem)==0){ fp = fl->f; incref(&fp->Ref); break; } runlock(&dir->RWLock); closefile(dir); return fp; }
static int envgen(Chan *c, char *name, Dirtab* dir, int i, int s, Dir *dp) { Proc *up = externup(); Egrp *eg; Evalue *e; if(s == DEVDOTDOT){ devdir(c, c->qid, "#e", 0, eve, DMDIR|0775, dp); return 1; } eg = envgrp(c); rlock(&eg->rwl); e = 0; if(name) e = envlookup(eg, name, -1); else if(s < eg->nent) e = eg->ent[s]; if(e == 0) { runlock(&eg->rwl); return -1; } /* make sure name string continues to exist after we release lock */ kstrcpy(up->genbuf, e->name, sizeof up->genbuf); devdir(c, e->qid, up->genbuf, e->len, eve, 0666, dp); runlock(&eg->rwl); return 1; }
static void mntscan(Mntwalk *mw, Pgrp *pg) { Mount *t; Mhead *f; int nxt, i; ulong last, bestmid; rlock(&pg->ns); nxt = 0; bestmid = ~0; last = 0; if(mw->mh) last = mw->cm->mountid; for(i = 0; i < MNTHASH; i++) { for(f = pg->mnthash[i]; f; f = f->hash) { for(t = f->mount; t; t = t->next) { if(mw->mh == 0 || (t->mountid > last && t->mountid < bestmid)) { mw->cm = t; mw->mh = f; bestmid = mw->cm->mountid; nxt = 1; } } } } if(nxt == 0) mw->mh = 0; runlock(&pg->ns); }
static int32_t envread(Chan *c, void *a, int32_t n, int64_t off) { Egrp *eg; Evalue *e; int32_t offset; if(c->qid.type & QTDIR) return devdirread(c, a, n, 0, 0, envgen); eg = envgrp(c); rlock(&eg->rwl); e = envlookup(eg, nil, c->qid.path); if(e == 0) { runlock(&eg->rwl); error(Enonexist); } offset = off; if(offset > e->len) /* protects against overflow converting int64_t to long */ n = 0; else if(offset + n > e->len) n = e->len - offset; if(n <= 0) n = 0; else memmove(a, e->value+offset, n); runlock(&eg->rwl); return n; }
void LIRGenerator::do_Convert(Convert* x) { // flags that vary for the different operations and different SSE-settings bool fixed_input, fixed_result, round_result, needs_stub; switch (x->op()) { case Bytecodes::_i2l: // fall through case Bytecodes::_l2i: // fall through case Bytecodes::_i2b: // fall through case Bytecodes::_i2c: // fall through case Bytecodes::_i2s: fixed_input = false; fixed_result = false; round_result = false; needs_stub = false; break; case Bytecodes::_f2d: fixed_input = UseSSE == 1; fixed_result = false; round_result = false; needs_stub = false; break; case Bytecodes::_d2f: fixed_input = false; fixed_result = UseSSE == 1; round_result = UseSSE < 1; needs_stub = false; break; case Bytecodes::_i2f: fixed_input = false; fixed_result = false; round_result = UseSSE < 1; needs_stub = false; break; case Bytecodes::_i2d: fixed_input = false; fixed_result = false; round_result = false; needs_stub = false; break; case Bytecodes::_f2i: fixed_input = false; fixed_result = false; round_result = false; needs_stub = true; break; case Bytecodes::_d2i: fixed_input = false; fixed_result = false; round_result = false; needs_stub = true; break; case Bytecodes::_l2f: fixed_input = false; fixed_result = UseSSE >= 1; round_result = UseSSE < 1; needs_stub = false; break; case Bytecodes::_l2d: fixed_input = false; fixed_result = UseSSE >= 2; round_result = UseSSE < 2; needs_stub = false; break; case Bytecodes::_f2l: fixed_input = true; fixed_result = true; round_result = false; needs_stub = false; break; case Bytecodes::_d2l: fixed_input = true; fixed_result = true; round_result = false; needs_stub = false; break; default: ShouldNotReachHere(); } LIRItem value(x->value(), this); value.load_item(); LIR_Opr input = value.result(); LIR_Opr result = rlock(x); // arguments of lir_convert LIR_Opr conv_input = input; LIR_Opr conv_result = result; ConversionStub* stub = NULL; if (fixed_input) { conv_input = fixed_register_for(input->type()); __ move(input, conv_input); } assert(fixed_result == false || round_result == false, "cannot set both"); if (fixed_result) { conv_result = fixed_register_for(result->type()); } else if (round_result) { result = new_register(result->type()); set_vreg_flag(result, must_start_in_memory); } if (needs_stub) { stub = new ConversionStub(x->op(), conv_input, conv_result); } __ convert(x->op(), conv_input, conv_result, stub); if (result != conv_result) { __ move(conv_result, result); } assert(result->is_virtual(), "result must be virtual register"); set_result(x, result); }
static long envread(Chan *c, void *a, long n, vlong off) { Egrp *eg; Evalue *e; ulong offset = off; if(c->qid.type & QTDIR) return devdirread(c, a, n, 0, 0, envgen); eg = envgrp(c); rlock(eg); if(waserror()){ runlock(eg); nexterror(); } e = envlookup(eg, nil, c->qid.path); if(e == nil) error(Enonexist); if(offset >= e->len || e->value == nil) n = 0; else if(offset + n > e->len) n = e->len - offset; if(n <= 0) n = 0; else memmove(a, e->value+offset, n); runlock(eg); poperror(); return n; }
bool FileServerHandler::HandleFileQuery(SocketHandler *socket, QStringList &slist) { QStringList res; if (slist.size() != 4) { LOG(VB_GENERAL, LOG_ERR, QString("Invalid Request. %1") .arg(slist.join("[]:[]"))); res << "EMPTY LIST"; socket->SendStringList(res); return true; } QString wantHost = slist[1]; QString groupname = slist[2]; QString filename = slist[3]; LOG(VB_FILE, LOG_DEBUG, QString("HandleSGFileQuery: myth://%1@%2/%3") .arg(groupname).arg(wantHost).arg(filename)); if ((wantHost.toLower() == gCoreContext->GetHostName().toLower()) || gCoreContext->IsThisHost(wantHost)) { // handle request locally LOG(VB_FILE, LOG_DEBUG, QString("Getting local info")); StorageGroup sg(groupname, gCoreContext->GetHostName()); res = sg.GetFileInfo(filename); if (res.count() == 0) res << "EMPTY LIST"; } else { // handle request on remote server SocketHandler *remsock = NULL; { QReadLocker rlock(&m_fsLock); if (m_fsMap.contains(wantHost)) { remsock = m_fsMap[wantHost]; remsock->IncrRef(); } } if (remsock) { res << "QUERY_SG_FILEQUERY" << wantHost << groupname << filename; remsock->SendReceiveStringList(res); remsock->DecrRef(); } else { res << "SLAVE UNREACHABLE: " << wantHost; } } socket->SendStringList(res); return true; }
Component& Comploader::fetchComp(const Compident& ci, const Urlmapper& rootmapper) { log_debug("fetchComp \"" << ci << '"'); cxxtools::ReadLock rlock(mutex); cxxtools::WriteLock wlock(mutex, false); // lookup Component componentmap_type::iterator it = componentmap.find(ci); if (it == componentmap.end()) { rlock.unlock(); wlock.lock(); it = componentmap.find(ci); if (it == componentmap.end()) { ComponentLibrary& lib = fetchLib(ci.libname); Component* comp = lib.create(ci.compname, *this, rootmapper); componentmap[ci] = comp; return *comp; } } return *(it->second); }
static int envgen(Chan *c, char *name, Dirtab*, int, int s, Dir *dp) { Egrp *eg; Evalue *e; if(s == DEVDOTDOT){ devdir(c, c->qid, "#e", 0, eve, DMDIR|0775, dp); return 1; } eg = envgrp(c); rlock(eg); if(name != nil) e = envlookup(eg, name, -1); else if(s < eg->nent) e = &eg->ent[s]; else e = nil; if(e == nil || name != nil && (strlen(e->name) >= sizeof(up->genbuf))) { runlock(eg); return -1; } /* make sure name string continues to exist after we release lock */ kstrcpy(up->genbuf, e->name, sizeof up->genbuf); devdir(c, e->qid, up->genbuf, e->len, eve, 0666, dp); runlock(eg); return 1; }
/* * allocate 'count' contiguous channels * of type 'type' and return pointer to base */ Chan* fs_chaninit(int type, int count, int data) { uint8_t *p; Chan *cp, *icp; int i; p = malloc(count * (sizeof(Chan)+data)); icp = (Chan*)p; for(i = 0; i < count; i++) { cp = (Chan*)p; cp->next = chans; chans = cp; cp->type = type; cp->chan = cons.chano; cons.chano++; strncpy(cp->whoname, "<none>", sizeof cp->whoname); wlock(&cp->reflock); wunlock(&cp->reflock); rlock(&cp->reflock); runlock(&cp->reflock); p += sizeof(Chan); if(data){ cp->pdata = p; p += data; } } return icp; }
void Config::scheduledQueries(std::function< void(const std::string& name, const ScheduledQuery& query)> predicate) { ReadLock rlock(config_schedule_mutex_); for (Pack& pack : *schedule_) { for (const auto& it : pack.getSchedule()) { std::string name = it.first; // The query name may be synthetic. if (pack.getName() != "main" && pack.getName() != "legacy_main") { name = "pack" + FLAGS_pack_delimiter + pack.getName() + FLAGS_pack_delimiter + it.first; } // They query may have failed and been added to the schedule's blacklist. if (schedule_->blacklist_.count(name) > 0) { auto blacklisted_query = schedule_->blacklist_.find(name); if (getUnixTime() > blacklisted_query->second) { // The blacklisted query passed the expiration time (remove). schedule_->blacklist_.erase(blacklisted_query); saveScheduleBlacklist(schedule_->blacklist_); } else { // The query is still blacklisted. continue; } } // Call the predicate. predicate(name, it.second); } } }
/* * main filesystem server loop. * entered by many processes. * they wait for message buffers and * then process them. */ void serve(void *) { int i; Chan *cp; Msgbuf *mb; for (;;) { qlock(&reflock); /* read 9P request from a network input process */ mb = fs_recv(serveq, 0); assert(mb->magic == Mbmagic); /* fs kernel sets chan in /sys/src/fs/ip/il.c:/^getchan */ cp = mb->chan; if (cp == nil) panic("serve: nil mb->chan"); rlock(&cp->reflock); qunlock(&reflock); rlock(&mainlock); if (mb->data == nil) panic("serve: nil mb->data"); /* better sniffing code in /sys/src/cmd/disk/kfs/9p12.c */ if(cp->protocol == nil){ /* do we recognise the protocol in this packet? */ /* better sniffing code: /sys/src/cmd/disk/kfs/9p12.c */ for(i = 0; fsprotocol[i] != nil; i++) if(fsprotocol[i](mb) != 0) { cp->protocol = fsprotocol[i]; break; } if(cp->protocol == nil){ print("no protocol for message\n"); for(i = 0; i < 12; i++) print(" %2.2X", mb->data[i]); print("\n"); } } else /* process the request, generate an answer and reply */ cp->protocol(mb); mbfree(mb); runlock(&mainlock); runlock(&cp->reflock); } }
int lock_release(lock_t *lock) { /*int* a; int t =10; a=&t;*/ rlock(lock); return -1; }
void Config::getPerformanceStats( const std::string& name, std::function<void(const QueryPerformance& query)> predicate) { if (performance_.count(name) > 0) { ReadLock rlock(config_performance_mutex_); predicate(performance_.at(name)); } }
/*public */void ASDDeviceTransceiver::getData(DataRec* result) const { if (result != NULL) { boost::shared_lock<boost::shared_mutex> rlock(_mutex); *result = _data; } return; }
// _ineg, _lneg, _fneg, _dneg void LIRGenerator::do_NegateOp(NegateOp* x) { LIRItem value(x->x(), this); value.set_destroys_register(); value.load_item(); LIR_Opr reg = rlock(x); __ negate(value.result(), reg); set_result(x, round_item(reg)); }
bool Cache::exists( const Key &key ) { rlock(); std::unordered_map<std::string, Document*>::const_iterator got = data.find( key.str() ); bool exists = got != data.end(); unlock(); return exists; }
SnapshotPtr ExpDecaySample::getSnapshot() const { ValueVector vals; vals.reserve(values_.size()); std::lock_guard<std::mutex> rlock(mutex_); for(Double2Int64Map::const_iterator it = values_.begin(); it != values_.end(); ++it) { vals.push_back(it->second); } return SnapshotPtr(new Snapshot(vals)); }
void Config::files( std::function<void(const std::string& category, const std::vector<std::string>& files)> predicate) { ReadLock rlock(config_files_mutex_); for (const auto& it : files_) { for (const auto& category : it.second) { predicate(category.first, category.second); } } }
void Compiler::gen_if_icmp(JavaByteCodes opcod, unsigned target) { if (target <= m_pc) { // have back branch here gen_prof_be(); gen_gc_safe_point(); } if (opcod == OPCODE_IF_ACMPEQ) { opcod = OPCODE_IF_ICMPEQ; } else if (opcod == OPCODE_IF_ACMPNE) { opcod = OPCODE_IF_ICMPNE; } Opnd op2 = vstack(0).as_opnd(); vpop(); rlock(op2); OpndKind kind = m_jframe->dip(0).kind(); // 'Bad' combinations are 'm,m' and 'imm,<any, but imm>' - have to force // an item into a register bool forceReg = (op2.is_mem() && kind == opnd_mem) || (op2.is_imm() && kind == opnd_imm); Opnd op1 = vstack(0, forceReg).as_opnd(); vpop(); rlock(op1); COND cond = to_cond(opcod); if ( (op1.is_mem() && op2.is_reg()) || op1.is_imm()) { // here we have 'mem, reg' or 'imm, mem-or-reg' - swap them so it // become 'reg, mem' (more efficient) or 'mem-or-reg, imm' (existent) // operations. change the branch condition appropriately. alu(alu_cmp, op2, op1); cond = flip(cond); } else { alu(alu_cmp, op1, op2); } runlock(op1); runlock(op2); gen_bb_leave(target); br(cond, target, m_bbinfo->start); }
static int fileRLock(File *f) { assert(!canwlock(&f->fs->elk)); rlock(&f->lk); if(!chkSource(f)){ fileRUnlock(f); return 0; } return 1; }
SIPTCPConnection::Ptr SIPTCPConnectionManager::findConnectionByAddress(const OSS::Net::IPAddress& target) { OSS::mutex_read_lock rlock(_rwConnectionsMutex); for (std::map<OSS::UInt64, SIPTCPConnection::Ptr>::iterator iter = _connections.begin(); iter != _connections.end(); iter++) { if (iter->second->getRemoteAddress().compare(target, true)) return iter->second; } return SIPTCPConnection::Ptr(); }
int SafeQueue::back() { std::lock_guard<std::mutex> rlock(readLock); int data = -1; //For debugging, not acceptable in production if(!dataQueue.empty()) { data = dataQueue.back(); } return data; }