!!! THIS function should be always preferred, since it determines single/multi thread from config !!! multi threaded run: create threads for multiple tick loops and control/synchronize them...
| 2094 | // |
| 2095 | // multi threaded run: create threads for multiple tick loops and control/synchronize them... |
| 2096 | long long cComponentManager::runMultiThreaded(long long maxtick) |
| 2097 | { |
| 2098 | if (!ready) return 0; |
| 2099 | int i; |
| 2100 | |
| 2101 | // |
| 2102 | if (nThreads == 1) { |
| 2103 | return runSingleThreaded(maxtick); |
| 2104 | } else { |
| 2105 | if (nThreads == 0) { |
| 2106 | int j=0; |
| 2107 | // int * threadPrios = (int *) calloc(1,sizeof(int)*lastComponent); |
| 2108 | for (i=0; i<lastComponent; i++) { |
| 2109 | if (componentThreadId[i] != -2) { |
| 2110 | /* TODO : store threadPrio in an array indexed by 1..j |
| 2111 | threadPrios[j] = componentThreadPrio[i]; |
| 2112 | */ |
| 2113 | componentThreadId[i] = j++; |
| 2114 | } |
| 2115 | } |
| 2116 | nThreads = j; |
| 2117 | } else { |
| 2118 | for (i=0; i<lastComponent; i++) { |
| 2119 | if (componentThreadId[i] == -1) componentThreadId[i] = 0; // run in first thread |
| 2120 | } |
| 2121 | } |
| 2122 | SMILE_MSG(2,"starting mutli-thread processing with %i threads",nThreads); |
| 2123 | // create thread handles and data structures |
| 2124 | runFlag = (int *)malloc(sizeof(int) * nThreads); |
| 2125 | sThreadData *threadData = (sThreadData*)malloc(sizeof(sThreadData) * nThreads); |
| 2126 | smileThread *threadHandles = (smileThread *)malloc(sizeof(smileThread) * nThreads); |
| 2127 | smileMutexCreate(syncCondMtx); |
| 2128 | smileCondCreate(syncCond); |
| 2129 | smileCondCreate(waitEndCond); |
| 2130 | smileCondCreate(waitControllerCond); |
| 2131 | |
| 2132 | nActive = nThreads; |
| 2133 | waitEndCnt = 0; |
| 2134 | smileMutexCreate(waitEndMtx); |
| 2135 | |
| 2136 | // create the controller thread... |
| 2137 | smileCondCreate(controlCond); |
| 2138 | sThreadData control; |
| 2139 | control.obj = this; |
| 2140 | control.threadId = -1; |
| 2141 | smileThread controlThr; |
| 2142 | runStatus = 1; compRunFlag = 0; probeFlag = 0; nProbe = 0; endOfLoop=0; controllerPresent=0; |
| 2143 | if (!(int)smileThreadCreate(controlThr, threadRunnerControl, &control)) { |
| 2144 | SMILE_ERR(1,"error creating controller thread!!",i); |
| 2145 | } |
| 2146 | // create all the threads.... |
| 2147 | for (i=0; i<nThreads; i++) { |
| 2148 | threadData[i].obj = this; |
| 2149 | threadData[i].maxtick = maxtick; |
| 2150 | threadData[i].threadId = i; |
| 2151 | threadData[i].status = THREAD_ACTIVE; |
| 2152 | if (!(int)smileThreadCreate(threadHandles[i], threadRunner, &(threadData[i]))) { |
| 2153 | // if (!((long)(threadHandles[i] = (HANDLE)_beginthread(threadRunner,0,&(threadData[i]))) != -1)) { |
no test coverage detected