PooledThread* ThreadPool::getThread() { FastMutex::ScopedLock lock(_mutex); if (++_age == 32) housekeep(); PooledThread* pThread = 0; for (ThreadVec::iterator it = _threads.begin(); !pThread && it != _threads.end(); ++it) { if ((*it)->idle()) pThread = *it; } if (!pThread) { if (_threads.size() < _maxCapacity) { pThread = createThread(); try { pThread->start(); _threads.push_back(pThread); } catch (...) { delete pThread; throw; } } else throw NoThreadAvailableException(); } pThread->activate(); return pThread; }
void main() { unsigned int8 d; // delay value unsigned int8 i; unsigned int8 obyte=0; int16 spin; // pwm 0->1023 char c; // port D (7:0) SW,DW SZ,DZ SY,DY SX,DX init(); usb_cdc_init(); usb_init(); usb_wait_for_enumeration(); while (TRUE) { // get chars from usb housekeep(); // now process the input and feed output queue if (nqueue(&rxque0) > 0) { c = dequeue(&rxque0); if ((c&0xf0) == 0x90) { // step code obyte &= 0x55; // zero step bits if (c&XMASK) { obyte|= 0x02; } if (c&YMASK) { obyte|= 0x08; } if (c&ZMASK) { obyte|= 0x20; } if (c&WMASK) { obyte|= 0x80; } feed(obyte); } else if ((c&0x80)==0) { // delay code d = ((unsigned int8)c)&0x7f; for (i=1; i<=d; i++) { feed(0x00); // stall } } else if ((c&0xf0) == 0x80) { // dir code obyte &= 0xaa; // zero dir bits if (c&XMASK) { obyte|= 0x01; } if (c&YMASK) { obyte|= 0x04; } if (c&ZMASK) { obyte|= 0x10; } if (c&WMASK) { obyte|= 0x40; } } else if ((c&0xf0) == 0xa0) { if ((c&0x08)) { d = ((unsigned int8)c)&0x07; enable(d); // state } else { d = ((unsigned int8)c)&0x07; set_step(d); // mode } } else if ((c&0xc0) == 0xc0) { // spindle PWM speed spin=c&0x3f; // get the bits spin = spin<<4; set_pwm1_duty(spin); // duty cycle is val/(4*(255+1)) } else { ; // unknown code, silently ignore } } } }
void feed(char *c) { while (nqueue(&txque0) >= NBUF-4) { output_bit(LED0,1); housekeep(); // was a spin lock } output_bit(LED0,0); enqueue(&txque0, c); }
void ThreadPool::addCapacity(int n) { FastMutex::ScopedLock lock(_mutex); poco_assert (_maxCapacity + n >= _minCapacity); _maxCapacity += n; housekeep(); }
void ThreadPool::joinAll() { FastMutex::ScopedLock lock(_mutex); for (ThreadVec::iterator it = _threads.begin(); it != _threads.end(); ++it) { (*it)->join(); } housekeep(); }
void ThreadPool::collect() { FastMutex::ScopedLock lock(_mutex); housekeep(); }