read_names() { struct iocc_device iod; register struct iocc_device *mp; struct iocc_driver mdrv; union { short two_short; char two_char[2]; } two; char *cp = two.two_char; mp = (struct iocc_device *) namelist[X_MBDINIT].n_value; if (mp == 0) { fprintf(stderr, "iostat: Disk init info not in namelist\n"); exit(1); } for (;;) { steal(mp++, iod); if (iod.iod_driver == 0) break; /* if (debug) printf("dk=%d alive=%d ctlr=%d\n",iod.iod_dk, iod.iod_alive,iod.iod_ctlr); */ if (iod.iod_dk < 0 || iod.iod_alive == 0) continue; if (iod.iod_ctlr < 0) continue; steal(iod.iod_driver, mdrv); steal(mdrv.idr_dname, two.two_short); sprintf(dr_name[iod.iod_dk], "%c%c%d", cp[0], cp[1], dr_unit[iod.iod_dk] = iod.iod_unit); } }
read_names() { struct iocc_device mdev; register struct iocc_device *mp; struct iocc_driver mdrv; short two_char; char *cp = (char *) &two_char; mp = (struct iocc_device *) namelist[X_MBDINIT].n_value; if (mp == 0) { fprintf(stderr, "iostat: Disk init info not in namelist\n"); exit(1); } for (;;) { steal(mp++, mdev); if (mdev.md_driver == 0) break; if (mdev.md_dk < 0 || mdev.md_alive == 0) continue; steal(mdev.md_driver, mdrv); steal(mdrv.mdr_dname, two_char); sprintf(dr_name[mdev.md_dk], "%c%c%d", cp[0], cp[1]); dr_unit[mdev.md_dk] = mdev.md_unit; } }
/** * Run a scheduler thread until termination. */ static void run(scheduler_t* sched) { pony_actor_t* actor = pop_global(sched); while(true) { if(actor == NULL) { // We had an empty queue and no rescheduled actor. actor = steal(sched, NULL); if(actor == NULL) { // Termination. assert(pop(sched) == NULL); return; } } // Run the current actor and get the next actor. bool reschedule = ponyint_actor_run(&sched->ctx, actor, SCHED_BATCH); pony_actor_t* next = pop_global(sched); if(reschedule) { if(next != NULL) { // If we have a next actor, we go on the back of the queue. Otherwise, // we continue to run this actor. push(sched, actor); actor = next; } else if(ponyint_is_cycle(actor)) { // If all we have is the cycle detector, try to steal something else to // run as well. next = steal(sched, actor); if(next == NULL) { // Termination. return; } // Push the cycle detector and run the actor we stole. if(actor != next) { push(sched, actor); actor = next; } } } else { // We aren't rescheduling, so run the next actor. This may be NULL if our // queue was empty. actor = next; } } }
/** Work-stealing loop for workers */ static int worker_run (void * data) { struct worker_args * args = (struct worker_args*) data; int id = args->id; errval_t err = thread_detach(thread_self()); assert(err_is_ok(err)); free(args); workers[id].worker_thr = thread_self(); workers[id].id = id; workers[id].core_id = disp_get_core_id(); struct generic_task_desc * _tweed_top_ = NULL; thread_set_tls( &(workers[id])); trace_init_disp(); num_dispatchers += 1; // start trying to steal work int steal_id = (id+1) % num_workers; while(!do_quit) { int success = steal(_tweed_top_, &workers[steal_id]); if (!success) { // try next worker steal_id = (steal_id+1) % num_workers; } } exit(0); return 0; }
void acquire(Context* c, Resource* resource, Value* value, Site* site) { assert(c, value); assert(c, site); if (not resource->reserved) { if (DebugResources) { char buffer[256]; resource->toString(c, buffer, 256); fprintf(stderr, "%p acquire %s\n", value, buffer); } if (resource->value) { assert(c, resource->value->findSite(resource->site)); assert(c, not value->findSite(resource->site)); steal(c, resource, value); } if (c->acquiredResources) { c->acquiredResources->previousAcquired = resource; resource->nextAcquired = c->acquiredResources; } c->acquiredResources = resource; resource->value = value; resource->site = site; } }
void * thief(void * arg) { int i; for(i=0; i<ROUNDS; ++i) { steal(); } }
void TPDynamic::policy() { useconds_t usecs = 1, range = 1; #ifdef TRACE addRecord(getTime(), (void*) &TPDynamic::policy); #endif while (alive()) { //Check if we have any work in our deque tpClosure * tempClosure; if (!(tempClosure = popTP())) tempClosure = steal(); if (tempClosure) { usecs = range; // reset sleep time; #ifdef TRACE addRecord(getTime(), (void*) tempClosure->factory); #endif tempClosure->factory(tempClosure); #ifdef TRACE addRecord(getTime(), (void*) &TPDynamic::policy); #endif delete tempClosure; //Get the work ready! } else { usleep(usecs); if (usecs < 500) usecs *= 2; } //Lets do the work! Codelet * tempCodelet = popCodelet(); while (tempCodelet) { ThreadedProcedure * checkTP = tempCodelet->getTP(); bool deleteTP = (checkTP) ? checkTP->checkParent() : false; #ifdef TRACE addRecord(getTime(), tempCodelet->returnFunct()); #endif #ifdef COUNT if(getAffinity()) getAffinity()->startCounters(getID()); #endif tempCodelet->fire(); #ifdef COUNT if(getAffinity()) getAffinity()->incrementCounters(getID()); #endif #ifdef TRACE addRecord(getTime(), (void*) &TPDynamic::policy); #endif if (deleteTP) { if (checkTP->decRef()) delete checkTP; } tempCodelet = popCodelet(); } } }
const String Bytes::decode ( const std::string& encoding ) const { ::PyObject *const object = ::PyString_Decode (data(), size(), encoding.c_str(), "strict"); if ( object == 0 ) { } return (String(object, steal())); }
// ---------------------------------------------------------- // steal an enchantment // --------------------------------------------------------- bool t_steal_enchantment::cast_on( t_combat_creature_ptr caster, t_combat_creature_ptr target, int power, bool is_mirror ) { t_combat_action_message message; // Steal will fill in message if (!steal( caster, target, true, message )) return false; // Use message passed out from steal m_battlefield.start_animation( *target, k_spell_steal_enchantment, message ); return true; }
static void adv_sleep(int usec) { /* * usleep is depracated by linux, but nanosleep is a bit clumpsy. * here implement usleep interface by nanosleep . nanosleep accept * long, but it cannot prevent passing in a signed value, because the * compiler perform implict type conversion. So, assert * * From the man page, nanosleep only sleep a thread. */ assert(usec >= 0); struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1e3 }; nanosleep(&req, NULL); } static void *stealer(void *amountptr) { int amount = *(int*)amountptr; char name[15]; sprintf(name, "stealer %d", amount); notify("born\n", name); int *mypocketptr = malloc(sizeof(int)); /* no money, so have to steal */ int trial = 0; while (trial < MAXTRIAL){ pthread_mutex_lock(&acc1.mutex); trial++; notify("start trial %d\n", name, trial); if (amount > acc1.balance){ notify("failed to steal\n", name); pthread_mutex_unlock(&acc1.mutex); punish(amount); } else { steal(name, amount, &acc1); if (sleepedguy != 0){ pthread_cond_broadcast(&acc1.cond_full); } pthread_mutex_unlock(&acc1.mutex); save(amount, mypocketptr); } } return mypocketptr; }
void processor(int n) { while (!terminate) { // TODO int poped = job_queue[n].PopLeft(); if (poped >= 0) { printf("%i poped: %i\n",n, poped); sleep(poped); } else { int stolen = steal(n); if(stolen > 0) printf("%i sleeping %i (stolen)\n", n, stolen); sleep(stolen); } } }
unsigned int Thief::think() { switch( _state ) { case SEEK: seek(); // Procura por uma vítima break; case CHASE: chase(); // Persegue uma vítima break; case STEAL: steal(); // Rouba o meteorito break; } return 0; }
void TPRoundRobin::policy() { useconds_t usecs = 1, range = 1; #ifdef TRACE addRecord(getTime(), (void*) &TPRoundRobin::policy); #endif while (alive()) { //Check if we have any work in our deque tpClosure * tempClosure; if (!(tempClosure = popTP())) tempClosure = steal(); if (tempClosure) { usecs = range; // reset sleep time #ifdef TRACE addRecord(getTime(), (void*) tempClosure->factory); #endif tempClosure->factory(tempClosure); #ifdef TRACE addRecord(getTime(), (void*) &TPRoundRobin::policy); #endif delete tempClosure; //Get the work ready! } else { usleep(usecs); if (usecs < 500) usecs *= 2; } //Lets do the work! Codelet * tempCodelet = popCodelet(); while (tempCodelet) { MScheduler * myCDS = static_cast<MScheduler*> (getSubScheduler(getSubIndexInc())); while (!myCDS->pushCodelet(tempCodelet)) { myCDS = static_cast<MScheduler*> (getSubScheduler(getSubIndexInc())); } tempCodelet = popCodelet(); } } }
/* main parallism function */ void worker() { int i,j; for(;;) { /* find work */ if (!(cj = freejob()) && !(cj = steal())) return; if (cj->status == JOB_DONE) // done return; /* do work */ cj->status = JOB_TAKEN; p_head = p_tail = 0; slow_crunch(); /* aftermath */ switch (cj->status) { case JOB_BLOCKED: add_jobarray(cj); cj = 0; break; case JOB_DONE: if (!cj->jid && master) // done return; else { send_done(cj); free(cj); } break; case JOB_CANCELLED: free(cj); } } }
read_names() { struct mba_device mdev; register struct mba_device *mp; struct mba_driver mdrv; short two_char; char *cp = (char *) &two_char; struct uba_device udev, *up; struct uba_driver udrv; mp = (struct mba_device *) namelist[X_MBDINIT].n_value; up = (struct uba_device *) namelist[X_UBDINIT].n_value; if (up == 0) { fprintf(stderr, "vmstat: Disk init info not in namelist\n"); exit(1); } if (mp) for (;;) { steal(mp++, mdev); if (mdev.mi_driver == 0) break; if (mdev.mi_dk < 0 || mdev.mi_alive == 0) continue; steal(mdev.mi_driver, mdrv); steal(mdrv.md_dname, two_char); sprintf(dr_name[mdev.mi_dk], "%c%c", cp[0], cp[1]); dr_unit[mdev.mi_dk] = mdev.mi_unit; } for (;;) { steal(up++, udev); if (udev.ui_driver == 0) break; if (udev.ui_dk < 0 || udev.ui_alive == 0) continue; steal(udev.ui_driver, udrv); steal(udrv.ud_dname, two_char); sprintf(dr_name[udev.ui_dk], "%c%c", cp[0], cp[1]); dr_unit[udev.ui_dk] = udev.ui_unit; } }
int main(void){ double res; res = steal(1.23456789); printf("%lf\n", res); }
void Allocator::steal(Allocator *allocator) { while(allocator->ptrs) steal(allocator->ptrs->ptr, allocator); }
static inline int waiting(struct generic_task_desc * _tweed_top_) { // steal work from the thief return steal(_tweed_top_, (struct worker_desc *) GET_THIEF(_tweed_top_)); }
/* * mhitu: monster hits you * returns 1 if monster dies (e.g. 'y', 'F'), 0 otherwise */ bool mhitu(struct monst *mtmp) { struct permonst *mdat = mtmp->data; int tmp, ctmp; nomul(0); /* If swallowed, can only be affected by hissers and by u.ustuck */ if (u.uswallow) { if (mtmp != u.ustuck) { if (mdat->mlet == 'c' && !rn2(13)) { pline("Outside, you hear %s's hissing!", monnam(mtmp)); pline("%s gets turned to stone!", Monnam(u.ustuck)); pline("And the same fate befalls you."); done_in_by(mtmp); /* "notreached": not return(1); */ } return (0); } switch (mdat->mlet) { /* now mtmp == u.ustuck */ case ',': youswld(mtmp, (u.uac > 0) ? u.uac + 4 : 4, 5, "The trapper"); break; case '\'': youswld(mtmp, rnd(6), 7, "The lurker above"); break; case 'P': youswld(mtmp, d(2, 4), 12, "The purple worm"); break; default: /* This is not impossible! */ pline("The mysterious monster totally digests you."); u.uhp = 0; } if (u.uhp < 1) done_in_by(mtmp); return (0); } if (mdat->mlet == 'c' && Stoned) return (0); /* make eels visible the moment they hit/miss us */ if (mdat->mlet == ';' && mtmp->minvis && cansee(mtmp->mx, mtmp->my)) { mtmp->minvis = 0; pmon(mtmp); } if (!strchr("1&DuxynNF", mdat->mlet)) tmp = hitu(mtmp, d(mdat->damn, mdat->damd)); else tmp = 0; if (strchr(UNDEAD, mdat->mlet) && midnight()) tmp += hitu(mtmp, d(mdat->damn, mdat->damd)); ctmp = tmp && !mtmp->mcan && (!uarm || objects[uarm->otyp].a_can < rnd(3) || !rn2(50)); switch (mdat->mlet) { case '1': if (wiz_hit(mtmp)) /* he disappeared */ return (1); break; case '&': if (!mtmp->cham && !mtmp->mcan && !rn2(13)) { makemon(PM_DEMON, u.ux, u.uy); } else { hitu(mtmp, d(2, 6)); hitu(mtmp, d(2, 6)); hitu(mtmp, rnd(3)); hitu(mtmp, rnd(3)); hitu(mtmp, rn1(4, 2)); } break; case ',': if (tmp) justswld(mtmp, "The trapper"); break; case '\'': if (tmp) justswld(mtmp, "The lurker above"); break; case ';': if (ctmp) { if (!u.ustuck && !rn2(10)) { pline("%s swings itself around you!", Monnam(mtmp)); u.ustuck = mtmp; } else if (u.ustuck == mtmp && levl[mtmp->mx][mtmp->my].typ == POOL) { pline("%s drowns you ...", Monnam(mtmp)); done("drowned"); } } break; case 'A': if (ctmp && rn2(2)) { if (Poison_resistance) pline("The sting doesn't seem to affect you."); else { pline("You feel weaker!"); losestr(1); } } break; case 'C': hitu(mtmp, rnd(6)); break; case 'c': if (!rn2(5)) { pline("You hear %s's hissing!", monnam(mtmp)); if (ctmp || !rn2(20) || (flags.moonphase == NEW_MOON && !carrying(DEAD_LIZARD))) Stoned = 5; } break; case 'D': if (rn2(6) || mtmp->mcan) { hitu(mtmp, d(3, 10)); hitu(mtmp, rnd(8)); hitu(mtmp, rnd(8)); break; } kludge("%s breathes fire!", "The dragon"); buzz(-1, mtmp->mx, mtmp->my, u.ux - mtmp->mx, u.uy - mtmp->my); break; case 'd': hitu(mtmp, d(2, (flags.moonphase == FULL_MOON) ? 3 : 4)); break; case 'e': hitu(mtmp, d(3, 6)); break; case 'F': if (mtmp->mcan) break; kludge("%s explodes!", "The freezing sphere"); if (Cold_resistance) pline("You don't seem affected by it."); else { xchar dn; if (17 - (u.ulevel / 2) > rnd(20)) { pline("You get blasted!"); dn = 6; } else { pline("You duck the blast..."); dn = 3; } losehp_m(d(dn, 6), mtmp); } mondead(mtmp); return (1); case 'g': if (ctmp && multi >= 0 && !rn2(3)) { kludge("You are frozen by %ss juices", "the cube'"); nomul(-rnd(10)); } break; case 'h': if (ctmp && multi >= 0 && !rn2(5)) { nomul(-rnd(10)); kludge("You are put to sleep by %ss bite!", "the homunculus'"); } break; case 'j': tmp = hitu(mtmp, rnd(3)); tmp &= hitu(mtmp, rnd(3)); if (tmp) { hitu(mtmp, rnd(4)); hitu(mtmp, rnd(4)); } break; case 'k': if ((hitu(mtmp, rnd(4)) || !rn2(3)) && ctmp) poisoned("bee's sting", mdat->mname); break; case 'L': if (tmp) stealgold(mtmp); break; case 'N': if (mtmp->mcan && !Blind) { pline("%s tries to seduce you, but you seem not interested.", Amonnam(mtmp, "plain")); if (rn2(3)) rloc(mtmp); } else if (steal(mtmp)) { rloc(mtmp); mtmp->mflee = 1; } break; case 'n': if (!uwep && !uarm && !uarmh && !uarms && !uarmg) { pline("%s hits! (I hope you don't mind)", Monnam(mtmp)); u.uhp += rnd(7); if (!rn2(7)) u.uhpmax++; if (u.uhp > u.uhpmax) u.uhp = u.uhpmax; flags.botl = 1; if (!rn2(50)) rloc(mtmp); } else { hitu(mtmp, d(2, 6)); hitu(mtmp, d(2, 6)); } break; case 'o': tmp = hitu(mtmp, rnd(6)); if (hitu(mtmp, rnd(6)) && tmp && /* hits with both paws */ !u.ustuck && rn2(2)) { u.ustuck = mtmp; kludge("%s has grabbed you!", "The owlbear"); u.uhp -= d(2, 8); } else if (u.ustuck == mtmp) { u.uhp -= d(2, 8); pline("You are being crushed."); } break; case 'P': if (ctmp && !rn2(4)) justswld(mtmp, "The purple worm"); else hitu(mtmp, d(2, 4)); break; case 'Q': hitu(mtmp, rnd(2)); hitu(mtmp, rnd(2)); break; case 'R': if (tmp && uarmh && !uarmh->rustfree && (int)uarmh->spe >= -1) { pline("Your helmet rusts!"); uarmh->spe--; } else if (ctmp && uarm && !uarm->rustfree && /* Mike Newton */ uarm->otyp < STUDDED_LEATHER_ARMOR && (int)uarm->spe >= -1) { pline("Your armor rusts!"); uarm->spe--; } break; case 'S': if (ctmp && !rn2(8)) poisoned("snake's bite", mdat->mname); break; case 's': if (tmp && !rn2(8)) poisoned("scorpion's sting", mdat->mname); hitu(mtmp, rnd(8)); hitu(mtmp, rnd(8)); break; case 'T': hitu(mtmp, rnd(6)); hitu(mtmp, rnd(6)); break; case 't': if (!rn2(5)) rloc(mtmp); break; case 'u': mtmp->mflee = 1; break; case 'U': hitu(mtmp, d(3, 4)); hitu(mtmp, d(3, 4)); break; case 'v': if (ctmp && !u.ustuck) u.ustuck = mtmp; break; case 'V': if (tmp) u.uhp -= 4; if (ctmp) losexp(); break; case 'W': if (ctmp) losexp(); break; #ifndef NOWORM case 'w': if (tmp) wormhit(mtmp); #endif /* NOWORM */ break; case 'X': hitu(mtmp, rnd(5)); hitu(mtmp, rnd(5)); hitu(mtmp, rnd(5)); break; case 'x': { long side = rn2(2) ? RIGHT_SIDE : LEFT_SIDE; pline("%s pricks in your %s leg!", Monnam(mtmp), (side == RIGHT_SIDE) ? "right" : "left"); set_wounded_legs(side, rnd(50)); losehp_m(2, mtmp); break; } case 'y': if (mtmp->mcan) break; mondead(mtmp); if (!Blind) { pline("You are blinded by a blast of light!"); Blind = d(4, 12); seeoff(0); } return (1); case 'Y': hitu(mtmp, rnd(6)); break; } if (u.uhp < 1) done_in_by(mtmp); return (0); }
void TPPushFull::policy() { useconds_t usecs = 1, range = 1; #ifdef TRACE addRecord(getTime(), (void*) &TPPushFull::policy); #endif while (alive()) { //Check if we have any work in our deque tpClosure * tempClosure = popTP(); if (!tempClosure) tempClosure = steal(); if (tempClosure) { usecs = range; // reset sleep time #ifdef TRACE addRecord(getTime(), (void*) tempClosure->factory); #endif tempClosure->factory(tempClosure); #ifdef TRACE addRecord(getTime(), (void*) &TPPushFull::policy); #endif delete tempClosure; //Get the work ready! } else { usleep(usecs); if (usecs < 500) usecs *= 2; } //Lets do the work! Codelet * tempCodelet = popCodelet(); while (tempCodelet) { //Here we are going to try to push bool fail = true; for (size_t i = 0; i < getNumSub(); i++) { MScheduler * myCDS = static_cast<MScheduler*> (getSubScheduler(getSubIndexInc())); if (myCDS->pushCodelet(tempCodelet)) { fail = false; break; } } //If we fail do it ourself if (fail) { ThreadedProcedure * checkTP = tempCodelet->getTP(); bool deleteTP = (checkTP) ? checkTP->checkParent() : false; #ifdef TRACE addRecord(getTime(), tempCodelet->returnFunct()); #endif #ifdef COUNT if(getAffinity()) getAffinity()->startCounters(getID()); #endif tempCodelet->fire(); #ifdef COUNT if(getAffinity()) getAffinity()->incrementCounters(getID()); #endif #ifdef TRACE addRecord(getTime(), (void*) &TPPushFull::policy); #endif if (deleteTP) { if (checkTP->decRef()) delete checkTP; } } tempCodelet = popCodelet(); } } }