int FixedThreadPool::start() { bslmt::LockGuard<bslmt::Mutex> lock(&d_metaMutex); if (e_STOP != d_control.loadRelaxed()) { return 0; // RETURN } for (int i = d_threadGroup.numThreads(); i < d_numThreads; ++i) { if (0 != startNewThread()) { releaseWorkerThreads(); d_threadGroup.joinAll(); return -1; // RETURN } } waitWorkerThreads(); d_queue.enable(); d_control = e_RUN; // 'releaseWorkerThreads' emits a release barrier so that the worker // threads can't return from wait without observing the previous store. releaseWorkerThreads(); return 0; }
NPT_Result TaskGroup::startTask(Task *task) { WriteLocker locker(m_stateLock); if (m_state != State_Running) { delete task; return NPT_ERROR_INVALID_STATE; } joinThreads(); task->m_taskGroup = this; { TaskThread *thread = NULL; WriteLocker locker(m_freeThreadListLock); NPT_List<TaskThread*>::Iterator it = m_freeThreadList.GetFirstItem(); if (it) { thread = *it; m_freeThreadList.Erase(it); thread->setTask(task); return NPT_SUCCESS; } } return startNewThread(task); }
/* * Execute the specified native method. Return true if the method * is executed, false if it is rolled back for garbage collection. */ int executeNativeMethod(Ref target, Ref method, int offset) { int magic = getInt(method, METHOD_MAGIC); if (magic == 0) { printf("Native method not supported\n"); debugTrace(); exit(1); } switch (magic) { // print a debug message case MAGIC_RUNTIME_CORE_DEBUG: debugLine(popRef()); pc += offset; break; // execute the given static method case MAGIC_RUNTIME_CORE_EXECUTE_STATIC: { Ref staticMethod = popRef(); if (staticMethod == NULL) { throwException(CORE_THROW_NULL_POINTER); return; } Ref owner = getRef(staticMethod, ENTRY_OWNER); if (!executeMethod(owner, staticMethod, offset)) { // restore stack if rolled back for gc pushRef(staticMethod); } } break; // return the current frame case MAGIC_RUNTIME_FRAME_CURRENT: pushRef(frame); pc += offset; break; // return the core object case MAGIC_RUNTIME_CORE_GET: pushRef(core); pc += offset; break; // create a statics object case MAGIC_RUNTIME_STATICS_CREATE: { // allocate space for statics object Ref type = popRef(); Int numStaticFields = getInt(type, TYPE_STATIC_FIELD_COUNT); Int numWords = STATICS_FIELDS + numStaticFields; Ref statics = allocate(numWords, HEADER_STATIC_FIELDS); if (statics == NULL) { pushRef(type); // restore stack return; // rollback, gc done } // initialise statics object and set in type object int hash = (char*) statics - (char*) core; setInt(statics, OBJECT_HASHCODE, hash); Ref staticsType = getRef(core, CORE_STATICS_TYPE); setRef(statics, OBJECT_TYPE, staticsType); Ref map = getRef(type, TYPE_STATIC_MAP); setRef(statics, STATICS_MAP, map); setRef(type, TYPE_STATICS, statics); } pc += offset; break; // read the type from a class case MAGIC_RUNTIME_CORE_GET_TYPE: { Ref class = popRef(); // type is first field in class... Ref type = getRef(class, OBJECT_FIELDS); pushRef(type); } pc += offset; break; // put system to sleep to avoid wasting processor time while idle case MAGIC_RUNTIME_IDLE_SLEEP: idleSleep(); pc += offset; break; // return the currently executing thread case MAGIC_RUNTIME_THREAD_CURRENT: pushRef(thread); pc += offset; break; // start a new thread case MAGIC_RUNTIME_THREAD_START_HOOK: startNewThread(offset); break; // return an object's class case MAGIC_JAVA_OBJECT_GET_CLASS: { Ref obj = popRef(); Ref type = getRef(obj, OBJECT_TYPE); Ref peer = getRef(type, TYPE_PEER); pushRef(peer); } pc += offset; break; // set system output stream case MAGIC_JAVA_SYSTEM_SET_OUT: { Ref arg = popRef(); Ref type = getRef(method, ENTRY_OWNER); Ref statics = getRef(type, TYPE_STATICS); setRef(statics, STATICS_FIELDS + SYSTEM_OUT, arg); } pc += offset; break; // set system error stream case MAGIC_JAVA_SYSTEM_SET_ERR: { Ref arg = popRef(); Ref type = getRef(method, ENTRY_OWNER); Ref statics = getRef(type, TYPE_STATICS); setRef(statics, STATICS_FIELDS + SYSTEM_ERR, arg); } pc += offset; break; // set system input stream case MAGIC_JAVA_SYSTEM_SET_IN: { Ref arg = popRef(); Ref type = getRef(method, ENTRY_OWNER); Ref statics = getRef(type, TYPE_STATICS); setRef(statics, STATICS_FIELDS + SYSTEM_IN, arg); } pc += offset; break; // read an ascii character from the console case MAGIC_TEST_READ_FROM_CONSOLE: { char c = readFromConsole(); pushInt(c); } pc += offset; break; // write an ascii character to the console case MAGIC_TEST_WRITE_TO_CONSOLE: { char c = popInt() & 0xFF; printf("%c", c); } pc += offset; break; // read a byte from the floppy drive case MAGIC_TEST_READ_FROM_FLOPPY: { Int pos = popInt(); pushInt(readFromFloppy(pos)); } pc += offset; break; // magic id not recognised default: printf("Invalid magic method id: %d\n", magic); exit(1); break; } }