z_Linux_util.cpp 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  1. /*
  2. * z_Linux_util.cpp -- platform specific routines.
  3. */
  4. //===----------------------------------------------------------------------===//
  5. //
  6. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  7. // See https://llvm.org/LICENSE.txt for license information.
  8. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "kmp.h"
  12. #include "kmp_affinity.h"
  13. #include "kmp_i18n.h"
  14. #include "kmp_io.h"
  15. #include "kmp_itt.h"
  16. #include "kmp_lock.h"
  17. #include "kmp_stats.h"
  18. #include "kmp_str.h"
  19. #include "kmp_wait_release.h"
  20. #include "kmp_wrapper_getpid.h"
  21. #if !KMP_OS_DRAGONFLY && !KMP_OS_FREEBSD && !KMP_OS_NETBSD && !KMP_OS_OPENBSD
  22. #include <alloca.h>
  23. #endif
  24. #include <math.h> // HUGE_VAL.
  25. #if KMP_OS_LINUX
  26. #include <semaphore.h>
  27. #endif // KMP_OS_LINUX
  28. #include <sys/resource.h>
  29. #include <sys/syscall.h>
  30. #include <sys/time.h>
  31. #include <sys/times.h>
  32. #include <unistd.h>
  33. #if KMP_OS_LINUX
  34. #include <sys/sysinfo.h>
  35. #if KMP_USE_FUTEX
  36. // We should really include <futex.h>, but that causes compatibility problems on
  37. // different Linux* OS distributions that either require that you include (or
  38. // break when you try to include) <pci/types.h>. Since all we need is the two
  39. // macros below (which are part of the kernel ABI, so can't change) we just
  40. // define the constants here and don't include <futex.h>
  41. #ifndef FUTEX_WAIT
  42. #define FUTEX_WAIT 0
  43. #endif
  44. #ifndef FUTEX_WAKE
  45. #define FUTEX_WAKE 1
  46. #endif
  47. #endif
  48. #elif KMP_OS_DARWIN
  49. #include <mach/mach.h>
  50. #include <sys/sysctl.h>
  51. #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD
  52. #include <sys/types.h>
  53. #include <sys/sysctl.h>
  54. #include <sys/user.h>
  55. #include <pthread_np.h>
  56. #elif KMP_OS_NETBSD || KMP_OS_OPENBSD
  57. #include <sys/types.h>
  58. #include <sys/sysctl.h>
  59. #endif
  60. #include <ctype.h>
  61. #include <dirent.h>
  62. #include <fcntl.h>
  63. struct kmp_sys_timer {
  64. struct timespec start;
  65. };
  66. // Convert timespec to nanoseconds.
  67. #define TS2NS(timespec) \
  68. (((timespec).tv_sec * (long int)1e9) + (timespec).tv_nsec)
  69. static struct kmp_sys_timer __kmp_sys_timer_data;
  70. #if KMP_HANDLE_SIGNALS
  71. typedef void (*sig_func_t)(int);
  72. STATIC_EFI2_WORKAROUND struct sigaction __kmp_sighldrs[NSIG];
  73. static sigset_t __kmp_sigset;
  74. #endif
  75. static int __kmp_init_runtime = FALSE;
  76. static int __kmp_fork_count = 0;
  77. static pthread_condattr_t __kmp_suspend_cond_attr;
  78. static pthread_mutexattr_t __kmp_suspend_mutex_attr;
  79. static kmp_cond_align_t __kmp_wait_cv;
  80. static kmp_mutex_align_t __kmp_wait_mx;
  81. kmp_uint64 __kmp_ticks_per_msec = 1000000;
  82. #ifdef DEBUG_SUSPEND
  83. static void __kmp_print_cond(char *buffer, kmp_cond_align_t *cond) {
  84. KMP_SNPRINTF(buffer, 128, "(cond (lock (%ld, %d)), (descr (%p)))",
  85. cond->c_cond.__c_lock.__status, cond->c_cond.__c_lock.__spinlock,
  86. cond->c_cond.__c_waiting);
  87. }
  88. #endif
  89. #if ((KMP_OS_LINUX || KMP_OS_FREEBSD) && KMP_AFFINITY_SUPPORTED)
  90. /* Affinity support */
  91. void __kmp_affinity_bind_thread(int which) {
  92. KMP_ASSERT2(KMP_AFFINITY_CAPABLE(),
  93. "Illegal set affinity operation when not capable");
  94. kmp_affin_mask_t *mask;
  95. KMP_CPU_ALLOC_ON_STACK(mask);
  96. KMP_CPU_ZERO(mask);
  97. KMP_CPU_SET(which, mask);
  98. __kmp_set_system_affinity(mask, TRUE);
  99. KMP_CPU_FREE_FROM_STACK(mask);
  100. }
  101. /* Determine if we can access affinity functionality on this version of
  102. * Linux* OS by checking __NR_sched_{get,set}affinity system calls, and set
  103. * __kmp_affin_mask_size to the appropriate value (0 means not capable). */
  104. void __kmp_affinity_determine_capable(const char *env_var) {
  105. // Check and see if the OS supports thread affinity.
  106. #if KMP_OS_LINUX
  107. #define KMP_CPU_SET_SIZE_LIMIT (1024 * 1024)
  108. #define KMP_CPU_SET_TRY_SIZE CACHE_LINE
  109. #elif KMP_OS_FREEBSD
  110. #define KMP_CPU_SET_SIZE_LIMIT (sizeof(cpuset_t))
  111. #endif
  112. #if KMP_OS_LINUX
  113. long gCode;
  114. unsigned char *buf;
  115. buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
  116. // If the syscall returns a suggestion for the size,
  117. // then we don't have to search for an appropriate size.
  118. gCode = syscall(__NR_sched_getaffinity, 0, KMP_CPU_SET_TRY_SIZE, buf);
  119. KA_TRACE(30, ("__kmp_affinity_determine_capable: "
  120. "initial getaffinity call returned %ld errno = %d\n",
  121. gCode, errno));
  122. if (gCode < 0 && errno != EINVAL) {
  123. // System call not supported
  124. if (__kmp_affinity_verbose ||
  125. (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
  126. (__kmp_affinity_type != affinity_default) &&
  127. (__kmp_affinity_type != affinity_disabled))) {
  128. int error = errno;
  129. kmp_msg_t err_code = KMP_ERR(error);
  130. __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
  131. err_code, __kmp_msg_null);
  132. if (__kmp_generate_warnings == kmp_warnings_off) {
  133. __kmp_str_free(&err_code.str);
  134. }
  135. }
  136. KMP_AFFINITY_DISABLE();
  137. KMP_INTERNAL_FREE(buf);
  138. return;
  139. } else if (gCode > 0) {
  140. // The optimal situation: the OS returns the size of the buffer it expects.
  141. KMP_AFFINITY_ENABLE(gCode);
  142. KA_TRACE(10, ("__kmp_affinity_determine_capable: "
  143. "affinity supported (mask size %d)\n",
  144. (int)__kmp_affin_mask_size));
  145. KMP_INTERNAL_FREE(buf);
  146. return;
  147. }
  148. // Call the getaffinity system call repeatedly with increasing set sizes
  149. // until we succeed, or reach an upper bound on the search.
  150. KA_TRACE(30, ("__kmp_affinity_determine_capable: "
  151. "searching for proper set size\n"));
  152. int size;
  153. for (size = 1; size <= KMP_CPU_SET_SIZE_LIMIT; size *= 2) {
  154. gCode = syscall(__NR_sched_getaffinity, 0, size, buf);
  155. KA_TRACE(30, ("__kmp_affinity_determine_capable: "
  156. "getaffinity for mask size %ld returned %ld errno = %d\n",
  157. size, gCode, errno));
  158. if (gCode < 0) {
  159. if (errno == ENOSYS) {
  160. // We shouldn't get here
  161. KA_TRACE(30, ("__kmp_affinity_determine_capable: "
  162. "inconsistent OS call behavior: errno == ENOSYS for mask "
  163. "size %d\n",
  164. size));
  165. if (__kmp_affinity_verbose ||
  166. (__kmp_affinity_warnings &&
  167. (__kmp_affinity_type != affinity_none) &&
  168. (__kmp_affinity_type != affinity_default) &&
  169. (__kmp_affinity_type != affinity_disabled))) {
  170. int error = errno;
  171. kmp_msg_t err_code = KMP_ERR(error);
  172. __kmp_msg(kmp_ms_warning, KMP_MSG(GetAffSysCallNotSupported, env_var),
  173. err_code, __kmp_msg_null);
  174. if (__kmp_generate_warnings == kmp_warnings_off) {
  175. __kmp_str_free(&err_code.str);
  176. }
  177. }
  178. KMP_AFFINITY_DISABLE();
  179. KMP_INTERNAL_FREE(buf);
  180. return;
  181. }
  182. continue;
  183. }
  184. KMP_AFFINITY_ENABLE(gCode);
  185. KA_TRACE(10, ("__kmp_affinity_determine_capable: "
  186. "affinity supported (mask size %d)\n",
  187. (int)__kmp_affin_mask_size));
  188. KMP_INTERNAL_FREE(buf);
  189. return;
  190. }
  191. #elif KMP_OS_FREEBSD
  192. long gCode;
  193. unsigned char *buf;
  194. buf = (unsigned char *)KMP_INTERNAL_MALLOC(KMP_CPU_SET_SIZE_LIMIT);
  195. gCode = pthread_getaffinity_np(pthread_self(), KMP_CPU_SET_SIZE_LIMIT,
  196. reinterpret_cast<cpuset_t *>(buf));
  197. KA_TRACE(30, ("__kmp_affinity_determine_capable: "
  198. "initial getaffinity call returned %d errno = %d\n",
  199. gCode, errno));
  200. if (gCode == 0) {
  201. KMP_AFFINITY_ENABLE(KMP_CPU_SET_SIZE_LIMIT);
  202. KA_TRACE(10, ("__kmp_affinity_determine_capable: "
  203. "affinity supported (mask size %d)\n",
  204. (int)__kmp_affin_mask_size));
  205. KMP_INTERNAL_FREE(buf);
  206. return;
  207. }
  208. #endif
  209. KMP_INTERNAL_FREE(buf);
  210. // Affinity is not supported
  211. KMP_AFFINITY_DISABLE();
  212. KA_TRACE(10, ("__kmp_affinity_determine_capable: "
  213. "cannot determine mask size - affinity not supported\n"));
  214. if (__kmp_affinity_verbose ||
  215. (__kmp_affinity_warnings && (__kmp_affinity_type != affinity_none) &&
  216. (__kmp_affinity_type != affinity_default) &&
  217. (__kmp_affinity_type != affinity_disabled))) {
  218. KMP_WARNING(AffCantGetMaskSize, env_var);
  219. }
  220. }
  221. #endif // KMP_OS_LINUX && KMP_AFFINITY_SUPPORTED
  222. #if KMP_USE_FUTEX
  223. int __kmp_futex_determine_capable() {
  224. int loc = 0;
  225. long rc = syscall(__NR_futex, &loc, FUTEX_WAKE, 1, NULL, NULL, 0);
  226. int retval = (rc == 0) || (errno != ENOSYS);
  227. KA_TRACE(10,
  228. ("__kmp_futex_determine_capable: rc = %d errno = %d\n", rc, errno));
  229. KA_TRACE(10, ("__kmp_futex_determine_capable: futex syscall%s supported\n",
  230. retval ? "" : " not"));
  231. return retval;
  232. }
  233. #endif // KMP_USE_FUTEX
  234. #if (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (!KMP_ASM_INTRINS)
  235. /* Only 32-bit "add-exchange" instruction on IA-32 architecture causes us to
  236. use compare_and_store for these routines */
  237. kmp_int8 __kmp_test_then_or8(volatile kmp_int8 *p, kmp_int8 d) {
  238. kmp_int8 old_value, new_value;
  239. old_value = TCR_1(*p);
  240. new_value = old_value | d;
  241. while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
  242. KMP_CPU_PAUSE();
  243. old_value = TCR_1(*p);
  244. new_value = old_value | d;
  245. }
  246. return old_value;
  247. }
  248. kmp_int8 __kmp_test_then_and8(volatile kmp_int8 *p, kmp_int8 d) {
  249. kmp_int8 old_value, new_value;
  250. old_value = TCR_1(*p);
  251. new_value = old_value & d;
  252. while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
  253. KMP_CPU_PAUSE();
  254. old_value = TCR_1(*p);
  255. new_value = old_value & d;
  256. }
  257. return old_value;
  258. }
  259. kmp_uint32 __kmp_test_then_or32(volatile kmp_uint32 *p, kmp_uint32 d) {
  260. kmp_uint32 old_value, new_value;
  261. old_value = TCR_4(*p);
  262. new_value = old_value | d;
  263. while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
  264. KMP_CPU_PAUSE();
  265. old_value = TCR_4(*p);
  266. new_value = old_value | d;
  267. }
  268. return old_value;
  269. }
  270. kmp_uint32 __kmp_test_then_and32(volatile kmp_uint32 *p, kmp_uint32 d) {
  271. kmp_uint32 old_value, new_value;
  272. old_value = TCR_4(*p);
  273. new_value = old_value & d;
  274. while (!KMP_COMPARE_AND_STORE_REL32(p, old_value, new_value)) {
  275. KMP_CPU_PAUSE();
  276. old_value = TCR_4(*p);
  277. new_value = old_value & d;
  278. }
  279. return old_value;
  280. }
  281. #if KMP_ARCH_X86
  282. kmp_int8 __kmp_test_then_add8(volatile kmp_int8 *p, kmp_int8 d) {
  283. kmp_int8 old_value, new_value;
  284. old_value = TCR_1(*p);
  285. new_value = old_value + d;
  286. while (!KMP_COMPARE_AND_STORE_REL8(p, old_value, new_value)) {
  287. KMP_CPU_PAUSE();
  288. old_value = TCR_1(*p);
  289. new_value = old_value + d;
  290. }
  291. return old_value;
  292. }
  293. kmp_int64 __kmp_test_then_add64(volatile kmp_int64 *p, kmp_int64 d) {
  294. kmp_int64 old_value, new_value;
  295. old_value = TCR_8(*p);
  296. new_value = old_value + d;
  297. while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
  298. KMP_CPU_PAUSE();
  299. old_value = TCR_8(*p);
  300. new_value = old_value + d;
  301. }
  302. return old_value;
  303. }
  304. #endif /* KMP_ARCH_X86 */
  305. kmp_uint64 __kmp_test_then_or64(volatile kmp_uint64 *p, kmp_uint64 d) {
  306. kmp_uint64 old_value, new_value;
  307. old_value = TCR_8(*p);
  308. new_value = old_value | d;
  309. while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
  310. KMP_CPU_PAUSE();
  311. old_value = TCR_8(*p);
  312. new_value = old_value | d;
  313. }
  314. return old_value;
  315. }
  316. kmp_uint64 __kmp_test_then_and64(volatile kmp_uint64 *p, kmp_uint64 d) {
  317. kmp_uint64 old_value, new_value;
  318. old_value = TCR_8(*p);
  319. new_value = old_value & d;
  320. while (!KMP_COMPARE_AND_STORE_REL64(p, old_value, new_value)) {
  321. KMP_CPU_PAUSE();
  322. old_value = TCR_8(*p);
  323. new_value = old_value & d;
  324. }
  325. return old_value;
  326. }
  327. #endif /* (KMP_ARCH_X86 || KMP_ARCH_X86_64) && (! KMP_ASM_INTRINS) */
  328. void __kmp_terminate_thread(int gtid) {
  329. int status;
  330. kmp_info_t *th = __kmp_threads[gtid];
  331. if (!th)
  332. return;
  333. #ifdef KMP_CANCEL_THREADS
  334. KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
  335. status = pthread_cancel(th->th.th_info.ds.ds_thread);
  336. if (status != 0 && status != ESRCH) {
  337. __kmp_fatal(KMP_MSG(CantTerminateWorkerThread), KMP_ERR(status),
  338. __kmp_msg_null);
  339. }
  340. #endif
  341. KMP_YIELD(TRUE);
  342. } //
  343. /* Set thread stack info according to values returned by pthread_getattr_np().
  344. If values are unreasonable, assume call failed and use incremental stack
  345. refinement method instead. Returns TRUE if the stack parameters could be
  346. determined exactly, FALSE if incremental refinement is necessary. */
  347. static kmp_int32 __kmp_set_stack_info(int gtid, kmp_info_t *th) {
  348. int stack_data;
  349. #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
  350. KMP_OS_HURD
  351. pthread_attr_t attr;
  352. int status;
  353. size_t size = 0;
  354. void *addr = 0;
  355. /* Always do incremental stack refinement for ubermaster threads since the
  356. initial thread stack range can be reduced by sibling thread creation so
  357. pthread_attr_getstack may cause thread gtid aliasing */
  358. if (!KMP_UBER_GTID(gtid)) {
  359. /* Fetch the real thread attributes */
  360. status = pthread_attr_init(&attr);
  361. KMP_CHECK_SYSFAIL("pthread_attr_init", status);
  362. #if KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD
  363. status = pthread_attr_get_np(pthread_self(), &attr);
  364. KMP_CHECK_SYSFAIL("pthread_attr_get_np", status);
  365. #else
  366. status = pthread_getattr_np(pthread_self(), &attr);
  367. KMP_CHECK_SYSFAIL("pthread_getattr_np", status);
  368. #endif
  369. status = pthread_attr_getstack(&attr, &addr, &size);
  370. KMP_CHECK_SYSFAIL("pthread_attr_getstack", status);
  371. KA_TRACE(60,
  372. ("__kmp_set_stack_info: T#%d pthread_attr_getstack returned size:"
  373. " %lu, low addr: %p\n",
  374. gtid, size, addr));
  375. status = pthread_attr_destroy(&attr);
  376. KMP_CHECK_SYSFAIL("pthread_attr_destroy", status);
  377. }
  378. if (size != 0 && addr != 0) { // was stack parameter determination successful?
  379. /* Store the correct base and size */
  380. TCW_PTR(th->th.th_info.ds.ds_stackbase, (((char *)addr) + size));
  381. TCW_PTR(th->th.th_info.ds.ds_stacksize, size);
  382. TCW_4(th->th.th_info.ds.ds_stackgrow, FALSE);
  383. return TRUE;
  384. }
  385. #endif /* KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD \
  386. || KMP_OS_HURD */
  387. /* Use incremental refinement starting from initial conservative estimate */
  388. TCW_PTR(th->th.th_info.ds.ds_stacksize, 0);
  389. TCW_PTR(th->th.th_info.ds.ds_stackbase, &stack_data);
  390. TCW_4(th->th.th_info.ds.ds_stackgrow, TRUE);
  391. return FALSE;
  392. }
  393. static void *__kmp_launch_worker(void *thr) {
  394. int status, old_type, old_state;
  395. #ifdef KMP_BLOCK_SIGNALS
  396. sigset_t new_set, old_set;
  397. #endif /* KMP_BLOCK_SIGNALS */
  398. void *exit_val;
  399. #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
  400. KMP_OS_OPENBSD || KMP_OS_HURD
  401. void *volatile padding = 0;
  402. #endif
  403. int gtid;
  404. gtid = ((kmp_info_t *)thr)->th.th_info.ds.ds_gtid;
  405. __kmp_gtid_set_specific(gtid);
  406. #ifdef KMP_TDATA_GTID
  407. __kmp_gtid = gtid;
  408. #endif
  409. #if KMP_STATS_ENABLED
  410. // set thread local index to point to thread-specific stats
  411. __kmp_stats_thread_ptr = ((kmp_info_t *)thr)->th.th_stats;
  412. __kmp_stats_thread_ptr->startLife();
  413. KMP_SET_THREAD_STATE(IDLE);
  414. KMP_INIT_PARTITIONED_TIMERS(OMP_idle);
  415. #endif
  416. #if USE_ITT_BUILD
  417. __kmp_itt_thread_name(gtid);
  418. #endif /* USE_ITT_BUILD */
  419. #if KMP_AFFINITY_SUPPORTED
  420. __kmp_affinity_set_init_mask(gtid, FALSE);
  421. #endif
  422. #ifdef KMP_CANCEL_THREADS
  423. status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
  424. KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
  425. // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
  426. status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
  427. KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
  428. #endif
  429. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  430. // Set FP control regs to be a copy of the parallel initialization thread's.
  431. __kmp_clear_x87_fpu_status_word();
  432. __kmp_load_x87_fpu_control_word(&__kmp_init_x87_fpu_control_word);
  433. __kmp_load_mxcsr(&__kmp_init_mxcsr);
  434. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  435. #ifdef KMP_BLOCK_SIGNALS
  436. status = sigfillset(&new_set);
  437. KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
  438. status = pthread_sigmask(SIG_BLOCK, &new_set, &old_set);
  439. KMP_CHECK_SYSFAIL("pthread_sigmask", status);
  440. #endif /* KMP_BLOCK_SIGNALS */
  441. #if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
  442. KMP_OS_OPENBSD
  443. if (__kmp_stkoffset > 0 && gtid > 0) {
  444. padding = KMP_ALLOCA(gtid * __kmp_stkoffset);
  445. (void)padding;
  446. }
  447. #endif
  448. KMP_MB();
  449. __kmp_set_stack_info(gtid, (kmp_info_t *)thr);
  450. __kmp_check_stack_overlap((kmp_info_t *)thr);
  451. exit_val = __kmp_launch_thread((kmp_info_t *)thr);
  452. #ifdef KMP_BLOCK_SIGNALS
  453. status = pthread_sigmask(SIG_SETMASK, &old_set, NULL);
  454. KMP_CHECK_SYSFAIL("pthread_sigmask", status);
  455. #endif /* KMP_BLOCK_SIGNALS */
  456. return exit_val;
  457. }
  458. #if KMP_USE_MONITOR
  459. /* The monitor thread controls all of the threads in the complex */
  460. static void *__kmp_launch_monitor(void *thr) {
  461. int status, old_type, old_state;
  462. #ifdef KMP_BLOCK_SIGNALS
  463. sigset_t new_set;
  464. #endif /* KMP_BLOCK_SIGNALS */
  465. struct timespec interval;
  466. KMP_MB(); /* Flush all pending memory write invalidates. */
  467. KA_TRACE(10, ("__kmp_launch_monitor: #1 launched\n"));
  468. /* register us as the monitor thread */
  469. __kmp_gtid_set_specific(KMP_GTID_MONITOR);
  470. #ifdef KMP_TDATA_GTID
  471. __kmp_gtid = KMP_GTID_MONITOR;
  472. #endif
  473. KMP_MB();
  474. #if USE_ITT_BUILD
  475. // Instruct Intel(R) Threading Tools to ignore monitor thread.
  476. __kmp_itt_thread_ignore();
  477. #endif /* USE_ITT_BUILD */
  478. __kmp_set_stack_info(((kmp_info_t *)thr)->th.th_info.ds.ds_gtid,
  479. (kmp_info_t *)thr);
  480. __kmp_check_stack_overlap((kmp_info_t *)thr);
  481. #ifdef KMP_CANCEL_THREADS
  482. status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
  483. KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
  484. // josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
  485. status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_state);
  486. KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
  487. #endif
  488. #if KMP_REAL_TIME_FIX
  489. // This is a potential fix which allows application with real-time scheduling
  490. // policy work. However, decision about the fix is not made yet, so it is
  491. // disabled by default.
  492. { // Are program started with real-time scheduling policy?
  493. int sched = sched_getscheduler(0);
  494. if (sched == SCHED_FIFO || sched == SCHED_RR) {
  495. // Yes, we are a part of real-time application. Try to increase the
  496. // priority of the monitor.
  497. struct sched_param param;
  498. int max_priority = sched_get_priority_max(sched);
  499. int rc;
  500. KMP_WARNING(RealTimeSchedNotSupported);
  501. sched_getparam(0, &param);
  502. if (param.sched_priority < max_priority) {
  503. param.sched_priority += 1;
  504. rc = sched_setscheduler(0, sched, &param);
  505. if (rc != 0) {
  506. int error = errno;
  507. kmp_msg_t err_code = KMP_ERR(error);
  508. __kmp_msg(kmp_ms_warning, KMP_MSG(CantChangeMonitorPriority),
  509. err_code, KMP_MSG(MonitorWillStarve), __kmp_msg_null);
  510. if (__kmp_generate_warnings == kmp_warnings_off) {
  511. __kmp_str_free(&err_code.str);
  512. }
  513. }
  514. } else {
  515. // We cannot abort here, because number of CPUs may be enough for all
  516. // the threads, including the monitor thread, so application could
  517. // potentially work...
  518. __kmp_msg(kmp_ms_warning, KMP_MSG(RunningAtMaxPriority),
  519. KMP_MSG(MonitorWillStarve), KMP_HNT(RunningAtMaxPriority),
  520. __kmp_msg_null);
  521. }
  522. }
  523. // AC: free thread that waits for monitor started
  524. TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
  525. }
  526. #endif // KMP_REAL_TIME_FIX
  527. KMP_MB(); /* Flush all pending memory write invalidates. */
  528. if (__kmp_monitor_wakeups == 1) {
  529. interval.tv_sec = 1;
  530. interval.tv_nsec = 0;
  531. } else {
  532. interval.tv_sec = 0;
  533. interval.tv_nsec = (KMP_NSEC_PER_SEC / __kmp_monitor_wakeups);
  534. }
  535. KA_TRACE(10, ("__kmp_launch_monitor: #2 monitor\n"));
  536. while (!TCR_4(__kmp_global.g.g_done)) {
  537. struct timespec now;
  538. struct timeval tval;
  539. /* This thread monitors the state of the system */
  540. KA_TRACE(15, ("__kmp_launch_monitor: update\n"));
  541. status = gettimeofday(&tval, NULL);
  542. KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
  543. TIMEVAL_TO_TIMESPEC(&tval, &now);
  544. now.tv_sec += interval.tv_sec;
  545. now.tv_nsec += interval.tv_nsec;
  546. if (now.tv_nsec >= KMP_NSEC_PER_SEC) {
  547. now.tv_sec += 1;
  548. now.tv_nsec -= KMP_NSEC_PER_SEC;
  549. }
  550. status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
  551. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  552. // AC: the monitor should not fall asleep if g_done has been set
  553. if (!TCR_4(__kmp_global.g.g_done)) { // check once more under mutex
  554. status = pthread_cond_timedwait(&__kmp_wait_cv.c_cond,
  555. &__kmp_wait_mx.m_mutex, &now);
  556. if (status != 0) {
  557. if (status != ETIMEDOUT && status != EINTR) {
  558. KMP_SYSFAIL("pthread_cond_timedwait", status);
  559. }
  560. }
  561. }
  562. status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
  563. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  564. TCW_4(__kmp_global.g.g_time.dt.t_value,
  565. TCR_4(__kmp_global.g.g_time.dt.t_value) + 1);
  566. KMP_MB(); /* Flush all pending memory write invalidates. */
  567. }
  568. KA_TRACE(10, ("__kmp_launch_monitor: #3 cleanup\n"));
  569. #ifdef KMP_BLOCK_SIGNALS
  570. status = sigfillset(&new_set);
  571. KMP_CHECK_SYSFAIL_ERRNO("sigfillset", status);
  572. status = pthread_sigmask(SIG_UNBLOCK, &new_set, NULL);
  573. KMP_CHECK_SYSFAIL("pthread_sigmask", status);
  574. #endif /* KMP_BLOCK_SIGNALS */
  575. KA_TRACE(10, ("__kmp_launch_monitor: #4 finished\n"));
  576. if (__kmp_global.g.g_abort != 0) {
  577. /* now we need to terminate the worker threads */
  578. /* the value of t_abort is the signal we caught */
  579. int gtid;
  580. KA_TRACE(10, ("__kmp_launch_monitor: #5 terminate sig=%d\n",
  581. __kmp_global.g.g_abort));
  582. /* terminate the OpenMP worker threads */
  583. /* TODO this is not valid for sibling threads!!
  584. * the uber master might not be 0 anymore.. */
  585. for (gtid = 1; gtid < __kmp_threads_capacity; ++gtid)
  586. __kmp_terminate_thread(gtid);
  587. __kmp_cleanup();
  588. KA_TRACE(10, ("__kmp_launch_monitor: #6 raise sig=%d\n",
  589. __kmp_global.g.g_abort));
  590. if (__kmp_global.g.g_abort > 0)
  591. raise(__kmp_global.g.g_abort);
  592. }
  593. KA_TRACE(10, ("__kmp_launch_monitor: #7 exit\n"));
  594. return thr;
  595. }
  596. #endif // KMP_USE_MONITOR
  597. void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
  598. pthread_t handle;
  599. pthread_attr_t thread_attr;
  600. int status;
  601. th->th.th_info.ds.ds_gtid = gtid;
  602. #if KMP_STATS_ENABLED
  603. // sets up worker thread stats
  604. __kmp_acquire_tas_lock(&__kmp_stats_lock, gtid);
  605. // th->th.th_stats is used to transfer thread-specific stats-pointer to
  606. // __kmp_launch_worker. So when thread is created (goes into
  607. // __kmp_launch_worker) it will set its thread local pointer to
  608. // th->th.th_stats
  609. if (!KMP_UBER_GTID(gtid)) {
  610. th->th.th_stats = __kmp_stats_list->push_back(gtid);
  611. } else {
  612. // For root threads, __kmp_stats_thread_ptr is set in __kmp_register_root(),
  613. // so set the th->th.th_stats field to it.
  614. th->th.th_stats = __kmp_stats_thread_ptr;
  615. }
  616. __kmp_release_tas_lock(&__kmp_stats_lock, gtid);
  617. #endif // KMP_STATS_ENABLED
  618. if (KMP_UBER_GTID(gtid)) {
  619. KA_TRACE(10, ("__kmp_create_worker: uber thread (%d)\n", gtid));
  620. th->th.th_info.ds.ds_thread = pthread_self();
  621. __kmp_set_stack_info(gtid, th);
  622. __kmp_check_stack_overlap(th);
  623. return;
  624. }
  625. KA_TRACE(10, ("__kmp_create_worker: try to create thread (%d)\n", gtid));
  626. KMP_MB(); /* Flush all pending memory write invalidates. */
  627. #ifdef KMP_THREAD_ATTR
  628. status = pthread_attr_init(&thread_attr);
  629. if (status != 0) {
  630. __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
  631. }
  632. status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
  633. if (status != 0) {
  634. __kmp_fatal(KMP_MSG(CantSetWorkerState), KMP_ERR(status), __kmp_msg_null);
  635. }
  636. /* Set stack size for this thread now.
  637. The multiple of 2 is there because on some machines, requesting an unusual
  638. stacksize causes the thread to have an offset before the dummy alloca()
  639. takes place to create the offset. Since we want the user to have a
  640. sufficient stacksize AND support a stack offset, we alloca() twice the
  641. offset so that the upcoming alloca() does not eliminate any premade offset,
  642. and also gives the user the stack space they requested for all threads */
  643. stack_size += gtid * __kmp_stkoffset * 2;
  644. #if defined(__ANDROID__) && __ANDROID_API__ < 19
  645. // Round the stack size to a multiple of the page size. Older versions of
  646. // Android (until KitKat) would fail pthread_attr_setstacksize with EINVAL
  647. // if the stack size was not a multiple of the page size.
  648. stack_size = (stack_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
  649. #endif
  650. KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
  651. "__kmp_stksize = %lu bytes, final stacksize = %lu bytes\n",
  652. gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
  653. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  654. status = pthread_attr_setstacksize(&thread_attr, stack_size);
  655. #ifdef KMP_BACKUP_STKSIZE
  656. if (status != 0) {
  657. if (!__kmp_env_stksize) {
  658. stack_size = KMP_BACKUP_STKSIZE + gtid * __kmp_stkoffset;
  659. __kmp_stksize = KMP_BACKUP_STKSIZE;
  660. KA_TRACE(10, ("__kmp_create_worker: T#%d, default stacksize = %lu bytes, "
  661. "__kmp_stksize = %lu bytes, (backup) final stacksize = %lu "
  662. "bytes\n",
  663. gtid, KMP_DEFAULT_STKSIZE, __kmp_stksize, stack_size));
  664. status = pthread_attr_setstacksize(&thread_attr, stack_size);
  665. }
  666. }
  667. #endif /* KMP_BACKUP_STKSIZE */
  668. if (status != 0) {
  669. __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
  670. KMP_HNT(ChangeWorkerStackSize), __kmp_msg_null);
  671. }
  672. #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
  673. #endif /* KMP_THREAD_ATTR */
  674. status =
  675. pthread_create(&handle, &thread_attr, __kmp_launch_worker, (void *)th);
  676. if (status != 0 || !handle) { // ??? Why do we check handle??
  677. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  678. if (status == EINVAL) {
  679. __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
  680. KMP_HNT(IncreaseWorkerStackSize), __kmp_msg_null);
  681. }
  682. if (status == ENOMEM) {
  683. __kmp_fatal(KMP_MSG(CantSetWorkerStackSize, stack_size), KMP_ERR(status),
  684. KMP_HNT(DecreaseWorkerStackSize), __kmp_msg_null);
  685. }
  686. #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
  687. if (status == EAGAIN) {
  688. __kmp_fatal(KMP_MSG(NoResourcesForWorkerThread), KMP_ERR(status),
  689. KMP_HNT(Decrease_NUM_THREADS), __kmp_msg_null);
  690. }
  691. KMP_SYSFAIL("pthread_create", status);
  692. }
  693. th->th.th_info.ds.ds_thread = handle;
  694. #ifdef KMP_THREAD_ATTR
  695. status = pthread_attr_destroy(&thread_attr);
  696. if (status) {
  697. kmp_msg_t err_code = KMP_ERR(status);
  698. __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
  699. __kmp_msg_null);
  700. if (__kmp_generate_warnings == kmp_warnings_off) {
  701. __kmp_str_free(&err_code.str);
  702. }
  703. }
  704. #endif /* KMP_THREAD_ATTR */
  705. KMP_MB(); /* Flush all pending memory write invalidates. */
  706. KA_TRACE(10, ("__kmp_create_worker: done creating thread (%d)\n", gtid));
  707. } // __kmp_create_worker
  708. #if KMP_USE_MONITOR
  709. void __kmp_create_monitor(kmp_info_t *th) {
  710. pthread_t handle;
  711. pthread_attr_t thread_attr;
  712. size_t size;
  713. int status;
  714. int auto_adj_size = FALSE;
  715. if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME) {
  716. // We don't need monitor thread in case of MAX_BLOCKTIME
  717. KA_TRACE(10, ("__kmp_create_monitor: skipping monitor thread because of "
  718. "MAX blocktime\n"));
  719. th->th.th_info.ds.ds_tid = 0; // this makes reap_monitor no-op
  720. th->th.th_info.ds.ds_gtid = 0;
  721. return;
  722. }
  723. KA_TRACE(10, ("__kmp_create_monitor: try to create monitor\n"));
  724. KMP_MB(); /* Flush all pending memory write invalidates. */
  725. th->th.th_info.ds.ds_tid = KMP_GTID_MONITOR;
  726. th->th.th_info.ds.ds_gtid = KMP_GTID_MONITOR;
  727. #if KMP_REAL_TIME_FIX
  728. TCW_4(__kmp_global.g.g_time.dt.t_value,
  729. -1); // Will use it for synchronization a bit later.
  730. #else
  731. TCW_4(__kmp_global.g.g_time.dt.t_value, 0);
  732. #endif // KMP_REAL_TIME_FIX
  733. #ifdef KMP_THREAD_ATTR
  734. if (__kmp_monitor_stksize == 0) {
  735. __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
  736. auto_adj_size = TRUE;
  737. }
  738. status = pthread_attr_init(&thread_attr);
  739. if (status != 0) {
  740. __kmp_fatal(KMP_MSG(CantInitThreadAttrs), KMP_ERR(status), __kmp_msg_null);
  741. }
  742. status = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
  743. if (status != 0) {
  744. __kmp_fatal(KMP_MSG(CantSetMonitorState), KMP_ERR(status), __kmp_msg_null);
  745. }
  746. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  747. status = pthread_attr_getstacksize(&thread_attr, &size);
  748. KMP_CHECK_SYSFAIL("pthread_attr_getstacksize", status);
  749. #else
  750. size = __kmp_sys_min_stksize;
  751. #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
  752. #endif /* KMP_THREAD_ATTR */
  753. if (__kmp_monitor_stksize == 0) {
  754. __kmp_monitor_stksize = KMP_DEFAULT_MONITOR_STKSIZE;
  755. }
  756. if (__kmp_monitor_stksize < __kmp_sys_min_stksize) {
  757. __kmp_monitor_stksize = __kmp_sys_min_stksize;
  758. }
  759. KA_TRACE(10, ("__kmp_create_monitor: default stacksize = %lu bytes,"
  760. "requested stacksize = %lu bytes\n",
  761. size, __kmp_monitor_stksize));
  762. retry:
  763. /* Set stack size for this thread now. */
  764. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  765. KA_TRACE(10, ("__kmp_create_monitor: setting stacksize = %lu bytes,",
  766. __kmp_monitor_stksize));
  767. status = pthread_attr_setstacksize(&thread_attr, __kmp_monitor_stksize);
  768. if (status != 0) {
  769. if (auto_adj_size) {
  770. __kmp_monitor_stksize *= 2;
  771. goto retry;
  772. }
  773. kmp_msg_t err_code = KMP_ERR(status);
  774. __kmp_msg(kmp_ms_warning, // should this be fatal? BB
  775. KMP_MSG(CantSetMonitorStackSize, (long int)__kmp_monitor_stksize),
  776. err_code, KMP_HNT(ChangeMonitorStackSize), __kmp_msg_null);
  777. if (__kmp_generate_warnings == kmp_warnings_off) {
  778. __kmp_str_free(&err_code.str);
  779. }
  780. }
  781. #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
  782. status =
  783. pthread_create(&handle, &thread_attr, __kmp_launch_monitor, (void *)th);
  784. if (status != 0) {
  785. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  786. if (status == EINVAL) {
  787. if (auto_adj_size && (__kmp_monitor_stksize < (size_t)0x40000000)) {
  788. __kmp_monitor_stksize *= 2;
  789. goto retry;
  790. }
  791. __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
  792. KMP_ERR(status), KMP_HNT(IncreaseMonitorStackSize),
  793. __kmp_msg_null);
  794. }
  795. if (status == ENOMEM) {
  796. __kmp_fatal(KMP_MSG(CantSetMonitorStackSize, __kmp_monitor_stksize),
  797. KMP_ERR(status), KMP_HNT(DecreaseMonitorStackSize),
  798. __kmp_msg_null);
  799. }
  800. #endif /* _POSIX_THREAD_ATTR_STACKSIZE */
  801. if (status == EAGAIN) {
  802. __kmp_fatal(KMP_MSG(NoResourcesForMonitorThread), KMP_ERR(status),
  803. KMP_HNT(DecreaseNumberOfThreadsInUse), __kmp_msg_null);
  804. }
  805. KMP_SYSFAIL("pthread_create", status);
  806. }
  807. th->th.th_info.ds.ds_thread = handle;
  808. #if KMP_REAL_TIME_FIX
  809. // Wait for the monitor thread is really started and set its *priority*.
  810. KMP_DEBUG_ASSERT(sizeof(kmp_uint32) ==
  811. sizeof(__kmp_global.g.g_time.dt.t_value));
  812. __kmp_wait_4((kmp_uint32 volatile *)&__kmp_global.g.g_time.dt.t_value, -1,
  813. &__kmp_neq_4, NULL);
  814. #endif // KMP_REAL_TIME_FIX
  815. #ifdef KMP_THREAD_ATTR
  816. status = pthread_attr_destroy(&thread_attr);
  817. if (status != 0) {
  818. kmp_msg_t err_code = KMP_ERR(status);
  819. __kmp_msg(kmp_ms_warning, KMP_MSG(CantDestroyThreadAttrs), err_code,
  820. __kmp_msg_null);
  821. if (__kmp_generate_warnings == kmp_warnings_off) {
  822. __kmp_str_free(&err_code.str);
  823. }
  824. }
  825. #endif
  826. KMP_MB(); /* Flush all pending memory write invalidates. */
  827. KA_TRACE(10, ("__kmp_create_monitor: monitor created %#.8lx\n",
  828. th->th.th_info.ds.ds_thread));
  829. } // __kmp_create_monitor
  830. #endif // KMP_USE_MONITOR
  831. void __kmp_exit_thread(int exit_status) {
  832. pthread_exit((void *)(intptr_t)exit_status);
  833. } // __kmp_exit_thread
  834. #if KMP_USE_MONITOR
  835. void __kmp_resume_monitor();
  836. void __kmp_reap_monitor(kmp_info_t *th) {
  837. int status;
  838. void *exit_val;
  839. KA_TRACE(10, ("__kmp_reap_monitor: try to reap monitor thread with handle"
  840. " %#.8lx\n",
  841. th->th.th_info.ds.ds_thread));
  842. // If monitor has been created, its tid and gtid should be KMP_GTID_MONITOR.
  843. // If both tid and gtid are 0, it means the monitor did not ever start.
  844. // If both tid and gtid are KMP_GTID_DNE, the monitor has been shut down.
  845. KMP_DEBUG_ASSERT(th->th.th_info.ds.ds_tid == th->th.th_info.ds.ds_gtid);
  846. if (th->th.th_info.ds.ds_gtid != KMP_GTID_MONITOR) {
  847. KA_TRACE(10, ("__kmp_reap_monitor: monitor did not start, returning\n"));
  848. return;
  849. }
  850. KMP_MB(); /* Flush all pending memory write invalidates. */
  851. /* First, check to see whether the monitor thread exists to wake it up. This
  852. is to avoid performance problem when the monitor sleeps during
  853. blocktime-size interval */
  854. status = pthread_kill(th->th.th_info.ds.ds_thread, 0);
  855. if (status != ESRCH) {
  856. __kmp_resume_monitor(); // Wake up the monitor thread
  857. }
  858. KA_TRACE(10, ("__kmp_reap_monitor: try to join with monitor\n"));
  859. status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
  860. if (exit_val != th) {
  861. __kmp_fatal(KMP_MSG(ReapMonitorError), KMP_ERR(status), __kmp_msg_null);
  862. }
  863. th->th.th_info.ds.ds_tid = KMP_GTID_DNE;
  864. th->th.th_info.ds.ds_gtid = KMP_GTID_DNE;
  865. KA_TRACE(10, ("__kmp_reap_monitor: done reaping monitor thread with handle"
  866. " %#.8lx\n",
  867. th->th.th_info.ds.ds_thread));
  868. KMP_MB(); /* Flush all pending memory write invalidates. */
  869. }
  870. #endif // KMP_USE_MONITOR
  871. void __kmp_reap_worker(kmp_info_t *th) {
  872. int status;
  873. void *exit_val;
  874. KMP_MB(); /* Flush all pending memory write invalidates. */
  875. KA_TRACE(
  876. 10, ("__kmp_reap_worker: try to reap T#%d\n", th->th.th_info.ds.ds_gtid));
  877. status = pthread_join(th->th.th_info.ds.ds_thread, &exit_val);
  878. #ifdef KMP_DEBUG
  879. /* Don't expose these to the user until we understand when they trigger */
  880. if (status != 0) {
  881. __kmp_fatal(KMP_MSG(ReapWorkerError), KMP_ERR(status), __kmp_msg_null);
  882. }
  883. if (exit_val != th) {
  884. KA_TRACE(10, ("__kmp_reap_worker: worker T#%d did not reap properly, "
  885. "exit_val = %p\n",
  886. th->th.th_info.ds.ds_gtid, exit_val));
  887. }
  888. #else
  889. (void)status; // unused variable
  890. #endif /* KMP_DEBUG */
  891. KA_TRACE(10, ("__kmp_reap_worker: done reaping T#%d\n",
  892. th->th.th_info.ds.ds_gtid));
  893. KMP_MB(); /* Flush all pending memory write invalidates. */
  894. }
  895. #if KMP_HANDLE_SIGNALS
  896. static void __kmp_null_handler(int signo) {
  897. // Do nothing, for doing SIG_IGN-type actions.
  898. } // __kmp_null_handler
  899. static void __kmp_team_handler(int signo) {
  900. if (__kmp_global.g.g_abort == 0) {
  901. /* Stage 1 signal handler, let's shut down all of the threads */
  902. #ifdef KMP_DEBUG
  903. __kmp_debug_printf("__kmp_team_handler: caught signal = %d\n", signo);
  904. #endif
  905. switch (signo) {
  906. case SIGHUP:
  907. case SIGINT:
  908. case SIGQUIT:
  909. case SIGILL:
  910. case SIGABRT:
  911. case SIGFPE:
  912. case SIGBUS:
  913. case SIGSEGV:
  914. #ifdef SIGSYS
  915. case SIGSYS:
  916. #endif
  917. case SIGTERM:
  918. if (__kmp_debug_buf) {
  919. __kmp_dump_debug_buffer();
  920. }
  921. __kmp_unregister_library(); // cleanup shared memory
  922. KMP_MB(); // Flush all pending memory write invalidates.
  923. TCW_4(__kmp_global.g.g_abort, signo);
  924. KMP_MB(); // Flush all pending memory write invalidates.
  925. TCW_4(__kmp_global.g.g_done, TRUE);
  926. KMP_MB(); // Flush all pending memory write invalidates.
  927. break;
  928. default:
  929. #ifdef KMP_DEBUG
  930. __kmp_debug_printf("__kmp_team_handler: unknown signal type");
  931. #endif
  932. break;
  933. }
  934. }
  935. } // __kmp_team_handler
  936. static void __kmp_sigaction(int signum, const struct sigaction *act,
  937. struct sigaction *oldact) {
  938. int rc = sigaction(signum, act, oldact);
  939. KMP_CHECK_SYSFAIL_ERRNO("sigaction", rc);
  940. }
  941. static void __kmp_install_one_handler(int sig, sig_func_t handler_func,
  942. int parallel_init) {
  943. KMP_MB(); // Flush all pending memory write invalidates.
  944. KB_TRACE(60,
  945. ("__kmp_install_one_handler( %d, ..., %d )\n", sig, parallel_init));
  946. if (parallel_init) {
  947. struct sigaction new_action;
  948. struct sigaction old_action;
  949. new_action.sa_handler = handler_func;
  950. new_action.sa_flags = 0;
  951. sigfillset(&new_action.sa_mask);
  952. __kmp_sigaction(sig, &new_action, &old_action);
  953. if (old_action.sa_handler == __kmp_sighldrs[sig].sa_handler) {
  954. sigaddset(&__kmp_sigset, sig);
  955. } else {
  956. // Restore/keep user's handler if one previously installed.
  957. __kmp_sigaction(sig, &old_action, NULL);
  958. }
  959. } else {
  960. // Save initial/system signal handlers to see if user handlers installed.
  961. __kmp_sigaction(sig, NULL, &__kmp_sighldrs[sig]);
  962. }
  963. KMP_MB(); // Flush all pending memory write invalidates.
  964. } // __kmp_install_one_handler
  965. static void __kmp_remove_one_handler(int sig) {
  966. KB_TRACE(60, ("__kmp_remove_one_handler( %d )\n", sig));
  967. if (sigismember(&__kmp_sigset, sig)) {
  968. struct sigaction old;
  969. KMP_MB(); // Flush all pending memory write invalidates.
  970. __kmp_sigaction(sig, &__kmp_sighldrs[sig], &old);
  971. if ((old.sa_handler != __kmp_team_handler) &&
  972. (old.sa_handler != __kmp_null_handler)) {
  973. // Restore the users signal handler.
  974. KB_TRACE(10, ("__kmp_remove_one_handler: oops, not our handler, "
  975. "restoring: sig=%d\n",
  976. sig));
  977. __kmp_sigaction(sig, &old, NULL);
  978. }
  979. sigdelset(&__kmp_sigset, sig);
  980. KMP_MB(); // Flush all pending memory write invalidates.
  981. }
  982. } // __kmp_remove_one_handler
  983. void __kmp_install_signals(int parallel_init) {
  984. KB_TRACE(10, ("__kmp_install_signals( %d )\n", parallel_init));
  985. if (__kmp_handle_signals || !parallel_init) {
  986. // If ! parallel_init, we do not install handlers, just save original
  987. // handlers. Let us do it even __handle_signals is 0.
  988. sigemptyset(&__kmp_sigset);
  989. __kmp_install_one_handler(SIGHUP, __kmp_team_handler, parallel_init);
  990. __kmp_install_one_handler(SIGINT, __kmp_team_handler, parallel_init);
  991. __kmp_install_one_handler(SIGQUIT, __kmp_team_handler, parallel_init);
  992. __kmp_install_one_handler(SIGILL, __kmp_team_handler, parallel_init);
  993. __kmp_install_one_handler(SIGABRT, __kmp_team_handler, parallel_init);
  994. __kmp_install_one_handler(SIGFPE, __kmp_team_handler, parallel_init);
  995. __kmp_install_one_handler(SIGBUS, __kmp_team_handler, parallel_init);
  996. __kmp_install_one_handler(SIGSEGV, __kmp_team_handler, parallel_init);
  997. #ifdef SIGSYS
  998. __kmp_install_one_handler(SIGSYS, __kmp_team_handler, parallel_init);
  999. #endif // SIGSYS
  1000. __kmp_install_one_handler(SIGTERM, __kmp_team_handler, parallel_init);
  1001. #ifdef SIGPIPE
  1002. __kmp_install_one_handler(SIGPIPE, __kmp_team_handler, parallel_init);
  1003. #endif // SIGPIPE
  1004. }
  1005. } // __kmp_install_signals
  1006. void __kmp_remove_signals(void) {
  1007. int sig;
  1008. KB_TRACE(10, ("__kmp_remove_signals()\n"));
  1009. for (sig = 1; sig < NSIG; ++sig) {
  1010. __kmp_remove_one_handler(sig);
  1011. }
  1012. } // __kmp_remove_signals
  1013. #endif // KMP_HANDLE_SIGNALS
  1014. void __kmp_enable(int new_state) {
  1015. #ifdef KMP_CANCEL_THREADS
  1016. int status, old_state;
  1017. status = pthread_setcancelstate(new_state, &old_state);
  1018. KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
  1019. KMP_DEBUG_ASSERT(old_state == PTHREAD_CANCEL_DISABLE);
  1020. #endif
  1021. }
  1022. void __kmp_disable(int *old_state) {
  1023. #ifdef KMP_CANCEL_THREADS
  1024. int status;
  1025. status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
  1026. KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
  1027. #endif
  1028. }
  1029. static void __kmp_atfork_prepare(void) {
  1030. __kmp_acquire_bootstrap_lock(&__kmp_initz_lock);
  1031. __kmp_acquire_bootstrap_lock(&__kmp_forkjoin_lock);
  1032. }
  1033. static void __kmp_atfork_parent(void) {
  1034. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  1035. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  1036. }
  1037. /* Reset the library so execution in the child starts "all over again" with
  1038. clean data structures in initial states. Don't worry about freeing memory
  1039. allocated by parent, just abandon it to be safe. */
  1040. static void __kmp_atfork_child(void) {
  1041. __kmp_release_bootstrap_lock(&__kmp_forkjoin_lock);
  1042. __kmp_release_bootstrap_lock(&__kmp_initz_lock);
  1043. /* TODO make sure this is done right for nested/sibling */
  1044. // ATT: Memory leaks are here? TODO: Check it and fix.
  1045. /* KMP_ASSERT( 0 ); */
  1046. ++__kmp_fork_count;
  1047. #if KMP_AFFINITY_SUPPORTED
  1048. #if KMP_OS_LINUX || KMP_OS_FREEBSD
  1049. // reset the affinity in the child to the initial thread
  1050. // affinity in the parent
  1051. kmp_set_thread_affinity_mask_initial();
  1052. #endif
  1053. // Set default not to bind threads tightly in the child (we're expecting
  1054. // over-subscription after the fork and this can improve things for
  1055. // scripting languages that use OpenMP inside process-parallel code).
  1056. __kmp_affinity_type = affinity_none;
  1057. if (__kmp_nested_proc_bind.bind_types != NULL) {
  1058. __kmp_nested_proc_bind.bind_types[0] = proc_bind_false;
  1059. }
  1060. __kmp_affinity_masks = NULL;
  1061. __kmp_affinity_num_masks = 0;
  1062. #endif // KMP_AFFINITY_SUPPORTED
  1063. #if KMP_USE_MONITOR
  1064. __kmp_init_monitor = 0;
  1065. #endif
  1066. __kmp_init_parallel = FALSE;
  1067. __kmp_init_middle = FALSE;
  1068. __kmp_init_serial = FALSE;
  1069. TCW_4(__kmp_init_gtid, FALSE);
  1070. __kmp_init_common = FALSE;
  1071. TCW_4(__kmp_init_user_locks, FALSE);
  1072. #if !KMP_USE_DYNAMIC_LOCK
  1073. __kmp_user_lock_table.used = 1;
  1074. __kmp_user_lock_table.allocated = 0;
  1075. __kmp_user_lock_table.table = NULL;
  1076. __kmp_lock_blocks = NULL;
  1077. #endif
  1078. __kmp_all_nth = 0;
  1079. TCW_4(__kmp_nth, 0);
  1080. __kmp_thread_pool = NULL;
  1081. __kmp_thread_pool_insert_pt = NULL;
  1082. __kmp_team_pool = NULL;
  1083. /* Must actually zero all the *cache arguments passed to __kmpc_threadprivate
  1084. here so threadprivate doesn't use stale data */
  1085. KA_TRACE(10, ("__kmp_atfork_child: checking cache address list %p\n",
  1086. __kmp_threadpriv_cache_list));
  1087. while (__kmp_threadpriv_cache_list != NULL) {
  1088. if (*__kmp_threadpriv_cache_list->addr != NULL) {
  1089. KC_TRACE(50, ("__kmp_atfork_child: zeroing cache at address %p\n",
  1090. &(*__kmp_threadpriv_cache_list->addr)));
  1091. *__kmp_threadpriv_cache_list->addr = NULL;
  1092. }
  1093. __kmp_threadpriv_cache_list = __kmp_threadpriv_cache_list->next;
  1094. }
  1095. __kmp_init_runtime = FALSE;
  1096. /* reset statically initialized locks */
  1097. __kmp_init_bootstrap_lock(&__kmp_initz_lock);
  1098. __kmp_init_bootstrap_lock(&__kmp_stdio_lock);
  1099. __kmp_init_bootstrap_lock(&__kmp_console_lock);
  1100. __kmp_init_bootstrap_lock(&__kmp_task_team_lock);
  1101. #if USE_ITT_BUILD
  1102. __kmp_itt_reset(); // reset ITT's global state
  1103. #endif /* USE_ITT_BUILD */
  1104. {
  1105. // Child process often get terminated without any use of OpenMP. That might
  1106. // cause mapped shared memory file to be left unattended. Thus we postpone
  1107. // library registration till middle initialization in the child process.
  1108. __kmp_need_register_serial = FALSE;
  1109. __kmp_serial_initialize();
  1110. }
  1111. /* This is necessary to make sure no stale data is left around */
  1112. /* AC: customers complain that we use unsafe routines in the atfork
  1113. handler. Mathworks: dlsym() is unsafe. We call dlsym and dlopen
  1114. in dynamic_link when check the presence of shared tbbmalloc library.
  1115. Suggestion is to make the library initialization lazier, similar
  1116. to what done for __kmpc_begin(). */
  1117. // TODO: synchronize all static initializations with regular library
  1118. // startup; look at kmp_global.cpp and etc.
  1119. //__kmp_internal_begin ();
  1120. }
  1121. void __kmp_register_atfork(void) {
  1122. if (__kmp_need_register_atfork) {
  1123. int status = pthread_atfork(__kmp_atfork_prepare, __kmp_atfork_parent,
  1124. __kmp_atfork_child);
  1125. KMP_CHECK_SYSFAIL("pthread_atfork", status);
  1126. __kmp_need_register_atfork = FALSE;
  1127. }
  1128. }
  1129. void __kmp_suspend_initialize(void) {
  1130. int status;
  1131. status = pthread_mutexattr_init(&__kmp_suspend_mutex_attr);
  1132. KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
  1133. status = pthread_condattr_init(&__kmp_suspend_cond_attr);
  1134. KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
  1135. }
  1136. void __kmp_suspend_initialize_thread(kmp_info_t *th) {
  1137. int old_value = KMP_ATOMIC_LD_RLX(&th->th.th_suspend_init_count);
  1138. int new_value = __kmp_fork_count + 1;
  1139. // Return if already initialized
  1140. if (old_value == new_value)
  1141. return;
  1142. // Wait, then return if being initialized
  1143. if (old_value == -1 || !__kmp_atomic_compare_store(
  1144. &th->th.th_suspend_init_count, old_value, -1)) {
  1145. while (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init_count) != new_value) {
  1146. KMP_CPU_PAUSE();
  1147. }
  1148. } else {
  1149. // Claim to be the initializer and do initializations
  1150. int status;
  1151. status = pthread_cond_init(&th->th.th_suspend_cv.c_cond,
  1152. &__kmp_suspend_cond_attr);
  1153. KMP_CHECK_SYSFAIL("pthread_cond_init", status);
  1154. status = pthread_mutex_init(&th->th.th_suspend_mx.m_mutex,
  1155. &__kmp_suspend_mutex_attr);
  1156. KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
  1157. KMP_ATOMIC_ST_REL(&th->th.th_suspend_init_count, new_value);
  1158. }
  1159. }
  1160. void __kmp_suspend_uninitialize_thread(kmp_info_t *th) {
  1161. if (KMP_ATOMIC_LD_ACQ(&th->th.th_suspend_init_count) > __kmp_fork_count) {
  1162. /* this means we have initialize the suspension pthread objects for this
  1163. thread in this instance of the process */
  1164. int status;
  1165. status = pthread_cond_destroy(&th->th.th_suspend_cv.c_cond);
  1166. if (status != 0 && status != EBUSY) {
  1167. KMP_SYSFAIL("pthread_cond_destroy", status);
  1168. }
  1169. status = pthread_mutex_destroy(&th->th.th_suspend_mx.m_mutex);
  1170. if (status != 0 && status != EBUSY) {
  1171. KMP_SYSFAIL("pthread_mutex_destroy", status);
  1172. }
  1173. --th->th.th_suspend_init_count;
  1174. KMP_DEBUG_ASSERT(KMP_ATOMIC_LD_RLX(&th->th.th_suspend_init_count) ==
  1175. __kmp_fork_count);
  1176. }
  1177. }
  1178. // return true if lock obtained, false otherwise
  1179. int __kmp_try_suspend_mx(kmp_info_t *th) {
  1180. return (pthread_mutex_trylock(&th->th.th_suspend_mx.m_mutex) == 0);
  1181. }
  1182. void __kmp_lock_suspend_mx(kmp_info_t *th) {
  1183. int status = pthread_mutex_lock(&th->th.th_suspend_mx.m_mutex);
  1184. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  1185. }
  1186. void __kmp_unlock_suspend_mx(kmp_info_t *th) {
  1187. int status = pthread_mutex_unlock(&th->th.th_suspend_mx.m_mutex);
  1188. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  1189. }
  1190. /* This routine puts the calling thread to sleep after setting the
  1191. sleep bit for the indicated flag variable to true. */
  1192. template <class C>
  1193. static inline void __kmp_suspend_template(int th_gtid, C *flag) {
  1194. KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_suspend);
  1195. kmp_info_t *th = __kmp_threads[th_gtid];
  1196. int status;
  1197. typename C::flag_t old_spin;
  1198. KF_TRACE(30, ("__kmp_suspend_template: T#%d enter for flag = %p\n", th_gtid,
  1199. flag->get()));
  1200. __kmp_suspend_initialize_thread(th);
  1201. __kmp_lock_suspend_mx(th);
  1202. KF_TRACE(10, ("__kmp_suspend_template: T#%d setting sleep bit for spin(%p)\n",
  1203. th_gtid, flag->get()));
  1204. /* TODO: shouldn't this use release semantics to ensure that
  1205. __kmp_suspend_initialize_thread gets called first? */
  1206. old_spin = flag->set_sleeping();
  1207. TCW_PTR(th->th.th_sleep_loc, (void *)flag);
  1208. th->th.th_sleep_loc_type = flag->get_type();
  1209. if (__kmp_dflt_blocktime == KMP_MAX_BLOCKTIME &&
  1210. __kmp_pause_status != kmp_soft_paused) {
  1211. flag->unset_sleeping();
  1212. TCW_PTR(th->th.th_sleep_loc, NULL);
  1213. th->th.th_sleep_loc_type = flag_unset;
  1214. __kmp_unlock_suspend_mx(th);
  1215. return;
  1216. }
  1217. KF_TRACE(5, ("__kmp_suspend_template: T#%d set sleep bit for spin(%p)==%x,"
  1218. " was %x\n",
  1219. th_gtid, flag->get(), flag->load(), old_spin));
  1220. if (flag->done_check_val(old_spin) || flag->done_check()) {
  1221. flag->unset_sleeping();
  1222. TCW_PTR(th->th.th_sleep_loc, NULL);
  1223. th->th.th_sleep_loc_type = flag_unset;
  1224. KF_TRACE(5, ("__kmp_suspend_template: T#%d false alarm, reset sleep bit "
  1225. "for spin(%p)\n",
  1226. th_gtid, flag->get()));
  1227. } else {
  1228. /* Encapsulate in a loop as the documentation states that this may
  1229. "with low probability" return when the condition variable has
  1230. not been signaled or broadcast */
  1231. int deactivated = FALSE;
  1232. while (flag->is_sleeping()) {
  1233. #ifdef DEBUG_SUSPEND
  1234. char buffer[128];
  1235. __kmp_suspend_count++;
  1236. __kmp_print_cond(buffer, &th->th.th_suspend_cv);
  1237. __kmp_printf("__kmp_suspend_template: suspending T#%d: %s\n", th_gtid,
  1238. buffer);
  1239. #endif
  1240. // Mark the thread as no longer active (only in the first iteration of the
  1241. // loop).
  1242. if (!deactivated) {
  1243. th->th.th_active = FALSE;
  1244. if (th->th.th_active_in_pool) {
  1245. th->th.th_active_in_pool = FALSE;
  1246. KMP_ATOMIC_DEC(&__kmp_thread_pool_active_nth);
  1247. KMP_DEBUG_ASSERT(TCR_4(__kmp_thread_pool_active_nth) >= 0);
  1248. }
  1249. deactivated = TRUE;
  1250. }
  1251. KMP_DEBUG_ASSERT(th->th.th_sleep_loc);
  1252. KMP_DEBUG_ASSERT(flag->get_type() == th->th.th_sleep_loc_type);
  1253. #if USE_SUSPEND_TIMEOUT
  1254. struct timespec now;
  1255. struct timeval tval;
  1256. int msecs;
  1257. status = gettimeofday(&tval, NULL);
  1258. KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
  1259. TIMEVAL_TO_TIMESPEC(&tval, &now);
  1260. msecs = (4 * __kmp_dflt_blocktime) + 200;
  1261. now.tv_sec += msecs / 1000;
  1262. now.tv_nsec += (msecs % 1000) * 1000;
  1263. KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform "
  1264. "pthread_cond_timedwait\n",
  1265. th_gtid));
  1266. status = pthread_cond_timedwait(&th->th.th_suspend_cv.c_cond,
  1267. &th->th.th_suspend_mx.m_mutex, &now);
  1268. #else
  1269. KF_TRACE(15, ("__kmp_suspend_template: T#%d about to perform"
  1270. " pthread_cond_wait\n",
  1271. th_gtid));
  1272. status = pthread_cond_wait(&th->th.th_suspend_cv.c_cond,
  1273. &th->th.th_suspend_mx.m_mutex);
  1274. #endif // USE_SUSPEND_TIMEOUT
  1275. if ((status != 0) && (status != EINTR) && (status != ETIMEDOUT)) {
  1276. KMP_SYSFAIL("pthread_cond_wait", status);
  1277. }
  1278. KMP_DEBUG_ASSERT(flag->get_type() == flag->get_ptr_type());
  1279. if (!flag->is_sleeping() &&
  1280. ((status == EINTR) || (status == ETIMEDOUT))) {
  1281. // if interrupt or timeout, and thread is no longer sleeping, we need to
  1282. // make sure sleep_loc gets reset; however, this shouldn't be needed if
  1283. // we woke up with resume
  1284. flag->unset_sleeping();
  1285. TCW_PTR(th->th.th_sleep_loc, NULL);
  1286. th->th.th_sleep_loc_type = flag_unset;
  1287. }
  1288. #ifdef KMP_DEBUG
  1289. if (status == ETIMEDOUT) {
  1290. if (flag->is_sleeping()) {
  1291. KF_TRACE(100,
  1292. ("__kmp_suspend_template: T#%d timeout wakeup\n", th_gtid));
  1293. } else {
  1294. KF_TRACE(2, ("__kmp_suspend_template: T#%d timeout wakeup, sleep bit "
  1295. "not set!\n",
  1296. th_gtid));
  1297. TCW_PTR(th->th.th_sleep_loc, NULL);
  1298. th->th.th_sleep_loc_type = flag_unset;
  1299. }
  1300. } else if (flag->is_sleeping()) {
  1301. KF_TRACE(100,
  1302. ("__kmp_suspend_template: T#%d spurious wakeup\n", th_gtid));
  1303. }
  1304. #endif
  1305. } // while
  1306. // Mark the thread as active again (if it was previous marked as inactive)
  1307. if (deactivated) {
  1308. th->th.th_active = TRUE;
  1309. if (TCR_4(th->th.th_in_pool)) {
  1310. KMP_ATOMIC_INC(&__kmp_thread_pool_active_nth);
  1311. th->th.th_active_in_pool = TRUE;
  1312. }
  1313. }
  1314. }
  1315. // We may have had the loop variable set before entering the loop body;
  1316. // so we need to reset sleep_loc.
  1317. TCW_PTR(th->th.th_sleep_loc, NULL);
  1318. th->th.th_sleep_loc_type = flag_unset;
  1319. KMP_DEBUG_ASSERT(!flag->is_sleeping());
  1320. KMP_DEBUG_ASSERT(!th->th.th_sleep_loc);
  1321. #ifdef DEBUG_SUSPEND
  1322. {
  1323. char buffer[128];
  1324. __kmp_print_cond(buffer, &th->th.th_suspend_cv);
  1325. __kmp_printf("__kmp_suspend_template: T#%d has awakened: %s\n", th_gtid,
  1326. buffer);
  1327. }
  1328. #endif
  1329. __kmp_unlock_suspend_mx(th);
  1330. KF_TRACE(30, ("__kmp_suspend_template: T#%d exit\n", th_gtid));
  1331. }
  1332. template <bool C, bool S>
  1333. void __kmp_suspend_32(int th_gtid, kmp_flag_32<C, S> *flag) {
  1334. __kmp_suspend_template(th_gtid, flag);
  1335. }
  1336. template <bool C, bool S>
  1337. void __kmp_suspend_64(int th_gtid, kmp_flag_64<C, S> *flag) {
  1338. __kmp_suspend_template(th_gtid, flag);
  1339. }
  1340. template <bool C, bool S>
  1341. void __kmp_atomic_suspend_64(int th_gtid, kmp_atomic_flag_64<C, S> *flag) {
  1342. __kmp_suspend_template(th_gtid, flag);
  1343. }
  1344. void __kmp_suspend_oncore(int th_gtid, kmp_flag_oncore *flag) {
  1345. __kmp_suspend_template(th_gtid, flag);
  1346. }
  1347. template void __kmp_suspend_32<false, false>(int, kmp_flag_32<false, false> *);
  1348. template void __kmp_suspend_64<false, true>(int, kmp_flag_64<false, true> *);
  1349. template void __kmp_suspend_64<true, false>(int, kmp_flag_64<true, false> *);
  1350. template void
  1351. __kmp_atomic_suspend_64<false, true>(int, kmp_atomic_flag_64<false, true> *);
  1352. template void
  1353. __kmp_atomic_suspend_64<true, false>(int, kmp_atomic_flag_64<true, false> *);
  1354. /* This routine signals the thread specified by target_gtid to wake up
  1355. after setting the sleep bit indicated by the flag argument to FALSE.
  1356. The target thread must already have called __kmp_suspend_template() */
  1357. template <class C>
  1358. static inline void __kmp_resume_template(int target_gtid, C *flag) {
  1359. KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
  1360. kmp_info_t *th = __kmp_threads[target_gtid];
  1361. int status;
  1362. #ifdef KMP_DEBUG
  1363. int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
  1364. #endif
  1365. KF_TRACE(30, ("__kmp_resume_template: T#%d wants to wakeup T#%d enter\n",
  1366. gtid, target_gtid));
  1367. KMP_DEBUG_ASSERT(gtid != target_gtid);
  1368. __kmp_suspend_initialize_thread(th);
  1369. __kmp_lock_suspend_mx(th);
  1370. if (!flag || flag != th->th.th_sleep_loc) {
  1371. // coming from __kmp_null_resume_wrapper, or thread is now sleeping on a
  1372. // different location; wake up at new location
  1373. flag = (C *)CCAST(void *, th->th.th_sleep_loc);
  1374. }
  1375. // First, check if the flag is null or its type has changed. If so, someone
  1376. // else woke it up.
  1377. if (!flag) { // Thread doesn't appear to be sleeping on anything
  1378. KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
  1379. "awake: flag(%p)\n",
  1380. gtid, target_gtid, (void *)NULL));
  1381. __kmp_unlock_suspend_mx(th);
  1382. return;
  1383. } else if (flag->get_type() != th->th.th_sleep_loc_type) {
  1384. // Flag type does not appear to match this function template; possibly the
  1385. // thread is sleeping on something else. Try null resume again.
  1386. KF_TRACE(
  1387. 5,
  1388. ("__kmp_resume_template: T#%d retrying, thread T#%d Mismatch flag(%p), "
  1389. "spin(%p) type=%d ptr_type=%d\n",
  1390. gtid, target_gtid, flag, flag->get(), flag->get_type(),
  1391. th->th.th_sleep_loc_type));
  1392. __kmp_unlock_suspend_mx(th);
  1393. __kmp_null_resume_wrapper(th);
  1394. return;
  1395. } else { // if multiple threads are sleeping, flag should be internally
  1396. // referring to a specific thread here
  1397. if (!flag->is_sleeping()) {
  1398. KF_TRACE(5, ("__kmp_resume_template: T#%d exiting, thread T#%d already "
  1399. "awake: flag(%p): %u\n",
  1400. gtid, target_gtid, flag->get(), (unsigned int)flag->load()));
  1401. __kmp_unlock_suspend_mx(th);
  1402. return;
  1403. }
  1404. }
  1405. KMP_DEBUG_ASSERT(flag);
  1406. flag->unset_sleeping();
  1407. TCW_PTR(th->th.th_sleep_loc, NULL);
  1408. th->th.th_sleep_loc_type = flag_unset;
  1409. KF_TRACE(5, ("__kmp_resume_template: T#%d about to wakeup T#%d, reset "
  1410. "sleep bit for flag's loc(%p): %u\n",
  1411. gtid, target_gtid, flag->get(), (unsigned int)flag->load()));
  1412. #ifdef DEBUG_SUSPEND
  1413. {
  1414. char buffer[128];
  1415. __kmp_print_cond(buffer, &th->th.th_suspend_cv);
  1416. __kmp_printf("__kmp_resume_template: T#%d resuming T#%d: %s\n", gtid,
  1417. target_gtid, buffer);
  1418. }
  1419. #endif
  1420. status = pthread_cond_signal(&th->th.th_suspend_cv.c_cond);
  1421. KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
  1422. __kmp_unlock_suspend_mx(th);
  1423. KF_TRACE(30, ("__kmp_resume_template: T#%d exiting after signaling wake up"
  1424. " for T#%d\n",
  1425. gtid, target_gtid));
  1426. }
  1427. template <bool C, bool S>
  1428. void __kmp_resume_32(int target_gtid, kmp_flag_32<C, S> *flag) {
  1429. __kmp_resume_template(target_gtid, flag);
  1430. }
  1431. template <bool C, bool S>
  1432. void __kmp_resume_64(int target_gtid, kmp_flag_64<C, S> *flag) {
  1433. __kmp_resume_template(target_gtid, flag);
  1434. }
  1435. template <bool C, bool S>
  1436. void __kmp_atomic_resume_64(int target_gtid, kmp_atomic_flag_64<C, S> *flag) {
  1437. __kmp_resume_template(target_gtid, flag);
  1438. }
  1439. void __kmp_resume_oncore(int target_gtid, kmp_flag_oncore *flag) {
  1440. __kmp_resume_template(target_gtid, flag);
  1441. }
  1442. template void __kmp_resume_32<false, true>(int, kmp_flag_32<false, true> *);
  1443. template void __kmp_resume_32<false, false>(int, kmp_flag_32<false, false> *);
  1444. template void __kmp_resume_64<false, true>(int, kmp_flag_64<false, true> *);
  1445. template void
  1446. __kmp_atomic_resume_64<false, true>(int, kmp_atomic_flag_64<false, true> *);
  1447. #if KMP_USE_MONITOR
  1448. void __kmp_resume_monitor() {
  1449. KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(USER_resume);
  1450. int status;
  1451. #ifdef KMP_DEBUG
  1452. int gtid = TCR_4(__kmp_init_gtid) ? __kmp_get_gtid() : -1;
  1453. KF_TRACE(30, ("__kmp_resume_monitor: T#%d wants to wakeup T#%d enter\n", gtid,
  1454. KMP_GTID_MONITOR));
  1455. KMP_DEBUG_ASSERT(gtid != KMP_GTID_MONITOR);
  1456. #endif
  1457. status = pthread_mutex_lock(&__kmp_wait_mx.m_mutex);
  1458. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  1459. #ifdef DEBUG_SUSPEND
  1460. {
  1461. char buffer[128];
  1462. __kmp_print_cond(buffer, &__kmp_wait_cv.c_cond);
  1463. __kmp_printf("__kmp_resume_monitor: T#%d resuming T#%d: %s\n", gtid,
  1464. KMP_GTID_MONITOR, buffer);
  1465. }
  1466. #endif
  1467. status = pthread_cond_signal(&__kmp_wait_cv.c_cond);
  1468. KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
  1469. status = pthread_mutex_unlock(&__kmp_wait_mx.m_mutex);
  1470. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  1471. KF_TRACE(30, ("__kmp_resume_monitor: T#%d exiting after signaling wake up"
  1472. " for T#%d\n",
  1473. gtid, KMP_GTID_MONITOR));
  1474. }
  1475. #endif // KMP_USE_MONITOR
  1476. void __kmp_yield() { sched_yield(); }
  1477. void __kmp_gtid_set_specific(int gtid) {
  1478. if (__kmp_init_gtid) {
  1479. int status;
  1480. status = pthread_setspecific(__kmp_gtid_threadprivate_key,
  1481. (void *)(intptr_t)(gtid + 1));
  1482. KMP_CHECK_SYSFAIL("pthread_setspecific", status);
  1483. } else {
  1484. KA_TRACE(50, ("__kmp_gtid_set_specific: runtime shutdown, returning\n"));
  1485. }
  1486. }
  1487. int __kmp_gtid_get_specific() {
  1488. int gtid;
  1489. if (!__kmp_init_gtid) {
  1490. KA_TRACE(50, ("__kmp_gtid_get_specific: runtime shutdown, returning "
  1491. "KMP_GTID_SHUTDOWN\n"));
  1492. return KMP_GTID_SHUTDOWN;
  1493. }
  1494. gtid = (int)(size_t)pthread_getspecific(__kmp_gtid_threadprivate_key);
  1495. if (gtid == 0) {
  1496. gtid = KMP_GTID_DNE;
  1497. } else {
  1498. gtid--;
  1499. }
  1500. KA_TRACE(50, ("__kmp_gtid_get_specific: key:%d gtid:%d\n",
  1501. __kmp_gtid_threadprivate_key, gtid));
  1502. return gtid;
  1503. }
  1504. double __kmp_read_cpu_time(void) {
  1505. /*clock_t t;*/
  1506. struct tms buffer;
  1507. /*t =*/times(&buffer);
  1508. return (double)(buffer.tms_utime + buffer.tms_cutime) /
  1509. (double)CLOCKS_PER_SEC;
  1510. }
  1511. int __kmp_read_system_info(struct kmp_sys_info *info) {
  1512. int status;
  1513. struct rusage r_usage;
  1514. memset(info, 0, sizeof(*info));
  1515. status = getrusage(RUSAGE_SELF, &r_usage);
  1516. KMP_CHECK_SYSFAIL_ERRNO("getrusage", status);
  1517. // The maximum resident set size utilized (in kilobytes)
  1518. info->maxrss = r_usage.ru_maxrss;
  1519. // The number of page faults serviced without any I/O
  1520. info->minflt = r_usage.ru_minflt;
  1521. // The number of page faults serviced that required I/O
  1522. info->majflt = r_usage.ru_majflt;
  1523. // The number of times a process was "swapped" out of memory
  1524. info->nswap = r_usage.ru_nswap;
  1525. // The number of times the file system had to perform input
  1526. info->inblock = r_usage.ru_inblock;
  1527. // The number of times the file system had to perform output
  1528. info->oublock = r_usage.ru_oublock;
  1529. // The number of times a context switch was voluntarily
  1530. info->nvcsw = r_usage.ru_nvcsw;
  1531. // The number of times a context switch was forced
  1532. info->nivcsw = r_usage.ru_nivcsw;
  1533. return (status != 0);
  1534. }
  1535. void __kmp_read_system_time(double *delta) {
  1536. double t_ns;
  1537. struct timeval tval;
  1538. struct timespec stop;
  1539. int status;
  1540. status = gettimeofday(&tval, NULL);
  1541. KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
  1542. TIMEVAL_TO_TIMESPEC(&tval, &stop);
  1543. t_ns = (double)(TS2NS(stop) - TS2NS(__kmp_sys_timer_data.start));
  1544. *delta = (t_ns * 1e-9);
  1545. }
  1546. void __kmp_clear_system_time(void) {
  1547. struct timeval tval;
  1548. int status;
  1549. status = gettimeofday(&tval, NULL);
  1550. KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
  1551. TIMEVAL_TO_TIMESPEC(&tval, &__kmp_sys_timer_data.start);
  1552. }
  1553. static int __kmp_get_xproc(void) {
  1554. int r = 0;
  1555. #if KMP_OS_LINUX
  1556. __kmp_type_convert(sysconf(_SC_NPROCESSORS_CONF), &(r));
  1557. #elif KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || KMP_OS_OPENBSD || \
  1558. KMP_OS_HURD
  1559. __kmp_type_convert(sysconf(_SC_NPROCESSORS_ONLN), &(r));
  1560. #elif KMP_OS_DARWIN
  1561. // Bug C77011 High "OpenMP Threads and number of active cores".
  1562. // Find the number of available CPUs.
  1563. kern_return_t rc;
  1564. host_basic_info_data_t info;
  1565. mach_msg_type_number_t num = HOST_BASIC_INFO_COUNT;
  1566. rc = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&info, &num);
  1567. if (rc == 0 && num == HOST_BASIC_INFO_COUNT) {
  1568. // Cannot use KA_TRACE() here because this code works before trace support
  1569. // is initialized.
  1570. r = info.avail_cpus;
  1571. } else {
  1572. KMP_WARNING(CantGetNumAvailCPU);
  1573. KMP_INFORM(AssumedNumCPU);
  1574. }
  1575. #else
  1576. #error "Unknown or unsupported OS."
  1577. #endif
  1578. return r > 0 ? r : 2; /* guess value of 2 if OS told us 0 */
  1579. } // __kmp_get_xproc
  1580. int __kmp_read_from_file(char const *path, char const *format, ...) {
  1581. int result;
  1582. va_list args;
  1583. va_start(args, format);
  1584. FILE *f = fopen(path, "rb");
  1585. if (f == NULL)
  1586. return 0;
  1587. result = vfscanf(f, format, args);
  1588. fclose(f);
  1589. return result;
  1590. }
  1591. void __kmp_runtime_initialize(void) {
  1592. int status;
  1593. pthread_mutexattr_t mutex_attr;
  1594. pthread_condattr_t cond_attr;
  1595. if (__kmp_init_runtime) {
  1596. return;
  1597. }
  1598. #if (KMP_ARCH_X86 || KMP_ARCH_X86_64)
  1599. if (!__kmp_cpuinfo.initialized) {
  1600. __kmp_query_cpuid(&__kmp_cpuinfo);
  1601. }
  1602. #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
  1603. __kmp_xproc = __kmp_get_xproc();
  1604. #if !KMP_32_BIT_ARCH
  1605. struct rlimit rlim;
  1606. // read stack size of calling thread, save it as default for worker threads;
  1607. // this should be done before reading environment variables
  1608. status = getrlimit(RLIMIT_STACK, &rlim);
  1609. if (status == 0) { // success?
  1610. __kmp_stksize = rlim.rlim_cur;
  1611. __kmp_check_stksize(&__kmp_stksize); // check value and adjust if needed
  1612. }
  1613. #endif /* KMP_32_BIT_ARCH */
  1614. if (sysconf(_SC_THREADS)) {
  1615. /* Query the maximum number of threads */
  1616. __kmp_type_convert(sysconf(_SC_THREAD_THREADS_MAX), &(__kmp_sys_max_nth));
  1617. if (__kmp_sys_max_nth == -1) {
  1618. /* Unlimited threads for NPTL */
  1619. __kmp_sys_max_nth = INT_MAX;
  1620. } else if (__kmp_sys_max_nth <= 1) {
  1621. /* Can't tell, just use PTHREAD_THREADS_MAX */
  1622. __kmp_sys_max_nth = KMP_MAX_NTH;
  1623. }
  1624. /* Query the minimum stack size */
  1625. __kmp_sys_min_stksize = sysconf(_SC_THREAD_STACK_MIN);
  1626. if (__kmp_sys_min_stksize <= 1) {
  1627. __kmp_sys_min_stksize = KMP_MIN_STKSIZE;
  1628. }
  1629. }
  1630. /* Set up minimum number of threads to switch to TLS gtid */
  1631. __kmp_tls_gtid_min = KMP_TLS_GTID_MIN;
  1632. status = pthread_key_create(&__kmp_gtid_threadprivate_key,
  1633. __kmp_internal_end_dest);
  1634. KMP_CHECK_SYSFAIL("pthread_key_create", status);
  1635. status = pthread_mutexattr_init(&mutex_attr);
  1636. KMP_CHECK_SYSFAIL("pthread_mutexattr_init", status);
  1637. status = pthread_mutex_init(&__kmp_wait_mx.m_mutex, &mutex_attr);
  1638. KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
  1639. status = pthread_mutexattr_destroy(&mutex_attr);
  1640. KMP_CHECK_SYSFAIL("pthread_mutexattr_destroy", status);
  1641. status = pthread_condattr_init(&cond_attr);
  1642. KMP_CHECK_SYSFAIL("pthread_condattr_init", status);
  1643. status = pthread_cond_init(&__kmp_wait_cv.c_cond, &cond_attr);
  1644. KMP_CHECK_SYSFAIL("pthread_cond_init", status);
  1645. status = pthread_condattr_destroy(&cond_attr);
  1646. KMP_CHECK_SYSFAIL("pthread_condattr_destroy", status);
  1647. #if USE_ITT_BUILD
  1648. __kmp_itt_initialize();
  1649. #endif /* USE_ITT_BUILD */
  1650. __kmp_init_runtime = TRUE;
  1651. }
  1652. void __kmp_runtime_destroy(void) {
  1653. int status;
  1654. if (!__kmp_init_runtime) {
  1655. return; // Nothing to do.
  1656. }
  1657. #if USE_ITT_BUILD
  1658. __kmp_itt_destroy();
  1659. #endif /* USE_ITT_BUILD */
  1660. status = pthread_key_delete(__kmp_gtid_threadprivate_key);
  1661. KMP_CHECK_SYSFAIL("pthread_key_delete", status);
  1662. status = pthread_mutex_destroy(&__kmp_wait_mx.m_mutex);
  1663. if (status != 0 && status != EBUSY) {
  1664. KMP_SYSFAIL("pthread_mutex_destroy", status);
  1665. }
  1666. status = pthread_cond_destroy(&__kmp_wait_cv.c_cond);
  1667. if (status != 0 && status != EBUSY) {
  1668. KMP_SYSFAIL("pthread_cond_destroy", status);
  1669. }
  1670. #if KMP_AFFINITY_SUPPORTED
  1671. __kmp_affinity_uninitialize();
  1672. #endif
  1673. __kmp_init_runtime = FALSE;
  1674. }
  1675. /* Put the thread to sleep for a time period */
  1676. /* NOTE: not currently used anywhere */
  1677. void __kmp_thread_sleep(int millis) { sleep((millis + 500) / 1000); }
  1678. /* Calculate the elapsed wall clock time for the user */
  1679. void __kmp_elapsed(double *t) {
  1680. int status;
  1681. #ifdef FIX_SGI_CLOCK
  1682. struct timespec ts;
  1683. status = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  1684. KMP_CHECK_SYSFAIL_ERRNO("clock_gettime", status);
  1685. *t =
  1686. (double)ts.tv_nsec * (1.0 / (double)KMP_NSEC_PER_SEC) + (double)ts.tv_sec;
  1687. #else
  1688. struct timeval tv;
  1689. status = gettimeofday(&tv, NULL);
  1690. KMP_CHECK_SYSFAIL_ERRNO("gettimeofday", status);
  1691. *t =
  1692. (double)tv.tv_usec * (1.0 / (double)KMP_USEC_PER_SEC) + (double)tv.tv_sec;
  1693. #endif
  1694. }
  1695. /* Calculate the elapsed wall clock tick for the user */
  1696. void __kmp_elapsed_tick(double *t) { *t = 1 / (double)CLOCKS_PER_SEC; }
  1697. /* Return the current time stamp in nsec */
  1698. kmp_uint64 __kmp_now_nsec() {
  1699. struct timeval t;
  1700. gettimeofday(&t, NULL);
  1701. kmp_uint64 nsec = (kmp_uint64)KMP_NSEC_PER_SEC * (kmp_uint64)t.tv_sec +
  1702. (kmp_uint64)1000 * (kmp_uint64)t.tv_usec;
  1703. return nsec;
  1704. }
  1705. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  1706. /* Measure clock ticks per millisecond */
  1707. void __kmp_initialize_system_tick() {
  1708. kmp_uint64 now, nsec2, diff;
  1709. kmp_uint64 delay = 100000; // 50~100 usec on most machines.
  1710. kmp_uint64 nsec = __kmp_now_nsec();
  1711. kmp_uint64 goal = __kmp_hardware_timestamp() + delay;
  1712. while ((now = __kmp_hardware_timestamp()) < goal)
  1713. ;
  1714. nsec2 = __kmp_now_nsec();
  1715. diff = nsec2 - nsec;
  1716. if (diff > 0) {
  1717. kmp_uint64 tpms = ((kmp_uint64)1e6 * (delay + (now - goal)) / diff);
  1718. if (tpms > 0)
  1719. __kmp_ticks_per_msec = tpms;
  1720. }
  1721. }
  1722. #endif
  1723. /* Determine whether the given address is mapped into the current address
  1724. space. */
  1725. int __kmp_is_address_mapped(void *addr) {
  1726. int found = 0;
  1727. int rc;
  1728. #if KMP_OS_LINUX || KMP_OS_HURD
  1729. /* On GNUish OSes, read the /proc/<pid>/maps pseudo-file to get all the
  1730. address ranges mapped into the address space. */
  1731. char *name = __kmp_str_format("/proc/%d/maps", getpid());
  1732. FILE *file = NULL;
  1733. file = fopen(name, "r");
  1734. KMP_ASSERT(file != NULL);
  1735. for (;;) {
  1736. void *beginning = NULL;
  1737. void *ending = NULL;
  1738. char perms[5];
  1739. rc = fscanf(file, "%p-%p %4s %*[^\n]\n", &beginning, &ending, perms);
  1740. if (rc == EOF) {
  1741. break;
  1742. }
  1743. KMP_ASSERT(rc == 3 &&
  1744. KMP_STRLEN(perms) == 4); // Make sure all fields are read.
  1745. // Ending address is not included in the region, but beginning is.
  1746. if ((addr >= beginning) && (addr < ending)) {
  1747. perms[2] = 0; // 3th and 4th character does not matter.
  1748. if (strcmp(perms, "rw") == 0) {
  1749. // Memory we are looking for should be readable and writable.
  1750. found = 1;
  1751. }
  1752. break;
  1753. }
  1754. }
  1755. // Free resources.
  1756. fclose(file);
  1757. KMP_INTERNAL_FREE(name);
  1758. #elif KMP_OS_FREEBSD
  1759. char *buf;
  1760. size_t lstsz;
  1761. int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid()};
  1762. rc = sysctl(mib, 4, NULL, &lstsz, NULL, 0);
  1763. if (rc < 0)
  1764. return 0;
  1765. // We pass from number of vm entry's semantic
  1766. // to size of whole entry map list.
  1767. lstsz = lstsz * 4 / 3;
  1768. buf = reinterpret_cast<char *>(kmpc_malloc(lstsz));
  1769. rc = sysctl(mib, 4, buf, &lstsz, NULL, 0);
  1770. if (rc < 0) {
  1771. kmpc_free(buf);
  1772. return 0;
  1773. }
  1774. char *lw = buf;
  1775. char *up = buf + lstsz;
  1776. while (lw < up) {
  1777. struct kinfo_vmentry *cur = reinterpret_cast<struct kinfo_vmentry *>(lw);
  1778. size_t cursz = cur->kve_structsize;
  1779. if (cursz == 0)
  1780. break;
  1781. void *start = reinterpret_cast<void *>(cur->kve_start);
  1782. void *end = reinterpret_cast<void *>(cur->kve_end);
  1783. // Readable/Writable addresses within current map entry
  1784. if ((addr >= start) && (addr < end)) {
  1785. if ((cur->kve_protection & KVME_PROT_READ) != 0 &&
  1786. (cur->kve_protection & KVME_PROT_WRITE) != 0) {
  1787. found = 1;
  1788. break;
  1789. }
  1790. }
  1791. lw += cursz;
  1792. }
  1793. kmpc_free(buf);
  1794. #elif KMP_OS_DARWIN
  1795. /* On OS X*, /proc pseudo filesystem is not available. Try to read memory
  1796. using vm interface. */
  1797. int buffer;
  1798. vm_size_t count;
  1799. rc = vm_read_overwrite(
  1800. mach_task_self(), // Task to read memory of.
  1801. (vm_address_t)(addr), // Address to read from.
  1802. 1, // Number of bytes to be read.
  1803. (vm_address_t)(&buffer), // Address of buffer to save read bytes in.
  1804. &count // Address of var to save number of read bytes in.
  1805. );
  1806. if (rc == 0) {
  1807. // Memory successfully read.
  1808. found = 1;
  1809. }
  1810. #elif KMP_OS_NETBSD
  1811. int mib[5];
  1812. mib[0] = CTL_VM;
  1813. mib[1] = VM_PROC;
  1814. mib[2] = VM_PROC_MAP;
  1815. mib[3] = getpid();
  1816. mib[4] = sizeof(struct kinfo_vmentry);
  1817. size_t size;
  1818. rc = sysctl(mib, __arraycount(mib), NULL, &size, NULL, 0);
  1819. KMP_ASSERT(!rc);
  1820. KMP_ASSERT(size);
  1821. size = size * 4 / 3;
  1822. struct kinfo_vmentry *kiv = (struct kinfo_vmentry *)KMP_INTERNAL_MALLOC(size);
  1823. KMP_ASSERT(kiv);
  1824. rc = sysctl(mib, __arraycount(mib), kiv, &size, NULL, 0);
  1825. KMP_ASSERT(!rc);
  1826. KMP_ASSERT(size);
  1827. for (size_t i = 0; i < size; i++) {
  1828. if (kiv[i].kve_start >= (uint64_t)addr &&
  1829. kiv[i].kve_end <= (uint64_t)addr) {
  1830. found = 1;
  1831. break;
  1832. }
  1833. }
  1834. KMP_INTERNAL_FREE(kiv);
  1835. #elif KMP_OS_OPENBSD
  1836. int mib[3];
  1837. mib[0] = CTL_KERN;
  1838. mib[1] = KERN_PROC_VMMAP;
  1839. mib[2] = getpid();
  1840. size_t size;
  1841. uint64_t end;
  1842. rc = sysctl(mib, 3, NULL, &size, NULL, 0);
  1843. KMP_ASSERT(!rc);
  1844. KMP_ASSERT(size);
  1845. end = size;
  1846. struct kinfo_vmentry kiv = {.kve_start = 0};
  1847. while ((rc = sysctl(mib, 3, &kiv, &size, NULL, 0)) == 0) {
  1848. KMP_ASSERT(size);
  1849. if (kiv.kve_end == end)
  1850. break;
  1851. if (kiv.kve_start >= (uint64_t)addr && kiv.kve_end <= (uint64_t)addr) {
  1852. found = 1;
  1853. break;
  1854. }
  1855. kiv.kve_start += 1;
  1856. }
  1857. #elif KMP_OS_DRAGONFLY
  1858. // FIXME(DragonFly): Implement this
  1859. found = 1;
  1860. #else
  1861. #error "Unknown or unsupported OS"
  1862. #endif
  1863. return found;
  1864. } // __kmp_is_address_mapped
  1865. #ifdef USE_LOAD_BALANCE
  1866. #if KMP_OS_DARWIN || KMP_OS_NETBSD
  1867. // The function returns the rounded value of the system load average
  1868. // during given time interval which depends on the value of
  1869. // __kmp_load_balance_interval variable (default is 60 sec, other values
  1870. // may be 300 sec or 900 sec).
  1871. // It returns -1 in case of error.
  1872. int __kmp_get_load_balance(int max) {
  1873. double averages[3];
  1874. int ret_avg = 0;
  1875. int res = getloadavg(averages, 3);
  1876. // Check __kmp_load_balance_interval to determine which of averages to use.
  1877. // getloadavg() may return the number of samples less than requested that is
  1878. // less than 3.
  1879. if (__kmp_load_balance_interval < 180 && (res >= 1)) {
  1880. ret_avg = (int)averages[0]; // 1 min
  1881. } else if ((__kmp_load_balance_interval >= 180 &&
  1882. __kmp_load_balance_interval < 600) &&
  1883. (res >= 2)) {
  1884. ret_avg = (int)averages[1]; // 5 min
  1885. } else if ((__kmp_load_balance_interval >= 600) && (res == 3)) {
  1886. ret_avg = (int)averages[2]; // 15 min
  1887. } else { // Error occurred
  1888. return -1;
  1889. }
  1890. return ret_avg;
  1891. }
  1892. #else // Linux* OS
  1893. // The function returns number of running (not sleeping) threads, or -1 in case
  1894. // of error. Error could be reported if Linux* OS kernel too old (without
  1895. // "/proc" support). Counting running threads stops if max running threads
  1896. // encountered.
  1897. int __kmp_get_load_balance(int max) {
  1898. static int permanent_error = 0;
  1899. static int glb_running_threads = 0; // Saved count of the running threads for
  1900. // the thread balance algorithm
  1901. static double glb_call_time = 0; /* Thread balance algorithm call time */
  1902. int running_threads = 0; // Number of running threads in the system.
  1903. DIR *proc_dir = NULL; // Handle of "/proc/" directory.
  1904. struct dirent *proc_entry = NULL;
  1905. kmp_str_buf_t task_path; // "/proc/<pid>/task/<tid>/" path.
  1906. DIR *task_dir = NULL; // Handle of "/proc/<pid>/task/<tid>/" directory.
  1907. struct dirent *task_entry = NULL;
  1908. int task_path_fixed_len;
  1909. kmp_str_buf_t stat_path; // "/proc/<pid>/task/<tid>/stat" path.
  1910. int stat_file = -1;
  1911. int stat_path_fixed_len;
  1912. int total_processes = 0; // Total number of processes in system.
  1913. int total_threads = 0; // Total number of threads in system.
  1914. double call_time = 0.0;
  1915. __kmp_str_buf_init(&task_path);
  1916. __kmp_str_buf_init(&stat_path);
  1917. __kmp_elapsed(&call_time);
  1918. if (glb_call_time &&
  1919. (call_time - glb_call_time < __kmp_load_balance_interval)) {
  1920. running_threads = glb_running_threads;
  1921. goto finish;
  1922. }
  1923. glb_call_time = call_time;
  1924. // Do not spend time on scanning "/proc/" if we have a permanent error.
  1925. if (permanent_error) {
  1926. running_threads = -1;
  1927. goto finish;
  1928. }
  1929. if (max <= 0) {
  1930. max = INT_MAX;
  1931. }
  1932. // Open "/proc/" directory.
  1933. proc_dir = opendir("/proc");
  1934. if (proc_dir == NULL) {
  1935. // Cannot open "/prroc/". Probably the kernel does not support it. Return an
  1936. // error now and in subsequent calls.
  1937. running_threads = -1;
  1938. permanent_error = 1;
  1939. goto finish;
  1940. }
  1941. // Initialize fixed part of task_path. This part will not change.
  1942. __kmp_str_buf_cat(&task_path, "/proc/", 6);
  1943. task_path_fixed_len = task_path.used; // Remember number of used characters.
  1944. proc_entry = readdir(proc_dir);
  1945. while (proc_entry != NULL) {
  1946. // Proc entry is a directory and name starts with a digit. Assume it is a
  1947. // process' directory.
  1948. if (proc_entry->d_type == DT_DIR && isdigit(proc_entry->d_name[0])) {
  1949. ++total_processes;
  1950. // Make sure init process is the very first in "/proc", so we can replace
  1951. // strcmp( proc_entry->d_name, "1" ) == 0 with simpler total_processes ==
  1952. // 1. We are going to check that total_processes == 1 => d_name == "1" is
  1953. // true (where "=>" is implication). Since C++ does not have => operator,
  1954. // let us replace it with its equivalent: a => b == ! a || b.
  1955. KMP_DEBUG_ASSERT(total_processes != 1 ||
  1956. strcmp(proc_entry->d_name, "1") == 0);
  1957. // Construct task_path.
  1958. task_path.used = task_path_fixed_len; // Reset task_path to "/proc/".
  1959. __kmp_str_buf_cat(&task_path, proc_entry->d_name,
  1960. KMP_STRLEN(proc_entry->d_name));
  1961. __kmp_str_buf_cat(&task_path, "/task", 5);
  1962. task_dir = opendir(task_path.str);
  1963. if (task_dir == NULL) {
  1964. // Process can finish between reading "/proc/" directory entry and
  1965. // opening process' "task/" directory. So, in general case we should not
  1966. // complain, but have to skip this process and read the next one. But on
  1967. // systems with no "task/" support we will spend lot of time to scan
  1968. // "/proc/" tree again and again without any benefit. "init" process
  1969. // (its pid is 1) should exist always, so, if we cannot open
  1970. // "/proc/1/task/" directory, it means "task/" is not supported by
  1971. // kernel. Report an error now and in the future.
  1972. if (strcmp(proc_entry->d_name, "1") == 0) {
  1973. running_threads = -1;
  1974. permanent_error = 1;
  1975. goto finish;
  1976. }
  1977. } else {
  1978. // Construct fixed part of stat file path.
  1979. __kmp_str_buf_clear(&stat_path);
  1980. __kmp_str_buf_cat(&stat_path, task_path.str, task_path.used);
  1981. __kmp_str_buf_cat(&stat_path, "/", 1);
  1982. stat_path_fixed_len = stat_path.used;
  1983. task_entry = readdir(task_dir);
  1984. while (task_entry != NULL) {
  1985. // It is a directory and name starts with a digit.
  1986. if (proc_entry->d_type == DT_DIR && isdigit(task_entry->d_name[0])) {
  1987. ++total_threads;
  1988. // Construct complete stat file path. Easiest way would be:
  1989. // __kmp_str_buf_print( & stat_path, "%s/%s/stat", task_path.str,
  1990. // task_entry->d_name );
  1991. // but seriae of __kmp_str_buf_cat works a bit faster.
  1992. stat_path.used =
  1993. stat_path_fixed_len; // Reset stat path to its fixed part.
  1994. __kmp_str_buf_cat(&stat_path, task_entry->d_name,
  1995. KMP_STRLEN(task_entry->d_name));
  1996. __kmp_str_buf_cat(&stat_path, "/stat", 5);
  1997. // Note: Low-level API (open/read/close) is used. High-level API
  1998. // (fopen/fclose) works ~ 30 % slower.
  1999. stat_file = open(stat_path.str, O_RDONLY);
  2000. if (stat_file == -1) {
  2001. // We cannot report an error because task (thread) can terminate
  2002. // just before reading this file.
  2003. } else {
  2004. /* Content of "stat" file looks like:
  2005. 24285 (program) S ...
  2006. It is a single line (if program name does not include funny
  2007. symbols). First number is a thread id, then name of executable
  2008. file name in paretheses, then state of the thread. We need just
  2009. thread state.
  2010. Good news: Length of program name is 15 characters max. Longer
  2011. names are truncated.
  2012. Thus, we need rather short buffer: 15 chars for program name +
  2013. 2 parenthesis, + 3 spaces + ~7 digits of pid = 37.
  2014. Bad news: Program name may contain special symbols like space,
  2015. closing parenthesis, or even new line. This makes parsing
  2016. "stat" file not 100 % reliable. In case of fanny program names
  2017. parsing may fail (report incorrect thread state).
  2018. Parsing "status" file looks more promissing (due to different
  2019. file structure and escaping special symbols) but reading and
  2020. parsing of "status" file works slower.
  2021. -- ln
  2022. */
  2023. char buffer[65];
  2024. ssize_t len;
  2025. len = read(stat_file, buffer, sizeof(buffer) - 1);
  2026. if (len >= 0) {
  2027. buffer[len] = 0;
  2028. // Using scanf:
  2029. // sscanf( buffer, "%*d (%*s) %c ", & state );
  2030. // looks very nice, but searching for a closing parenthesis
  2031. // works a bit faster.
  2032. char *close_parent = strstr(buffer, ") ");
  2033. if (close_parent != NULL) {
  2034. char state = *(close_parent + 2);
  2035. if (state == 'R') {
  2036. ++running_threads;
  2037. if (running_threads >= max) {
  2038. goto finish;
  2039. }
  2040. }
  2041. }
  2042. }
  2043. close(stat_file);
  2044. stat_file = -1;
  2045. }
  2046. }
  2047. task_entry = readdir(task_dir);
  2048. }
  2049. closedir(task_dir);
  2050. task_dir = NULL;
  2051. }
  2052. }
  2053. proc_entry = readdir(proc_dir);
  2054. }
  2055. // There _might_ be a timing hole where the thread executing this
  2056. // code get skipped in the load balance, and running_threads is 0.
  2057. // Assert in the debug builds only!!!
  2058. KMP_DEBUG_ASSERT(running_threads > 0);
  2059. if (running_threads <= 0) {
  2060. running_threads = 1;
  2061. }
  2062. finish: // Clean up and exit.
  2063. if (proc_dir != NULL) {
  2064. closedir(proc_dir);
  2065. }
  2066. __kmp_str_buf_free(&task_path);
  2067. if (task_dir != NULL) {
  2068. closedir(task_dir);
  2069. }
  2070. __kmp_str_buf_free(&stat_path);
  2071. if (stat_file != -1) {
  2072. close(stat_file);
  2073. }
  2074. glb_running_threads = running_threads;
  2075. return running_threads;
  2076. } // __kmp_get_load_balance
  2077. #endif // KMP_OS_DARWIN
  2078. #endif // USE_LOAD_BALANCE
  2079. #if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \
  2080. ((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || \
  2081. KMP_ARCH_PPC64 || KMP_ARCH_RISCV64)
  2082. // we really only need the case with 1 argument, because CLANG always build
  2083. // a struct of pointers to shared variables referenced in the outlined function
  2084. int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int tid, int argc,
  2085. void *p_argv[]
  2086. #if OMPT_SUPPORT
  2087. ,
  2088. void **exit_frame_ptr
  2089. #endif
  2090. ) {
  2091. #if OMPT_SUPPORT
  2092. *exit_frame_ptr = OMPT_GET_FRAME_ADDRESS(0);
  2093. #endif
  2094. switch (argc) {
  2095. default:
  2096. fprintf(stderr, "Too many args to microtask: %d!\n", argc);
  2097. fflush(stderr);
  2098. exit(-1);
  2099. case 0:
  2100. (*pkfn)(&gtid, &tid);
  2101. break;
  2102. case 1:
  2103. (*pkfn)(&gtid, &tid, p_argv[0]);
  2104. break;
  2105. case 2:
  2106. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1]);
  2107. break;
  2108. case 3:
  2109. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2]);
  2110. break;
  2111. case 4:
  2112. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3]);
  2113. break;
  2114. case 5:
  2115. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4]);
  2116. break;
  2117. case 6:
  2118. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2119. p_argv[5]);
  2120. break;
  2121. case 7:
  2122. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2123. p_argv[5], p_argv[6]);
  2124. break;
  2125. case 8:
  2126. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2127. p_argv[5], p_argv[6], p_argv[7]);
  2128. break;
  2129. case 9:
  2130. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2131. p_argv[5], p_argv[6], p_argv[7], p_argv[8]);
  2132. break;
  2133. case 10:
  2134. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2135. p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9]);
  2136. break;
  2137. case 11:
  2138. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2139. p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10]);
  2140. break;
  2141. case 12:
  2142. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2143. p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
  2144. p_argv[11]);
  2145. break;
  2146. case 13:
  2147. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2148. p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
  2149. p_argv[11], p_argv[12]);
  2150. break;
  2151. case 14:
  2152. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2153. p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
  2154. p_argv[11], p_argv[12], p_argv[13]);
  2155. break;
  2156. case 15:
  2157. (*pkfn)(&gtid, &tid, p_argv[0], p_argv[1], p_argv[2], p_argv[3], p_argv[4],
  2158. p_argv[5], p_argv[6], p_argv[7], p_argv[8], p_argv[9], p_argv[10],
  2159. p_argv[11], p_argv[12], p_argv[13], p_argv[14]);
  2160. break;
  2161. }
  2162. return 1;
  2163. }
  2164. #endif
  2165. #if KMP_OS_LINUX
  2166. // Functions for hidden helper task
  2167. namespace {
  2168. // Condition variable for initializing hidden helper team
  2169. pthread_cond_t hidden_helper_threads_initz_cond_var;
  2170. pthread_mutex_t hidden_helper_threads_initz_lock;
  2171. volatile int hidden_helper_initz_signaled = FALSE;
  2172. // Condition variable for deinitializing hidden helper team
  2173. pthread_cond_t hidden_helper_threads_deinitz_cond_var;
  2174. pthread_mutex_t hidden_helper_threads_deinitz_lock;
  2175. volatile int hidden_helper_deinitz_signaled = FALSE;
  2176. // Condition variable for the wrapper function of main thread
  2177. pthread_cond_t hidden_helper_main_thread_cond_var;
  2178. pthread_mutex_t hidden_helper_main_thread_lock;
  2179. volatile int hidden_helper_main_thread_signaled = FALSE;
  2180. // Semaphore for worker threads. We don't use condition variable here in case
  2181. // that when multiple signals are sent at the same time, only one thread might
  2182. // be waken.
  2183. sem_t hidden_helper_task_sem;
  2184. } // namespace
  2185. void __kmp_hidden_helper_worker_thread_wait() {
  2186. int status = sem_wait(&hidden_helper_task_sem);
  2187. KMP_CHECK_SYSFAIL("sem_wait", status);
  2188. }
  2189. void __kmp_do_initialize_hidden_helper_threads() {
  2190. // Initialize condition variable
  2191. int status =
  2192. pthread_cond_init(&hidden_helper_threads_initz_cond_var, nullptr);
  2193. KMP_CHECK_SYSFAIL("pthread_cond_init", status);
  2194. status = pthread_cond_init(&hidden_helper_threads_deinitz_cond_var, nullptr);
  2195. KMP_CHECK_SYSFAIL("pthread_cond_init", status);
  2196. status = pthread_cond_init(&hidden_helper_main_thread_cond_var, nullptr);
  2197. KMP_CHECK_SYSFAIL("pthread_cond_init", status);
  2198. status = pthread_mutex_init(&hidden_helper_threads_initz_lock, nullptr);
  2199. KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
  2200. status = pthread_mutex_init(&hidden_helper_threads_deinitz_lock, nullptr);
  2201. KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
  2202. status = pthread_mutex_init(&hidden_helper_main_thread_lock, nullptr);
  2203. KMP_CHECK_SYSFAIL("pthread_mutex_init", status);
  2204. // Initialize the semaphore
  2205. status = sem_init(&hidden_helper_task_sem, 0, 0);
  2206. KMP_CHECK_SYSFAIL("sem_init", status);
  2207. // Create a new thread to finish initialization
  2208. pthread_t handle;
  2209. status = pthread_create(
  2210. &handle, nullptr,
  2211. [](void *) -> void * {
  2212. __kmp_hidden_helper_threads_initz_routine();
  2213. return nullptr;
  2214. },
  2215. nullptr);
  2216. KMP_CHECK_SYSFAIL("pthread_create", status);
  2217. }
  2218. void __kmp_hidden_helper_threads_initz_wait() {
  2219. // Initial thread waits here for the completion of the initialization. The
  2220. // condition variable will be notified by main thread of hidden helper teams.
  2221. int status = pthread_mutex_lock(&hidden_helper_threads_initz_lock);
  2222. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  2223. if (!TCR_4(hidden_helper_initz_signaled)) {
  2224. status = pthread_cond_wait(&hidden_helper_threads_initz_cond_var,
  2225. &hidden_helper_threads_initz_lock);
  2226. KMP_CHECK_SYSFAIL("pthread_cond_wait", status);
  2227. }
  2228. status = pthread_mutex_unlock(&hidden_helper_threads_initz_lock);
  2229. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  2230. }
  2231. void __kmp_hidden_helper_initz_release() {
  2232. // After all initialization, reset __kmp_init_hidden_helper_threads to false.
  2233. int status = pthread_mutex_lock(&hidden_helper_threads_initz_lock);
  2234. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  2235. status = pthread_cond_signal(&hidden_helper_threads_initz_cond_var);
  2236. KMP_CHECK_SYSFAIL("pthread_cond_wait", status);
  2237. TCW_SYNC_4(hidden_helper_initz_signaled, TRUE);
  2238. status = pthread_mutex_unlock(&hidden_helper_threads_initz_lock);
  2239. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  2240. }
  2241. void __kmp_hidden_helper_main_thread_wait() {
  2242. // The main thread of hidden helper team will be blocked here. The
  2243. // condition variable can only be signal in the destructor of RTL.
  2244. int status = pthread_mutex_lock(&hidden_helper_main_thread_lock);
  2245. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  2246. if (!TCR_4(hidden_helper_main_thread_signaled)) {
  2247. status = pthread_cond_wait(&hidden_helper_main_thread_cond_var,
  2248. &hidden_helper_main_thread_lock);
  2249. KMP_CHECK_SYSFAIL("pthread_cond_wait", status);
  2250. }
  2251. status = pthread_mutex_unlock(&hidden_helper_main_thread_lock);
  2252. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  2253. }
  2254. void __kmp_hidden_helper_main_thread_release() {
  2255. // The initial thread of OpenMP RTL should call this function to wake up the
  2256. // main thread of hidden helper team.
  2257. int status = pthread_mutex_lock(&hidden_helper_main_thread_lock);
  2258. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  2259. status = pthread_cond_signal(&hidden_helper_main_thread_cond_var);
  2260. KMP_CHECK_SYSFAIL("pthread_cond_signal", status);
  2261. // The hidden helper team is done here
  2262. TCW_SYNC_4(hidden_helper_main_thread_signaled, TRUE);
  2263. status = pthread_mutex_unlock(&hidden_helper_main_thread_lock);
  2264. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  2265. }
  2266. void __kmp_hidden_helper_worker_thread_signal() {
  2267. int status = sem_post(&hidden_helper_task_sem);
  2268. KMP_CHECK_SYSFAIL("sem_post", status);
  2269. }
  2270. void __kmp_hidden_helper_threads_deinitz_wait() {
  2271. // Initial thread waits here for the completion of the deinitialization. The
  2272. // condition variable will be notified by main thread of hidden helper teams.
  2273. int status = pthread_mutex_lock(&hidden_helper_threads_deinitz_lock);
  2274. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  2275. if (!TCR_4(hidden_helper_deinitz_signaled)) {
  2276. status = pthread_cond_wait(&hidden_helper_threads_deinitz_cond_var,
  2277. &hidden_helper_threads_deinitz_lock);
  2278. KMP_CHECK_SYSFAIL("pthread_cond_wait", status);
  2279. }
  2280. status = pthread_mutex_unlock(&hidden_helper_threads_deinitz_lock);
  2281. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  2282. }
  2283. void __kmp_hidden_helper_threads_deinitz_release() {
  2284. int status = pthread_mutex_lock(&hidden_helper_threads_deinitz_lock);
  2285. KMP_CHECK_SYSFAIL("pthread_mutex_lock", status);
  2286. status = pthread_cond_signal(&hidden_helper_threads_deinitz_cond_var);
  2287. KMP_CHECK_SYSFAIL("pthread_cond_wait", status);
  2288. TCW_SYNC_4(hidden_helper_deinitz_signaled, TRUE);
  2289. status = pthread_mutex_unlock(&hidden_helper_threads_deinitz_lock);
  2290. KMP_CHECK_SYSFAIL("pthread_mutex_unlock", status);
  2291. }
  2292. #else // KMP_OS_LINUX
  2293. void __kmp_hidden_helper_worker_thread_wait() {
  2294. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2295. }
  2296. void __kmp_do_initialize_hidden_helper_threads() {
  2297. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2298. }
  2299. void __kmp_hidden_helper_threads_initz_wait() {
  2300. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2301. }
  2302. void __kmp_hidden_helper_initz_release() {
  2303. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2304. }
  2305. void __kmp_hidden_helper_main_thread_wait() {
  2306. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2307. }
  2308. void __kmp_hidden_helper_main_thread_release() {
  2309. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2310. }
  2311. void __kmp_hidden_helper_worker_thread_signal() {
  2312. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2313. }
  2314. void __kmp_hidden_helper_threads_deinitz_wait() {
  2315. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2316. }
  2317. void __kmp_hidden_helper_threads_deinitz_release() {
  2318. KMP_ASSERT(0 && "Hidden helper task is not supported on this OS");
  2319. }
  2320. #endif // KMP_OS_LINUX
  2321. // end of file //