void ValueArray::pushBack(CountPtr<Value>& val) { if(val->isLValue()) m_val.push_back(CountPtr<Value>(new ValueReference(val->getReferredValue()))); else m_val.push_back(CountPtr<Value>(new ValueReference(val))); }
virtual size_t run( void ) { for(;;) { CountPtr< JobQueue::Job > job = q->getJob(); if( size_t(-1) == job->work( q ) ) return 0; Thread::yield(); } }
CountPtr<Value> ValueReference::assign(CountPtr<Value> val) { ACCESS_MUTEX_LOCKER; // Pointers support; array and struct items can have two references inside ValueReference* assign_to = m_val->toValueReference(); if(assign_to == NULL) assign_to = this; if(val->isLValue()) return assign_to->m_val = val->getReferredValue(); else return assign_to->m_val = val; }
void test_multiple_workers( void ) { CountPtr< JobQueue > q = new JobQueue( 0 ); const size_t num_workers = 32; const size_t num_jobs = 64; const size_t times = 512; std::vector< int > a( num_jobs ); std::vector< CountPtr< IncJob > > j( num_jobs ); for( size_t i=0; i<num_jobs; ++i ) { j[ i ] = new IncJob; j[ i ]->p = &a[ i ]; j[ i ]->setPriority( times ); q->addJob( j[ i ].get() ); } std::vector< CountPtr< Worker > > w( num_workers ); for( size_t i=0; i<num_workers; ++i ) { w[ i ] = new Worker; w[ i ]->q = q; CPPUNIT_ASSERT_NO_THROW( w[ i ]->start() ); } // Has lower priority than the IncJobs CountPtr< EndWorker > e( new EndWorker ); for( size_t i=0; i<num_workers; ++i ) { CPPUNIT_ASSERT_NO_THROW( q->addJob( e.get() ) ); } for( size_t i=0; i<num_workers; ++i ) { CPPUNIT_ASSERT_NO_THROW( w[ i ]->join() ); w[ i ]->unref(); // TODO: Fix Refcounted for Collectable with NULL GarbageCollector } for( size_t i=0; i<num_jobs; ++i ) { CPPUNIT_ASSERT_EQUAL( int( times ), a[ i ] ); } }
virtual size_t doit( CountPtr< JobQueue > queue ) { ++(*p); if( getPriority() > 1 ) { setPriority( getPriority()-1 ); queue->addJob( this ); } return 0; }
CountPtr<Value> ValueArray::setItem(uint pos, CountPtr<Value> val) { if(pos >= m_val.size()) { stringstream ss; //ss << _("Index out of bounds (size: ") << m_val.size() // << _(", index: ") << pos << _("), resizing array"); //WARN_P(ss.str()); resize(pos+1); } if(val->isLValue()) m_val[pos]->toValueReference()->assign(val->getReferredValue()); else m_val[pos]->toValueReference()->assign(val); return val; }
void AST_Node_Item::ExecReplaceCurrent(void) { if (this->down.isValid() == false) { if (rule4Replace!="") value = Replace(value, rule4Replace); return; } CountPtr<AST_Node_Item> currentNode = this->down; std::map<std::string, std::string> mapFounded; std::ostringstream os; while (currentNode.isValid()) { if (mtk::s_trim(rule4Replace, ' ') !="") { int contador = 0; std::string symbol = currentNode->name; while (mapFounded.find(symbol) != mapFounded.end()) { ++contador; symbol = MTK_SS(currentNode->name << "#" << contador); } mapFounded[symbol] = currentNode->value; } else os << currentNode->value; currentNode = currentNode->next; } if (rule4Replace !="") this->value = Replace(mapFounded, rule4Replace); else this->value = os.str(); }
void test_push_pop_priority( void ) { CountPtr< JobQueue > q = new JobQueue( 0 ); const size_t num_jobs = 16; std::vector< CountPtr< NrJob > > jobs( num_jobs ); for( size_t i=0; i<num_jobs; ++i ) { jobs[ i ] = new NrJob; jobs[ i ]->nr = i; jobs[ i ]->setPriority( i ); CPPUNIT_ASSERT_EQUAL( i, jobs[ i ]->getPriority() ); q->addJob( jobs[ i ].get() ); // std::cerr << "q.addJob( " << (void*)&jobs[ i ] << " ), nr = " << i << std::endl; } for( size_t i=0; i<num_jobs; ++i ) { CountPtr< JobQueue::Job > job = q->getJob(); // std::cerr << "utest_JobQueue: job = " << (void*)job.get() << ", nr = " << job->doit() << std::endl; CPPUNIT_ASSERT_EQUAL( num_jobs-i-1, job->work( q ) ); CPPUNIT_ASSERT_EQUAL( num_jobs-i-1, job->getPriority() ); } }
ValueReference::ValueReference(CountPtr<Value> val) : Value(), m_val(val) { assert(!val->isLValue()); }