Created
May 19, 2014 15:33
-
-
Save kevinlynx/3f55ccfc7ecb492bf43f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_status : | |
HANGUP, // nothing to do | |
INCING, // increasing threads | |
DECING, // decreasing threads | |
manager_thread_proc { | |
while (_run) { | |
CondtionLock guard(ctrlCond); | |
while (_run && _status != INCING) { // no event | |
ctrlCond.wait(); | |
} | |
if (!_run) break; | |
if (_status == INCIING) { // create new threads | |
for (i <- 0 to threadCnt) { | |
new Thread() | |
sleep(1000) // to make cpu smooth | |
} | |
} | |
} | |
} | |
// called by worker thread after `doWork' | |
inc_active { | |
CondtionLock guard(ctrlCond); | |
if (_status == HANGUP) { | |
_status = INCING; | |
ctrlCond.signal(); | |
} | |
} | |
pushWorker { | |
if (queue.size > maxQueueSize / 2) { // busy queue | |
freeStartTime = 0; | |
} else if (freeStartTime == 0) | |
freeStartTime = now(); | |
// ... | |
} | |
pop { | |
if (_decCount > 0 && freeStartTime + threhold < now()) { // free for a while | |
CondtionLock guard(ctrlCond); | |
if (_status == HANGUP) { | |
// which means it's safe to decreasing threads | |
_status = DECING; | |
_decCount = DEC_COUNT; | |
} | |
} | |
if (_decCount-- > 0) { | |
// return a special flag to tell the calling thread to exit after processed the queue item | |
} else { | |
CondtionLock guard(ctrlCond); | |
// decreasing done | |
_status = HANGUP; | |
} | |
// other normal stuff... | |
} | |
worker_thread_proc { | |
while (_run) { | |
// signal.wait | |
item = pop(&type); | |
doWork(item); | |
if (type == exit) | |
break; // exit this worker thread | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment