int Processor::AdjustThreadPool(int c, int check) { Thread * t; ThreadLocalData *tld; if (num == c) return rnum; num = c; if (rnum > c) { /* First shrink the idle threads in the pool. */ while (rnum > c) { t = NextIdleThreadFromPool(); if (t == NULL) break; tld = t->GetTLD(); delete t; delete tld; rnum--; } /* * If there are some threads still in work state, * release it once finishe the work. */ //num = c; LASSERT(idle >= 0); if (idle == 0) poolState = pool_FullLoadState; } else { /* add the new threads into the pool */ while (rnum < c) { t = new Thread; tld = new ThreadLocalData; memset(tld, 0, sizeof(*tld)); tld->m = NULL; tld->p = this; tld->n = site; tld->t = t; tld->id = counter++; tld->flags = TLD_FROM_POOL; t->CreateThread(ProcessOneTask, tld); t->Insert(&idleQ); rnum++; idle++; } if (poolState == pool_FullLoadState && idle > 0 && check) { Signal(); poolState = pool_RunState; } } return rnum; }
Processor::~Processor() { Thread *t; while ((t = NextIdleThreadFromPool()) != NULL) { ThreadLocalData *tld; tld = t->GetTLD(); delete tld; delete t; } }
void Processor::PoolStateMachine() { switch (poolState) { case pool_RunState: { Message *msg; Thread *t; ThreadLocalData *tld; /* I/O is throttled. */ if (nrs->Throttling()) { poolState = pool_IdleState; return; } /* Initialize the working thread to handle * various tasks and start it in the next tick. */ msg = (Message *)nrs->Dequeue(); if (msg == NULL) { poolState = pool_IdleState; return; } Print(NOW "Processor dequeues ioreq@%p, %d:%llu:%llu\n", Event::Clock(), &msg->io, msg->io.cmd, msg->io.off, msg->io.count); t = NextIdleThreadFromPool(); assert(t != NULL); tld = t->GetTLD(); tld->m = msg; tld->f = site->GetHandler(); //t->Run(); RunOneTask(t); /* Check for left queued tasks. */ PoolStateMachine(); break; } case pool_StartState: poolState = pool_IdleState; case pool_IdleState: case pool_FullLoadState: break; } }