/* setup all application wide items */ int xsldbgThreadInit(void) { int result = 0; fprintf(stderr, "mainInit()\n"); xsltSetGenericErrorFunc(0, xsldbgGenericErrorFunc); setThreadStatus(XSLDBG_MSG_THREAD_INIT); xsldbgSetAppFunc(qtNotifyXsldbgApp); xsldbgSetAppStateFunc(qtNotifyStateXsldbgApp); xsldbgSetTextFunc(qtNotifyTextXsldbgApp); xsldbgSetReadlineFunc(qtXslDbgShellReadline); /* create the thread */ if (pthread_create(&mythread, NULL, xsldbgThreadMain, NULL) != EAGAIN) { int counter; for (counter = 0; counter < 11; counter++){ if (getThreadStatus() != XSLDBG_MSG_THREAD_INIT) break; usleep(250000); /*guess that it will take at most 2.5 seconds to startup */ } /* xsldbg should have started by now if it can */ if (getThreadStatus() == XSLDBG_MSG_THREAD_RUN){ fprintf(stderr, "Created thread\n"); result++; }else fprintf(stderr, "Thread did not start\n"); } else { fprintf(stderr, "Failed to create thread\n"); } return result; }
/* this is where the thread get to do all its work */ void * xsldbgThreadMain(void *) { // int defaultArgc = 2; // char *defaultArgv[2]; // int i; if (getThreadStatus() != XSLDBG_MSG_THREAD_INIT){ fprintf(stderr, "xsldbg thread is not ready to be started. Or one is already running.\n"); return NULL; /* we can't start more than one thread of xsldbg */ } // defaultArgv[0] = xmlMemStrdup("xsldbg"); // defaultArgv[1] = xmlMemStrdup("--shell"); /* defaultArgv[2] = xmlMemStrdup("xsldoc.xsl"); defaultArgv[3] = xmlMemStrdup("xsldoc.xml"); */ /* for (i = 0; i < defaultArgc; i++){ if (defaultArgv[i] == NULL){ fprintf(stderr, "Start thread failed. Unable to create xsldbg arguments\n"); return NULL; } } */ xsldbgSetThreadCleanupFunc(xsldbgThreadCleanupQt); setThreadStatus(XSLDBG_MSG_THREAD_RUN); setInputStatus(XSLDBG_MSG_AWAITING_INPUT); fprintf(stderr, "Starting thread\n"); /* call the "main of xsldbg" found in debugXSL.c */ // xsldbgMain(defaultArgc, defaultArgv); xsldbgMain(0,0); fprintf(stderr, "Stopping thread\n"); /* for (i = 0; i < defaultArgc; i++){ xmlFree(defaultArgv[i]); } */ setThreadStatus(XSLDBG_MSG_THREAD_DEAD); setInputStatus(XSLDBG_MSG_PROCESSING_INPUT); notifyXsldbgApp(XSLDBG_MSG_THREAD_DEAD, NULL); return NULL; }
/* thread has died so cleanup after it not called directly but via notifyXsldbgApp*/ void xsldbgThreadCleanupQt(void) { fprintf(stderr, "Thread has finished\n"); if (getThreadStatus() == XSLDBG_MSG_THREAD_RUN) { xsldbgThreadFree(); } /* its safe to modify threadStatus as the thread is now dead */ setThreadStatus(XSLDBG_MSG_THREAD_DEAD); }
/* tell the thread to stop and free that memory !*/ void xsldbgThreadFree(void) { fprintf(stderr, "xsldbgThreadFree()\n"); if (getThreadStatus() != XSLDBG_MSG_THREAD_DEAD) { int counter; fprintf(stderr, "Killing xsldbg thread\n"); setThreadStatus(XSLDBG_MSG_THREAD_STOP); for (counter = 0; counter < 11; counter++){ if (getThreadStatus() == XSLDBG_MSG_THREAD_DEAD) break; usleep(250000); /*guess that it will take at most 2.5 seconds to stop */ } } }
/*virtual*/ Thread::~Thread() { /* In the off-chance memory is accessed after we are deleted... */ setThreadStatus ( Thread::DELETED ); }
void LightField::generate(VRMap *vrmap_p, Color ambient_color, unsigned int sample_treshold_p, float sample_affect_distance_p, float min_world_cube_size_p, unsigned int threads) { sampling_points.clear(); world_aabb = vrmap_p->getAABB(); total_area = world_aabb.size(); LightFieldCube *first_cube=new LightFieldCube(); AABB aabb; while(!cube_stack.empty()) { cube_stack.pop(); } CubeParameter *first_object=new CubeParameter; first_object->aabb = world_aabb; first_object->cube = first_cube; first_object->lightfield = this; vrmap = vrmap_p; sample_treshold = sample_treshold_p; sample_affect_distance = sample_affect_distance_p; min_world_cube_size = min_world_cube_size_p; // Setup a lookup grid for sampling point creation grid_size = vrmap->getGridSize(); w=(int)(world_aabb.sizeX()/grid_size)+1; h=(int)(world_aabb.sizeY()/grid_size)+1; d=(int)(world_aabb.sizeZ()/grid_size)+1; grid.resize(w); for (int x=0; x<w; x++) { grid[x].resize(h); for (int y=0; y<h; y++) { grid[x][y].resize(d); } } // Create the first cube in the thread bool *first=new bool[threads]; cube_stack.push(first_object); pthread_t *generation_threads = new pthread_t[threads]; thread_status = new bool[threads]; for (size_t i=0; i<threads; i++) { first[i] = true; thread_status[i] = false; } thread_stacks.resize(threads); // Setup scheduler to generate octree while(true) { bool done=true; for (size_t i=0; i<threads; i++) { if (!thread_status[i]) { if (!first[i]) { pthread_join(generation_threads[i],NULL); } while (!thread_stacks[i].empty()) { CubeParameter *object = thread_stacks[i].top(); thread_stacks[i].pop(); cube_stack.push(object); } if (!cube_stack.empty()) { CubeParameter *object = cube_stack.top(); cube_stack.pop(); setThreadStatus(i, true); object->thread = i; first[i] = false; pthread_create(&generation_threads[i], NULL, generateCube, object); done = false; } } else done = false; } if (done) break; } cube=first_cube; paintSamplingPoints(vrmap, ambient_color, sample_affect_distance); }
void HcaThreadPool::threadInitialized(HcaThread *t) { qWarning() << "Thread " << t->id() << " is ready"; m_threads.insert(t->id(), t); setThreadStatus(t->id(), true); }