jthread.cpp 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jthread.hpp"
  14. #include "jlib.hpp"
  15. #include "jfile.hpp"
  16. #include "jmutex.hpp"
  17. #include "jexcept.hpp"
  18. #include "jmisc.hpp"
  19. #include "jqueue.tpp"
  20. #include "jregexp.hpp"
  21. #include <assert.h>
  22. #ifdef _WIN32
  23. #include <process.h>
  24. #else
  25. #include <unistd.h>
  26. #include <sys/wait.h>
  27. #include <sys/syscall.h>
  28. #include <sys/types.h>
  29. #include <sys/resource.h>
  30. #ifndef __APPLE__
  31. #include <numa.h>
  32. #endif
  33. #endif
  34. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  35. #undef new
  36. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  37. #endif
  38. #define LINUX_STACKSIZE_CAP (0x200000)
  39. //#define NO_CATCHALL
  40. static __thread ThreadTermFunc threadTerminationHook;
  41. ThreadTermFunc addThreadTermFunc(ThreadTermFunc onTerm)
  42. {
  43. ThreadTermFunc old = threadTerminationHook;
  44. threadTerminationHook = onTerm;
  45. return old;
  46. }
  47. PointerArray *exceptionHandlers = NULL;
  48. MODULE_INIT(INIT_PRIORITY_JTHREAD)
  49. {
  50. if (threadTerminationHook)
  51. (*threadTerminationHook)(); // May be too late :(
  52. exceptionHandlers = new PointerArray();
  53. return true;
  54. }
  55. MODULE_EXIT()
  56. {
  57. delete exceptionHandlers;
  58. }
  59. void addThreadExceptionHandler(IExceptionHandler *handler)
  60. {
  61. assertex(exceptionHandlers); // have to ensure MODULE_INIT has appropriate priority.
  62. exceptionHandlers->append(handler);
  63. }
  64. void removeThreadExceptionHandler(IExceptionHandler *handler)
  65. {
  66. exceptionHandlers->zap(handler);
  67. }
  68. static bool SEHHandling = false;
  69. void enableThreadSEH() { SEHHandling=true; }
  70. void disableThreadSEH() { SEHHandling=false; } // only prevents new threads from having SEH handler, no mech. for turning off existing threads SEH handling.
  71. static ICopyArrayOf<Thread> ThreadList;
  72. static CriticalSection ThreadListSem;
  73. static size32_t defaultThreadStackSize=0;
  74. static ICopyArrayOf<Thread> ThreadDestroyList;
  75. static SpinLock ThreadDestroyListLock;
  76. #ifdef _WIN32
  77. extern void *EnableSEHtranslation();
  78. unsigned WINAPI Thread::_threadmain(LPVOID v)
  79. #else
  80. void *Thread::_threadmain(void *v)
  81. #endif
  82. {
  83. Thread * t = (Thread *)v;
  84. #ifdef _WIN32
  85. if (SEHHandling)
  86. EnableSEHtranslation();
  87. #else
  88. t->tidlog = threadLogID();
  89. #endif
  90. int ret = t->begin();
  91. char *&threadname = t->cthreadname.threadname;
  92. if (threadname) {
  93. memsize_t l=strlen(threadname);
  94. char *newname = (char *)malloc(l+8+1);
  95. memcpy(newname,"Stopped ",8);
  96. memcpy(newname+8,threadname,l+1);
  97. char *oldname = threadname;
  98. threadname = newname;
  99. free(oldname);
  100. }
  101. {
  102. // need to ensure joining thread does not race with us to release
  103. t->Link(); // extra safety link
  104. {
  105. SpinBlock block(ThreadDestroyListLock);
  106. ThreadDestroyList.append(*t);
  107. }
  108. try {
  109. t->stopped.signal();
  110. if (t->Release()) {
  111. PROGLOG("extra unlinked thread");
  112. PrintStackReport();
  113. }
  114. else
  115. t->Release();
  116. }
  117. catch (...) {
  118. PROGLOG("thread release exception");
  119. throw;
  120. }
  121. {
  122. SpinBlock block(ThreadDestroyListLock);
  123. ThreadDestroyList.zap(*t); // hopefully won't get too big (i.e. one entry!)
  124. }
  125. }
  126. #if defined(_WIN32)
  127. return ret;
  128. #else
  129. return (void *) (memsize_t)ret;
  130. #endif
  131. }
  132. // JCSMORE - should have a setPriority(), unsupported under _WIN32
  133. void Thread::adjustPriority(int delta)
  134. {
  135. if (delta < -2)
  136. prioritydelta = -2;
  137. else if (delta > 2)
  138. prioritydelta = 2;
  139. else
  140. prioritydelta = delta;
  141. if (alive)
  142. {
  143. #if defined(_WIN32)
  144. int priority;
  145. switch (delta)
  146. {
  147. case -2: priority = THREAD_PRIORITY_LOWEST; break;
  148. case -1: priority = THREAD_PRIORITY_BELOW_NORMAL; break;
  149. case 0: priority = THREAD_PRIORITY_NORMAL; break;
  150. case +1: priority = THREAD_PRIORITY_ABOVE_NORMAL; break;
  151. case +2: priority = THREAD_PRIORITY_HIGHEST; break;
  152. }
  153. SetThreadPriority(hThread, priority);
  154. #else
  155. //MORE - What control is there?
  156. int policy;
  157. sched_param param;
  158. int rc;
  159. if (( rc = pthread_getschedparam(threadid, &policy, &param)) != 0)
  160. DBGLOG("pthread_getschedparam error: %d", rc);
  161. switch (delta)
  162. {
  163. // JCS - doubtful whether these good values...
  164. case -2: param.sched_priority = 0; policy =SCHED_OTHER; break;
  165. case -1: param.sched_priority = 0; policy =SCHED_OTHER; break;
  166. case 0: param.sched_priority = 0; policy =SCHED_OTHER; break;
  167. case +1: param.sched_priority = (sched_get_priority_max(SCHED_RR)-sched_get_priority_min(SCHED_RR))/2; policy =SCHED_RR; break;
  168. case +2: param.sched_priority = sched_get_priority_max(SCHED_RR); policy =SCHED_RR; break;
  169. }
  170. if(( rc = pthread_setschedparam(threadid, policy, &param)) != 0)
  171. DBGLOG("pthread_setschedparam error: %d policy=%i pr=%i id=%" I64F "u PID=%i", rc,policy,param.sched_priority,(unsigned __int64) threadid,getpid());
  172. else
  173. DBGLOG("priority set id=%" I64F "u policy=%i pri=%i PID=%i",(unsigned __int64) threadid,policy,param.sched_priority,getpid());
  174. #endif
  175. }
  176. }
  177. void Thread::adjustNiceLevel()
  178. {
  179. #if defined(_WIN32)
  180. int priority;
  181. if(nicelevel < -15)
  182. priority = THREAD_PRIORITY_TIME_CRITICAL;
  183. else if(nicelevel >= -15 && nicelevel < -10)
  184. priority = THREAD_PRIORITY_HIGHEST;
  185. else if(nicelevel >= -10 && nicelevel < 0)
  186. priority = THREAD_PRIORITY_ABOVE_NORMAL;
  187. else if(nicelevel == 0)
  188. priority = THREAD_PRIORITY_NORMAL;
  189. else if(nicelevel > 0 && nicelevel <= 10)
  190. priority = THREAD_PRIORITY_BELOW_NORMAL;
  191. else if(nicelevel > 10 && nicelevel <= 15)
  192. priority = THREAD_PRIORITY_LOWEST;
  193. else if(nicelevel >15)
  194. priority = THREAD_PRIORITY_IDLE;
  195. SetThreadPriority(hThread, priority);
  196. #elif defined(__linux__)
  197. setpriority(PRIO_PROCESS, 0, nicelevel);
  198. #else
  199. UNIMPLEMENTED;
  200. #endif
  201. }
  202. bool Thread::isCurrentThread() const
  203. {
  204. return GetCurrentThreadId() == threadid;
  205. }
  206. // _nicelevel ranges from -20 to 19, the higher the nice level, the less cpu time the thread will get.
  207. void Thread::setNice(int _nicelevel)
  208. {
  209. if (_nicelevel < -20 || _nicelevel > 19)
  210. throw MakeStringException(0, "nice level should be between -20 and 19");
  211. if(alive)
  212. throw MakeStringException(0, "nice can only be set before the thread is started.");
  213. nicelevel = _nicelevel;
  214. }
  215. void Thread::setStackSize(size32_t size)
  216. {
  217. stacksize = (unsigned short)(size/0x1000);
  218. }
  219. void Thread::setDefaultStackSize(size32_t size)
  220. {
  221. defaultThreadStackSize = size; // has no effect under windows (though may be used for calculations later)
  222. }
  223. int Thread::begin()
  224. {
  225. if(nicelevel)
  226. adjustNiceLevel();
  227. #ifndef _WIN32
  228. starting.signal();
  229. suspend.wait();
  230. #endif
  231. int ret=-1;
  232. try {
  233. ret = run();
  234. }
  235. catch (IException *e)
  236. {
  237. handleException(e);
  238. }
  239. #ifndef NO_CATCHALL
  240. catch (...)
  241. {
  242. handleException(MakeStringException(0, "Unknown exception in Thread %s", getName()));
  243. }
  244. #endif
  245. if (threadTerminationHook)
  246. {
  247. (*threadTerminationHook)();
  248. threadTerminationHook = NULL;
  249. }
  250. #ifdef _WIN32
  251. #ifndef _DEBUG
  252. CloseHandle(hThread); // leak handle when debugging,
  253. // fixes some lockups/crashes in the debugger when lots of threads being created
  254. #endif
  255. hThread = NULL;
  256. #endif
  257. //alive = false; // not safe here
  258. return ret;
  259. }
  260. void Thread::handleException(IException *e)
  261. {
  262. assertex(exceptionHandlers);
  263. if (exceptionHandlers->ordinality() == 0)
  264. {
  265. PrintExceptionLog(e,getName());
  266. //throw; // don't rethrow unhandled, preferable over alternative of causing process death
  267. e->Release();
  268. }
  269. else
  270. {
  271. PrintExceptionLog(e,getName());
  272. bool handled = false;
  273. ForEachItemIn(ie, *exceptionHandlers)
  274. {
  275. IExceptionHandler *handler = (IExceptionHandler *) exceptionHandlers->item(ie);
  276. handled = handler->fireException(e) || handled;
  277. }
  278. if (!handled)
  279. {
  280. // if nothing choose to handle it.
  281. EXCLOG(e, NULL);
  282. //throw e; // don't rethrow unhandled, preferable over alternative of causing process death
  283. }
  284. e->Release();
  285. }
  286. }
  287. void Thread::init(const char *_name)
  288. {
  289. #ifdef _WIN32
  290. hThread = NULL;
  291. #endif
  292. threadid = 0;
  293. tidlog = 0;
  294. alive = false;
  295. cthreadname.threadname = (NULL == _name) ? NULL : strdup(_name);
  296. ithreadname = &cthreadname;
  297. prioritydelta = 0;
  298. nicelevel = 0;
  299. stacksize = 0; // default is EXE default stack size (set by /STACK)
  300. }
  301. void Thread::start()
  302. {
  303. if (alive) {
  304. WARNLOG("Thread::start(%s) - Thread already started!",getName());
  305. PrintStackReport();
  306. #ifdef _DEBUG
  307. throw MakeStringException(-1,"Thread::start(%s) - Thread already started!",getName());
  308. #endif
  309. return;
  310. }
  311. Link();
  312. startRelease();
  313. }
  314. void Thread::startRelease()
  315. {
  316. assertex(!alive);
  317. stopped.reinit(0); // just in case restarting
  318. #ifdef _WIN32
  319. hThread = (HANDLE)_beginthreadex(NULL, 0x1000*(unsigned)stacksize, Thread::_threadmain, this, CREATE_SUSPENDED, (unsigned *)&threadid);
  320. if (!hThread || !threadid)
  321. {
  322. Release();
  323. throw makeOsException(GetLastError());
  324. }
  325. #else
  326. int status;
  327. unsigned numretrys = 8;
  328. unsigned delay = 1000;
  329. loop {
  330. pthread_attr_t attr;
  331. pthread_attr_init(&attr);
  332. pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  333. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  334. if (stacksize)
  335. pthread_attr_setstacksize(&attr, (unsigned)stacksize*0x1000);
  336. else if (defaultThreadStackSize)
  337. pthread_attr_setstacksize(&attr, defaultThreadStackSize);
  338. else {
  339. #ifndef __64BIT__ // no need to cap 64bit
  340. size_t defss=0;
  341. pthread_attr_getstacksize(&attr, &defss);
  342. if (defss>LINUX_STACKSIZE_CAP)
  343. pthread_attr_setstacksize(&attr, LINUX_STACKSIZE_CAP);
  344. #endif
  345. }
  346. status = pthread_create(&threadid, &attr, Thread::_threadmain, this);
  347. if ((status==EAGAIN)||(status==EINTR)) {
  348. if (numretrys--==0)
  349. break;
  350. WARNLOG("pthread_create(%d): Out of threads, retrying...",status);
  351. Sleep(delay);
  352. delay *= 2;
  353. }
  354. else
  355. break;
  356. }
  357. if (status) {
  358. threadid = 0;
  359. Release();
  360. ERRLOG("pthread_create returns %d",status);
  361. PrintStackReport();
  362. PrintMemoryReport();
  363. StringBuffer s;
  364. getThreadList(s);
  365. ERRLOG("Running threads:\n %s",s.str());
  366. throw makeOsException(status);
  367. }
  368. unsigned retryCount = 10;
  369. loop
  370. {
  371. if (starting.wait(1000*10))
  372. break;
  373. else if (0 == --retryCount)
  374. throw MakeStringException(-1, "Thread::start(%s) failed", getName());
  375. WARNLOG("Thread::start(%s) stalled, waiting to start, retrying", getName());
  376. }
  377. #endif
  378. alive = true;
  379. if (prioritydelta)
  380. adjustPriority(prioritydelta);
  381. {
  382. CriticalBlock block(ThreadListSem);
  383. ThreadList.zap(*this); // just in case restarting
  384. ThreadList.append(*this);
  385. }
  386. #ifdef _WIN32
  387. DWORD count = ResumeThread(hThread);
  388. assertex(count == 1);
  389. #else
  390. suspend.signal();
  391. #endif
  392. }
  393. bool Thread::join(unsigned timeout)
  394. {
  395. if (!alive&&!threadid) {
  396. #ifdef _DEBUG
  397. PROGLOG("join on unstarted thread!");
  398. PrintStackReport();
  399. #endif
  400. return true;
  401. }
  402. if (!stopped.wait(timeout))
  403. return false;
  404. if (!alive) // already joined
  405. {
  406. stopped.signal();
  407. return true;
  408. }
  409. unsigned st = 0;
  410. loop { // this is to prevent race with destroy
  411. // (because Thread objects are not always link counted!)
  412. {
  413. SpinBlock block(ThreadDestroyListLock);
  414. if (ThreadDestroyList.find(*this)==NotFound)
  415. break;
  416. }
  417. #ifdef _DEBUG
  418. if (st==10)
  419. PROGLOG("Thread::join race");
  420. #endif
  421. Sleep(st); // switch back to exiting thread (not very elegant!)
  422. st++;
  423. if (st>10)
  424. st = 10; // note must be non-zero for high priority threads
  425. }
  426. #ifdef _DEBUG
  427. int c = getLinkCount();
  428. if (c>=DEAD_PSEUDO_COUNT) {
  429. PROGLOG("Dead/Dying thread joined! %d",c);
  430. PrintStackReport();
  431. }
  432. #endif
  433. alive = false; // should be safe here
  434. stopped.signal(); // signal stopped again, to prevent any parallel call from blocking.
  435. return true;
  436. }
  437. Thread::~Thread()
  438. {
  439. ithreadname = &cthreadname; // safer (as derived classes destroyed)
  440. #ifdef _DEBUG
  441. if (alive) {
  442. if (!stopped.wait(0)) { // see if fell out of threadmain and signal stopped
  443. PROGLOG("Live thread killed! %s",getName());
  444. PrintStackReport();
  445. }
  446. // don't need to resignal as we are on way out
  447. }
  448. #endif
  449. Link();
  450. // DBGLOG("Thread %x (%s) destroyed\n", threadid, threadname);
  451. {
  452. CriticalBlock block(ThreadListSem);
  453. ThreadList.zap(*this);
  454. }
  455. free(cthreadname.threadname);
  456. cthreadname.threadname = NULL;
  457. }
  458. unsigned getThreadCount()
  459. {
  460. CriticalBlock block(ThreadListSem);
  461. return ThreadList.ordinality();
  462. }
  463. StringBuffer & getThreadList(StringBuffer &str)
  464. {
  465. CriticalBlock block(ThreadListSem);
  466. ForEachItemIn(i,ThreadList) {
  467. Thread &item=ThreadList.item(i);
  468. item.getInfo(str).append("\n");
  469. }
  470. return str;
  471. }
  472. StringBuffer &getThreadName(int thandle,unsigned tid,StringBuffer &name)
  473. {
  474. CriticalBlock block(ThreadListSem);
  475. bool found=false;
  476. ForEachItemIn(i,ThreadList) {
  477. Thread &item=ThreadList.item(i);
  478. int h;
  479. unsigned t;
  480. const char *s = item.getLogInfo(h,t);
  481. if (s&&*s&&((thandle==0)||(h==thandle))&&((tid==0)||(t==tid))) {
  482. if (found) {
  483. name.clear();
  484. break; // only return if unambiguous
  485. }
  486. name.append(s);
  487. found = true;
  488. }
  489. }
  490. return name;
  491. }
  492. // CThreadedPersistent
  493. CThreadedPersistent::CThreadedPersistent(const char *name, IThreaded *_owner) : athread(*this, name), owner(_owner)
  494. {
  495. halt = false;
  496. atomic_set(&state, s_ready);
  497. athread.start();
  498. }
  499. CThreadedPersistent::~CThreadedPersistent()
  500. {
  501. join(INFINITE);
  502. halt = true;
  503. sem.signal();
  504. athread.join();
  505. }
  506. void CThreadedPersistent::main()
  507. {
  508. loop
  509. {
  510. sem.wait();
  511. if (halt)
  512. break;
  513. try
  514. {
  515. owner->main();
  516. // Note we do NOT call the thread reset hook here - these threads are expected to be able to preserve state, I think
  517. }
  518. catch (IException *e)
  519. {
  520. VStringBuffer errMsg("CThreadedPersistent (%s)", athread.getName());
  521. EXCLOG(e, errMsg.str());
  522. exception.setown(e);
  523. joinSem.signal(); // leave in running state, signal to join to handle
  524. continue;
  525. }
  526. if (!atomic_cas(&state, s_ready, s_running))
  527. if (atomic_cas(&state, s_ready, s_joining))
  528. joinSem.signal();
  529. }
  530. }
  531. void CThreadedPersistent::start()
  532. {
  533. if (!atomic_cas(&state, s_running, s_ready))
  534. {
  535. VStringBuffer msg("CThreadedPersistent::start(%s) - not ready", athread.getName());
  536. WARNLOG("%s", msg.str());
  537. PrintStackReport();
  538. throw MakeStringExceptionDirect(-1, msg.str());
  539. }
  540. sem.signal();
  541. }
  542. bool CThreadedPersistent::join(unsigned timeout)
  543. {
  544. if (atomic_cas(&state, s_joining, s_running))
  545. {
  546. if (!joinSem.wait(timeout))
  547. {
  548. if (atomic_cas(&state, s_running, s_joining)) // if still joining, restore running state
  549. return false;
  550. // if here, main() set s_ready after timeout and has or will signal
  551. if (!joinSem.wait(60000)) // should be instant
  552. throwUnexpected();
  553. return true;
  554. }
  555. if (exception.get())
  556. {
  557. // switch back to ready state and throw
  558. Owned<IException> e = exception.getClear();
  559. if (!atomic_cas(&state, s_ready, s_joining))
  560. throwUnexpected();
  561. throw e.getClear();
  562. }
  563. }
  564. return true;
  565. }
  566. //class CAsyncFor
  567. void CAsyncFor::For(unsigned num,unsigned maxatonce,bool abortFollowingException, bool shuffled)
  568. {
  569. if (num <= 1)
  570. {
  571. if (num == 1)
  572. Do(0);
  573. return;
  574. }
  575. Mutex errmutex;
  576. Semaphore ready;
  577. Semaphore finished;
  578. IException *e=NULL;
  579. Owned<IShuffledIterator> shuffler;
  580. if (shuffled) {
  581. shuffler.setown(createShuffledIterator(num));
  582. shuffler->first(); // prime (needed to make thread safe)
  583. }
  584. unsigned i;
  585. if (maxatonce==1) { // no need for threads
  586. for (i=0;i<num;i++) {
  587. unsigned idx = shuffled?shuffler->lookup(i):i;
  588. try {
  589. Do(idx);
  590. }
  591. catch (IException * _e)
  592. {
  593. if (e)
  594. _e->Release(); // only return first
  595. else
  596. e = _e;
  597. if (abortFollowingException)
  598. break;
  599. }
  600. }
  601. }
  602. else {
  603. class cdothread: public Thread
  604. {
  605. public:
  606. Mutex *errmutex;
  607. Semaphore &ready;
  608. Semaphore &finished;
  609. int timeout;
  610. IException *&erre;
  611. unsigned idx;
  612. CAsyncFor *self;
  613. cdothread(CAsyncFor *_self,unsigned _idx,Semaphore &_ready,Semaphore &_finished,Mutex *_errmutex,IException *&_e)
  614. : Thread("CAsyncFor"),ready(_ready),finished(_finished),erre(_e)
  615. {
  616. errmutex =_errmutex;
  617. idx = _idx;
  618. self = _self;
  619. }
  620. int run()
  621. {
  622. try {
  623. self->Do(idx);
  624. }
  625. catch (IException * _e)
  626. {
  627. synchronized block(*errmutex);
  628. if (erre)
  629. _e->Release(); // only return first
  630. else
  631. erre = _e;
  632. }
  633. #ifndef NO_CATCHALL
  634. catch (...)
  635. {
  636. synchronized block(*errmutex);
  637. if (!erre)
  638. erre = MakeStringException(0, "Unknown exception in Thread %s", getName());
  639. }
  640. #endif
  641. ready.signal();
  642. finished.signal();
  643. return 0;
  644. }
  645. };
  646. if (maxatonce==0)
  647. maxatonce = num;
  648. for (i=0;(i<num)&&(i<maxatonce);i++)
  649. ready.signal();
  650. for (i=0;i<num;i++) {
  651. ready.wait();
  652. if (abortFollowingException && e) break;
  653. Thread *thread = new cdothread(this,shuffled?shuffler->lookup(i):i,ready,finished,&errmutex,e);
  654. thread->startRelease();
  655. }
  656. while (i--)
  657. finished.wait();
  658. }
  659. if (e)
  660. throw e;
  661. }
  662. // ---------------------------------------------------------------------------
  663. // Thread Pools
  664. // ---------------------------------------------------------------------------
  665. class CPooledThreadWrapper;
  666. class CThreadPoolBase
  667. {
  668. public:
  669. virtual ~CThreadPoolBase() {}
  670. protected: friend class CPooledThreadWrapper;
  671. IExceptionHandler *exceptionHandler;
  672. CriticalSection crit;
  673. StringAttr poolname;
  674. int donewaiting;
  675. Semaphore donesem;
  676. PointerArray waitingsems;
  677. UnsignedArray waitingids;
  678. bool stopall;
  679. unsigned defaultmax;
  680. unsigned targetpoolsize;
  681. unsigned delay;
  682. Semaphore availsem;
  683. atomic_t numrunning;
  684. virtual void notifyStarted(CPooledThreadWrapper *item)=0;
  685. virtual bool notifyStopped(CPooledThreadWrapper *item)=0;
  686. };
  687. class CPooledThreadWrapper: public Thread
  688. {
  689. PooledThreadHandle handle;
  690. IPooledThread *thread;
  691. Semaphore sem;
  692. CThreadPoolBase &parent;
  693. char *runningname;
  694. public:
  695. IMPLEMENT_IINTERFACE;
  696. CPooledThreadWrapper(CThreadPoolBase &_parent,
  697. PooledThreadHandle _handle,
  698. IPooledThread *_thread) // takes ownership of thread
  699. : Thread(StringBuffer("Member of thread pool: ").append(_parent.poolname).str()), parent(_parent)
  700. {
  701. thread = _thread;
  702. handle = _handle;
  703. runningname = strdup(_parent.poolname);
  704. }
  705. ~CPooledThreadWrapper()
  706. {
  707. thread->Release();
  708. free(runningname);
  709. }
  710. void setName(const char *name) { free(runningname); runningname=strdup(name); }
  711. void setHandle(PooledThreadHandle _handle) { handle = _handle; }
  712. PooledThreadHandle queryHandle() { return handle; }
  713. IPooledThread &queryThread() { return *thread; }
  714. void setThread(IPooledThread *_thread) { thread = _thread; } // takes ownership
  715. bool isStopped() { return (handle==0); }
  716. PooledThreadHandle markStopped()
  717. {
  718. PooledThreadHandle ret=handle;
  719. handle = 0;
  720. if (ret) // JCSMORE - I can't see how handle can not be set if here..
  721. atomic_dec(&parent.numrunning);
  722. return ret;
  723. }
  724. void markStarted()
  725. {
  726. atomic_inc(&parent.numrunning);
  727. }
  728. int run()
  729. {
  730. do
  731. {
  732. sem.wait();
  733. {
  734. CriticalBlock block(parent.crit); // to synchronize
  735. if (parent.stopall)
  736. break;
  737. }
  738. parent.notifyStarted(this);
  739. try
  740. {
  741. char *&threadname = cthreadname.threadname;
  742. char *temp = threadname; // swap running name and threadname
  743. threadname = runningname;
  744. runningname = temp;
  745. thread->main();
  746. temp = threadname; // and back
  747. threadname = runningname;
  748. runningname = temp;
  749. }
  750. catch (IException *e)
  751. {
  752. char *&threadname = cthreadname.threadname;
  753. char *temp = threadname; // swap back
  754. threadname = runningname;
  755. runningname = temp;
  756. handleException(e);
  757. }
  758. #ifndef NO_CATCHALL
  759. catch (...)
  760. {
  761. char *&threadname = cthreadname.threadname;
  762. char *temp = threadname; // swap back
  763. threadname = runningname;
  764. runningname = temp;
  765. handleException(MakeStringException(0, "Unknown exception in Thread from pool %s", parent.poolname.get()));
  766. }
  767. #endif
  768. if (threadTerminationHook)
  769. {
  770. (*threadTerminationHook)(); // Reset any pre-thread state.
  771. threadTerminationHook = NULL;
  772. }
  773. } while (parent.notifyStopped(this));
  774. return 0;
  775. }
  776. void cycle()
  777. {
  778. sem.signal();
  779. }
  780. void go(void *param)
  781. {
  782. thread->init(param);
  783. cycle();
  784. }
  785. bool stop()
  786. {
  787. if (handle)
  788. return thread->stop();
  789. return true;
  790. }
  791. void handleException(IException *e)
  792. {
  793. CriticalBlock block(parent.crit);
  794. PrintExceptionLog(e,parent.poolname.get());
  795. if (!parent.exceptionHandler||!parent.exceptionHandler->fireException(e)) {
  796. }
  797. e->Release();
  798. }
  799. };
  800. class CPooledThreadIterator: public CInterface , implements IPooledThreadIterator
  801. {
  802. unsigned current;
  803. public:
  804. IArrayOf<IPooledThread> threads;
  805. IMPLEMENT_IINTERFACE;
  806. CPooledThreadIterator()
  807. {
  808. current = 0;
  809. }
  810. bool first()
  811. {
  812. current = 0;
  813. return threads.isItem(current);
  814. }
  815. bool next()
  816. {
  817. current++;
  818. return threads.isItem(current);
  819. }
  820. bool isValid()
  821. {
  822. return threads.isItem(current);
  823. }
  824. IPooledThread & query()
  825. {
  826. return threads.item(current);
  827. }
  828. };
  829. class CThreadPool: public CThreadPoolBase, implements IThreadPool, public CInterface
  830. {
  831. CIArrayOf<CPooledThreadWrapper> threadwrappers;
  832. PooledThreadHandle nextid;
  833. IThreadFactory *factory;
  834. unsigned stacksize;
  835. unsigned timeoutOnRelease;
  836. unsigned traceStartDelayPeriod;
  837. unsigned startsInPeriod;
  838. cycle_t startDelayInPeriod;
  839. CCycleTimer overAllTimer;
  840. PooledThreadHandle _start(void *param,const char *name, bool noBlock, unsigned timeout=0)
  841. {
  842. CCycleTimer startTimer;
  843. bool timedout = defaultmax && !availsem.wait(noBlock ? 0 : (timeout>0?timeout:delay));
  844. PooledThreadHandle ret;
  845. {
  846. CriticalBlock block(crit);
  847. if (timedout)
  848. {
  849. if (!availsem.wait(0)) { // make sure take allocated sem if has become available
  850. if (noBlock || timeout > 0)
  851. throw MakeStringException(0, "No threads available in pool %s", poolname.get());
  852. WARNLOG("Pool limit exceeded for %s", poolname.get());
  853. }
  854. else
  855. timedout = false;
  856. }
  857. if (traceStartDelayPeriod)
  858. {
  859. ++startsInPeriod;
  860. if (timedout)
  861. {
  862. startDelayInPeriod += startTimer.elapsedCycles();
  863. if (overAllTimer.elapsedCycles() >= queryOneSecCycles()*traceStartDelayPeriod) // check avg. delay per minute
  864. {
  865. double totalDelayMs = (static_cast<double>(cycle_to_nanosec(startDelayInPeriod)))/1000000;
  866. double avgDelayMs = (static_cast<double>(cycle_to_nanosec(startDelayInPeriod/startsInPeriod)))/1000000;
  867. unsigned totalElapsedSecs = overAllTimer.elapsedMs()/1000;
  868. PROGLOG("%s: %u threads started in last %u seconds, total delay = %0.2f milliseconds, average delay = %0.2f milliseconds, currently running = %u", poolname.get(), startsInPeriod, totalElapsedSecs, totalDelayMs, avgDelayMs, runningCount());
  869. startsInPeriod = 0;
  870. startDelayInPeriod = 0;
  871. overAllTimer.reset();
  872. }
  873. }
  874. }
  875. CPooledThreadWrapper &t = allocThread();
  876. if (name)
  877. t.setName(name);
  878. t.go(param);
  879. ret = t.queryHandle();
  880. }
  881. Sleep(0);
  882. return ret;
  883. }
  884. public:
  885. IMPLEMENT_IINTERFACE;
  886. CThreadPool(IThreadFactory *_factory,IExceptionHandler *_exceptionHandler,const char *_poolname,unsigned _defaultmax, unsigned _delay, unsigned _stacksize, unsigned _timeoutOnRelease, unsigned _targetpoolsize)
  887. {
  888. poolname.set(_poolname);
  889. factory = LINK(_factory);
  890. exceptionHandler = _exceptionHandler;
  891. nextid = 1;
  892. stopall = false;
  893. defaultmax = _defaultmax;
  894. delay = _delay;
  895. if (defaultmax)
  896. availsem.signal(defaultmax);
  897. stacksize = _stacksize;
  898. timeoutOnRelease = _timeoutOnRelease;
  899. targetpoolsize = _targetpoolsize?_targetpoolsize:defaultmax;
  900. atomic_set(&numrunning,0);
  901. traceStartDelayPeriod = 0;
  902. startsInPeriod = 0;
  903. startDelayInPeriod = 0;
  904. }
  905. ~CThreadPool()
  906. {
  907. stopAll(true);
  908. if (!joinAll(true, timeoutOnRelease))
  909. WARNLOG("%s; timedout[%d] waiting for threads in pool", poolname.get(), timeoutOnRelease);
  910. CriticalBlock block(crit);
  911. bool first=true;
  912. ForEachItemIn(i,threadwrappers)
  913. {
  914. CPooledThreadWrapper &t = threadwrappers.item(i);
  915. if (!t.isStopped())
  916. {
  917. if (first)
  918. {
  919. WARNLOG("Threads still active: ");
  920. first = false;
  921. }
  922. StringBuffer threadInfo;
  923. PROGLOG("Active thread: %s, info: %s", t.getName(), t.getInfo(threadInfo).str());
  924. }
  925. }
  926. factory->Release();
  927. }
  928. CPooledThreadWrapper &allocThread()
  929. { // called in critical section
  930. PooledThreadHandle newid=nextid++;
  931. if (newid==0)
  932. newid=nextid++;
  933. ForEachItemIn(i,threadwrappers) {
  934. CPooledThreadWrapper &it = threadwrappers.item(i);
  935. if (it.isStopped()) {
  936. it.setHandle(newid);
  937. if (!it.queryThread().canReuse()) {
  938. it.queryThread().Release();
  939. it.setThread(factory->createNew());
  940. }
  941. return it;
  942. }
  943. }
  944. CPooledThreadWrapper &ret = *new CPooledThreadWrapper(*this,newid,factory->createNew());
  945. if (stacksize)
  946. ret.setStackSize(stacksize);
  947. ret.start();
  948. threadwrappers.append(ret);
  949. return ret;
  950. }
  951. CPooledThreadWrapper *findThread(PooledThreadHandle handle)
  952. { // called in critical section
  953. ForEachItemIn(i,threadwrappers) {
  954. CPooledThreadWrapper &it = threadwrappers.item(i);
  955. if (it.queryHandle()==handle)
  956. return &it;
  957. }
  958. return NULL;
  959. }
  960. PooledThreadHandle startNoBlock(void *param)
  961. {
  962. return _start(param, NULL, true);
  963. }
  964. PooledThreadHandle startNoBlock(void *param,const char *name)
  965. {
  966. return _start(param, name, true);
  967. }
  968. PooledThreadHandle start(void *param)
  969. {
  970. return _start(param, NULL, false);
  971. }
  972. PooledThreadHandle start(void *param,const char *name)
  973. {
  974. return _start(param, name, false);
  975. }
  976. PooledThreadHandle start(void *param,const char *name, unsigned timeout)
  977. {
  978. return _start(param, name, false, timeout);
  979. }
  980. bool stop(PooledThreadHandle handle)
  981. {
  982. CriticalBlock block(crit);
  983. CPooledThreadWrapper *t = findThread(handle);
  984. if (t)
  985. return t->stop();
  986. return true; // already stopped
  987. }
  988. bool stopAll(bool tryall=false)
  989. {
  990. availsem.signal(1000);
  991. availsem.wait();
  992. CriticalBlock block(crit);
  993. bool ret=true;
  994. ForEachItemIn(i,threadwrappers) {
  995. CPooledThreadWrapper &it = threadwrappers.item(i);
  996. if (!it.stop()) {
  997. ret = false;
  998. if (!tryall)
  999. break;
  1000. }
  1001. }
  1002. return ret;
  1003. }
  1004. bool joinWait(CPooledThreadWrapper &t,unsigned timeout)
  1005. {
  1006. // called in critical section
  1007. if (t.isStopped())
  1008. return true;
  1009. Semaphore sem;
  1010. waitingsems.append(&sem);
  1011. waitingids.append(t.queryHandle());
  1012. crit.leave();
  1013. bool ret = sem.wait(timeout);
  1014. crit.enter();
  1015. unsigned i = waitingsems.find(&sem);
  1016. if (i!=NotFound) {
  1017. waitingids.remove(i);
  1018. waitingsems.remove(i);
  1019. }
  1020. return ret;
  1021. }
  1022. bool join(PooledThreadHandle handle,unsigned timeout=INFINITE)
  1023. {
  1024. CriticalBlock block(crit);
  1025. CPooledThreadWrapper *t = findThread(handle);
  1026. if (!t)
  1027. return true; // already stopped
  1028. return joinWait(*t,timeout);
  1029. }
  1030. virtual bool joinAll(bool del,unsigned timeout=INFINITE)
  1031. { // note timeout is for each join
  1032. CriticalBlock block(crit);
  1033. CIArrayOf<CPooledThreadWrapper> tojoin;
  1034. ForEachItemIn(i1,threadwrappers) {
  1035. CPooledThreadWrapper &it = threadwrappers.item(i1);
  1036. it.Link();
  1037. tojoin.append(it);
  1038. }
  1039. ForEachItemIn(i2,tojoin)
  1040. if (!joinWait(tojoin.item(i2),timeout))
  1041. return false;
  1042. if (del) {
  1043. stopall = true;
  1044. ForEachItemIn(i3,tojoin)
  1045. tojoin.item(i3).cycle();
  1046. {
  1047. CriticalUnblock unblock(crit);
  1048. ForEachItemIn(i4,tojoin)
  1049. tojoin.item(i4).join();
  1050. }
  1051. threadwrappers.kill();
  1052. stopall = false;
  1053. }
  1054. return true;
  1055. }
  1056. IPooledThreadIterator *running()
  1057. {
  1058. CriticalBlock block(crit);
  1059. CPooledThreadIterator *ret = new CPooledThreadIterator;
  1060. ForEachItemIn(i,threadwrappers) {
  1061. CPooledThreadWrapper &it = threadwrappers.item(i);
  1062. if (!it.isStopped()) {
  1063. IPooledThread &t = it.queryThread();
  1064. t.Link();
  1065. ret->threads.append(t);
  1066. }
  1067. }
  1068. return ret;
  1069. }
  1070. unsigned runningCount()
  1071. {
  1072. return (unsigned)atomic_read(&numrunning);
  1073. }
  1074. void notifyStarted(CPooledThreadWrapper *item)
  1075. {
  1076. item->markStarted();
  1077. }
  1078. bool notifyStopped(CPooledThreadWrapper *item)
  1079. {
  1080. CriticalBlock block(crit);
  1081. PooledThreadHandle myid = item->markStopped();
  1082. ForEachItemIn(i1,waitingids) { // tell anyone waiting
  1083. if (waitingids.item(i1)==myid)
  1084. ((Semaphore *)waitingsems.item(i1))->signal();
  1085. }
  1086. bool ret = true;
  1087. if (defaultmax) {
  1088. unsigned n=threadwrappers.ordinality();
  1089. for (unsigned i2=targetpoolsize;i2<n;i2++) { // only check excess for efficiency
  1090. if (item==&threadwrappers.item(i2)) {
  1091. threadwrappers.remove(i2);
  1092. ret = false;
  1093. break;
  1094. }
  1095. }
  1096. availsem.signal();
  1097. }
  1098. return ret;
  1099. }
  1100. void setStartDelayTracing(unsigned secs)
  1101. {
  1102. traceStartDelayPeriod = secs;
  1103. }
  1104. };
  1105. IThreadPool *createThreadPool(const char *poolname,IThreadFactory *factory,IExceptionHandler *exceptionHandler,unsigned defaultmax, unsigned delay, unsigned stacksize, unsigned timeoutOnRelease, unsigned targetpoolsize)
  1106. {
  1107. return new CThreadPool(factory,exceptionHandler,poolname,defaultmax,delay,stacksize,timeoutOnRelease,targetpoolsize);
  1108. }
  1109. //=======================================================================================================
  1110. static void CheckAllowedProgram(const char *prog,const char *allowed)
  1111. {
  1112. if (!prog||!allowed||(strcmp(allowed,"*")==0))
  1113. return;
  1114. StringBuffer head;
  1115. bool inq = false;
  1116. // note don't have to be too worried about odd quoting as matching fixed list
  1117. while (*prog&&((*prog!=' ')||inq)) {
  1118. if (*prog=='"')
  1119. inq = !inq;
  1120. head.append(*(prog++));
  1121. }
  1122. StringArray list;
  1123. list.appendList(allowed, ",");
  1124. ForEachItemIn(i,list) {
  1125. if (WildMatch(head.str(),list.item(i)))
  1126. return;
  1127. }
  1128. ERRLOG("Unauthorized pipe program(%s)",head.str());
  1129. throw MakeStringException(-1,"Unauthorized pipe program(%s)",head.str());
  1130. }
  1131. class CSimplePipeStream: public CInterface, implements ISimpleReadStream
  1132. {
  1133. public:
  1134. IMPLEMENT_IINTERFACE;
  1135. CSimplePipeStream(IPipeProcess *_pipe, bool _isStderr) : pipe(_pipe), isStderr(_isStderr) {}
  1136. virtual size32_t read(size32_t sz, void * data)
  1137. {
  1138. if (isStderr)
  1139. return pipe->readError(sz, data);
  1140. else
  1141. return pipe->read(sz, data);
  1142. }
  1143. private:
  1144. Owned<IPipeProcess> pipe;
  1145. bool isStderr;
  1146. };
  1147. #ifdef _WIN32
  1148. class CWindowsPipeProcess: public CInterface, implements IPipeProcess
  1149. {
  1150. HANDLE pipeProcess;
  1151. HANDLE hInput;
  1152. HANDLE hOutput;
  1153. HANDLE hError;
  1154. StringAttr title;
  1155. unsigned retcode;
  1156. CriticalSection sect;
  1157. bool aborted;
  1158. StringAttr allowedprogs;
  1159. StringArray envVars;
  1160. StringArray envValues;
  1161. public:
  1162. IMPLEMENT_IINTERFACE;
  1163. CWindowsPipeProcess(const char *_allowedprogs)
  1164. : allowedprogs(_allowedprogs)
  1165. {
  1166. pipeProcess = (HANDLE)-1;
  1167. hInput=(HANDLE)-1;
  1168. hOutput=(HANDLE)-1;
  1169. hError=(HANDLE)-1;
  1170. retcode = (unsigned)-1;
  1171. aborted = false;
  1172. }
  1173. ~CWindowsPipeProcess()
  1174. {
  1175. kill();
  1176. }
  1177. void kill()
  1178. {
  1179. doCloseInput();
  1180. doCloseOutput();
  1181. doCloseError();
  1182. if (pipeProcess != (HANDLE)-1) {
  1183. CloseHandle(pipeProcess);
  1184. pipeProcess = (HANDLE)-1;
  1185. }
  1186. }
  1187. bool run(const char *_title,const char *prog,const char *dir,bool hasinput,bool hasoutput,bool haserror, size32_t stderrbufsize,bool newProcessGroup)
  1188. {
  1189. // size32_t stderrbufsize ignored as not required (I think)
  1190. CriticalBlock block(sect);
  1191. kill();
  1192. title.clear();
  1193. if (_title) {
  1194. title.set(_title);
  1195. PROGLOG("%s: Creating PIPE process : %s", title.get(), prog);
  1196. }
  1197. CheckAllowedProgram(prog,allowedprogs);
  1198. SECURITY_ATTRIBUTES sa;
  1199. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  1200. sa.bInheritHandle = TRUE;
  1201. sa.lpSecurityDescriptor = NULL;
  1202. HANDLE hProgOutput=(HANDLE)-1;
  1203. HANDLE hProgInput=(HANDLE)-1;
  1204. HANDLE hProgError=(HANDLE)-1;
  1205. HANDLE h;
  1206. //NB: Create a pipe handles that are not inherited our end
  1207. if (hasinput) {
  1208. CreatePipe(&hProgInput,&h,&sa,0);
  1209. DuplicateHandle(GetCurrentProcess(),h, GetCurrentProcess(), &hInput, 0, FALSE, DUPLICATE_SAME_ACCESS);
  1210. CloseHandle(h);
  1211. }
  1212. if (hasoutput) {
  1213. CreatePipe(&h,&hProgOutput,&sa,0);
  1214. DuplicateHandle(GetCurrentProcess(),h, GetCurrentProcess(), &hOutput, 0, FALSE, DUPLICATE_SAME_ACCESS);
  1215. CloseHandle(h);
  1216. }
  1217. if (haserror) {
  1218. CreatePipe(&h,&hProgError,&sa,0);
  1219. DuplicateHandle(GetCurrentProcess(),h, GetCurrentProcess(), &hError, 0, FALSE, DUPLICATE_SAME_ACCESS);
  1220. CloseHandle(h);
  1221. }
  1222. STARTUPINFO StartupInfo;
  1223. _clear(StartupInfo);
  1224. StartupInfo.cb = sizeof(StartupInfo);
  1225. StartupInfo.wShowWindow = SW_HIDE;
  1226. StartupInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW ;
  1227. StartupInfo.hStdOutput = hasoutput?hProgOutput:GetStdHandle(STD_OUTPUT_HANDLE);
  1228. StartupInfo.hStdError = haserror?hProgError:GetStdHandle(STD_ERROR_HANDLE);
  1229. StartupInfo.hStdInput = hasinput?hProgInput:GetStdHandle(STD_INPUT_HANDLE);
  1230. PROCESS_INFORMATION ProcessInformation;
  1231. // MORE - should create a new environment block that is copy of parent's, then set all the values in envVars/envValues, and pass it
  1232. if (!CreateProcess(NULL, (char *)prog, NULL,NULL,TRUE,0,NULL, dir&&*dir?dir:NULL, &StartupInfo,&ProcessInformation)) {
  1233. if (_title) {
  1234. StringBuffer errstr;
  1235. formatSystemError(errstr, GetLastError());
  1236. ERRLOG("%s: PIPE process '%s' failed: %s", title.get(), prog, errstr.str());
  1237. }
  1238. return false;
  1239. }
  1240. pipeProcess = ProcessInformation.hProcess;
  1241. CloseHandle(ProcessInformation.hThread);
  1242. if (hasoutput)
  1243. CloseHandle(hProgOutput);
  1244. if (hasinput)
  1245. CloseHandle(hProgInput);
  1246. if (haserror)
  1247. CloseHandle(hProgError);
  1248. return true;
  1249. }
  1250. virtual void setenv(const char *var, const char *value)
  1251. {
  1252. assertex(var);
  1253. if (!value)
  1254. value = "";
  1255. envVars.append(var);
  1256. envValues.append(value);
  1257. }
  1258. size32_t read(size32_t sz, void *buf)
  1259. {
  1260. DWORD sizeRead;
  1261. if (!ReadFile(hOutput, buf, sz, &sizeRead, NULL)) {
  1262. //raise error here
  1263. if(aborted)
  1264. return 0;
  1265. int err=GetLastError();
  1266. switch(err)
  1267. {
  1268. case ERROR_HANDLE_EOF:
  1269. case ERROR_BROKEN_PIPE:
  1270. case ERROR_NO_DATA:
  1271. return 0;
  1272. default:
  1273. aborted = true;
  1274. IException *e = makeOsExceptionV(err, "Pipe: ReadFile failed (size %d)", sz);
  1275. PrintExceptionLog(e, NULL);
  1276. throw e;
  1277. }
  1278. }
  1279. return aborted?((size32_t)-1):((size32_t)sizeRead);
  1280. }
  1281. ISimpleReadStream *getOutputStream()
  1282. {
  1283. return new CSimplePipeStream(LINK(this), false);
  1284. }
  1285. size32_t readError(size32_t sz, void *buf)
  1286. {
  1287. DWORD sizeRead;
  1288. if (!ReadFile(hError, buf, sz, &sizeRead, NULL)) {
  1289. //raise error here
  1290. if(aborted)
  1291. return 0;
  1292. int err=GetLastError();
  1293. switch(err)
  1294. {
  1295. case ERROR_HANDLE_EOF:
  1296. case ERROR_BROKEN_PIPE:
  1297. case ERROR_NO_DATA:
  1298. return 0;
  1299. default:
  1300. aborted = true;
  1301. IException *e = makeOsExceptionV(err, "Pipe: ReadError failed (size %d)", sz);
  1302. PrintExceptionLog(e, NULL);
  1303. throw e;
  1304. }
  1305. }
  1306. return aborted?((size32_t)-1):((size32_t)sizeRead);
  1307. }
  1308. ISimpleReadStream *getErrorStream()
  1309. {
  1310. return new CSimplePipeStream(LINK(this), true);
  1311. }
  1312. size32_t write(size32_t sz, const void *buf)
  1313. {
  1314. DWORD sizeWritten;
  1315. if (!WriteFile(hInput, buf, sz, &sizeWritten, NULL)) {
  1316. int err=GetLastError();
  1317. if ((err==ERROR_HANDLE_EOF)||aborted)
  1318. sizeWritten = 0;
  1319. else {
  1320. IException *e = makeOsExceptionV(err, "Pipe: WriteFile failed (size %d)", sz);
  1321. PrintExceptionLog(e, NULL);
  1322. throw e;
  1323. }
  1324. }
  1325. return aborted?((size32_t)-1):((size32_t)sizeWritten);
  1326. }
  1327. unsigned wait()
  1328. {
  1329. CriticalBlock block(sect);
  1330. if (pipeProcess != (HANDLE)-1) {
  1331. if (title.length())
  1332. PROGLOG("%s: Pipe: Waiting for process to complete %d",title.get(),(unsigned)pipeProcess);
  1333. {
  1334. CriticalUnblock unblock(sect);
  1335. WaitForSingleObject(pipeProcess, INFINITE);
  1336. }
  1337. if (pipeProcess != (HANDLE)-1) {
  1338. GetExitCodeProcess(pipeProcess,(LPDWORD)&retcode); // already got if notified
  1339. CloseHandle(pipeProcess);
  1340. pipeProcess = (HANDLE)-1;
  1341. }
  1342. if (title.length())
  1343. PROGLOG("%s: Pipe: process complete",title.get());
  1344. }
  1345. return retcode;
  1346. }
  1347. unsigned wait(unsigned timeoutms, bool &timedout)
  1348. {
  1349. CriticalBlock block(sect);
  1350. timedout = false;
  1351. if (pipeProcess != (HANDLE)-1) {
  1352. if (title.length())
  1353. PROGLOG("%s: Pipe: Waiting for process to complete %d",title.get(),(unsigned)pipeProcess);
  1354. {
  1355. CriticalUnblock unblock(sect);
  1356. if (WaitForSingleObject(pipeProcess, timeoutms)!=WAIT_OBJECT_0) {
  1357. timedout = true;
  1358. return retcode;
  1359. }
  1360. }
  1361. if (pipeProcess != (HANDLE)-1) {
  1362. GetExitCodeProcess(pipeProcess,(LPDWORD)&retcode); // already got if notified
  1363. CloseHandle(pipeProcess);
  1364. pipeProcess = (HANDLE)-1;
  1365. }
  1366. if (title.length())
  1367. PROGLOG("%s: Pipe: process complete",title.get());
  1368. }
  1369. return retcode;
  1370. }
  1371. void notifyTerminated(HANDLE pid,unsigned _retcode)
  1372. {
  1373. CriticalBlock block(sect);
  1374. if ((pid!=(HANDLE)-1)&&(pid==pipeProcess)) {
  1375. retcode = _retcode;
  1376. pipeProcess = (HANDLE)-1;
  1377. }
  1378. }
  1379. void doCloseInput()
  1380. {
  1381. CriticalBlock block(sect);
  1382. if (hInput != (HANDLE)-1) {
  1383. CloseHandle(hInput);
  1384. hInput = (HANDLE)-1;
  1385. }
  1386. }
  1387. void doCloseOutput()
  1388. {
  1389. CriticalBlock block(sect);
  1390. if (hOutput != (HANDLE)-1) {
  1391. CloseHandle(hOutput);
  1392. hOutput = (HANDLE)-1;
  1393. }
  1394. }
  1395. void doCloseError()
  1396. {
  1397. CriticalBlock block(sect);
  1398. if (hError != (HANDLE)-1) {
  1399. CloseHandle(hError);
  1400. hError = (HANDLE)-1;
  1401. }
  1402. }
  1403. void closeInput()
  1404. {
  1405. doCloseInput();
  1406. }
  1407. void closeOutput()
  1408. {
  1409. doCloseOutput();
  1410. }
  1411. void closeError()
  1412. {
  1413. doCloseError();
  1414. }
  1415. void abort()
  1416. {
  1417. CriticalBlock block(sect);
  1418. if (pipeProcess != (HANDLE)-1) {
  1419. if (title.length())
  1420. PROGLOG("%s: Pipe Aborting",title.get());
  1421. aborted = true;
  1422. //doCloseOutput(); // seems to work better without this
  1423. doCloseInput();
  1424. {
  1425. CriticalUnblock unblock(sect);
  1426. Sleep(100);
  1427. }
  1428. try { // this code is problematic for some reason
  1429. if (pipeProcess != (HANDLE)-1) {
  1430. TerminateProcess(pipeProcess, 255);
  1431. CloseHandle(pipeProcess);
  1432. pipeProcess = (HANDLE)-1;
  1433. }
  1434. }
  1435. catch (...) {
  1436. // ignore errors
  1437. }
  1438. if (title.length())
  1439. PROGLOG("%s: Pipe Aborted",title.get());
  1440. }
  1441. }
  1442. bool hasInput()
  1443. {
  1444. return hInput!=(HANDLE)-1;
  1445. }
  1446. bool hasOutput()
  1447. {
  1448. return hOutput!=(HANDLE)-1;
  1449. }
  1450. bool hasError()
  1451. {
  1452. return hError!=(HANDLE)-1;
  1453. }
  1454. HANDLE getProcessHandle()
  1455. {
  1456. return pipeProcess;
  1457. }
  1458. };
  1459. IPipeProcess *createPipeProcess(const char *allowedprogs)
  1460. {
  1461. return new CWindowsPipeProcess(allowedprogs);
  1462. }
  1463. #else
  1464. class CIgnoreSIGPIPE
  1465. {
  1466. public:
  1467. CIgnoreSIGPIPE()
  1468. {
  1469. struct sigaction act;
  1470. sigset_t blockset;
  1471. sigemptyset(&blockset);
  1472. act.sa_mask = blockset;
  1473. act.sa_handler = SIG_IGN;
  1474. act.sa_flags = 0;
  1475. sigaction(SIGPIPE, &act, NULL);
  1476. }
  1477. ~CIgnoreSIGPIPE()
  1478. {
  1479. signal(SIGPIPE, SIG_DFL);
  1480. }
  1481. };
  1482. #define WHITESPACE " \t\n\r"
  1483. static unsigned dowaitpid(HANDLE pid, int mode)
  1484. {
  1485. while (pid != (HANDLE)-1) {
  1486. int stat=-1;
  1487. int ret = waitpid(pid, &stat, mode);
  1488. if (ret>0)
  1489. {
  1490. if (WIFEXITED(stat))
  1491. return WEXITSTATUS(stat);
  1492. else if (WIFSIGNALED(stat))
  1493. {
  1494. ERRLOG("Program was terminated by signal %u", (unsigned) WTERMSIG(stat));
  1495. if (WTERMSIG(stat)==SIGPIPE)
  1496. return 0;
  1497. return 254;
  1498. }
  1499. else
  1500. {
  1501. return 254;
  1502. }
  1503. }
  1504. if (ret==0)
  1505. break;
  1506. int err = errno;
  1507. if (err == ECHILD)
  1508. break;
  1509. if (err!=EINTR) {
  1510. ERRLOG("dowait failed with errcode %d",err);
  1511. return (unsigned)-1;
  1512. }
  1513. }
  1514. return 0;
  1515. }
  1516. class CLinuxPipeProcess: public CInterface, implements IPipeProcess
  1517. {
  1518. class cForkThread: public Thread
  1519. {
  1520. CLinuxPipeProcess *parent;
  1521. public:
  1522. cForkThread(CLinuxPipeProcess *_parent)
  1523. {
  1524. parent = _parent;
  1525. }
  1526. int run()
  1527. {
  1528. parent->run();
  1529. return 0;
  1530. }
  1531. };
  1532. Owned<cForkThread> forkthread;
  1533. class cStdErrorBufferThread: public Thread
  1534. {
  1535. MemoryAttr buf;
  1536. size32_t bufsize;
  1537. Semaphore stopsem;
  1538. CriticalSection &sect;
  1539. int &hError;
  1540. public:
  1541. cStdErrorBufferThread(size32_t maxbufsize,int &_hError,CriticalSection &_sect)
  1542. : sect(_sect), hError(_hError)
  1543. {
  1544. buf.allocate(maxbufsize);
  1545. bufsize = 0;
  1546. }
  1547. int run()
  1548. {
  1549. while (!stopsem.wait(1000)) {
  1550. CriticalBlock block(sect);
  1551. if (hError!=(HANDLE)-1) { // hmm who did that
  1552. fcntl(hError,F_SETFL,O_NONBLOCK); // make sure non-blocking
  1553. if (bufsize<buf.length()) {
  1554. size32_t sizeRead = (size32_t)::read(hError, (byte *)buf.bufferBase()+bufsize, buf.length()-bufsize);
  1555. if ((int)sizeRead>0) {
  1556. bufsize += sizeRead;
  1557. }
  1558. }
  1559. else { // flush (to avoid process blocking)
  1560. byte tmp[1024];
  1561. size32_t totsz = 0;
  1562. for (unsigned i=0;i<1024;i++) {
  1563. size32_t sz = (size32_t)::read(hError, tmp, sizeof(tmp));
  1564. if ((int)sz<=0)
  1565. break;
  1566. totsz+=sz;
  1567. }
  1568. if (totsz)
  1569. WARNLOG("Lost %d bytes of stderr output",totsz);
  1570. }
  1571. }
  1572. }
  1573. if (hError!=(HANDLE)-1) { // hmm who did that
  1574. fcntl(hError,F_SETFL,0); // read any remaining data in blocking mode
  1575. while (bufsize<buf.length()) {
  1576. size32_t sizeRead = (size32_t)::read(hError, (byte *)buf.bufferBase()+bufsize, buf.length()-bufsize);
  1577. if ((int)sizeRead>0)
  1578. bufsize += sizeRead;
  1579. else
  1580. break;
  1581. }
  1582. }
  1583. return 0;
  1584. }
  1585. void stop()
  1586. {
  1587. stopsem.signal();
  1588. Thread::join();
  1589. }
  1590. size32_t read(size32_t sz,void *out)
  1591. {
  1592. CriticalBlock block(sect);
  1593. if (bufsize<sz)
  1594. sz = bufsize;
  1595. if (sz>0) {
  1596. memcpy(out,buf.bufferBase(),sz);
  1597. if (sz!=bufsize) {
  1598. bufsize -= sz;
  1599. memmove(buf.bufferBase(),(byte *)buf.bufferBase()+sz,bufsize); // not ideal but hopefully not large
  1600. }
  1601. else
  1602. bufsize = 0;
  1603. }
  1604. return sz;
  1605. }
  1606. } *stderrbufferthread;
  1607. protected: friend class PipeWriterThread;
  1608. HANDLE pipeProcess;
  1609. HANDLE hInput;
  1610. HANDLE hOutput;
  1611. HANDLE hError;
  1612. bool hasinput;
  1613. bool hasoutput;
  1614. bool haserror;
  1615. bool newProcessGroup;
  1616. StringAttr title;
  1617. StringAttr cmd;
  1618. StringAttr prog;
  1619. StringAttr dir;
  1620. int retcode;
  1621. CriticalSection sect;
  1622. Semaphore started;
  1623. bool aborted;
  1624. MemoryBuffer stderrbuf;
  1625. size32_t stderrbufsize;
  1626. StringAttr allowedprogs;
  1627. StringArray envVars;
  1628. StringArray envValues;
  1629. void clearUtilityThreads()
  1630. {
  1631. Owned<cForkThread> ft;
  1632. cStdErrorBufferThread *et;
  1633. {
  1634. CriticalBlock block(sect); // clear forkthread and stderrbufferthread
  1635. ft.setown(forkthread.getClear());
  1636. et = stderrbufferthread;
  1637. stderrbufferthread = NULL;
  1638. }
  1639. if (ft)
  1640. {
  1641. ft->join();
  1642. ft.clear();
  1643. }
  1644. if (et)
  1645. {
  1646. et->stop();
  1647. delete et;
  1648. }
  1649. }
  1650. public:
  1651. IMPLEMENT_IINTERFACE;
  1652. CLinuxPipeProcess(const char *_allowedprogs)
  1653. : allowedprogs(_allowedprogs)
  1654. {
  1655. pipeProcess = (HANDLE)-1;
  1656. hInput=(HANDLE)-1;
  1657. hOutput=(HANDLE)-1;
  1658. hError=(HANDLE)-1;
  1659. retcode = -1;
  1660. aborted = false;
  1661. stderrbufferthread = NULL;
  1662. newProcessGroup = false;
  1663. }
  1664. ~CLinuxPipeProcess()
  1665. {
  1666. kill();
  1667. }
  1668. void kill()
  1669. {
  1670. closeInput();
  1671. closeOutput();
  1672. closeError();
  1673. clearUtilityThreads();
  1674. }
  1675. char **splitargs(const char *line,unsigned &argc)
  1676. {
  1677. char *buf = strdup(line);
  1678. // first count params (this probably could be improved)
  1679. char *s = buf;
  1680. argc = 0;
  1681. while (readarg(s))
  1682. argc++;
  1683. free(buf);
  1684. size32_t l = strlen(line)+1;
  1685. size32_t al = (argc+1)*sizeof(char *);
  1686. char **argv = (char **)malloc(al+l);
  1687. argv[argc] = NULL;
  1688. s = ((char *)argv)+al;
  1689. memcpy(s,line,l);
  1690. for (unsigned i=0;i<argc;i++)
  1691. argv[i] = readarg(s);
  1692. return argv;
  1693. }
  1694. void run()
  1695. {
  1696. int inpipe[2];
  1697. int outpipe[2];
  1698. int errpipe[2];
  1699. if (hasinput)
  1700. if (::pipe(inpipe)==-1)
  1701. throw makeOsException(errno);
  1702. if (hasoutput)
  1703. if (::pipe(outpipe)==-1)
  1704. throw makeOsException(errno);
  1705. if (haserror)
  1706. if (::pipe(errpipe)==-1)
  1707. throw makeOsException(errno);
  1708. loop
  1709. {
  1710. pipeProcess = (HANDLE)fork();
  1711. if (pipeProcess!=(HANDLE)-1)
  1712. break;
  1713. if (errno!=EAGAIN) {
  1714. if (hasinput) {
  1715. close(inpipe[0]);
  1716. close(inpipe[1]);
  1717. }
  1718. if (hasoutput) {
  1719. close(outpipe[0]);
  1720. close(outpipe[1]);
  1721. }
  1722. if (haserror) {
  1723. close(errpipe[0]);
  1724. close(errpipe[1]);
  1725. }
  1726. retcode = START_FAILURE;
  1727. started.signal();
  1728. return;
  1729. }
  1730. }
  1731. if (pipeProcess==0) { // child
  1732. if (newProcessGroup)//Force the child process into its own process group, so we can terminate it and its children.
  1733. setpgid(0,0);
  1734. if (hasinput) {
  1735. dup2(inpipe[0],0);
  1736. close(inpipe[0]);
  1737. close(inpipe[1]);
  1738. }
  1739. if (hasoutput) {
  1740. dup2(outpipe[1],1);
  1741. close(outpipe[0]);
  1742. close(outpipe[1]);
  1743. }
  1744. if (haserror) {
  1745. dup2(errpipe[1],2);
  1746. close(errpipe[0]);
  1747. close(errpipe[1]);
  1748. }
  1749. unsigned argc;
  1750. char **argv=splitargs(prog,argc);
  1751. if (dir.get()) {
  1752. if (chdir(dir) == -1)
  1753. throw MakeStringException(-1, "CLinuxPipeProcess::run: could not change dir to %s", dir.get());
  1754. }
  1755. ForEachItemIn(idx, envVars)
  1756. {
  1757. ::setenv(envVars.item(idx), envValues.item(idx), 1);
  1758. }
  1759. execvp(argv[0],argv);
  1760. _exit(START_FAILURE); // must be _exit!!
  1761. }
  1762. if (hasinput)
  1763. close(inpipe[0]);
  1764. if (hasoutput)
  1765. close(outpipe[1]);
  1766. if (haserror)
  1767. close(errpipe[1]);
  1768. hInput = hasinput?inpipe[1]:((HANDLE)-1);
  1769. hOutput = hasoutput?outpipe[0]:((HANDLE)-1);
  1770. hError = haserror?errpipe[0]:((HANDLE)-1);
  1771. started.signal();
  1772. retcode = dowaitpid(pipeProcess, 0);
  1773. if (retcode==START_FAILURE)
  1774. closeOutput();
  1775. }
  1776. bool run(const char *_title,const char *_prog,const char *_dir,bool _hasinput,bool _hasoutput, bool _haserror, size32_t stderrbufsize, bool _newProcessGroup)
  1777. {
  1778. static CriticalSection runsect; // single thread process start to avoid forked handle open/closes interleaving
  1779. CriticalBlock runblock(runsect);
  1780. kill();
  1781. CriticalBlock block(sect);
  1782. hasinput = _hasinput;
  1783. hasoutput = _hasoutput;
  1784. haserror = _haserror;
  1785. newProcessGroup = _newProcessGroup;
  1786. title.clear();
  1787. prog.set(_prog);
  1788. dir.set(_dir);
  1789. if (_title) {
  1790. title.set(_title);
  1791. PROGLOG("%s: Creating PIPE program process : '%s' - hasinput=%d, hasoutput=%d stderrbufsize=%d", title.get(), prog.get(),(int)hasinput, (int)hasoutput, stderrbufsize);
  1792. }
  1793. CheckAllowedProgram(prog,allowedprogs);
  1794. retcode = 0;
  1795. if (forkthread) {
  1796. {
  1797. CriticalUnblock unblock(sect);
  1798. forkthread->join();
  1799. }
  1800. forkthread.clear();
  1801. }
  1802. forkthread.setown(new cForkThread(this));
  1803. forkthread->start();
  1804. {
  1805. CriticalUnblock unblock(sect);
  1806. started.wait();
  1807. forkthread->join(50); // give a chance to fail
  1808. }
  1809. if (retcode==START_FAILURE) {
  1810. DBGLOG("%s: PIPE process '%s' failed to start", title.get()?title.get():"CLinuxPipeProcess", prog.get());
  1811. forkthread.clear();
  1812. return false;
  1813. }
  1814. if (stderrbufsize) {
  1815. if (stderrbufferthread) {
  1816. stderrbufferthread->stop();
  1817. delete stderrbufferthread;
  1818. }
  1819. stderrbufferthread = new cStdErrorBufferThread(stderrbufsize,hError,sect);
  1820. stderrbufferthread->start();
  1821. }
  1822. return true;
  1823. }
  1824. virtual void setenv(const char *var, const char *value)
  1825. {
  1826. assertex(var);
  1827. if (!value)
  1828. value = "";
  1829. envVars.append(var);
  1830. envValues.append(value);
  1831. }
  1832. size32_t read(size32_t sz, void *buf)
  1833. {
  1834. CriticalBlock block(sect);
  1835. if (aborted)
  1836. return (size32_t)-1;
  1837. if (hOutput==(HANDLE)-1)
  1838. return 0;
  1839. size32_t sizeRead;
  1840. loop {
  1841. {
  1842. CriticalUnblock unblock(sect);
  1843. sizeRead = (size32_t)::read(hOutput, buf, sz);
  1844. }
  1845. if (sizeRead!=(size32_t)-1)
  1846. break;
  1847. if (aborted)
  1848. break;
  1849. if (errno!=EINTR) {
  1850. aborted = true;
  1851. throw makeErrnoExceptionV(errno,"Pipe: read failed (size %d)", sz);
  1852. }
  1853. }
  1854. return aborted?((size32_t)-1):((size32_t)sizeRead);
  1855. }
  1856. ISimpleReadStream *getOutputStream()
  1857. {
  1858. return new CSimplePipeStream(LINK(this), false);
  1859. }
  1860. size32_t write(size32_t sz, const void *buf)
  1861. {
  1862. CriticalBlock block(sect);
  1863. CIgnoreSIGPIPE ignoresigpipe;
  1864. if (aborted)
  1865. return (size32_t)-1;
  1866. if (hInput==(HANDLE)-1)
  1867. return 0;
  1868. size32_t sizeWritten;
  1869. loop {
  1870. {
  1871. CriticalUnblock unblock(sect);
  1872. sizeWritten = (size32_t)::write(hInput, buf, sz);
  1873. }
  1874. if (sizeWritten!=(size32_t)-1)
  1875. break;
  1876. if (aborted)
  1877. break;
  1878. if (errno!=EINTR) {
  1879. throw makeErrnoExceptionV(errno, "Pipe: write failed (size %d)", sz);
  1880. }
  1881. }
  1882. return aborted?((size32_t)-1):((size32_t)sizeWritten);
  1883. }
  1884. size32_t readError(size32_t sz, void *buf)
  1885. {
  1886. CriticalBlock block(sect);
  1887. if (stderrbufferthread)
  1888. return stderrbufferthread->read(sz,buf);
  1889. if (aborted)
  1890. return (size32_t)-1;
  1891. if (hError==(HANDLE)-1)
  1892. return 0;
  1893. size32_t sizeRead;
  1894. loop {
  1895. {
  1896. CriticalUnblock unblock(sect);
  1897. sizeRead = (size32_t)::read(hError, buf, sz);
  1898. }
  1899. if (sizeRead!=(size32_t)-1)
  1900. break;
  1901. if (aborted)
  1902. break;
  1903. if (errno!=EINTR) {
  1904. aborted = true;
  1905. throw makeErrnoExceptionV(errno, "Pipe: readError failed (size %d)", sz);
  1906. }
  1907. }
  1908. return aborted?((size32_t)-1):((size32_t)sizeRead);
  1909. }
  1910. ISimpleReadStream *getErrorStream()
  1911. {
  1912. return new CSimplePipeStream(LINK(this), true);
  1913. }
  1914. void notifyTerminated(HANDLE pid,unsigned _retcode)
  1915. {
  1916. CriticalBlock block(sect);
  1917. if (((int)pid>0)&&(pid==pipeProcess)) {
  1918. retcode = _retcode;
  1919. pipeProcess = (HANDLE)-1;
  1920. }
  1921. }
  1922. unsigned wait()
  1923. {
  1924. bool timedout;
  1925. return wait(INFINITE, timedout);
  1926. }
  1927. unsigned wait(unsigned timeoutms, bool &timedout)
  1928. {
  1929. timedout = false;
  1930. if (INFINITE != timeoutms)
  1931. {
  1932. CriticalBlock block(sect);
  1933. if (forkthread)
  1934. {
  1935. {
  1936. CriticalUnblock unblock(sect);
  1937. if (!forkthread->join(timeoutms))
  1938. {
  1939. timedout = true;
  1940. return retcode;
  1941. }
  1942. }
  1943. }
  1944. }
  1945. clearUtilityThreads(); // NB: will recall forkthread->join(), but doesn't matter
  1946. if (pipeProcess != (HANDLE)-1)
  1947. {
  1948. if (title.length())
  1949. PROGLOG("%s: Pipe: process %d complete %d", title.get(), pipeProcess, retcode);
  1950. pipeProcess = (HANDLE)-1;
  1951. }
  1952. return retcode;
  1953. }
  1954. void closeOutput()
  1955. {
  1956. CriticalBlock block(sect);
  1957. if (hOutput != (HANDLE)-1) {
  1958. ::close(hOutput);
  1959. hOutput = (HANDLE)-1;
  1960. }
  1961. }
  1962. void closeInput()
  1963. {
  1964. CriticalBlock block(sect);
  1965. if (hInput != (HANDLE)-1) {
  1966. ::close(hInput);
  1967. hInput = (HANDLE)-1;
  1968. }
  1969. }
  1970. void closeError()
  1971. {
  1972. CriticalBlock block(sect);
  1973. if (hError != (HANDLE)-1) {
  1974. ::close(hError);
  1975. hError = (HANDLE)-1;
  1976. }
  1977. }
  1978. void abort()
  1979. {
  1980. CriticalBlock block(sect);
  1981. if (pipeProcess != (HANDLE)-1) {
  1982. if (title.length())
  1983. PROGLOG("%s: Pipe Aborting",title.get());
  1984. aborted = true;
  1985. closeInput();
  1986. {
  1987. CriticalUnblock unblock(sect);
  1988. forkthread->join(1000);
  1989. }
  1990. if (pipeProcess != (HANDLE)-1) {
  1991. if (title.length())
  1992. PROGLOG("%s: Forcibly killing pipe process %d",title.get(),pipeProcess);
  1993. if (newProcessGroup)
  1994. ::kill(-pipeProcess,SIGKILL);
  1995. else
  1996. ::kill(pipeProcess,SIGKILL); // if this doesn't kill it we are in trouble
  1997. CriticalUnblock unblock(sect);
  1998. wait();
  1999. }
  2000. if (title.length())
  2001. PROGLOG("%s: Pipe Aborted",title.get());
  2002. retcode = -1;
  2003. forkthread.clear();
  2004. }
  2005. }
  2006. bool hasInput()
  2007. {
  2008. CriticalBlock block(sect);
  2009. return hInput!=(HANDLE)-1;
  2010. }
  2011. bool hasOutput()
  2012. {
  2013. CriticalBlock block(sect);
  2014. return hOutput!=(HANDLE)-1;
  2015. }
  2016. bool hasError()
  2017. {
  2018. CriticalBlock block(sect);
  2019. return hError!=(HANDLE)-1;
  2020. }
  2021. HANDLE getProcessHandle()
  2022. {
  2023. CriticalBlock block(sect);
  2024. return pipeProcess;
  2025. }
  2026. };
  2027. IPipeProcess *createPipeProcess(const char *allowedprogs)
  2028. {
  2029. return new CLinuxPipeProcess(allowedprogs);
  2030. }
  2031. #endif
  2032. // Worker thread
  2033. class CWorkQueueThread: public CInterface, implements IWorkQueueThread
  2034. {
  2035. public:
  2036. IMPLEMENT_IINTERFACE;
  2037. CriticalSection crit;
  2038. unsigned persisttime;
  2039. class cWorkerThread: public Thread
  2040. {
  2041. unsigned persisttime;
  2042. CWorkQueueThread *parent;
  2043. CriticalSection &crit;
  2044. public:
  2045. IMPLEMENT_IINTERFACE;
  2046. cWorkerThread(CWorkQueueThread *_parent,CriticalSection &_crit,unsigned _persisttime)
  2047. : crit(_crit)
  2048. {
  2049. parent = _parent;
  2050. persisttime = _persisttime;
  2051. }
  2052. QueueOf<IWorkQueueItem,false> queue;
  2053. Semaphore sem;
  2054. int run()
  2055. {
  2056. loop {
  2057. IWorkQueueItem * work;
  2058. bool wr = sem.wait(persisttime);
  2059. {
  2060. CriticalBlock block(crit);
  2061. if (!wr) {
  2062. wr = sem.wait(0); // catch race
  2063. if (!wr)
  2064. break; // timed out
  2065. }
  2066. work = queue.dequeue();
  2067. }
  2068. if (!work)
  2069. break;
  2070. try {
  2071. work->execute();
  2072. work->Release();
  2073. }
  2074. catch (IException *e)
  2075. {
  2076. EXCLOG(e,"CWorkQueueThread item execute");
  2077. e->Release();
  2078. }
  2079. }
  2080. CriticalBlock block(crit);
  2081. parent->worker=NULL; // this should be safe
  2082. return 0;
  2083. }
  2084. } *worker;
  2085. CWorkQueueThread(unsigned _persisttime)
  2086. {
  2087. persisttime = _persisttime;
  2088. worker = NULL;
  2089. }
  2090. ~CWorkQueueThread()
  2091. {
  2092. wait();
  2093. }
  2094. void post(IWorkQueueItem *packet)
  2095. {
  2096. CriticalBlock block(crit);
  2097. if (!worker) {
  2098. worker = new cWorkerThread(this,crit,persisttime);
  2099. worker->startRelease();
  2100. }
  2101. worker->queue.enqueue(packet);
  2102. worker->sem.signal();
  2103. }
  2104. void wait()
  2105. {
  2106. CriticalBlock block(crit);
  2107. if (worker) {
  2108. worker->queue.enqueue(NULL);
  2109. worker->sem.signal();
  2110. Linked<cWorkerThread> wt;
  2111. wt.set(worker);
  2112. CriticalUnblock unblock(crit);
  2113. wt->join();
  2114. }
  2115. }
  2116. unsigned pending()
  2117. {
  2118. CriticalBlock block(crit);
  2119. unsigned ret = 0;
  2120. if (worker)
  2121. ret = worker->queue.ordinality();
  2122. return ret;
  2123. }
  2124. };
  2125. IWorkQueueThread *createWorkQueueThread(unsigned persisttime)
  2126. {
  2127. return new CWorkQueueThread(persisttime);
  2128. }
  2129. unsigned threadLogID() // for use in logging
  2130. {
  2131. #ifndef _WIN32
  2132. #ifdef SYS_gettid
  2133. return (unsigned) (memsize_t) syscall(SYS_gettid);
  2134. #endif
  2135. #endif
  2136. return (unsigned)(memsize_t) GetCurrentThreadId(); // truncated in 64bit
  2137. }
  2138. //---------------------------------------------------------------------------------------------------------------------
  2139. //MORE: Not currently implemented for windows.
  2140. #ifdef CPU_SETSIZE
  2141. static unsigned getCpuId(const char * text, char * * next)
  2142. {
  2143. unsigned cpu = (unsigned)strtoul(text, next, 10);
  2144. if (*next == text)
  2145. throw makeStringExceptionV(1, "Invalid CPU: %s", text);
  2146. else if (cpu >= CPU_SETSIZE)
  2147. throw makeStringExceptionV(1, "CPU %u is out of range 0..%u", cpu, CPU_SETSIZE);
  2148. return cpu;
  2149. }
  2150. #endif
  2151. void setProcessAffinity(const char * cpuList)
  2152. {
  2153. assertex(cpuList);
  2154. #ifdef CPU_ZERO
  2155. cpu_set_t cpus;
  2156. CPU_ZERO(&cpus);
  2157. const char * cur = cpuList;
  2158. loop
  2159. {
  2160. char * next;
  2161. unsigned cpu1 = getCpuId(cur, &next);
  2162. if (*next == '-')
  2163. {
  2164. const char * range = next+1;
  2165. unsigned cpu2 = getCpuId(range, &next);
  2166. for (unsigned cpu= cpu1; cpu <= cpu2; cpu++)
  2167. CPU_SET(cpu, &cpus);
  2168. }
  2169. else
  2170. CPU_SET(cpu1, &cpus);
  2171. if (*next == '\0')
  2172. break;
  2173. if (*next != ',')
  2174. throw makeStringExceptionV(1, "Invalid cpu affinity list %s", cur);
  2175. cur = next+1;
  2176. }
  2177. if (sched_setaffinity(0, sizeof(cpu_set_t), &cpus))
  2178. throw makeStringException(errno, "Failed to set affinity");
  2179. DBGLOG("Process affinity set to %s", cpuList);
  2180. #endif
  2181. }
  2182. void setAutoAffinity(unsigned curProcess, unsigned processPerMachine, const char * optNodes)
  2183. {
  2184. #if defined(CPU_ZERO) && !defined(__APPLE__)
  2185. if (processPerMachine <= 1)
  2186. return;
  2187. if (numa_available() == -1)
  2188. {
  2189. DBGLOG("Numa functions not available");
  2190. return;
  2191. }
  2192. if (optNodes)
  2193. throw makeStringException(1, "Numa node list not yet supported");
  2194. unsigned numNumaNodes = numa_max_node()+1;
  2195. if (numNumaNodes <= 1)
  2196. return;
  2197. //MORE: If processPerMachine < numNumaNodes we may want to associate with > 1 node.
  2198. unsigned curNode = curProcess % numNumaNodes;
  2199. #if defined(LIBNUMA_API_VERSION) && (LIBNUMA_API_VERSION>=2)
  2200. struct bitmask * cpus = numa_allocate_cpumask();
  2201. numa_node_to_cpus(curNode, cpus);
  2202. bool ok = (numa_sched_setaffinity(0, cpus) == 0);
  2203. numa_bitmask_free(cpus);
  2204. #else
  2205. cpu_set_t cpus;
  2206. CPU_ZERO(&cpus);
  2207. numa_node_to_cpus(curNode, (unsigned long *) &cpus, sizeof (cpus));
  2208. bool ok = sched_setaffinity (0, sizeof(cpus), &cpus) != 0;
  2209. #endif
  2210. if (!ok)
  2211. throw makeStringExceptionV(1, "Failed to set affinity for node %u", curNode);
  2212. DBGLOG("Process bound to numa node %u of %u", curNode, numNumaNodes);
  2213. #endif
  2214. }
  2215. void bindMemoryToLocalNodes()
  2216. {
  2217. #if defined(LIBNUMA_API_VERSION) && (LIBNUMA_API_VERSION>=2)
  2218. numa_set_bind_policy(1);
  2219. unsigned numNumaNodes = numa_max_node() + 1;
  2220. if (numNumaNodes <= 1)
  2221. return;
  2222. struct bitmask *nodes = numa_get_run_node_mask();
  2223. numa_set_membind(nodes);
  2224. DBGLOG("Process memory bound to numa nodemask 0x%x (of %u nodes total)", (unsigned)(*(nodes->maskp)), numNumaNodes);
  2225. numa_bitmask_free(nodes);
  2226. #endif
  2227. }