kmp_lock.cpp 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052
  1. /*
  2. * kmp_lock.cpp -- lock-related functions
  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 <stddef.h>
  12. #include <atomic>
  13. #include "kmp.h"
  14. #include "kmp_i18n.h"
  15. #include "kmp_io.h"
  16. #include "kmp_itt.h"
  17. #include "kmp_lock.h"
  18. #include "kmp_wait_release.h"
  19. #include "kmp_wrapper_getpid.h"
  20. #if KMP_USE_FUTEX
  21. #include <sys/syscall.h>
  22. #include <unistd.h>
  23. // We should really include <futex.h>, but that causes compatibility problems on
  24. // different Linux* OS distributions that either require that you include (or
  25. // break when you try to include) <pci/types.h>. Since all we need is the two
  26. // macros below (which are part of the kernel ABI, so can't change) we just
  27. // define the constants here and don't include <futex.h>
  28. #ifndef FUTEX_WAIT
  29. #define FUTEX_WAIT 0
  30. #endif
  31. #ifndef FUTEX_WAKE
  32. #define FUTEX_WAKE 1
  33. #endif
  34. #endif
  35. /* Implement spin locks for internal library use. */
  36. /* The algorithm implemented is Lamport's bakery lock [1974]. */
  37. void __kmp_validate_locks(void) {
  38. int i;
  39. kmp_uint32 x, y;
  40. /* Check to make sure unsigned arithmetic does wraps properly */
  41. x = ~((kmp_uint32)0) - 2;
  42. y = x - 2;
  43. for (i = 0; i < 8; ++i, ++x, ++y) {
  44. kmp_uint32 z = (x - y);
  45. KMP_ASSERT(z == 2);
  46. }
  47. KMP_ASSERT(offsetof(kmp_base_queuing_lock, tail_id) % 8 == 0);
  48. }
  49. /* ------------------------------------------------------------------------ */
  50. /* test and set locks */
  51. // For the non-nested locks, we can only assume that the first 4 bytes were
  52. // allocated, since gcc only allocates 4 bytes for omp_lock_t, and the Intel
  53. // compiler only allocates a 4 byte pointer on IA-32 architecture. On
  54. // Windows* OS on Intel(R) 64, we can assume that all 8 bytes were allocated.
  55. //
  56. // gcc reserves >= 8 bytes for nested locks, so we can assume that the
  57. // entire 8 bytes were allocated for nested locks on all 64-bit platforms.
  58. static kmp_int32 __kmp_get_tas_lock_owner(kmp_tas_lock_t *lck) {
  59. return KMP_LOCK_STRIP(KMP_ATOMIC_LD_RLX(&lck->lk.poll)) - 1;
  60. }
  61. static inline bool __kmp_is_tas_lock_nestable(kmp_tas_lock_t *lck) {
  62. return lck->lk.depth_locked != -1;
  63. }
  64. __forceinline static int
  65. __kmp_acquire_tas_lock_timed_template(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  66. KMP_MB();
  67. #ifdef USE_LOCK_PROFILE
  68. kmp_uint32 curr = KMP_LOCK_STRIP(lck->lk.poll);
  69. if ((curr != 0) && (curr != gtid + 1))
  70. __kmp_printf("LOCK CONTENTION: %p\n", lck);
  71. /* else __kmp_printf( "." );*/
  72. #endif /* USE_LOCK_PROFILE */
  73. kmp_int32 tas_free = KMP_LOCK_FREE(tas);
  74. kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas);
  75. if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == tas_free &&
  76. __kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy)) {
  77. KMP_FSYNC_ACQUIRED(lck);
  78. return KMP_LOCK_ACQUIRED_FIRST;
  79. }
  80. kmp_uint32 spins;
  81. kmp_uint64 time;
  82. KMP_FSYNC_PREPARE(lck);
  83. KMP_INIT_YIELD(spins);
  84. KMP_INIT_BACKOFF(time);
  85. kmp_backoff_t backoff = __kmp_spin_backoff_params;
  86. do {
  87. #if !KMP_HAVE_UMWAIT
  88. __kmp_spin_backoff(&backoff);
  89. #else
  90. if (!__kmp_tpause_enabled)
  91. __kmp_spin_backoff(&backoff);
  92. #endif
  93. KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
  94. } while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != tas_free ||
  95. !__kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy));
  96. KMP_FSYNC_ACQUIRED(lck);
  97. return KMP_LOCK_ACQUIRED_FIRST;
  98. }
  99. int __kmp_acquire_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  100. int retval = __kmp_acquire_tas_lock_timed_template(lck, gtid);
  101. return retval;
  102. }
  103. static int __kmp_acquire_tas_lock_with_checks(kmp_tas_lock_t *lck,
  104. kmp_int32 gtid) {
  105. char const *const func = "omp_set_lock";
  106. if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
  107. __kmp_is_tas_lock_nestable(lck)) {
  108. KMP_FATAL(LockNestableUsedAsSimple, func);
  109. }
  110. if ((gtid >= 0) && (__kmp_get_tas_lock_owner(lck) == gtid)) {
  111. KMP_FATAL(LockIsAlreadyOwned, func);
  112. }
  113. return __kmp_acquire_tas_lock(lck, gtid);
  114. }
  115. int __kmp_test_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  116. kmp_int32 tas_free = KMP_LOCK_FREE(tas);
  117. kmp_int32 tas_busy = KMP_LOCK_BUSY(gtid + 1, tas);
  118. if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == tas_free &&
  119. __kmp_atomic_compare_store_acq(&lck->lk.poll, tas_free, tas_busy)) {
  120. KMP_FSYNC_ACQUIRED(lck);
  121. return TRUE;
  122. }
  123. return FALSE;
  124. }
  125. static int __kmp_test_tas_lock_with_checks(kmp_tas_lock_t *lck,
  126. kmp_int32 gtid) {
  127. char const *const func = "omp_test_lock";
  128. if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
  129. __kmp_is_tas_lock_nestable(lck)) {
  130. KMP_FATAL(LockNestableUsedAsSimple, func);
  131. }
  132. return __kmp_test_tas_lock(lck, gtid);
  133. }
  134. int __kmp_release_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  135. KMP_MB(); /* Flush all pending memory write invalidates. */
  136. KMP_FSYNC_RELEASING(lck);
  137. KMP_ATOMIC_ST_REL(&lck->lk.poll, KMP_LOCK_FREE(tas));
  138. KMP_MB(); /* Flush all pending memory write invalidates. */
  139. KMP_YIELD_OVERSUB();
  140. return KMP_LOCK_RELEASED;
  141. }
  142. static int __kmp_release_tas_lock_with_checks(kmp_tas_lock_t *lck,
  143. kmp_int32 gtid) {
  144. char const *const func = "omp_unset_lock";
  145. KMP_MB(); /* in case another processor initialized lock */
  146. if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
  147. __kmp_is_tas_lock_nestable(lck)) {
  148. KMP_FATAL(LockNestableUsedAsSimple, func);
  149. }
  150. if (__kmp_get_tas_lock_owner(lck) == -1) {
  151. KMP_FATAL(LockUnsettingFree, func);
  152. }
  153. if ((gtid >= 0) && (__kmp_get_tas_lock_owner(lck) >= 0) &&
  154. (__kmp_get_tas_lock_owner(lck) != gtid)) {
  155. KMP_FATAL(LockUnsettingSetByAnother, func);
  156. }
  157. return __kmp_release_tas_lock(lck, gtid);
  158. }
  159. void __kmp_init_tas_lock(kmp_tas_lock_t *lck) {
  160. lck->lk.poll = KMP_LOCK_FREE(tas);
  161. }
  162. void __kmp_destroy_tas_lock(kmp_tas_lock_t *lck) { lck->lk.poll = 0; }
  163. static void __kmp_destroy_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  164. char const *const func = "omp_destroy_lock";
  165. if ((sizeof(kmp_tas_lock_t) <= OMP_LOCK_T_SIZE) &&
  166. __kmp_is_tas_lock_nestable(lck)) {
  167. KMP_FATAL(LockNestableUsedAsSimple, func);
  168. }
  169. if (__kmp_get_tas_lock_owner(lck) != -1) {
  170. KMP_FATAL(LockStillOwned, func);
  171. }
  172. __kmp_destroy_tas_lock(lck);
  173. }
  174. // nested test and set locks
  175. int __kmp_acquire_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  176. KMP_DEBUG_ASSERT(gtid >= 0);
  177. if (__kmp_get_tas_lock_owner(lck) == gtid) {
  178. lck->lk.depth_locked += 1;
  179. return KMP_LOCK_ACQUIRED_NEXT;
  180. } else {
  181. __kmp_acquire_tas_lock_timed_template(lck, gtid);
  182. lck->lk.depth_locked = 1;
  183. return KMP_LOCK_ACQUIRED_FIRST;
  184. }
  185. }
  186. static int __kmp_acquire_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
  187. kmp_int32 gtid) {
  188. char const *const func = "omp_set_nest_lock";
  189. if (!__kmp_is_tas_lock_nestable(lck)) {
  190. KMP_FATAL(LockSimpleUsedAsNestable, func);
  191. }
  192. return __kmp_acquire_nested_tas_lock(lck, gtid);
  193. }
  194. int __kmp_test_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  195. int retval;
  196. KMP_DEBUG_ASSERT(gtid >= 0);
  197. if (__kmp_get_tas_lock_owner(lck) == gtid) {
  198. retval = ++lck->lk.depth_locked;
  199. } else if (!__kmp_test_tas_lock(lck, gtid)) {
  200. retval = 0;
  201. } else {
  202. KMP_MB();
  203. retval = lck->lk.depth_locked = 1;
  204. }
  205. return retval;
  206. }
  207. static int __kmp_test_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
  208. kmp_int32 gtid) {
  209. char const *const func = "omp_test_nest_lock";
  210. if (!__kmp_is_tas_lock_nestable(lck)) {
  211. KMP_FATAL(LockSimpleUsedAsNestable, func);
  212. }
  213. return __kmp_test_nested_tas_lock(lck, gtid);
  214. }
  215. int __kmp_release_nested_tas_lock(kmp_tas_lock_t *lck, kmp_int32 gtid) {
  216. KMP_DEBUG_ASSERT(gtid >= 0);
  217. KMP_MB();
  218. if (--(lck->lk.depth_locked) == 0) {
  219. __kmp_release_tas_lock(lck, gtid);
  220. return KMP_LOCK_RELEASED;
  221. }
  222. return KMP_LOCK_STILL_HELD;
  223. }
  224. static int __kmp_release_nested_tas_lock_with_checks(kmp_tas_lock_t *lck,
  225. kmp_int32 gtid) {
  226. char const *const func = "omp_unset_nest_lock";
  227. KMP_MB(); /* in case another processor initialized lock */
  228. if (!__kmp_is_tas_lock_nestable(lck)) {
  229. KMP_FATAL(LockSimpleUsedAsNestable, func);
  230. }
  231. if (__kmp_get_tas_lock_owner(lck) == -1) {
  232. KMP_FATAL(LockUnsettingFree, func);
  233. }
  234. if (__kmp_get_tas_lock_owner(lck) != gtid) {
  235. KMP_FATAL(LockUnsettingSetByAnother, func);
  236. }
  237. return __kmp_release_nested_tas_lock(lck, gtid);
  238. }
  239. void __kmp_init_nested_tas_lock(kmp_tas_lock_t *lck) {
  240. __kmp_init_tas_lock(lck);
  241. lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
  242. }
  243. void __kmp_destroy_nested_tas_lock(kmp_tas_lock_t *lck) {
  244. __kmp_destroy_tas_lock(lck);
  245. lck->lk.depth_locked = 0;
  246. }
  247. static void __kmp_destroy_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  248. char const *const func = "omp_destroy_nest_lock";
  249. if (!__kmp_is_tas_lock_nestable(lck)) {
  250. KMP_FATAL(LockSimpleUsedAsNestable, func);
  251. }
  252. if (__kmp_get_tas_lock_owner(lck) != -1) {
  253. KMP_FATAL(LockStillOwned, func);
  254. }
  255. __kmp_destroy_nested_tas_lock(lck);
  256. }
  257. #if KMP_USE_FUTEX
  258. /* ------------------------------------------------------------------------ */
  259. /* futex locks */
  260. // futex locks are really just test and set locks, with a different method
  261. // of handling contention. They take the same amount of space as test and
  262. // set locks, and are allocated the same way (i.e. use the area allocated by
  263. // the compiler for non-nested locks / allocate nested locks on the heap).
  264. static kmp_int32 __kmp_get_futex_lock_owner(kmp_futex_lock_t *lck) {
  265. return KMP_LOCK_STRIP((TCR_4(lck->lk.poll) >> 1)) - 1;
  266. }
  267. static inline bool __kmp_is_futex_lock_nestable(kmp_futex_lock_t *lck) {
  268. return lck->lk.depth_locked != -1;
  269. }
  270. __forceinline static int
  271. __kmp_acquire_futex_lock_timed_template(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  272. kmp_int32 gtid_code = (gtid + 1) << 1;
  273. KMP_MB();
  274. #ifdef USE_LOCK_PROFILE
  275. kmp_uint32 curr = KMP_LOCK_STRIP(TCR_4(lck->lk.poll));
  276. if ((curr != 0) && (curr != gtid_code))
  277. __kmp_printf("LOCK CONTENTION: %p\n", lck);
  278. /* else __kmp_printf( "." );*/
  279. #endif /* USE_LOCK_PROFILE */
  280. KMP_FSYNC_PREPARE(lck);
  281. KA_TRACE(1000, ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d entering\n",
  282. lck, lck->lk.poll, gtid));
  283. kmp_int32 poll_val;
  284. while ((poll_val = KMP_COMPARE_AND_STORE_RET32(
  285. &(lck->lk.poll), KMP_LOCK_FREE(futex),
  286. KMP_LOCK_BUSY(gtid_code, futex))) != KMP_LOCK_FREE(futex)) {
  287. kmp_int32 cond = KMP_LOCK_STRIP(poll_val) & 1;
  288. KA_TRACE(
  289. 1000,
  290. ("__kmp_acquire_futex_lock: lck:%p, T#%d poll_val = 0x%x cond = 0x%x\n",
  291. lck, gtid, poll_val, cond));
  292. // NOTE: if you try to use the following condition for this branch
  293. //
  294. // if ( poll_val & 1 == 0 )
  295. //
  296. // Then the 12.0 compiler has a bug where the following block will
  297. // always be skipped, regardless of the value of the LSB of poll_val.
  298. if (!cond) {
  299. // Try to set the lsb in the poll to indicate to the owner
  300. // thread that they need to wake this thread up.
  301. if (!KMP_COMPARE_AND_STORE_REL32(&(lck->lk.poll), poll_val,
  302. poll_val | KMP_LOCK_BUSY(1, futex))) {
  303. KA_TRACE(
  304. 1000,
  305. ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d can't set bit 0\n",
  306. lck, lck->lk.poll, gtid));
  307. continue;
  308. }
  309. poll_val |= KMP_LOCK_BUSY(1, futex);
  310. KA_TRACE(1000,
  311. ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d bit 0 set\n", lck,
  312. lck->lk.poll, gtid));
  313. }
  314. KA_TRACE(
  315. 1000,
  316. ("__kmp_acquire_futex_lock: lck:%p, T#%d before futex_wait(0x%x)\n",
  317. lck, gtid, poll_val));
  318. long rc;
  319. if ((rc = syscall(__NR_futex, &(lck->lk.poll), FUTEX_WAIT, poll_val, NULL,
  320. NULL, 0)) != 0) {
  321. KA_TRACE(1000, ("__kmp_acquire_futex_lock: lck:%p, T#%d futex_wait(0x%x) "
  322. "failed (rc=%ld errno=%d)\n",
  323. lck, gtid, poll_val, rc, errno));
  324. continue;
  325. }
  326. KA_TRACE(1000,
  327. ("__kmp_acquire_futex_lock: lck:%p, T#%d after futex_wait(0x%x)\n",
  328. lck, gtid, poll_val));
  329. // This thread has now done a successful futex wait call and was entered on
  330. // the OS futex queue. We must now perform a futex wake call when releasing
  331. // the lock, as we have no idea how many other threads are in the queue.
  332. gtid_code |= 1;
  333. }
  334. KMP_FSYNC_ACQUIRED(lck);
  335. KA_TRACE(1000, ("__kmp_acquire_futex_lock: lck:%p(0x%x), T#%d exiting\n", lck,
  336. lck->lk.poll, gtid));
  337. return KMP_LOCK_ACQUIRED_FIRST;
  338. }
  339. int __kmp_acquire_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  340. int retval = __kmp_acquire_futex_lock_timed_template(lck, gtid);
  341. return retval;
  342. }
  343. static int __kmp_acquire_futex_lock_with_checks(kmp_futex_lock_t *lck,
  344. kmp_int32 gtid) {
  345. char const *const func = "omp_set_lock";
  346. if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
  347. __kmp_is_futex_lock_nestable(lck)) {
  348. KMP_FATAL(LockNestableUsedAsSimple, func);
  349. }
  350. if ((gtid >= 0) && (__kmp_get_futex_lock_owner(lck) == gtid)) {
  351. KMP_FATAL(LockIsAlreadyOwned, func);
  352. }
  353. return __kmp_acquire_futex_lock(lck, gtid);
  354. }
  355. int __kmp_test_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  356. if (KMP_COMPARE_AND_STORE_ACQ32(&(lck->lk.poll), KMP_LOCK_FREE(futex),
  357. KMP_LOCK_BUSY((gtid + 1) << 1, futex))) {
  358. KMP_FSYNC_ACQUIRED(lck);
  359. return TRUE;
  360. }
  361. return FALSE;
  362. }
  363. static int __kmp_test_futex_lock_with_checks(kmp_futex_lock_t *lck,
  364. kmp_int32 gtid) {
  365. char const *const func = "omp_test_lock";
  366. if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
  367. __kmp_is_futex_lock_nestable(lck)) {
  368. KMP_FATAL(LockNestableUsedAsSimple, func);
  369. }
  370. return __kmp_test_futex_lock(lck, gtid);
  371. }
  372. int __kmp_release_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  373. KMP_MB(); /* Flush all pending memory write invalidates. */
  374. KA_TRACE(1000, ("__kmp_release_futex_lock: lck:%p(0x%x), T#%d entering\n",
  375. lck, lck->lk.poll, gtid));
  376. KMP_FSYNC_RELEASING(lck);
  377. kmp_int32 poll_val = KMP_XCHG_FIXED32(&(lck->lk.poll), KMP_LOCK_FREE(futex));
  378. KA_TRACE(1000,
  379. ("__kmp_release_futex_lock: lck:%p, T#%d released poll_val = 0x%x\n",
  380. lck, gtid, poll_val));
  381. if (KMP_LOCK_STRIP(poll_val) & 1) {
  382. KA_TRACE(1000,
  383. ("__kmp_release_futex_lock: lck:%p, T#%d futex_wake 1 thread\n",
  384. lck, gtid));
  385. syscall(__NR_futex, &(lck->lk.poll), FUTEX_WAKE, KMP_LOCK_BUSY(1, futex),
  386. NULL, NULL, 0);
  387. }
  388. KMP_MB(); /* Flush all pending memory write invalidates. */
  389. KA_TRACE(1000, ("__kmp_release_futex_lock: lck:%p(0x%x), T#%d exiting\n", lck,
  390. lck->lk.poll, gtid));
  391. KMP_YIELD_OVERSUB();
  392. return KMP_LOCK_RELEASED;
  393. }
  394. static int __kmp_release_futex_lock_with_checks(kmp_futex_lock_t *lck,
  395. kmp_int32 gtid) {
  396. char const *const func = "omp_unset_lock";
  397. KMP_MB(); /* in case another processor initialized lock */
  398. if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
  399. __kmp_is_futex_lock_nestable(lck)) {
  400. KMP_FATAL(LockNestableUsedAsSimple, func);
  401. }
  402. if (__kmp_get_futex_lock_owner(lck) == -1) {
  403. KMP_FATAL(LockUnsettingFree, func);
  404. }
  405. if ((gtid >= 0) && (__kmp_get_futex_lock_owner(lck) >= 0) &&
  406. (__kmp_get_futex_lock_owner(lck) != gtid)) {
  407. KMP_FATAL(LockUnsettingSetByAnother, func);
  408. }
  409. return __kmp_release_futex_lock(lck, gtid);
  410. }
  411. void __kmp_init_futex_lock(kmp_futex_lock_t *lck) {
  412. TCW_4(lck->lk.poll, KMP_LOCK_FREE(futex));
  413. }
  414. void __kmp_destroy_futex_lock(kmp_futex_lock_t *lck) { lck->lk.poll = 0; }
  415. static void __kmp_destroy_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  416. char const *const func = "omp_destroy_lock";
  417. if ((sizeof(kmp_futex_lock_t) <= OMP_LOCK_T_SIZE) &&
  418. __kmp_is_futex_lock_nestable(lck)) {
  419. KMP_FATAL(LockNestableUsedAsSimple, func);
  420. }
  421. if (__kmp_get_futex_lock_owner(lck) != -1) {
  422. KMP_FATAL(LockStillOwned, func);
  423. }
  424. __kmp_destroy_futex_lock(lck);
  425. }
  426. // nested futex locks
  427. int __kmp_acquire_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  428. KMP_DEBUG_ASSERT(gtid >= 0);
  429. if (__kmp_get_futex_lock_owner(lck) == gtid) {
  430. lck->lk.depth_locked += 1;
  431. return KMP_LOCK_ACQUIRED_NEXT;
  432. } else {
  433. __kmp_acquire_futex_lock_timed_template(lck, gtid);
  434. lck->lk.depth_locked = 1;
  435. return KMP_LOCK_ACQUIRED_FIRST;
  436. }
  437. }
  438. static int __kmp_acquire_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
  439. kmp_int32 gtid) {
  440. char const *const func = "omp_set_nest_lock";
  441. if (!__kmp_is_futex_lock_nestable(lck)) {
  442. KMP_FATAL(LockSimpleUsedAsNestable, func);
  443. }
  444. return __kmp_acquire_nested_futex_lock(lck, gtid);
  445. }
  446. int __kmp_test_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  447. int retval;
  448. KMP_DEBUG_ASSERT(gtid >= 0);
  449. if (__kmp_get_futex_lock_owner(lck) == gtid) {
  450. retval = ++lck->lk.depth_locked;
  451. } else if (!__kmp_test_futex_lock(lck, gtid)) {
  452. retval = 0;
  453. } else {
  454. KMP_MB();
  455. retval = lck->lk.depth_locked = 1;
  456. }
  457. return retval;
  458. }
  459. static int __kmp_test_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
  460. kmp_int32 gtid) {
  461. char const *const func = "omp_test_nest_lock";
  462. if (!__kmp_is_futex_lock_nestable(lck)) {
  463. KMP_FATAL(LockSimpleUsedAsNestable, func);
  464. }
  465. return __kmp_test_nested_futex_lock(lck, gtid);
  466. }
  467. int __kmp_release_nested_futex_lock(kmp_futex_lock_t *lck, kmp_int32 gtid) {
  468. KMP_DEBUG_ASSERT(gtid >= 0);
  469. KMP_MB();
  470. if (--(lck->lk.depth_locked) == 0) {
  471. __kmp_release_futex_lock(lck, gtid);
  472. return KMP_LOCK_RELEASED;
  473. }
  474. return KMP_LOCK_STILL_HELD;
  475. }
  476. static int __kmp_release_nested_futex_lock_with_checks(kmp_futex_lock_t *lck,
  477. kmp_int32 gtid) {
  478. char const *const func = "omp_unset_nest_lock";
  479. KMP_MB(); /* in case another processor initialized lock */
  480. if (!__kmp_is_futex_lock_nestable(lck)) {
  481. KMP_FATAL(LockSimpleUsedAsNestable, func);
  482. }
  483. if (__kmp_get_futex_lock_owner(lck) == -1) {
  484. KMP_FATAL(LockUnsettingFree, func);
  485. }
  486. if (__kmp_get_futex_lock_owner(lck) != gtid) {
  487. KMP_FATAL(LockUnsettingSetByAnother, func);
  488. }
  489. return __kmp_release_nested_futex_lock(lck, gtid);
  490. }
  491. void __kmp_init_nested_futex_lock(kmp_futex_lock_t *lck) {
  492. __kmp_init_futex_lock(lck);
  493. lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
  494. }
  495. void __kmp_destroy_nested_futex_lock(kmp_futex_lock_t *lck) {
  496. __kmp_destroy_futex_lock(lck);
  497. lck->lk.depth_locked = 0;
  498. }
  499. static void __kmp_destroy_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  500. char const *const func = "omp_destroy_nest_lock";
  501. if (!__kmp_is_futex_lock_nestable(lck)) {
  502. KMP_FATAL(LockSimpleUsedAsNestable, func);
  503. }
  504. if (__kmp_get_futex_lock_owner(lck) != -1) {
  505. KMP_FATAL(LockStillOwned, func);
  506. }
  507. __kmp_destroy_nested_futex_lock(lck);
  508. }
  509. #endif // KMP_USE_FUTEX
  510. /* ------------------------------------------------------------------------ */
  511. /* ticket (bakery) locks */
  512. static kmp_int32 __kmp_get_ticket_lock_owner(kmp_ticket_lock_t *lck) {
  513. return std::atomic_load_explicit(&lck->lk.owner_id,
  514. std::memory_order_relaxed) -
  515. 1;
  516. }
  517. static inline bool __kmp_is_ticket_lock_nestable(kmp_ticket_lock_t *lck) {
  518. return std::atomic_load_explicit(&lck->lk.depth_locked,
  519. std::memory_order_relaxed) != -1;
  520. }
  521. static kmp_uint32 __kmp_bakery_check(void *now_serving, kmp_uint32 my_ticket) {
  522. return std::atomic_load_explicit((std::atomic<unsigned> *)now_serving,
  523. std::memory_order_acquire) == my_ticket;
  524. }
  525. __forceinline static int
  526. __kmp_acquire_ticket_lock_timed_template(kmp_ticket_lock_t *lck,
  527. kmp_int32 gtid) {
  528. kmp_uint32 my_ticket = std::atomic_fetch_add_explicit(
  529. &lck->lk.next_ticket, 1U, std::memory_order_relaxed);
  530. #ifdef USE_LOCK_PROFILE
  531. if (std::atomic_load_explicit(&lck->lk.now_serving,
  532. std::memory_order_relaxed) != my_ticket)
  533. __kmp_printf("LOCK CONTENTION: %p\n", lck);
  534. /* else __kmp_printf( "." );*/
  535. #endif /* USE_LOCK_PROFILE */
  536. if (std::atomic_load_explicit(&lck->lk.now_serving,
  537. std::memory_order_acquire) == my_ticket) {
  538. return KMP_LOCK_ACQUIRED_FIRST;
  539. }
  540. KMP_WAIT_PTR(&lck->lk.now_serving, my_ticket, __kmp_bakery_check, lck);
  541. return KMP_LOCK_ACQUIRED_FIRST;
  542. }
  543. int __kmp_acquire_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  544. int retval = __kmp_acquire_ticket_lock_timed_template(lck, gtid);
  545. return retval;
  546. }
  547. static int __kmp_acquire_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
  548. kmp_int32 gtid) {
  549. char const *const func = "omp_set_lock";
  550. if (!std::atomic_load_explicit(&lck->lk.initialized,
  551. std::memory_order_relaxed)) {
  552. KMP_FATAL(LockIsUninitialized, func);
  553. }
  554. if (lck->lk.self != lck) {
  555. KMP_FATAL(LockIsUninitialized, func);
  556. }
  557. if (__kmp_is_ticket_lock_nestable(lck)) {
  558. KMP_FATAL(LockNestableUsedAsSimple, func);
  559. }
  560. if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) == gtid)) {
  561. KMP_FATAL(LockIsAlreadyOwned, func);
  562. }
  563. __kmp_acquire_ticket_lock(lck, gtid);
  564. std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
  565. std::memory_order_relaxed);
  566. return KMP_LOCK_ACQUIRED_FIRST;
  567. }
  568. int __kmp_test_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  569. kmp_uint32 my_ticket = std::atomic_load_explicit(&lck->lk.next_ticket,
  570. std::memory_order_relaxed);
  571. if (std::atomic_load_explicit(&lck->lk.now_serving,
  572. std::memory_order_relaxed) == my_ticket) {
  573. kmp_uint32 next_ticket = my_ticket + 1;
  574. if (std::atomic_compare_exchange_strong_explicit(
  575. &lck->lk.next_ticket, &my_ticket, next_ticket,
  576. std::memory_order_acquire, std::memory_order_acquire)) {
  577. return TRUE;
  578. }
  579. }
  580. return FALSE;
  581. }
  582. static int __kmp_test_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
  583. kmp_int32 gtid) {
  584. char const *const func = "omp_test_lock";
  585. if (!std::atomic_load_explicit(&lck->lk.initialized,
  586. std::memory_order_relaxed)) {
  587. KMP_FATAL(LockIsUninitialized, func);
  588. }
  589. if (lck->lk.self != lck) {
  590. KMP_FATAL(LockIsUninitialized, func);
  591. }
  592. if (__kmp_is_ticket_lock_nestable(lck)) {
  593. KMP_FATAL(LockNestableUsedAsSimple, func);
  594. }
  595. int retval = __kmp_test_ticket_lock(lck, gtid);
  596. if (retval) {
  597. std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
  598. std::memory_order_relaxed);
  599. }
  600. return retval;
  601. }
  602. int __kmp_release_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  603. kmp_uint32 distance = std::atomic_load_explicit(&lck->lk.next_ticket,
  604. std::memory_order_relaxed) -
  605. std::atomic_load_explicit(&lck->lk.now_serving,
  606. std::memory_order_relaxed);
  607. std::atomic_fetch_add_explicit(&lck->lk.now_serving, 1U,
  608. std::memory_order_release);
  609. KMP_YIELD(distance >
  610. (kmp_uint32)(__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc));
  611. return KMP_LOCK_RELEASED;
  612. }
  613. static int __kmp_release_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
  614. kmp_int32 gtid) {
  615. char const *const func = "omp_unset_lock";
  616. if (!std::atomic_load_explicit(&lck->lk.initialized,
  617. std::memory_order_relaxed)) {
  618. KMP_FATAL(LockIsUninitialized, func);
  619. }
  620. if (lck->lk.self != lck) {
  621. KMP_FATAL(LockIsUninitialized, func);
  622. }
  623. if (__kmp_is_ticket_lock_nestable(lck)) {
  624. KMP_FATAL(LockNestableUsedAsSimple, func);
  625. }
  626. if (__kmp_get_ticket_lock_owner(lck) == -1) {
  627. KMP_FATAL(LockUnsettingFree, func);
  628. }
  629. if ((gtid >= 0) && (__kmp_get_ticket_lock_owner(lck) >= 0) &&
  630. (__kmp_get_ticket_lock_owner(lck) != gtid)) {
  631. KMP_FATAL(LockUnsettingSetByAnother, func);
  632. }
  633. std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
  634. return __kmp_release_ticket_lock(lck, gtid);
  635. }
  636. void __kmp_init_ticket_lock(kmp_ticket_lock_t *lck) {
  637. lck->lk.location = NULL;
  638. lck->lk.self = lck;
  639. std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
  640. std::memory_order_relaxed);
  641. std::atomic_store_explicit(&lck->lk.now_serving, 0U,
  642. std::memory_order_relaxed);
  643. std::atomic_store_explicit(
  644. &lck->lk.owner_id, 0,
  645. std::memory_order_relaxed); // no thread owns the lock.
  646. std::atomic_store_explicit(
  647. &lck->lk.depth_locked, -1,
  648. std::memory_order_relaxed); // -1 => not a nested lock.
  649. std::atomic_store_explicit(&lck->lk.initialized, true,
  650. std::memory_order_release);
  651. }
  652. void __kmp_destroy_ticket_lock(kmp_ticket_lock_t *lck) {
  653. std::atomic_store_explicit(&lck->lk.initialized, false,
  654. std::memory_order_release);
  655. lck->lk.self = NULL;
  656. lck->lk.location = NULL;
  657. std::atomic_store_explicit(&lck->lk.next_ticket, 0U,
  658. std::memory_order_relaxed);
  659. std::atomic_store_explicit(&lck->lk.now_serving, 0U,
  660. std::memory_order_relaxed);
  661. std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
  662. std::atomic_store_explicit(&lck->lk.depth_locked, -1,
  663. std::memory_order_relaxed);
  664. }
  665. static void __kmp_destroy_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  666. char const *const func = "omp_destroy_lock";
  667. if (!std::atomic_load_explicit(&lck->lk.initialized,
  668. std::memory_order_relaxed)) {
  669. KMP_FATAL(LockIsUninitialized, func);
  670. }
  671. if (lck->lk.self != lck) {
  672. KMP_FATAL(LockIsUninitialized, func);
  673. }
  674. if (__kmp_is_ticket_lock_nestable(lck)) {
  675. KMP_FATAL(LockNestableUsedAsSimple, func);
  676. }
  677. if (__kmp_get_ticket_lock_owner(lck) != -1) {
  678. KMP_FATAL(LockStillOwned, func);
  679. }
  680. __kmp_destroy_ticket_lock(lck);
  681. }
  682. // nested ticket locks
  683. int __kmp_acquire_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  684. KMP_DEBUG_ASSERT(gtid >= 0);
  685. if (__kmp_get_ticket_lock_owner(lck) == gtid) {
  686. std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
  687. std::memory_order_relaxed);
  688. return KMP_LOCK_ACQUIRED_NEXT;
  689. } else {
  690. __kmp_acquire_ticket_lock_timed_template(lck, gtid);
  691. std::atomic_store_explicit(&lck->lk.depth_locked, 1,
  692. std::memory_order_relaxed);
  693. std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
  694. std::memory_order_relaxed);
  695. return KMP_LOCK_ACQUIRED_FIRST;
  696. }
  697. }
  698. static int __kmp_acquire_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
  699. kmp_int32 gtid) {
  700. char const *const func = "omp_set_nest_lock";
  701. if (!std::atomic_load_explicit(&lck->lk.initialized,
  702. std::memory_order_relaxed)) {
  703. KMP_FATAL(LockIsUninitialized, func);
  704. }
  705. if (lck->lk.self != lck) {
  706. KMP_FATAL(LockIsUninitialized, func);
  707. }
  708. if (!__kmp_is_ticket_lock_nestable(lck)) {
  709. KMP_FATAL(LockSimpleUsedAsNestable, func);
  710. }
  711. return __kmp_acquire_nested_ticket_lock(lck, gtid);
  712. }
  713. int __kmp_test_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  714. int retval;
  715. KMP_DEBUG_ASSERT(gtid >= 0);
  716. if (__kmp_get_ticket_lock_owner(lck) == gtid) {
  717. retval = std::atomic_fetch_add_explicit(&lck->lk.depth_locked, 1,
  718. std::memory_order_relaxed) +
  719. 1;
  720. } else if (!__kmp_test_ticket_lock(lck, gtid)) {
  721. retval = 0;
  722. } else {
  723. std::atomic_store_explicit(&lck->lk.depth_locked, 1,
  724. std::memory_order_relaxed);
  725. std::atomic_store_explicit(&lck->lk.owner_id, gtid + 1,
  726. std::memory_order_relaxed);
  727. retval = 1;
  728. }
  729. return retval;
  730. }
  731. static int __kmp_test_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
  732. kmp_int32 gtid) {
  733. char const *const func = "omp_test_nest_lock";
  734. if (!std::atomic_load_explicit(&lck->lk.initialized,
  735. std::memory_order_relaxed)) {
  736. KMP_FATAL(LockIsUninitialized, func);
  737. }
  738. if (lck->lk.self != lck) {
  739. KMP_FATAL(LockIsUninitialized, func);
  740. }
  741. if (!__kmp_is_ticket_lock_nestable(lck)) {
  742. KMP_FATAL(LockSimpleUsedAsNestable, func);
  743. }
  744. return __kmp_test_nested_ticket_lock(lck, gtid);
  745. }
  746. int __kmp_release_nested_ticket_lock(kmp_ticket_lock_t *lck, kmp_int32 gtid) {
  747. KMP_DEBUG_ASSERT(gtid >= 0);
  748. if ((std::atomic_fetch_add_explicit(&lck->lk.depth_locked, -1,
  749. std::memory_order_relaxed) -
  750. 1) == 0) {
  751. std::atomic_store_explicit(&lck->lk.owner_id, 0, std::memory_order_relaxed);
  752. __kmp_release_ticket_lock(lck, gtid);
  753. return KMP_LOCK_RELEASED;
  754. }
  755. return KMP_LOCK_STILL_HELD;
  756. }
  757. static int __kmp_release_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck,
  758. kmp_int32 gtid) {
  759. char const *const func = "omp_unset_nest_lock";
  760. if (!std::atomic_load_explicit(&lck->lk.initialized,
  761. std::memory_order_relaxed)) {
  762. KMP_FATAL(LockIsUninitialized, func);
  763. }
  764. if (lck->lk.self != lck) {
  765. KMP_FATAL(LockIsUninitialized, func);
  766. }
  767. if (!__kmp_is_ticket_lock_nestable(lck)) {
  768. KMP_FATAL(LockSimpleUsedAsNestable, func);
  769. }
  770. if (__kmp_get_ticket_lock_owner(lck) == -1) {
  771. KMP_FATAL(LockUnsettingFree, func);
  772. }
  773. if (__kmp_get_ticket_lock_owner(lck) != gtid) {
  774. KMP_FATAL(LockUnsettingSetByAnother, func);
  775. }
  776. return __kmp_release_nested_ticket_lock(lck, gtid);
  777. }
  778. void __kmp_init_nested_ticket_lock(kmp_ticket_lock_t *lck) {
  779. __kmp_init_ticket_lock(lck);
  780. std::atomic_store_explicit(&lck->lk.depth_locked, 0,
  781. std::memory_order_relaxed);
  782. // >= 0 for nestable locks, -1 for simple locks
  783. }
  784. void __kmp_destroy_nested_ticket_lock(kmp_ticket_lock_t *lck) {
  785. __kmp_destroy_ticket_lock(lck);
  786. std::atomic_store_explicit(&lck->lk.depth_locked, 0,
  787. std::memory_order_relaxed);
  788. }
  789. static void
  790. __kmp_destroy_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  791. char const *const func = "omp_destroy_nest_lock";
  792. if (!std::atomic_load_explicit(&lck->lk.initialized,
  793. std::memory_order_relaxed)) {
  794. KMP_FATAL(LockIsUninitialized, func);
  795. }
  796. if (lck->lk.self != lck) {
  797. KMP_FATAL(LockIsUninitialized, func);
  798. }
  799. if (!__kmp_is_ticket_lock_nestable(lck)) {
  800. KMP_FATAL(LockSimpleUsedAsNestable, func);
  801. }
  802. if (__kmp_get_ticket_lock_owner(lck) != -1) {
  803. KMP_FATAL(LockStillOwned, func);
  804. }
  805. __kmp_destroy_nested_ticket_lock(lck);
  806. }
  807. // access functions to fields which don't exist for all lock kinds.
  808. static const ident_t *__kmp_get_ticket_lock_location(kmp_ticket_lock_t *lck) {
  809. return lck->lk.location;
  810. }
  811. static void __kmp_set_ticket_lock_location(kmp_ticket_lock_t *lck,
  812. const ident_t *loc) {
  813. lck->lk.location = loc;
  814. }
  815. static kmp_lock_flags_t __kmp_get_ticket_lock_flags(kmp_ticket_lock_t *lck) {
  816. return lck->lk.flags;
  817. }
  818. static void __kmp_set_ticket_lock_flags(kmp_ticket_lock_t *lck,
  819. kmp_lock_flags_t flags) {
  820. lck->lk.flags = flags;
  821. }
  822. /* ------------------------------------------------------------------------ */
  823. /* queuing locks */
  824. /* First the states
  825. (head,tail) = 0, 0 means lock is unheld, nobody on queue
  826. UINT_MAX or -1, 0 means lock is held, nobody on queue
  827. h, h means lock held or about to transition,
  828. 1 element on queue
  829. h, t h <> t, means lock is held or about to
  830. transition, >1 elements on queue
  831. Now the transitions
  832. Acquire(0,0) = -1 ,0
  833. Release(0,0) = Error
  834. Acquire(-1,0) = h ,h h > 0
  835. Release(-1,0) = 0 ,0
  836. Acquire(h,h) = h ,t h > 0, t > 0, h <> t
  837. Release(h,h) = -1 ,0 h > 0
  838. Acquire(h,t) = h ,t' h > 0, t > 0, t' > 0, h <> t, h <> t', t <> t'
  839. Release(h,t) = h',t h > 0, t > 0, h <> t, h <> h', h' maybe = t
  840. And pictorially
  841. +-----+
  842. | 0, 0|------- release -------> Error
  843. +-----+
  844. | ^
  845. acquire| |release
  846. | |
  847. | |
  848. v |
  849. +-----+
  850. |-1, 0|
  851. +-----+
  852. | ^
  853. acquire| |release
  854. | |
  855. | |
  856. v |
  857. +-----+
  858. | h, h|
  859. +-----+
  860. | ^
  861. acquire| |release
  862. | |
  863. | |
  864. v |
  865. +-----+
  866. | h, t|----- acquire, release loopback ---+
  867. +-----+ |
  868. ^ |
  869. | |
  870. +------------------------------------+
  871. */
  872. #ifdef DEBUG_QUEUING_LOCKS
  873. /* Stuff for circular trace buffer */
  874. #define TRACE_BUF_ELE 1024
  875. static char traces[TRACE_BUF_ELE][128] = {0};
  876. static int tc = 0;
  877. #define TRACE_LOCK(X, Y) \
  878. KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s\n", X, Y);
  879. #define TRACE_LOCK_T(X, Y, Z) \
  880. KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s%d\n", X, Y, Z);
  881. #define TRACE_LOCK_HT(X, Y, Z, Q) \
  882. KMP_SNPRINTF(traces[tc++ % TRACE_BUF_ELE], 128, "t%d at %s %d,%d\n", X, Y, \
  883. Z, Q);
  884. static void __kmp_dump_queuing_lock(kmp_info_t *this_thr, kmp_int32 gtid,
  885. kmp_queuing_lock_t *lck, kmp_int32 head_id,
  886. kmp_int32 tail_id) {
  887. kmp_int32 t, i;
  888. __kmp_printf_no_lock("\n__kmp_dump_queuing_lock: TRACE BEGINS HERE! \n");
  889. i = tc % TRACE_BUF_ELE;
  890. __kmp_printf_no_lock("%s\n", traces[i]);
  891. i = (i + 1) % TRACE_BUF_ELE;
  892. while (i != (tc % TRACE_BUF_ELE)) {
  893. __kmp_printf_no_lock("%s", traces[i]);
  894. i = (i + 1) % TRACE_BUF_ELE;
  895. }
  896. __kmp_printf_no_lock("\n");
  897. __kmp_printf_no_lock("\n__kmp_dump_queuing_lock: gtid+1:%d, spin_here:%d, "
  898. "next_wait:%d, head_id:%d, tail_id:%d\n",
  899. gtid + 1, this_thr->th.th_spin_here,
  900. this_thr->th.th_next_waiting, head_id, tail_id);
  901. __kmp_printf_no_lock("\t\thead: %d ", lck->lk.head_id);
  902. if (lck->lk.head_id >= 1) {
  903. t = __kmp_threads[lck->lk.head_id - 1]->th.th_next_waiting;
  904. while (t > 0) {
  905. __kmp_printf_no_lock("-> %d ", t);
  906. t = __kmp_threads[t - 1]->th.th_next_waiting;
  907. }
  908. }
  909. __kmp_printf_no_lock("; tail: %d ", lck->lk.tail_id);
  910. __kmp_printf_no_lock("\n\n");
  911. }
  912. #endif /* DEBUG_QUEUING_LOCKS */
  913. static kmp_int32 __kmp_get_queuing_lock_owner(kmp_queuing_lock_t *lck) {
  914. return TCR_4(lck->lk.owner_id) - 1;
  915. }
  916. static inline bool __kmp_is_queuing_lock_nestable(kmp_queuing_lock_t *lck) {
  917. return lck->lk.depth_locked != -1;
  918. }
  919. /* Acquire a lock using a the queuing lock implementation */
  920. template <bool takeTime>
  921. /* [TLW] The unused template above is left behind because of what BEB believes
  922. is a potential compiler problem with __forceinline. */
  923. __forceinline static int
  924. __kmp_acquire_queuing_lock_timed_template(kmp_queuing_lock_t *lck,
  925. kmp_int32 gtid) {
  926. kmp_info_t *this_thr = __kmp_thread_from_gtid(gtid);
  927. volatile kmp_int32 *head_id_p = &lck->lk.head_id;
  928. volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
  929. volatile kmp_uint32 *spin_here_p;
  930. #if OMPT_SUPPORT
  931. ompt_state_t prev_state = ompt_state_undefined;
  932. #endif
  933. KA_TRACE(1000,
  934. ("__kmp_acquire_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
  935. KMP_FSYNC_PREPARE(lck);
  936. KMP_DEBUG_ASSERT(this_thr != NULL);
  937. spin_here_p = &this_thr->th.th_spin_here;
  938. #ifdef DEBUG_QUEUING_LOCKS
  939. TRACE_LOCK(gtid + 1, "acq ent");
  940. if (*spin_here_p)
  941. __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
  942. if (this_thr->th.th_next_waiting != 0)
  943. __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
  944. #endif
  945. KMP_DEBUG_ASSERT(!*spin_here_p);
  946. KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
  947. /* The following st.rel to spin_here_p needs to precede the cmpxchg.acq to
  948. head_id_p that may follow, not just in execution order, but also in
  949. visibility order. This way, when a releasing thread observes the changes to
  950. the queue by this thread, it can rightly assume that spin_here_p has
  951. already been set to TRUE, so that when it sets spin_here_p to FALSE, it is
  952. not premature. If the releasing thread sets spin_here_p to FALSE before
  953. this thread sets it to TRUE, this thread will hang. */
  954. *spin_here_p = TRUE; /* before enqueuing to prevent race */
  955. while (1) {
  956. kmp_int32 enqueued;
  957. kmp_int32 head;
  958. kmp_int32 tail;
  959. head = *head_id_p;
  960. switch (head) {
  961. case -1: {
  962. #ifdef DEBUG_QUEUING_LOCKS
  963. tail = *tail_id_p;
  964. TRACE_LOCK_HT(gtid + 1, "acq read: ", head, tail);
  965. #endif
  966. tail = 0; /* to make sure next link asynchronously read is not set
  967. accidentally; this assignment prevents us from entering the
  968. if ( t > 0 ) condition in the enqueued case below, which is not
  969. necessary for this state transition */
  970. /* try (-1,0)->(tid,tid) */
  971. enqueued = KMP_COMPARE_AND_STORE_ACQ64((volatile kmp_int64 *)tail_id_p,
  972. KMP_PACK_64(-1, 0),
  973. KMP_PACK_64(gtid + 1, gtid + 1));
  974. #ifdef DEBUG_QUEUING_LOCKS
  975. if (enqueued)
  976. TRACE_LOCK(gtid + 1, "acq enq: (-1,0)->(tid,tid)");
  977. #endif
  978. } break;
  979. default: {
  980. tail = *tail_id_p;
  981. KMP_DEBUG_ASSERT(tail != gtid + 1);
  982. #ifdef DEBUG_QUEUING_LOCKS
  983. TRACE_LOCK_HT(gtid + 1, "acq read: ", head, tail);
  984. #endif
  985. if (tail == 0) {
  986. enqueued = FALSE;
  987. } else {
  988. /* try (h,t) or (h,h)->(h,tid) */
  989. enqueued = KMP_COMPARE_AND_STORE_ACQ32(tail_id_p, tail, gtid + 1);
  990. #ifdef DEBUG_QUEUING_LOCKS
  991. if (enqueued)
  992. TRACE_LOCK(gtid + 1, "acq enq: (h,t)->(h,tid)");
  993. #endif
  994. }
  995. } break;
  996. case 0: /* empty queue */
  997. {
  998. kmp_int32 grabbed_lock;
  999. #ifdef DEBUG_QUEUING_LOCKS
  1000. tail = *tail_id_p;
  1001. TRACE_LOCK_HT(gtid + 1, "acq read: ", head, tail);
  1002. #endif
  1003. /* try (0,0)->(-1,0) */
  1004. /* only legal transition out of head = 0 is head = -1 with no change to
  1005. * tail */
  1006. grabbed_lock = KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1);
  1007. if (grabbed_lock) {
  1008. *spin_here_p = FALSE;
  1009. KA_TRACE(
  1010. 1000,
  1011. ("__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: no queuing\n",
  1012. lck, gtid));
  1013. #ifdef DEBUG_QUEUING_LOCKS
  1014. TRACE_LOCK_HT(gtid + 1, "acq exit: ", head, 0);
  1015. #endif
  1016. #if OMPT_SUPPORT
  1017. if (ompt_enabled.enabled && prev_state != ompt_state_undefined) {
  1018. /* change the state before clearing wait_id */
  1019. this_thr->th.ompt_thread_info.state = prev_state;
  1020. this_thr->th.ompt_thread_info.wait_id = 0;
  1021. }
  1022. #endif
  1023. KMP_FSYNC_ACQUIRED(lck);
  1024. return KMP_LOCK_ACQUIRED_FIRST; /* lock holder cannot be on queue */
  1025. }
  1026. enqueued = FALSE;
  1027. } break;
  1028. }
  1029. #if OMPT_SUPPORT
  1030. if (ompt_enabled.enabled && prev_state == ompt_state_undefined) {
  1031. /* this thread will spin; set wait_id before entering wait state */
  1032. prev_state = this_thr->th.ompt_thread_info.state;
  1033. this_thr->th.ompt_thread_info.wait_id = (uint64_t)lck;
  1034. this_thr->th.ompt_thread_info.state = ompt_state_wait_lock;
  1035. }
  1036. #endif
  1037. if (enqueued) {
  1038. if (tail > 0) {
  1039. kmp_info_t *tail_thr = __kmp_thread_from_gtid(tail - 1);
  1040. KMP_ASSERT(tail_thr != NULL);
  1041. tail_thr->th.th_next_waiting = gtid + 1;
  1042. /* corresponding wait for this write in release code */
  1043. }
  1044. KA_TRACE(1000,
  1045. ("__kmp_acquire_queuing_lock: lck:%p, T#%d waiting for lock\n",
  1046. lck, gtid));
  1047. KMP_MB();
  1048. // ToDo: Use __kmp_wait_sleep or similar when blocktime != inf
  1049. KMP_WAIT(spin_here_p, FALSE, KMP_EQ, lck);
  1050. // Synchronize writes to both runtime thread structures
  1051. // and writes in user code.
  1052. KMP_MB();
  1053. #ifdef DEBUG_QUEUING_LOCKS
  1054. TRACE_LOCK(gtid + 1, "acq spin");
  1055. if (this_thr->th.th_next_waiting != 0)
  1056. __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
  1057. #endif
  1058. KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
  1059. KA_TRACE(1000, ("__kmp_acquire_queuing_lock: lck:%p, T#%d exiting: after "
  1060. "waiting on queue\n",
  1061. lck, gtid));
  1062. #ifdef DEBUG_QUEUING_LOCKS
  1063. TRACE_LOCK(gtid + 1, "acq exit 2");
  1064. #endif
  1065. #if OMPT_SUPPORT
  1066. /* change the state before clearing wait_id */
  1067. this_thr->th.ompt_thread_info.state = prev_state;
  1068. this_thr->th.ompt_thread_info.wait_id = 0;
  1069. #endif
  1070. /* got lock, we were dequeued by the thread that released lock */
  1071. return KMP_LOCK_ACQUIRED_FIRST;
  1072. }
  1073. /* Yield if number of threads > number of logical processors */
  1074. /* ToDo: Not sure why this should only be in oversubscription case,
  1075. maybe should be traditional YIELD_INIT/YIELD_WHEN loop */
  1076. KMP_YIELD_OVERSUB();
  1077. #ifdef DEBUG_QUEUING_LOCKS
  1078. TRACE_LOCK(gtid + 1, "acq retry");
  1079. #endif
  1080. }
  1081. KMP_ASSERT2(0, "should not get here");
  1082. return KMP_LOCK_ACQUIRED_FIRST;
  1083. }
  1084. int __kmp_acquire_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  1085. KMP_DEBUG_ASSERT(gtid >= 0);
  1086. int retval = __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
  1087. return retval;
  1088. }
  1089. static int __kmp_acquire_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  1090. kmp_int32 gtid) {
  1091. char const *const func = "omp_set_lock";
  1092. if (lck->lk.initialized != lck) {
  1093. KMP_FATAL(LockIsUninitialized, func);
  1094. }
  1095. if (__kmp_is_queuing_lock_nestable(lck)) {
  1096. KMP_FATAL(LockNestableUsedAsSimple, func);
  1097. }
  1098. if (__kmp_get_queuing_lock_owner(lck) == gtid) {
  1099. KMP_FATAL(LockIsAlreadyOwned, func);
  1100. }
  1101. __kmp_acquire_queuing_lock(lck, gtid);
  1102. lck->lk.owner_id = gtid + 1;
  1103. return KMP_LOCK_ACQUIRED_FIRST;
  1104. }
  1105. int __kmp_test_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  1106. volatile kmp_int32 *head_id_p = &lck->lk.head_id;
  1107. kmp_int32 head;
  1108. #ifdef KMP_DEBUG
  1109. kmp_info_t *this_thr;
  1110. #endif
  1111. KA_TRACE(1000, ("__kmp_test_queuing_lock: T#%d entering\n", gtid));
  1112. KMP_DEBUG_ASSERT(gtid >= 0);
  1113. #ifdef KMP_DEBUG
  1114. this_thr = __kmp_thread_from_gtid(gtid);
  1115. KMP_DEBUG_ASSERT(this_thr != NULL);
  1116. KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
  1117. #endif
  1118. head = *head_id_p;
  1119. if (head == 0) { /* nobody on queue, nobody holding */
  1120. /* try (0,0)->(-1,0) */
  1121. if (KMP_COMPARE_AND_STORE_ACQ32(head_id_p, 0, -1)) {
  1122. KA_TRACE(1000,
  1123. ("__kmp_test_queuing_lock: T#%d exiting: holding lock\n", gtid));
  1124. KMP_FSYNC_ACQUIRED(lck);
  1125. return TRUE;
  1126. }
  1127. }
  1128. KA_TRACE(1000,
  1129. ("__kmp_test_queuing_lock: T#%d exiting: without lock\n", gtid));
  1130. return FALSE;
  1131. }
  1132. static int __kmp_test_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  1133. kmp_int32 gtid) {
  1134. char const *const func = "omp_test_lock";
  1135. if (lck->lk.initialized != lck) {
  1136. KMP_FATAL(LockIsUninitialized, func);
  1137. }
  1138. if (__kmp_is_queuing_lock_nestable(lck)) {
  1139. KMP_FATAL(LockNestableUsedAsSimple, func);
  1140. }
  1141. int retval = __kmp_test_queuing_lock(lck, gtid);
  1142. if (retval) {
  1143. lck->lk.owner_id = gtid + 1;
  1144. }
  1145. return retval;
  1146. }
  1147. int __kmp_release_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  1148. volatile kmp_int32 *head_id_p = &lck->lk.head_id;
  1149. volatile kmp_int32 *tail_id_p = &lck->lk.tail_id;
  1150. KA_TRACE(1000,
  1151. ("__kmp_release_queuing_lock: lck:%p, T#%d entering\n", lck, gtid));
  1152. KMP_DEBUG_ASSERT(gtid >= 0);
  1153. #if KMP_DEBUG || DEBUG_QUEUING_LOCKS
  1154. kmp_info_t *this_thr = __kmp_thread_from_gtid(gtid);
  1155. #endif
  1156. KMP_DEBUG_ASSERT(this_thr != NULL);
  1157. #ifdef DEBUG_QUEUING_LOCKS
  1158. TRACE_LOCK(gtid + 1, "rel ent");
  1159. if (this_thr->th.th_spin_here)
  1160. __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
  1161. if (this_thr->th.th_next_waiting != 0)
  1162. __kmp_dump_queuing_lock(this_thr, gtid, lck, *head_id_p, *tail_id_p);
  1163. #endif
  1164. KMP_DEBUG_ASSERT(!this_thr->th.th_spin_here);
  1165. KMP_DEBUG_ASSERT(this_thr->th.th_next_waiting == 0);
  1166. KMP_FSYNC_RELEASING(lck);
  1167. while (1) {
  1168. kmp_int32 dequeued;
  1169. kmp_int32 head;
  1170. kmp_int32 tail;
  1171. head = *head_id_p;
  1172. #ifdef DEBUG_QUEUING_LOCKS
  1173. tail = *tail_id_p;
  1174. TRACE_LOCK_HT(gtid + 1, "rel read: ", head, tail);
  1175. if (head == 0)
  1176. __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
  1177. #endif
  1178. KMP_DEBUG_ASSERT(head !=
  1179. 0); /* holding the lock, head must be -1 or queue head */
  1180. if (head == -1) { /* nobody on queue */
  1181. /* try (-1,0)->(0,0) */
  1182. if (KMP_COMPARE_AND_STORE_REL32(head_id_p, -1, 0)) {
  1183. KA_TRACE(
  1184. 1000,
  1185. ("__kmp_release_queuing_lock: lck:%p, T#%d exiting: queue empty\n",
  1186. lck, gtid));
  1187. #ifdef DEBUG_QUEUING_LOCKS
  1188. TRACE_LOCK_HT(gtid + 1, "rel exit: ", 0, 0);
  1189. #endif
  1190. #if OMPT_SUPPORT
  1191. /* nothing to do - no other thread is trying to shift blame */
  1192. #endif
  1193. return KMP_LOCK_RELEASED;
  1194. }
  1195. dequeued = FALSE;
  1196. } else {
  1197. KMP_MB();
  1198. tail = *tail_id_p;
  1199. if (head == tail) { /* only one thread on the queue */
  1200. #ifdef DEBUG_QUEUING_LOCKS
  1201. if (head <= 0)
  1202. __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
  1203. #endif
  1204. KMP_DEBUG_ASSERT(head > 0);
  1205. /* try (h,h)->(-1,0) */
  1206. dequeued = KMP_COMPARE_AND_STORE_REL64(
  1207. RCAST(volatile kmp_int64 *, tail_id_p), KMP_PACK_64(head, head),
  1208. KMP_PACK_64(-1, 0));
  1209. #ifdef DEBUG_QUEUING_LOCKS
  1210. TRACE_LOCK(gtid + 1, "rel deq: (h,h)->(-1,0)");
  1211. #endif
  1212. } else {
  1213. volatile kmp_int32 *waiting_id_p;
  1214. kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
  1215. KMP_DEBUG_ASSERT(head_thr != NULL);
  1216. waiting_id_p = &head_thr->th.th_next_waiting;
  1217. /* Does this require synchronous reads? */
  1218. #ifdef DEBUG_QUEUING_LOCKS
  1219. if (head <= 0 || tail <= 0)
  1220. __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
  1221. #endif
  1222. KMP_DEBUG_ASSERT(head > 0 && tail > 0);
  1223. /* try (h,t)->(h',t) or (t,t) */
  1224. KMP_MB();
  1225. /* make sure enqueuing thread has time to update next waiting thread
  1226. * field */
  1227. *head_id_p =
  1228. KMP_WAIT((volatile kmp_uint32 *)waiting_id_p, 0, KMP_NEQ, NULL);
  1229. #ifdef DEBUG_QUEUING_LOCKS
  1230. TRACE_LOCK(gtid + 1, "rel deq: (h,t)->(h',t)");
  1231. #endif
  1232. dequeued = TRUE;
  1233. }
  1234. }
  1235. if (dequeued) {
  1236. kmp_info_t *head_thr = __kmp_thread_from_gtid(head - 1);
  1237. KMP_DEBUG_ASSERT(head_thr != NULL);
  1238. /* Does this require synchronous reads? */
  1239. #ifdef DEBUG_QUEUING_LOCKS
  1240. if (head <= 0 || tail <= 0)
  1241. __kmp_dump_queuing_lock(this_thr, gtid, lck, head, tail);
  1242. #endif
  1243. KMP_DEBUG_ASSERT(head > 0 && tail > 0);
  1244. /* For clean code only. Thread not released until next statement prevents
  1245. race with acquire code. */
  1246. head_thr->th.th_next_waiting = 0;
  1247. #ifdef DEBUG_QUEUING_LOCKS
  1248. TRACE_LOCK_T(gtid + 1, "rel nw=0 for t=", head);
  1249. #endif
  1250. KMP_MB();
  1251. /* reset spin value */
  1252. head_thr->th.th_spin_here = FALSE;
  1253. KA_TRACE(1000, ("__kmp_release_queuing_lock: lck:%p, T#%d exiting: after "
  1254. "dequeuing\n",
  1255. lck, gtid));
  1256. #ifdef DEBUG_QUEUING_LOCKS
  1257. TRACE_LOCK(gtid + 1, "rel exit 2");
  1258. #endif
  1259. return KMP_LOCK_RELEASED;
  1260. }
  1261. /* KMP_CPU_PAUSE(); don't want to make releasing thread hold up acquiring
  1262. threads */
  1263. #ifdef DEBUG_QUEUING_LOCKS
  1264. TRACE_LOCK(gtid + 1, "rel retry");
  1265. #endif
  1266. } /* while */
  1267. KMP_ASSERT2(0, "should not get here");
  1268. return KMP_LOCK_RELEASED;
  1269. }
  1270. static int __kmp_release_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  1271. kmp_int32 gtid) {
  1272. char const *const func = "omp_unset_lock";
  1273. KMP_MB(); /* in case another processor initialized lock */
  1274. if (lck->lk.initialized != lck) {
  1275. KMP_FATAL(LockIsUninitialized, func);
  1276. }
  1277. if (__kmp_is_queuing_lock_nestable(lck)) {
  1278. KMP_FATAL(LockNestableUsedAsSimple, func);
  1279. }
  1280. if (__kmp_get_queuing_lock_owner(lck) == -1) {
  1281. KMP_FATAL(LockUnsettingFree, func);
  1282. }
  1283. if (__kmp_get_queuing_lock_owner(lck) != gtid) {
  1284. KMP_FATAL(LockUnsettingSetByAnother, func);
  1285. }
  1286. lck->lk.owner_id = 0;
  1287. return __kmp_release_queuing_lock(lck, gtid);
  1288. }
  1289. void __kmp_init_queuing_lock(kmp_queuing_lock_t *lck) {
  1290. lck->lk.location = NULL;
  1291. lck->lk.head_id = 0;
  1292. lck->lk.tail_id = 0;
  1293. lck->lk.next_ticket = 0;
  1294. lck->lk.now_serving = 0;
  1295. lck->lk.owner_id = 0; // no thread owns the lock.
  1296. lck->lk.depth_locked = -1; // >= 0 for nestable locks, -1 for simple locks.
  1297. lck->lk.initialized = lck;
  1298. KA_TRACE(1000, ("__kmp_init_queuing_lock: lock %p initialized\n", lck));
  1299. }
  1300. void __kmp_destroy_queuing_lock(kmp_queuing_lock_t *lck) {
  1301. lck->lk.initialized = NULL;
  1302. lck->lk.location = NULL;
  1303. lck->lk.head_id = 0;
  1304. lck->lk.tail_id = 0;
  1305. lck->lk.next_ticket = 0;
  1306. lck->lk.now_serving = 0;
  1307. lck->lk.owner_id = 0;
  1308. lck->lk.depth_locked = -1;
  1309. }
  1310. static void __kmp_destroy_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  1311. char const *const func = "omp_destroy_lock";
  1312. if (lck->lk.initialized != lck) {
  1313. KMP_FATAL(LockIsUninitialized, func);
  1314. }
  1315. if (__kmp_is_queuing_lock_nestable(lck)) {
  1316. KMP_FATAL(LockNestableUsedAsSimple, func);
  1317. }
  1318. if (__kmp_get_queuing_lock_owner(lck) != -1) {
  1319. KMP_FATAL(LockStillOwned, func);
  1320. }
  1321. __kmp_destroy_queuing_lock(lck);
  1322. }
  1323. // nested queuing locks
  1324. int __kmp_acquire_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  1325. KMP_DEBUG_ASSERT(gtid >= 0);
  1326. if (__kmp_get_queuing_lock_owner(lck) == gtid) {
  1327. lck->lk.depth_locked += 1;
  1328. return KMP_LOCK_ACQUIRED_NEXT;
  1329. } else {
  1330. __kmp_acquire_queuing_lock_timed_template<false>(lck, gtid);
  1331. KMP_MB();
  1332. lck->lk.depth_locked = 1;
  1333. KMP_MB();
  1334. lck->lk.owner_id = gtid + 1;
  1335. return KMP_LOCK_ACQUIRED_FIRST;
  1336. }
  1337. }
  1338. static int
  1339. __kmp_acquire_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  1340. kmp_int32 gtid) {
  1341. char const *const func = "omp_set_nest_lock";
  1342. if (lck->lk.initialized != lck) {
  1343. KMP_FATAL(LockIsUninitialized, func);
  1344. }
  1345. if (!__kmp_is_queuing_lock_nestable(lck)) {
  1346. KMP_FATAL(LockSimpleUsedAsNestable, func);
  1347. }
  1348. return __kmp_acquire_nested_queuing_lock(lck, gtid);
  1349. }
  1350. int __kmp_test_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  1351. int retval;
  1352. KMP_DEBUG_ASSERT(gtid >= 0);
  1353. if (__kmp_get_queuing_lock_owner(lck) == gtid) {
  1354. retval = ++lck->lk.depth_locked;
  1355. } else if (!__kmp_test_queuing_lock(lck, gtid)) {
  1356. retval = 0;
  1357. } else {
  1358. KMP_MB();
  1359. retval = lck->lk.depth_locked = 1;
  1360. KMP_MB();
  1361. lck->lk.owner_id = gtid + 1;
  1362. }
  1363. return retval;
  1364. }
  1365. static int __kmp_test_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  1366. kmp_int32 gtid) {
  1367. char const *const func = "omp_test_nest_lock";
  1368. if (lck->lk.initialized != lck) {
  1369. KMP_FATAL(LockIsUninitialized, func);
  1370. }
  1371. if (!__kmp_is_queuing_lock_nestable(lck)) {
  1372. KMP_FATAL(LockSimpleUsedAsNestable, func);
  1373. }
  1374. return __kmp_test_nested_queuing_lock(lck, gtid);
  1375. }
  1376. int __kmp_release_nested_queuing_lock(kmp_queuing_lock_t *lck, kmp_int32 gtid) {
  1377. KMP_DEBUG_ASSERT(gtid >= 0);
  1378. KMP_MB();
  1379. if (--(lck->lk.depth_locked) == 0) {
  1380. KMP_MB();
  1381. lck->lk.owner_id = 0;
  1382. __kmp_release_queuing_lock(lck, gtid);
  1383. return KMP_LOCK_RELEASED;
  1384. }
  1385. return KMP_LOCK_STILL_HELD;
  1386. }
  1387. static int
  1388. __kmp_release_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  1389. kmp_int32 gtid) {
  1390. char const *const func = "omp_unset_nest_lock";
  1391. KMP_MB(); /* in case another processor initialized lock */
  1392. if (lck->lk.initialized != lck) {
  1393. KMP_FATAL(LockIsUninitialized, func);
  1394. }
  1395. if (!__kmp_is_queuing_lock_nestable(lck)) {
  1396. KMP_FATAL(LockSimpleUsedAsNestable, func);
  1397. }
  1398. if (__kmp_get_queuing_lock_owner(lck) == -1) {
  1399. KMP_FATAL(LockUnsettingFree, func);
  1400. }
  1401. if (__kmp_get_queuing_lock_owner(lck) != gtid) {
  1402. KMP_FATAL(LockUnsettingSetByAnother, func);
  1403. }
  1404. return __kmp_release_nested_queuing_lock(lck, gtid);
  1405. }
  1406. void __kmp_init_nested_queuing_lock(kmp_queuing_lock_t *lck) {
  1407. __kmp_init_queuing_lock(lck);
  1408. lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
  1409. }
  1410. void __kmp_destroy_nested_queuing_lock(kmp_queuing_lock_t *lck) {
  1411. __kmp_destroy_queuing_lock(lck);
  1412. lck->lk.depth_locked = 0;
  1413. }
  1414. static void
  1415. __kmp_destroy_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  1416. char const *const func = "omp_destroy_nest_lock";
  1417. if (lck->lk.initialized != lck) {
  1418. KMP_FATAL(LockIsUninitialized, func);
  1419. }
  1420. if (!__kmp_is_queuing_lock_nestable(lck)) {
  1421. KMP_FATAL(LockSimpleUsedAsNestable, func);
  1422. }
  1423. if (__kmp_get_queuing_lock_owner(lck) != -1) {
  1424. KMP_FATAL(LockStillOwned, func);
  1425. }
  1426. __kmp_destroy_nested_queuing_lock(lck);
  1427. }
  1428. // access functions to fields which don't exist for all lock kinds.
  1429. static const ident_t *__kmp_get_queuing_lock_location(kmp_queuing_lock_t *lck) {
  1430. return lck->lk.location;
  1431. }
  1432. static void __kmp_set_queuing_lock_location(kmp_queuing_lock_t *lck,
  1433. const ident_t *loc) {
  1434. lck->lk.location = loc;
  1435. }
  1436. static kmp_lock_flags_t __kmp_get_queuing_lock_flags(kmp_queuing_lock_t *lck) {
  1437. return lck->lk.flags;
  1438. }
  1439. static void __kmp_set_queuing_lock_flags(kmp_queuing_lock_t *lck,
  1440. kmp_lock_flags_t flags) {
  1441. lck->lk.flags = flags;
  1442. }
  1443. #if KMP_USE_ADAPTIVE_LOCKS
  1444. /* RTM Adaptive locks */
  1445. #if KMP_HAVE_RTM_INTRINSICS
  1446. #include <immintrin.h>
  1447. #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)
  1448. #else
  1449. // Values from the status register after failed speculation.
  1450. #define _XBEGIN_STARTED (~0u)
  1451. #define _XABORT_EXPLICIT (1 << 0)
  1452. #define _XABORT_RETRY (1 << 1)
  1453. #define _XABORT_CONFLICT (1 << 2)
  1454. #define _XABORT_CAPACITY (1 << 3)
  1455. #define _XABORT_DEBUG (1 << 4)
  1456. #define _XABORT_NESTED (1 << 5)
  1457. #define _XABORT_CODE(x) ((unsigned char)(((x) >> 24) & 0xFF))
  1458. // Aborts for which it's worth trying again immediately
  1459. #define SOFT_ABORT_MASK (_XABORT_RETRY | _XABORT_CONFLICT | _XABORT_EXPLICIT)
  1460. #define STRINGIZE_INTERNAL(arg) #arg
  1461. #define STRINGIZE(arg) STRINGIZE_INTERNAL(arg)
  1462. // Access to RTM instructions
  1463. /*A version of XBegin which returns -1 on speculation, and the value of EAX on
  1464. an abort. This is the same definition as the compiler intrinsic that will be
  1465. supported at some point. */
  1466. static __inline int _xbegin() {
  1467. int res = -1;
  1468. #if KMP_OS_WINDOWS
  1469. #if KMP_ARCH_X86_64
  1470. _asm {
  1471. _emit 0xC7
  1472. _emit 0xF8
  1473. _emit 2
  1474. _emit 0
  1475. _emit 0
  1476. _emit 0
  1477. jmp L2
  1478. mov res, eax
  1479. L2:
  1480. }
  1481. #else /* IA32 */
  1482. _asm {
  1483. _emit 0xC7
  1484. _emit 0xF8
  1485. _emit 2
  1486. _emit 0
  1487. _emit 0
  1488. _emit 0
  1489. jmp L2
  1490. mov res, eax
  1491. L2:
  1492. }
  1493. #endif // KMP_ARCH_X86_64
  1494. #else
  1495. /* Note that %eax must be noted as killed (clobbered), because the XSR is
  1496. returned in %eax(%rax) on abort. Other register values are restored, so
  1497. don't need to be killed.
  1498. We must also mark 'res' as an input and an output, since otherwise
  1499. 'res=-1' may be dropped as being dead, whereas we do need the assignment on
  1500. the successful (i.e., non-abort) path. */
  1501. __asm__ volatile("1: .byte 0xC7; .byte 0xF8;\n"
  1502. " .long 1f-1b-6\n"
  1503. " jmp 2f\n"
  1504. "1: movl %%eax,%0\n"
  1505. "2:"
  1506. : "+r"(res)::"memory", "%eax");
  1507. #endif // KMP_OS_WINDOWS
  1508. return res;
  1509. }
  1510. /* Transaction end */
  1511. static __inline void _xend() {
  1512. #if KMP_OS_WINDOWS
  1513. __asm {
  1514. _emit 0x0f
  1515. _emit 0x01
  1516. _emit 0xd5
  1517. }
  1518. #else
  1519. __asm__ volatile(".byte 0x0f; .byte 0x01; .byte 0xd5" ::: "memory");
  1520. #endif
  1521. }
  1522. /* This is a macro, the argument must be a single byte constant which can be
  1523. evaluated by the inline assembler, since it is emitted as a byte into the
  1524. assembly code. */
  1525. // clang-format off
  1526. #if KMP_OS_WINDOWS
  1527. #define _xabort(ARG) _asm _emit 0xc6 _asm _emit 0xf8 _asm _emit ARG
  1528. #else
  1529. #define _xabort(ARG) \
  1530. __asm__ volatile(".byte 0xC6; .byte 0xF8; .byte " STRINGIZE(ARG):::"memory");
  1531. #endif
  1532. // clang-format on
  1533. #endif // KMP_COMPILER_ICC && __INTEL_COMPILER >= 1300
  1534. // Statistics is collected for testing purpose
  1535. #if KMP_DEBUG_ADAPTIVE_LOCKS
  1536. // We accumulate speculative lock statistics when the lock is destroyed. We
  1537. // keep locks that haven't been destroyed in the liveLocks list so that we can
  1538. // grab their statistics too.
  1539. static kmp_adaptive_lock_statistics_t destroyedStats;
  1540. // To hold the list of live locks.
  1541. static kmp_adaptive_lock_info_t liveLocks;
  1542. // A lock so we can safely update the list of locks.
  1543. static kmp_bootstrap_lock_t chain_lock =
  1544. KMP_BOOTSTRAP_LOCK_INITIALIZER(chain_lock);
  1545. // Initialize the list of stats.
  1546. void __kmp_init_speculative_stats() {
  1547. kmp_adaptive_lock_info_t *lck = &liveLocks;
  1548. memset(CCAST(kmp_adaptive_lock_statistics_t *, &(lck->stats)), 0,
  1549. sizeof(lck->stats));
  1550. lck->stats.next = lck;
  1551. lck->stats.prev = lck;
  1552. KMP_ASSERT(lck->stats.next->stats.prev == lck);
  1553. KMP_ASSERT(lck->stats.prev->stats.next == lck);
  1554. __kmp_init_bootstrap_lock(&chain_lock);
  1555. }
  1556. // Insert the lock into the circular list
  1557. static void __kmp_remember_lock(kmp_adaptive_lock_info_t *lck) {
  1558. __kmp_acquire_bootstrap_lock(&chain_lock);
  1559. lck->stats.next = liveLocks.stats.next;
  1560. lck->stats.prev = &liveLocks;
  1561. liveLocks.stats.next = lck;
  1562. lck->stats.next->stats.prev = lck;
  1563. KMP_ASSERT(lck->stats.next->stats.prev == lck);
  1564. KMP_ASSERT(lck->stats.prev->stats.next == lck);
  1565. __kmp_release_bootstrap_lock(&chain_lock);
  1566. }
  1567. static void __kmp_forget_lock(kmp_adaptive_lock_info_t *lck) {
  1568. KMP_ASSERT(lck->stats.next->stats.prev == lck);
  1569. KMP_ASSERT(lck->stats.prev->stats.next == lck);
  1570. kmp_adaptive_lock_info_t *n = lck->stats.next;
  1571. kmp_adaptive_lock_info_t *p = lck->stats.prev;
  1572. n->stats.prev = p;
  1573. p->stats.next = n;
  1574. }
  1575. static void __kmp_zero_speculative_stats(kmp_adaptive_lock_info_t *lck) {
  1576. memset(CCAST(kmp_adaptive_lock_statistics_t *, &lck->stats), 0,
  1577. sizeof(lck->stats));
  1578. __kmp_remember_lock(lck);
  1579. }
  1580. static void __kmp_add_stats(kmp_adaptive_lock_statistics_t *t,
  1581. kmp_adaptive_lock_info_t *lck) {
  1582. kmp_adaptive_lock_statistics_t volatile *s = &lck->stats;
  1583. t->nonSpeculativeAcquireAttempts += lck->acquire_attempts;
  1584. t->successfulSpeculations += s->successfulSpeculations;
  1585. t->hardFailedSpeculations += s->hardFailedSpeculations;
  1586. t->softFailedSpeculations += s->softFailedSpeculations;
  1587. t->nonSpeculativeAcquires += s->nonSpeculativeAcquires;
  1588. t->lemmingYields += s->lemmingYields;
  1589. }
  1590. static void __kmp_accumulate_speculative_stats(kmp_adaptive_lock_info_t *lck) {
  1591. __kmp_acquire_bootstrap_lock(&chain_lock);
  1592. __kmp_add_stats(&destroyedStats, lck);
  1593. __kmp_forget_lock(lck);
  1594. __kmp_release_bootstrap_lock(&chain_lock);
  1595. }
  1596. static float percent(kmp_uint32 count, kmp_uint32 total) {
  1597. return (total == 0) ? 0.0 : (100.0 * count) / total;
  1598. }
  1599. void __kmp_print_speculative_stats() {
  1600. kmp_adaptive_lock_statistics_t total = destroyedStats;
  1601. kmp_adaptive_lock_info_t *lck;
  1602. for (lck = liveLocks.stats.next; lck != &liveLocks; lck = lck->stats.next) {
  1603. __kmp_add_stats(&total, lck);
  1604. }
  1605. kmp_adaptive_lock_statistics_t *t = &total;
  1606. kmp_uint32 totalSections =
  1607. t->nonSpeculativeAcquires + t->successfulSpeculations;
  1608. kmp_uint32 totalSpeculations = t->successfulSpeculations +
  1609. t->hardFailedSpeculations +
  1610. t->softFailedSpeculations;
  1611. if (totalSections <= 0)
  1612. return;
  1613. kmp_safe_raii_file_t statsFile;
  1614. if (strcmp(__kmp_speculative_statsfile, "-") == 0) {
  1615. statsFile.set_stdout();
  1616. } else {
  1617. size_t buffLen = KMP_STRLEN(__kmp_speculative_statsfile) + 20;
  1618. char buffer[buffLen];
  1619. KMP_SNPRINTF(&buffer[0], buffLen, __kmp_speculative_statsfile,
  1620. (kmp_int32)getpid());
  1621. statsFile.open(buffer, "w");
  1622. }
  1623. fprintf(statsFile, "Speculative lock statistics (all approximate!)\n");
  1624. fprintf(statsFile,
  1625. " Lock parameters: \n"
  1626. " max_soft_retries : %10d\n"
  1627. " max_badness : %10d\n",
  1628. __kmp_adaptive_backoff_params.max_soft_retries,
  1629. __kmp_adaptive_backoff_params.max_badness);
  1630. fprintf(statsFile, " Non-speculative acquire attempts : %10d\n",
  1631. t->nonSpeculativeAcquireAttempts);
  1632. fprintf(statsFile, " Total critical sections : %10d\n",
  1633. totalSections);
  1634. fprintf(statsFile, " Successful speculations : %10d (%5.1f%%)\n",
  1635. t->successfulSpeculations,
  1636. percent(t->successfulSpeculations, totalSections));
  1637. fprintf(statsFile, " Non-speculative acquires : %10d (%5.1f%%)\n",
  1638. t->nonSpeculativeAcquires,
  1639. percent(t->nonSpeculativeAcquires, totalSections));
  1640. fprintf(statsFile, " Lemming yields : %10d\n\n",
  1641. t->lemmingYields);
  1642. fprintf(statsFile, " Speculative acquire attempts : %10d\n",
  1643. totalSpeculations);
  1644. fprintf(statsFile, " Successes : %10d (%5.1f%%)\n",
  1645. t->successfulSpeculations,
  1646. percent(t->successfulSpeculations, totalSpeculations));
  1647. fprintf(statsFile, " Soft failures : %10d (%5.1f%%)\n",
  1648. t->softFailedSpeculations,
  1649. percent(t->softFailedSpeculations, totalSpeculations));
  1650. fprintf(statsFile, " Hard failures : %10d (%5.1f%%)\n",
  1651. t->hardFailedSpeculations,
  1652. percent(t->hardFailedSpeculations, totalSpeculations));
  1653. }
  1654. #define KMP_INC_STAT(lck, stat) (lck->lk.adaptive.stats.stat++)
  1655. #else
  1656. #define KMP_INC_STAT(lck, stat)
  1657. #endif // KMP_DEBUG_ADAPTIVE_LOCKS
  1658. static inline bool __kmp_is_unlocked_queuing_lock(kmp_queuing_lock_t *lck) {
  1659. // It is enough to check that the head_id is zero.
  1660. // We don't also need to check the tail.
  1661. bool res = lck->lk.head_id == 0;
  1662. // We need a fence here, since we must ensure that no memory operations
  1663. // from later in this thread float above that read.
  1664. #if KMP_COMPILER_ICC || KMP_COMPILER_ICX
  1665. _mm_mfence();
  1666. #else
  1667. __sync_synchronize();
  1668. #endif
  1669. return res;
  1670. }
  1671. // Functions for manipulating the badness
  1672. static __inline void
  1673. __kmp_update_badness_after_success(kmp_adaptive_lock_t *lck) {
  1674. // Reset the badness to zero so we eagerly try to speculate again
  1675. lck->lk.adaptive.badness = 0;
  1676. KMP_INC_STAT(lck, successfulSpeculations);
  1677. }
  1678. // Create a bit mask with one more set bit.
  1679. static __inline void __kmp_step_badness(kmp_adaptive_lock_t *lck) {
  1680. kmp_uint32 newBadness = (lck->lk.adaptive.badness << 1) | 1;
  1681. if (newBadness > lck->lk.adaptive.max_badness) {
  1682. return;
  1683. } else {
  1684. lck->lk.adaptive.badness = newBadness;
  1685. }
  1686. }
  1687. // Check whether speculation should be attempted.
  1688. KMP_ATTRIBUTE_TARGET_RTM
  1689. static __inline int __kmp_should_speculate(kmp_adaptive_lock_t *lck,
  1690. kmp_int32 gtid) {
  1691. kmp_uint32 badness = lck->lk.adaptive.badness;
  1692. kmp_uint32 attempts = lck->lk.adaptive.acquire_attempts;
  1693. int res = (attempts & badness) == 0;
  1694. return res;
  1695. }
  1696. // Attempt to acquire only the speculative lock.
  1697. // Does not back off to the non-speculative lock.
  1698. KMP_ATTRIBUTE_TARGET_RTM
  1699. static int __kmp_test_adaptive_lock_only(kmp_adaptive_lock_t *lck,
  1700. kmp_int32 gtid) {
  1701. int retries = lck->lk.adaptive.max_soft_retries;
  1702. // We don't explicitly count the start of speculation, rather we record the
  1703. // results (success, hard fail, soft fail). The sum of all of those is the
  1704. // total number of times we started speculation since all speculations must
  1705. // end one of those ways.
  1706. do {
  1707. kmp_uint32 status = _xbegin();
  1708. // Switch this in to disable actual speculation but exercise at least some
  1709. // of the rest of the code. Useful for debugging...
  1710. // kmp_uint32 status = _XABORT_NESTED;
  1711. if (status == _XBEGIN_STARTED) {
  1712. /* We have successfully started speculation. Check that no-one acquired
  1713. the lock for real between when we last looked and now. This also gets
  1714. the lock cache line into our read-set, which we need so that we'll
  1715. abort if anyone later claims it for real. */
  1716. if (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
  1717. // Lock is now visibly acquired, so someone beat us to it. Abort the
  1718. // transaction so we'll restart from _xbegin with the failure status.
  1719. _xabort(0x01);
  1720. KMP_ASSERT2(0, "should not get here");
  1721. }
  1722. return 1; // Lock has been acquired (speculatively)
  1723. } else {
  1724. // We have aborted, update the statistics
  1725. if (status & SOFT_ABORT_MASK) {
  1726. KMP_INC_STAT(lck, softFailedSpeculations);
  1727. // and loop round to retry.
  1728. } else {
  1729. KMP_INC_STAT(lck, hardFailedSpeculations);
  1730. // Give up if we had a hard failure.
  1731. break;
  1732. }
  1733. }
  1734. } while (retries--); // Loop while we have retries, and didn't fail hard.
  1735. // Either we had a hard failure or we didn't succeed softly after
  1736. // the full set of attempts, so back off the badness.
  1737. __kmp_step_badness(lck);
  1738. return 0;
  1739. }
  1740. // Attempt to acquire the speculative lock, or back off to the non-speculative
  1741. // one if the speculative lock cannot be acquired.
  1742. // We can succeed speculatively, non-speculatively, or fail.
  1743. static int __kmp_test_adaptive_lock(kmp_adaptive_lock_t *lck, kmp_int32 gtid) {
  1744. // First try to acquire the lock speculatively
  1745. if (__kmp_should_speculate(lck, gtid) &&
  1746. __kmp_test_adaptive_lock_only(lck, gtid))
  1747. return 1;
  1748. // Speculative acquisition failed, so try to acquire it non-speculatively.
  1749. // Count the non-speculative acquire attempt
  1750. lck->lk.adaptive.acquire_attempts++;
  1751. // Use base, non-speculative lock.
  1752. if (__kmp_test_queuing_lock(GET_QLK_PTR(lck), gtid)) {
  1753. KMP_INC_STAT(lck, nonSpeculativeAcquires);
  1754. return 1; // Lock is acquired (non-speculatively)
  1755. } else {
  1756. return 0; // Failed to acquire the lock, it's already visibly locked.
  1757. }
  1758. }
  1759. static int __kmp_test_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
  1760. kmp_int32 gtid) {
  1761. char const *const func = "omp_test_lock";
  1762. if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
  1763. KMP_FATAL(LockIsUninitialized, func);
  1764. }
  1765. int retval = __kmp_test_adaptive_lock(lck, gtid);
  1766. if (retval) {
  1767. lck->lk.qlk.owner_id = gtid + 1;
  1768. }
  1769. return retval;
  1770. }
  1771. // Block until we can acquire a speculative, adaptive lock. We check whether we
  1772. // should be trying to speculate. If we should be, we check the real lock to see
  1773. // if it is free, and, if not, pause without attempting to acquire it until it
  1774. // is. Then we try the speculative acquire. This means that although we suffer
  1775. // from lemmings a little (because all we can't acquire the lock speculatively
  1776. // until the queue of threads waiting has cleared), we don't get into a state
  1777. // where we can never acquire the lock speculatively (because we force the queue
  1778. // to clear by preventing new arrivals from entering the queue). This does mean
  1779. // that when we're trying to break lemmings, the lock is no longer fair. However
  1780. // OpenMP makes no guarantee that its locks are fair, so this isn't a real
  1781. // problem.
  1782. static void __kmp_acquire_adaptive_lock(kmp_adaptive_lock_t *lck,
  1783. kmp_int32 gtid) {
  1784. if (__kmp_should_speculate(lck, gtid)) {
  1785. if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
  1786. if (__kmp_test_adaptive_lock_only(lck, gtid))
  1787. return;
  1788. // We tried speculation and failed, so give up.
  1789. } else {
  1790. // We can't try speculation until the lock is free, so we pause here
  1791. // (without suspending on the queueing lock, to allow it to drain, then
  1792. // try again. All other threads will also see the same result for
  1793. // shouldSpeculate, so will be doing the same if they try to claim the
  1794. // lock from now on.
  1795. while (!__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(lck))) {
  1796. KMP_INC_STAT(lck, lemmingYields);
  1797. KMP_YIELD(TRUE);
  1798. }
  1799. if (__kmp_test_adaptive_lock_only(lck, gtid))
  1800. return;
  1801. }
  1802. }
  1803. // Speculative acquisition failed, so acquire it non-speculatively.
  1804. // Count the non-speculative acquire attempt
  1805. lck->lk.adaptive.acquire_attempts++;
  1806. __kmp_acquire_queuing_lock_timed_template<FALSE>(GET_QLK_PTR(lck), gtid);
  1807. // We have acquired the base lock, so count that.
  1808. KMP_INC_STAT(lck, nonSpeculativeAcquires);
  1809. }
  1810. static void __kmp_acquire_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
  1811. kmp_int32 gtid) {
  1812. char const *const func = "omp_set_lock";
  1813. if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
  1814. KMP_FATAL(LockIsUninitialized, func);
  1815. }
  1816. if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == gtid) {
  1817. KMP_FATAL(LockIsAlreadyOwned, func);
  1818. }
  1819. __kmp_acquire_adaptive_lock(lck, gtid);
  1820. lck->lk.qlk.owner_id = gtid + 1;
  1821. }
  1822. KMP_ATTRIBUTE_TARGET_RTM
  1823. static int __kmp_release_adaptive_lock(kmp_adaptive_lock_t *lck,
  1824. kmp_int32 gtid) {
  1825. if (__kmp_is_unlocked_queuing_lock(GET_QLK_PTR(
  1826. lck))) { // If the lock doesn't look claimed we must be speculating.
  1827. // (Or the user's code is buggy and they're releasing without locking;
  1828. // if we had XTEST we'd be able to check that case...)
  1829. _xend(); // Exit speculation
  1830. __kmp_update_badness_after_success(lck);
  1831. } else { // Since the lock *is* visibly locked we're not speculating,
  1832. // so should use the underlying lock's release scheme.
  1833. __kmp_release_queuing_lock(GET_QLK_PTR(lck), gtid);
  1834. }
  1835. return KMP_LOCK_RELEASED;
  1836. }
  1837. static int __kmp_release_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck,
  1838. kmp_int32 gtid) {
  1839. char const *const func = "omp_unset_lock";
  1840. KMP_MB(); /* in case another processor initialized lock */
  1841. if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
  1842. KMP_FATAL(LockIsUninitialized, func);
  1843. }
  1844. if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) == -1) {
  1845. KMP_FATAL(LockUnsettingFree, func);
  1846. }
  1847. if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != gtid) {
  1848. KMP_FATAL(LockUnsettingSetByAnother, func);
  1849. }
  1850. lck->lk.qlk.owner_id = 0;
  1851. __kmp_release_adaptive_lock(lck, gtid);
  1852. return KMP_LOCK_RELEASED;
  1853. }
  1854. static void __kmp_init_adaptive_lock(kmp_adaptive_lock_t *lck) {
  1855. __kmp_init_queuing_lock(GET_QLK_PTR(lck));
  1856. lck->lk.adaptive.badness = 0;
  1857. lck->lk.adaptive.acquire_attempts = 0; // nonSpeculativeAcquireAttempts = 0;
  1858. lck->lk.adaptive.max_soft_retries =
  1859. __kmp_adaptive_backoff_params.max_soft_retries;
  1860. lck->lk.adaptive.max_badness = __kmp_adaptive_backoff_params.max_badness;
  1861. #if KMP_DEBUG_ADAPTIVE_LOCKS
  1862. __kmp_zero_speculative_stats(&lck->lk.adaptive);
  1863. #endif
  1864. KA_TRACE(1000, ("__kmp_init_adaptive_lock: lock %p initialized\n", lck));
  1865. }
  1866. static void __kmp_destroy_adaptive_lock(kmp_adaptive_lock_t *lck) {
  1867. #if KMP_DEBUG_ADAPTIVE_LOCKS
  1868. __kmp_accumulate_speculative_stats(&lck->lk.adaptive);
  1869. #endif
  1870. __kmp_destroy_queuing_lock(GET_QLK_PTR(lck));
  1871. // Nothing needed for the speculative part.
  1872. }
  1873. static void __kmp_destroy_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
  1874. char const *const func = "omp_destroy_lock";
  1875. if (lck->lk.qlk.initialized != GET_QLK_PTR(lck)) {
  1876. KMP_FATAL(LockIsUninitialized, func);
  1877. }
  1878. if (__kmp_get_queuing_lock_owner(GET_QLK_PTR(lck)) != -1) {
  1879. KMP_FATAL(LockStillOwned, func);
  1880. }
  1881. __kmp_destroy_adaptive_lock(lck);
  1882. }
  1883. #endif // KMP_USE_ADAPTIVE_LOCKS
  1884. /* ------------------------------------------------------------------------ */
  1885. /* DRDPA ticket locks */
  1886. /* "DRDPA" means Dynamically Reconfigurable Distributed Polling Area */
  1887. static kmp_int32 __kmp_get_drdpa_lock_owner(kmp_drdpa_lock_t *lck) {
  1888. return lck->lk.owner_id - 1;
  1889. }
  1890. static inline bool __kmp_is_drdpa_lock_nestable(kmp_drdpa_lock_t *lck) {
  1891. return lck->lk.depth_locked != -1;
  1892. }
  1893. __forceinline static int
  1894. __kmp_acquire_drdpa_lock_timed_template(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  1895. kmp_uint64 ticket = KMP_ATOMIC_INC(&lck->lk.next_ticket);
  1896. kmp_uint64 mask = lck->lk.mask; // atomic load
  1897. std::atomic<kmp_uint64> *polls = lck->lk.polls;
  1898. #ifdef USE_LOCK_PROFILE
  1899. if (polls[ticket & mask] != ticket)
  1900. __kmp_printf("LOCK CONTENTION: %p\n", lck);
  1901. /* else __kmp_printf( "." );*/
  1902. #endif /* USE_LOCK_PROFILE */
  1903. // Now spin-wait, but reload the polls pointer and mask, in case the
  1904. // polling area has been reconfigured. Unless it is reconfigured, the
  1905. // reloads stay in L1 cache and are cheap.
  1906. //
  1907. // Keep this code in sync with KMP_WAIT, in kmp_dispatch.cpp !!!
  1908. // The current implementation of KMP_WAIT doesn't allow for mask
  1909. // and poll to be re-read every spin iteration.
  1910. kmp_uint32 spins;
  1911. kmp_uint64 time;
  1912. KMP_FSYNC_PREPARE(lck);
  1913. KMP_INIT_YIELD(spins);
  1914. KMP_INIT_BACKOFF(time);
  1915. while (polls[ticket & mask] < ticket) { // atomic load
  1916. KMP_YIELD_OVERSUB_ELSE_SPIN(spins, time);
  1917. // Re-read the mask and the poll pointer from the lock structure.
  1918. //
  1919. // Make certain that "mask" is read before "polls" !!!
  1920. //
  1921. // If another thread picks reconfigures the polling area and updates their
  1922. // values, and we get the new value of mask and the old polls pointer, we
  1923. // could access memory beyond the end of the old polling area.
  1924. mask = lck->lk.mask; // atomic load
  1925. polls = lck->lk.polls; // atomic load
  1926. }
  1927. // Critical section starts here
  1928. KMP_FSYNC_ACQUIRED(lck);
  1929. KA_TRACE(1000, ("__kmp_acquire_drdpa_lock: ticket #%lld acquired lock %p\n",
  1930. ticket, lck));
  1931. lck->lk.now_serving = ticket; // non-volatile store
  1932. // Deallocate a garbage polling area if we know that we are the last
  1933. // thread that could possibly access it.
  1934. //
  1935. // The >= check is in case __kmp_test_drdpa_lock() allocated the cleanup
  1936. // ticket.
  1937. if ((lck->lk.old_polls != NULL) && (ticket >= lck->lk.cleanup_ticket)) {
  1938. __kmp_free(lck->lk.old_polls);
  1939. lck->lk.old_polls = NULL;
  1940. lck->lk.cleanup_ticket = 0;
  1941. }
  1942. // Check to see if we should reconfigure the polling area.
  1943. // If there is still a garbage polling area to be deallocated from a
  1944. // previous reconfiguration, let a later thread reconfigure it.
  1945. if (lck->lk.old_polls == NULL) {
  1946. bool reconfigure = false;
  1947. std::atomic<kmp_uint64> *old_polls = polls;
  1948. kmp_uint32 num_polls = TCR_4(lck->lk.num_polls);
  1949. if (TCR_4(__kmp_nth) >
  1950. (__kmp_avail_proc ? __kmp_avail_proc : __kmp_xproc)) {
  1951. // We are in oversubscription mode. Contract the polling area
  1952. // down to a single location, if that hasn't been done already.
  1953. if (num_polls > 1) {
  1954. reconfigure = true;
  1955. num_polls = TCR_4(lck->lk.num_polls);
  1956. mask = 0;
  1957. num_polls = 1;
  1958. polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
  1959. sizeof(*polls));
  1960. polls[0] = ticket;
  1961. }
  1962. } else {
  1963. // We are in under/fully subscribed mode. Check the number of
  1964. // threads waiting on the lock. The size of the polling area
  1965. // should be at least the number of threads waiting.
  1966. kmp_uint64 num_waiting = TCR_8(lck->lk.next_ticket) - ticket - 1;
  1967. if (num_waiting > num_polls) {
  1968. kmp_uint32 old_num_polls = num_polls;
  1969. reconfigure = true;
  1970. do {
  1971. mask = (mask << 1) | 1;
  1972. num_polls *= 2;
  1973. } while (num_polls <= num_waiting);
  1974. // Allocate the new polling area, and copy the relevant portion
  1975. // of the old polling area to the new area. __kmp_allocate()
  1976. // zeroes the memory it allocates, and most of the old area is
  1977. // just zero padding, so we only copy the release counters.
  1978. polls = (std::atomic<kmp_uint64> *)__kmp_allocate(num_polls *
  1979. sizeof(*polls));
  1980. kmp_uint32 i;
  1981. for (i = 0; i < old_num_polls; i++) {
  1982. polls[i].store(old_polls[i]);
  1983. }
  1984. }
  1985. }
  1986. if (reconfigure) {
  1987. // Now write the updated fields back to the lock structure.
  1988. //
  1989. // Make certain that "polls" is written before "mask" !!!
  1990. //
  1991. // If another thread picks up the new value of mask and the old polls
  1992. // pointer , it could access memory beyond the end of the old polling
  1993. // area.
  1994. //
  1995. // On x86, we need memory fences.
  1996. KA_TRACE(1000, ("__kmp_acquire_drdpa_lock: ticket #%lld reconfiguring "
  1997. "lock %p to %d polls\n",
  1998. ticket, lck, num_polls));
  1999. lck->lk.old_polls = old_polls;
  2000. lck->lk.polls = polls; // atomic store
  2001. KMP_MB();
  2002. lck->lk.num_polls = num_polls;
  2003. lck->lk.mask = mask; // atomic store
  2004. KMP_MB();
  2005. // Only after the new polling area and mask have been flushed
  2006. // to main memory can we update the cleanup ticket field.
  2007. //
  2008. // volatile load / non-volatile store
  2009. lck->lk.cleanup_ticket = lck->lk.next_ticket;
  2010. }
  2011. }
  2012. return KMP_LOCK_ACQUIRED_FIRST;
  2013. }
  2014. int __kmp_acquire_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  2015. int retval = __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
  2016. return retval;
  2017. }
  2018. static int __kmp_acquire_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
  2019. kmp_int32 gtid) {
  2020. char const *const func = "omp_set_lock";
  2021. if (lck->lk.initialized != lck) {
  2022. KMP_FATAL(LockIsUninitialized, func);
  2023. }
  2024. if (__kmp_is_drdpa_lock_nestable(lck)) {
  2025. KMP_FATAL(LockNestableUsedAsSimple, func);
  2026. }
  2027. if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) == gtid)) {
  2028. KMP_FATAL(LockIsAlreadyOwned, func);
  2029. }
  2030. __kmp_acquire_drdpa_lock(lck, gtid);
  2031. lck->lk.owner_id = gtid + 1;
  2032. return KMP_LOCK_ACQUIRED_FIRST;
  2033. }
  2034. int __kmp_test_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  2035. // First get a ticket, then read the polls pointer and the mask.
  2036. // The polls pointer must be read before the mask!!! (See above)
  2037. kmp_uint64 ticket = lck->lk.next_ticket; // atomic load
  2038. std::atomic<kmp_uint64> *polls = lck->lk.polls;
  2039. kmp_uint64 mask = lck->lk.mask; // atomic load
  2040. if (polls[ticket & mask] == ticket) {
  2041. kmp_uint64 next_ticket = ticket + 1;
  2042. if (__kmp_atomic_compare_store_acq(&lck->lk.next_ticket, ticket,
  2043. next_ticket)) {
  2044. KMP_FSYNC_ACQUIRED(lck);
  2045. KA_TRACE(1000, ("__kmp_test_drdpa_lock: ticket #%lld acquired lock %p\n",
  2046. ticket, lck));
  2047. lck->lk.now_serving = ticket; // non-volatile store
  2048. // Since no threads are waiting, there is no possibility that we would
  2049. // want to reconfigure the polling area. We might have the cleanup ticket
  2050. // value (which says that it is now safe to deallocate old_polls), but
  2051. // we'll let a later thread which calls __kmp_acquire_lock do that - this
  2052. // routine isn't supposed to block, and we would risk blocks if we called
  2053. // __kmp_free() to do the deallocation.
  2054. return TRUE;
  2055. }
  2056. }
  2057. return FALSE;
  2058. }
  2059. static int __kmp_test_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
  2060. kmp_int32 gtid) {
  2061. char const *const func = "omp_test_lock";
  2062. if (lck->lk.initialized != lck) {
  2063. KMP_FATAL(LockIsUninitialized, func);
  2064. }
  2065. if (__kmp_is_drdpa_lock_nestable(lck)) {
  2066. KMP_FATAL(LockNestableUsedAsSimple, func);
  2067. }
  2068. int retval = __kmp_test_drdpa_lock(lck, gtid);
  2069. if (retval) {
  2070. lck->lk.owner_id = gtid + 1;
  2071. }
  2072. return retval;
  2073. }
  2074. int __kmp_release_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  2075. // Read the ticket value from the lock data struct, then the polls pointer and
  2076. // the mask. The polls pointer must be read before the mask!!! (See above)
  2077. kmp_uint64 ticket = lck->lk.now_serving + 1; // non-atomic load
  2078. std::atomic<kmp_uint64> *polls = lck->lk.polls; // atomic load
  2079. kmp_uint64 mask = lck->lk.mask; // atomic load
  2080. KA_TRACE(1000, ("__kmp_release_drdpa_lock: ticket #%lld released lock %p\n",
  2081. ticket - 1, lck));
  2082. KMP_FSYNC_RELEASING(lck);
  2083. polls[ticket & mask] = ticket; // atomic store
  2084. return KMP_LOCK_RELEASED;
  2085. }
  2086. static int __kmp_release_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
  2087. kmp_int32 gtid) {
  2088. char const *const func = "omp_unset_lock";
  2089. KMP_MB(); /* in case another processor initialized lock */
  2090. if (lck->lk.initialized != lck) {
  2091. KMP_FATAL(LockIsUninitialized, func);
  2092. }
  2093. if (__kmp_is_drdpa_lock_nestable(lck)) {
  2094. KMP_FATAL(LockNestableUsedAsSimple, func);
  2095. }
  2096. if (__kmp_get_drdpa_lock_owner(lck) == -1) {
  2097. KMP_FATAL(LockUnsettingFree, func);
  2098. }
  2099. if ((gtid >= 0) && (__kmp_get_drdpa_lock_owner(lck) >= 0) &&
  2100. (__kmp_get_drdpa_lock_owner(lck) != gtid)) {
  2101. KMP_FATAL(LockUnsettingSetByAnother, func);
  2102. }
  2103. lck->lk.owner_id = 0;
  2104. return __kmp_release_drdpa_lock(lck, gtid);
  2105. }
  2106. void __kmp_init_drdpa_lock(kmp_drdpa_lock_t *lck) {
  2107. lck->lk.location = NULL;
  2108. lck->lk.mask = 0;
  2109. lck->lk.num_polls = 1;
  2110. lck->lk.polls = (std::atomic<kmp_uint64> *)__kmp_allocate(
  2111. lck->lk.num_polls * sizeof(*(lck->lk.polls)));
  2112. lck->lk.cleanup_ticket = 0;
  2113. lck->lk.old_polls = NULL;
  2114. lck->lk.next_ticket = 0;
  2115. lck->lk.now_serving = 0;
  2116. lck->lk.owner_id = 0; // no thread owns the lock.
  2117. lck->lk.depth_locked = -1; // >= 0 for nestable locks, -1 for simple locks.
  2118. lck->lk.initialized = lck;
  2119. KA_TRACE(1000, ("__kmp_init_drdpa_lock: lock %p initialized\n", lck));
  2120. }
  2121. void __kmp_destroy_drdpa_lock(kmp_drdpa_lock_t *lck) {
  2122. lck->lk.initialized = NULL;
  2123. lck->lk.location = NULL;
  2124. if (lck->lk.polls.load() != NULL) {
  2125. __kmp_free(lck->lk.polls.load());
  2126. lck->lk.polls = NULL;
  2127. }
  2128. if (lck->lk.old_polls != NULL) {
  2129. __kmp_free(lck->lk.old_polls);
  2130. lck->lk.old_polls = NULL;
  2131. }
  2132. lck->lk.mask = 0;
  2133. lck->lk.num_polls = 0;
  2134. lck->lk.cleanup_ticket = 0;
  2135. lck->lk.next_ticket = 0;
  2136. lck->lk.now_serving = 0;
  2137. lck->lk.owner_id = 0;
  2138. lck->lk.depth_locked = -1;
  2139. }
  2140. static void __kmp_destroy_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  2141. char const *const func = "omp_destroy_lock";
  2142. if (lck->lk.initialized != lck) {
  2143. KMP_FATAL(LockIsUninitialized, func);
  2144. }
  2145. if (__kmp_is_drdpa_lock_nestable(lck)) {
  2146. KMP_FATAL(LockNestableUsedAsSimple, func);
  2147. }
  2148. if (__kmp_get_drdpa_lock_owner(lck) != -1) {
  2149. KMP_FATAL(LockStillOwned, func);
  2150. }
  2151. __kmp_destroy_drdpa_lock(lck);
  2152. }
  2153. // nested drdpa ticket locks
  2154. int __kmp_acquire_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  2155. KMP_DEBUG_ASSERT(gtid >= 0);
  2156. if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
  2157. lck->lk.depth_locked += 1;
  2158. return KMP_LOCK_ACQUIRED_NEXT;
  2159. } else {
  2160. __kmp_acquire_drdpa_lock_timed_template(lck, gtid);
  2161. KMP_MB();
  2162. lck->lk.depth_locked = 1;
  2163. KMP_MB();
  2164. lck->lk.owner_id = gtid + 1;
  2165. return KMP_LOCK_ACQUIRED_FIRST;
  2166. }
  2167. }
  2168. static void __kmp_acquire_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
  2169. kmp_int32 gtid) {
  2170. char const *const func = "omp_set_nest_lock";
  2171. if (lck->lk.initialized != lck) {
  2172. KMP_FATAL(LockIsUninitialized, func);
  2173. }
  2174. if (!__kmp_is_drdpa_lock_nestable(lck)) {
  2175. KMP_FATAL(LockSimpleUsedAsNestable, func);
  2176. }
  2177. __kmp_acquire_nested_drdpa_lock(lck, gtid);
  2178. }
  2179. int __kmp_test_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  2180. int retval;
  2181. KMP_DEBUG_ASSERT(gtid >= 0);
  2182. if (__kmp_get_drdpa_lock_owner(lck) == gtid) {
  2183. retval = ++lck->lk.depth_locked;
  2184. } else if (!__kmp_test_drdpa_lock(lck, gtid)) {
  2185. retval = 0;
  2186. } else {
  2187. KMP_MB();
  2188. retval = lck->lk.depth_locked = 1;
  2189. KMP_MB();
  2190. lck->lk.owner_id = gtid + 1;
  2191. }
  2192. return retval;
  2193. }
  2194. static int __kmp_test_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
  2195. kmp_int32 gtid) {
  2196. char const *const func = "omp_test_nest_lock";
  2197. if (lck->lk.initialized != lck) {
  2198. KMP_FATAL(LockIsUninitialized, func);
  2199. }
  2200. if (!__kmp_is_drdpa_lock_nestable(lck)) {
  2201. KMP_FATAL(LockSimpleUsedAsNestable, func);
  2202. }
  2203. return __kmp_test_nested_drdpa_lock(lck, gtid);
  2204. }
  2205. int __kmp_release_nested_drdpa_lock(kmp_drdpa_lock_t *lck, kmp_int32 gtid) {
  2206. KMP_DEBUG_ASSERT(gtid >= 0);
  2207. KMP_MB();
  2208. if (--(lck->lk.depth_locked) == 0) {
  2209. KMP_MB();
  2210. lck->lk.owner_id = 0;
  2211. __kmp_release_drdpa_lock(lck, gtid);
  2212. return KMP_LOCK_RELEASED;
  2213. }
  2214. return KMP_LOCK_STILL_HELD;
  2215. }
  2216. static int __kmp_release_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck,
  2217. kmp_int32 gtid) {
  2218. char const *const func = "omp_unset_nest_lock";
  2219. KMP_MB(); /* in case another processor initialized lock */
  2220. if (lck->lk.initialized != lck) {
  2221. KMP_FATAL(LockIsUninitialized, func);
  2222. }
  2223. if (!__kmp_is_drdpa_lock_nestable(lck)) {
  2224. KMP_FATAL(LockSimpleUsedAsNestable, func);
  2225. }
  2226. if (__kmp_get_drdpa_lock_owner(lck) == -1) {
  2227. KMP_FATAL(LockUnsettingFree, func);
  2228. }
  2229. if (__kmp_get_drdpa_lock_owner(lck) != gtid) {
  2230. KMP_FATAL(LockUnsettingSetByAnother, func);
  2231. }
  2232. return __kmp_release_nested_drdpa_lock(lck, gtid);
  2233. }
  2234. void __kmp_init_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
  2235. __kmp_init_drdpa_lock(lck);
  2236. lck->lk.depth_locked = 0; // >= 0 for nestable locks, -1 for simple locks
  2237. }
  2238. void __kmp_destroy_nested_drdpa_lock(kmp_drdpa_lock_t *lck) {
  2239. __kmp_destroy_drdpa_lock(lck);
  2240. lck->lk.depth_locked = 0;
  2241. }
  2242. static void __kmp_destroy_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  2243. char const *const func = "omp_destroy_nest_lock";
  2244. if (lck->lk.initialized != lck) {
  2245. KMP_FATAL(LockIsUninitialized, func);
  2246. }
  2247. if (!__kmp_is_drdpa_lock_nestable(lck)) {
  2248. KMP_FATAL(LockSimpleUsedAsNestable, func);
  2249. }
  2250. if (__kmp_get_drdpa_lock_owner(lck) != -1) {
  2251. KMP_FATAL(LockStillOwned, func);
  2252. }
  2253. __kmp_destroy_nested_drdpa_lock(lck);
  2254. }
  2255. // access functions to fields which don't exist for all lock kinds.
  2256. static const ident_t *__kmp_get_drdpa_lock_location(kmp_drdpa_lock_t *lck) {
  2257. return lck->lk.location;
  2258. }
  2259. static void __kmp_set_drdpa_lock_location(kmp_drdpa_lock_t *lck,
  2260. const ident_t *loc) {
  2261. lck->lk.location = loc;
  2262. }
  2263. static kmp_lock_flags_t __kmp_get_drdpa_lock_flags(kmp_drdpa_lock_t *lck) {
  2264. return lck->lk.flags;
  2265. }
  2266. static void __kmp_set_drdpa_lock_flags(kmp_drdpa_lock_t *lck,
  2267. kmp_lock_flags_t flags) {
  2268. lck->lk.flags = flags;
  2269. }
  2270. // Time stamp counter
  2271. #if KMP_ARCH_X86 || KMP_ARCH_X86_64
  2272. #define __kmp_tsc() __kmp_hardware_timestamp()
  2273. // Runtime's default backoff parameters
  2274. kmp_backoff_t __kmp_spin_backoff_params = {1, 4096, 100};
  2275. #else
  2276. // Use nanoseconds for other platforms
  2277. extern kmp_uint64 __kmp_now_nsec();
  2278. kmp_backoff_t __kmp_spin_backoff_params = {1, 256, 100};
  2279. #define __kmp_tsc() __kmp_now_nsec()
  2280. #endif
  2281. // A useful predicate for dealing with timestamps that may wrap.
  2282. // Is a before b? Since the timestamps may wrap, this is asking whether it's
  2283. // shorter to go clockwise from a to b around the clock-face, or anti-clockwise.
  2284. // Times where going clockwise is less distance than going anti-clockwise
  2285. // are in the future, others are in the past. e.g. a = MAX-1, b = MAX+1 (=0),
  2286. // then a > b (true) does not mean a reached b; whereas signed(a) = -2,
  2287. // signed(b) = 0 captures the actual difference
  2288. static inline bool before(kmp_uint64 a, kmp_uint64 b) {
  2289. return ((kmp_int64)b - (kmp_int64)a) > 0;
  2290. }
  2291. // Truncated binary exponential backoff function
  2292. void __kmp_spin_backoff(kmp_backoff_t *boff) {
  2293. // We could flatten this loop, but making it a nested loop gives better result
  2294. kmp_uint32 i;
  2295. for (i = boff->step; i > 0; i--) {
  2296. kmp_uint64 goal = __kmp_tsc() + boff->min_tick;
  2297. #if KMP_HAVE_UMWAIT
  2298. if (__kmp_umwait_enabled) {
  2299. __kmp_tpause(0, boff->min_tick);
  2300. } else {
  2301. #endif
  2302. do {
  2303. KMP_CPU_PAUSE();
  2304. } while (before(__kmp_tsc(), goal));
  2305. #if KMP_HAVE_UMWAIT
  2306. }
  2307. #endif
  2308. }
  2309. boff->step = (boff->step << 1 | 1) & (boff->max_backoff - 1);
  2310. }
  2311. #if KMP_USE_DYNAMIC_LOCK
  2312. // Direct lock initializers. It simply writes a tag to the low 8 bits of the
  2313. // lock word.
  2314. static void __kmp_init_direct_lock(kmp_dyna_lock_t *lck,
  2315. kmp_dyna_lockseq_t seq) {
  2316. TCW_4(*lck, KMP_GET_D_TAG(seq));
  2317. KA_TRACE(
  2318. 20,
  2319. ("__kmp_init_direct_lock: initialized direct lock with type#%d\n", seq));
  2320. }
  2321. #if KMP_USE_TSX
  2322. // HLE lock functions - imported from the testbed runtime.
  2323. #define HLE_ACQUIRE ".byte 0xf2;"
  2324. #define HLE_RELEASE ".byte 0xf3;"
  2325. static inline kmp_uint32 swap4(kmp_uint32 volatile *p, kmp_uint32 v) {
  2326. __asm__ volatile(HLE_ACQUIRE "xchg %1,%0" : "+r"(v), "+m"(*p) : : "memory");
  2327. return v;
  2328. }
  2329. static void __kmp_destroy_hle_lock(kmp_dyna_lock_t *lck) { TCW_4(*lck, 0); }
  2330. static void __kmp_destroy_hle_lock_with_checks(kmp_dyna_lock_t *lck) {
  2331. TCW_4(*lck, 0);
  2332. }
  2333. static void __kmp_acquire_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
  2334. // Use gtid for KMP_LOCK_BUSY if necessary
  2335. if (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle)) {
  2336. int delay = 1;
  2337. do {
  2338. while (*(kmp_uint32 volatile *)lck != KMP_LOCK_FREE(hle)) {
  2339. for (int i = delay; i != 0; --i)
  2340. KMP_CPU_PAUSE();
  2341. delay = ((delay << 1) | 1) & 7;
  2342. }
  2343. } while (swap4(lck, KMP_LOCK_BUSY(1, hle)) != KMP_LOCK_FREE(hle));
  2344. }
  2345. }
  2346. static void __kmp_acquire_hle_lock_with_checks(kmp_dyna_lock_t *lck,
  2347. kmp_int32 gtid) {
  2348. __kmp_acquire_hle_lock(lck, gtid); // TODO: add checks
  2349. }
  2350. static int __kmp_release_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
  2351. __asm__ volatile(HLE_RELEASE "movl %1,%0"
  2352. : "=m"(*lck)
  2353. : "r"(KMP_LOCK_FREE(hle))
  2354. : "memory");
  2355. return KMP_LOCK_RELEASED;
  2356. }
  2357. static int __kmp_release_hle_lock_with_checks(kmp_dyna_lock_t *lck,
  2358. kmp_int32 gtid) {
  2359. return __kmp_release_hle_lock(lck, gtid); // TODO: add checks
  2360. }
  2361. static int __kmp_test_hle_lock(kmp_dyna_lock_t *lck, kmp_int32 gtid) {
  2362. return swap4(lck, KMP_LOCK_BUSY(1, hle)) == KMP_LOCK_FREE(hle);
  2363. }
  2364. static int __kmp_test_hle_lock_with_checks(kmp_dyna_lock_t *lck,
  2365. kmp_int32 gtid) {
  2366. return __kmp_test_hle_lock(lck, gtid); // TODO: add checks
  2367. }
  2368. static void __kmp_init_rtm_queuing_lock(kmp_queuing_lock_t *lck) {
  2369. __kmp_init_queuing_lock(lck);
  2370. }
  2371. static void __kmp_destroy_rtm_queuing_lock(kmp_queuing_lock_t *lck) {
  2372. __kmp_destroy_queuing_lock(lck);
  2373. }
  2374. static void
  2375. __kmp_destroy_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  2376. __kmp_destroy_queuing_lock_with_checks(lck);
  2377. }
  2378. KMP_ATTRIBUTE_TARGET_RTM
  2379. static void __kmp_acquire_rtm_queuing_lock(kmp_queuing_lock_t *lck,
  2380. kmp_int32 gtid) {
  2381. unsigned retries = 3, status;
  2382. do {
  2383. status = _xbegin();
  2384. if (status == _XBEGIN_STARTED) {
  2385. if (__kmp_is_unlocked_queuing_lock(lck))
  2386. return;
  2387. _xabort(0xff);
  2388. }
  2389. if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
  2390. // Wait until lock becomes free
  2391. while (!__kmp_is_unlocked_queuing_lock(lck)) {
  2392. KMP_YIELD(TRUE);
  2393. }
  2394. } else if (!(status & _XABORT_RETRY))
  2395. break;
  2396. } while (retries--);
  2397. // Fall-back non-speculative lock (xchg)
  2398. __kmp_acquire_queuing_lock(lck, gtid);
  2399. }
  2400. static void __kmp_acquire_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  2401. kmp_int32 gtid) {
  2402. __kmp_acquire_rtm_queuing_lock(lck, gtid);
  2403. }
  2404. KMP_ATTRIBUTE_TARGET_RTM
  2405. static int __kmp_release_rtm_queuing_lock(kmp_queuing_lock_t *lck,
  2406. kmp_int32 gtid) {
  2407. if (__kmp_is_unlocked_queuing_lock(lck)) {
  2408. // Releasing from speculation
  2409. _xend();
  2410. } else {
  2411. // Releasing from a real lock
  2412. __kmp_release_queuing_lock(lck, gtid);
  2413. }
  2414. return KMP_LOCK_RELEASED;
  2415. }
  2416. static int __kmp_release_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  2417. kmp_int32 gtid) {
  2418. return __kmp_release_rtm_queuing_lock(lck, gtid);
  2419. }
  2420. KMP_ATTRIBUTE_TARGET_RTM
  2421. static int __kmp_test_rtm_queuing_lock(kmp_queuing_lock_t *lck,
  2422. kmp_int32 gtid) {
  2423. unsigned retries = 3, status;
  2424. do {
  2425. status = _xbegin();
  2426. if (status == _XBEGIN_STARTED && __kmp_is_unlocked_queuing_lock(lck)) {
  2427. return 1;
  2428. }
  2429. if (!(status & _XABORT_RETRY))
  2430. break;
  2431. } while (retries--);
  2432. return __kmp_test_queuing_lock(lck, gtid);
  2433. }
  2434. static int __kmp_test_rtm_queuing_lock_with_checks(kmp_queuing_lock_t *lck,
  2435. kmp_int32 gtid) {
  2436. return __kmp_test_rtm_queuing_lock(lck, gtid);
  2437. }
  2438. // Reuse kmp_tas_lock_t for TSX lock which use RTM with fall-back spin lock.
  2439. typedef kmp_tas_lock_t kmp_rtm_spin_lock_t;
  2440. static void __kmp_destroy_rtm_spin_lock(kmp_rtm_spin_lock_t *lck) {
  2441. KMP_ATOMIC_ST_REL(&lck->lk.poll, 0);
  2442. }
  2443. static void __kmp_destroy_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck) {
  2444. __kmp_destroy_rtm_spin_lock(lck);
  2445. }
  2446. KMP_ATTRIBUTE_TARGET_RTM
  2447. static int __kmp_acquire_rtm_spin_lock(kmp_rtm_spin_lock_t *lck,
  2448. kmp_int32 gtid) {
  2449. unsigned retries = 3, status;
  2450. kmp_int32 lock_free = KMP_LOCK_FREE(rtm_spin);
  2451. kmp_int32 lock_busy = KMP_LOCK_BUSY(1, rtm_spin);
  2452. do {
  2453. status = _xbegin();
  2454. if (status == _XBEGIN_STARTED) {
  2455. if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free)
  2456. return KMP_LOCK_ACQUIRED_FIRST;
  2457. _xabort(0xff);
  2458. }
  2459. if ((status & _XABORT_EXPLICIT) && _XABORT_CODE(status) == 0xff) {
  2460. // Wait until lock becomes free
  2461. while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != lock_free) {
  2462. KMP_YIELD(TRUE);
  2463. }
  2464. } else if (!(status & _XABORT_RETRY))
  2465. break;
  2466. } while (retries--);
  2467. // Fall-back spin lock
  2468. KMP_FSYNC_PREPARE(lck);
  2469. kmp_backoff_t backoff = __kmp_spin_backoff_params;
  2470. while (KMP_ATOMIC_LD_RLX(&lck->lk.poll) != lock_free ||
  2471. !__kmp_atomic_compare_store_acq(&lck->lk.poll, lock_free, lock_busy)) {
  2472. __kmp_spin_backoff(&backoff);
  2473. }
  2474. KMP_FSYNC_ACQUIRED(lck);
  2475. return KMP_LOCK_ACQUIRED_FIRST;
  2476. }
  2477. static int __kmp_acquire_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
  2478. kmp_int32 gtid) {
  2479. return __kmp_acquire_rtm_spin_lock(lck, gtid);
  2480. }
  2481. KMP_ATTRIBUTE_TARGET_RTM
  2482. static int __kmp_release_rtm_spin_lock(kmp_rtm_spin_lock_t *lck,
  2483. kmp_int32 gtid) {
  2484. if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == KMP_LOCK_FREE(rtm_spin)) {
  2485. // Releasing from speculation
  2486. _xend();
  2487. } else {
  2488. // Releasing from a real lock
  2489. KMP_FSYNC_RELEASING(lck);
  2490. KMP_ATOMIC_ST_REL(&lck->lk.poll, KMP_LOCK_FREE(rtm_spin));
  2491. }
  2492. return KMP_LOCK_RELEASED;
  2493. }
  2494. static int __kmp_release_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
  2495. kmp_int32 gtid) {
  2496. return __kmp_release_rtm_spin_lock(lck, gtid);
  2497. }
  2498. KMP_ATTRIBUTE_TARGET_RTM
  2499. static int __kmp_test_rtm_spin_lock(kmp_rtm_spin_lock_t *lck, kmp_int32 gtid) {
  2500. unsigned retries = 3, status;
  2501. kmp_int32 lock_free = KMP_LOCK_FREE(rtm_spin);
  2502. kmp_int32 lock_busy = KMP_LOCK_BUSY(1, rtm_spin);
  2503. do {
  2504. status = _xbegin();
  2505. if (status == _XBEGIN_STARTED &&
  2506. KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free) {
  2507. return TRUE;
  2508. }
  2509. if (!(status & _XABORT_RETRY))
  2510. break;
  2511. } while (retries--);
  2512. if (KMP_ATOMIC_LD_RLX(&lck->lk.poll) == lock_free &&
  2513. __kmp_atomic_compare_store_acq(&lck->lk.poll, lock_free, lock_busy)) {
  2514. KMP_FSYNC_ACQUIRED(lck);
  2515. return TRUE;
  2516. }
  2517. return FALSE;
  2518. }
  2519. static int __kmp_test_rtm_spin_lock_with_checks(kmp_rtm_spin_lock_t *lck,
  2520. kmp_int32 gtid) {
  2521. return __kmp_test_rtm_spin_lock(lck, gtid);
  2522. }
  2523. #endif // KMP_USE_TSX
  2524. // Entry functions for indirect locks (first element of direct lock jump tables)
  2525. static void __kmp_init_indirect_lock(kmp_dyna_lock_t *l,
  2526. kmp_dyna_lockseq_t tag);
  2527. static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock);
  2528. static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
  2529. static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
  2530. static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32);
  2531. static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
  2532. kmp_int32);
  2533. static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
  2534. kmp_int32);
  2535. static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
  2536. kmp_int32);
  2537. // Lock function definitions for the union parameter type
  2538. #define KMP_FOREACH_LOCK_KIND(m, a) m(ticket, a) m(queuing, a) m(drdpa, a)
  2539. #define expand1(lk, op) \
  2540. static void __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock) { \
  2541. __kmp_##op##_##lk##_##lock(&lock->lk); \
  2542. }
  2543. #define expand2(lk, op) \
  2544. static int __kmp_##op##_##lk##_##lock(kmp_user_lock_p lock, \
  2545. kmp_int32 gtid) { \
  2546. return __kmp_##op##_##lk##_##lock(&lock->lk, gtid); \
  2547. }
  2548. #define expand3(lk, op) \
  2549. static void __kmp_set_##lk##_##lock_flags(kmp_user_lock_p lock, \
  2550. kmp_lock_flags_t flags) { \
  2551. __kmp_set_##lk##_lock_flags(&lock->lk, flags); \
  2552. }
  2553. #define expand4(lk, op) \
  2554. static void __kmp_set_##lk##_##lock_location(kmp_user_lock_p lock, \
  2555. const ident_t *loc) { \
  2556. __kmp_set_##lk##_lock_location(&lock->lk, loc); \
  2557. }
  2558. KMP_FOREACH_LOCK_KIND(expand1, init)
  2559. KMP_FOREACH_LOCK_KIND(expand1, init_nested)
  2560. KMP_FOREACH_LOCK_KIND(expand1, destroy)
  2561. KMP_FOREACH_LOCK_KIND(expand1, destroy_nested)
  2562. KMP_FOREACH_LOCK_KIND(expand2, acquire)
  2563. KMP_FOREACH_LOCK_KIND(expand2, acquire_nested)
  2564. KMP_FOREACH_LOCK_KIND(expand2, release)
  2565. KMP_FOREACH_LOCK_KIND(expand2, release_nested)
  2566. KMP_FOREACH_LOCK_KIND(expand2, test)
  2567. KMP_FOREACH_LOCK_KIND(expand2, test_nested)
  2568. KMP_FOREACH_LOCK_KIND(expand3, )
  2569. KMP_FOREACH_LOCK_KIND(expand4, )
  2570. #undef expand1
  2571. #undef expand2
  2572. #undef expand3
  2573. #undef expand4
  2574. // Jump tables for the indirect lock functions
  2575. // Only fill in the odd entries, that avoids the need to shift out the low bit
  2576. // init functions
  2577. #define expand(l, op) 0, __kmp_init_direct_lock,
  2578. void (*__kmp_direct_init[])(kmp_dyna_lock_t *, kmp_dyna_lockseq_t) = {
  2579. __kmp_init_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, init)};
  2580. #undef expand
  2581. // destroy functions
  2582. #define expand(l, op) 0, (void (*)(kmp_dyna_lock_t *))__kmp_##op##_##l##_lock,
  2583. static void (*direct_destroy[])(kmp_dyna_lock_t *) = {
  2584. __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
  2585. #undef expand
  2586. #define expand(l, op) \
  2587. 0, (void (*)(kmp_dyna_lock_t *))__kmp_destroy_##l##_lock_with_checks,
  2588. static void (*direct_destroy_check[])(kmp_dyna_lock_t *) = {
  2589. __kmp_destroy_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, destroy)};
  2590. #undef expand
  2591. // set/acquire functions
  2592. #define expand(l, op) \
  2593. 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
  2594. static int (*direct_set[])(kmp_dyna_lock_t *, kmp_int32) = {
  2595. __kmp_set_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, acquire)};
  2596. #undef expand
  2597. #define expand(l, op) \
  2598. 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
  2599. static int (*direct_set_check[])(kmp_dyna_lock_t *, kmp_int32) = {
  2600. __kmp_set_indirect_lock_with_checks, 0,
  2601. KMP_FOREACH_D_LOCK(expand, acquire)};
  2602. #undef expand
  2603. // unset/release and test functions
  2604. #define expand(l, op) \
  2605. 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock,
  2606. static int (*direct_unset[])(kmp_dyna_lock_t *, kmp_int32) = {
  2607. __kmp_unset_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, release)};
  2608. static int (*direct_test[])(kmp_dyna_lock_t *, kmp_int32) = {
  2609. __kmp_test_indirect_lock, 0, KMP_FOREACH_D_LOCK(expand, test)};
  2610. #undef expand
  2611. #define expand(l, op) \
  2612. 0, (int (*)(kmp_dyna_lock_t *, kmp_int32))__kmp_##op##_##l##_lock_with_checks,
  2613. static int (*direct_unset_check[])(kmp_dyna_lock_t *, kmp_int32) = {
  2614. __kmp_unset_indirect_lock_with_checks, 0,
  2615. KMP_FOREACH_D_LOCK(expand, release)};
  2616. static int (*direct_test_check[])(kmp_dyna_lock_t *, kmp_int32) = {
  2617. __kmp_test_indirect_lock_with_checks, 0, KMP_FOREACH_D_LOCK(expand, test)};
  2618. #undef expand
  2619. // Exposes only one set of jump tables (*lock or *lock_with_checks).
  2620. void (**__kmp_direct_destroy)(kmp_dyna_lock_t *) = 0;
  2621. int (**__kmp_direct_set)(kmp_dyna_lock_t *, kmp_int32) = 0;
  2622. int (**__kmp_direct_unset)(kmp_dyna_lock_t *, kmp_int32) = 0;
  2623. int (**__kmp_direct_test)(kmp_dyna_lock_t *, kmp_int32) = 0;
  2624. // Jump tables for the indirect lock functions
  2625. #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
  2626. void (*__kmp_indirect_init[])(kmp_user_lock_p) = {
  2627. KMP_FOREACH_I_LOCK(expand, init)};
  2628. #undef expand
  2629. #define expand(l, op) (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock,
  2630. static void (*indirect_destroy[])(kmp_user_lock_p) = {
  2631. KMP_FOREACH_I_LOCK(expand, destroy)};
  2632. #undef expand
  2633. #define expand(l, op) \
  2634. (void (*)(kmp_user_lock_p)) __kmp_##op##_##l##_##lock_with_checks,
  2635. static void (*indirect_destroy_check[])(kmp_user_lock_p) = {
  2636. KMP_FOREACH_I_LOCK(expand, destroy)};
  2637. #undef expand
  2638. // set/acquire functions
  2639. #define expand(l, op) \
  2640. (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
  2641. static int (*indirect_set[])(kmp_user_lock_p,
  2642. kmp_int32) = {KMP_FOREACH_I_LOCK(expand, acquire)};
  2643. #undef expand
  2644. #define expand(l, op) \
  2645. (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
  2646. static int (*indirect_set_check[])(kmp_user_lock_p, kmp_int32) = {
  2647. KMP_FOREACH_I_LOCK(expand, acquire)};
  2648. #undef expand
  2649. // unset/release and test functions
  2650. #define expand(l, op) \
  2651. (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock,
  2652. static int (*indirect_unset[])(kmp_user_lock_p, kmp_int32) = {
  2653. KMP_FOREACH_I_LOCK(expand, release)};
  2654. static int (*indirect_test[])(kmp_user_lock_p,
  2655. kmp_int32) = {KMP_FOREACH_I_LOCK(expand, test)};
  2656. #undef expand
  2657. #define expand(l, op) \
  2658. (int (*)(kmp_user_lock_p, kmp_int32)) __kmp_##op##_##l##_##lock_with_checks,
  2659. static int (*indirect_unset_check[])(kmp_user_lock_p, kmp_int32) = {
  2660. KMP_FOREACH_I_LOCK(expand, release)};
  2661. static int (*indirect_test_check[])(kmp_user_lock_p, kmp_int32) = {
  2662. KMP_FOREACH_I_LOCK(expand, test)};
  2663. #undef expand
  2664. // Exposes only one jump tables (*lock or *lock_with_checks).
  2665. void (**__kmp_indirect_destroy)(kmp_user_lock_p) = 0;
  2666. int (**__kmp_indirect_set)(kmp_user_lock_p, kmp_int32) = 0;
  2667. int (**__kmp_indirect_unset)(kmp_user_lock_p, kmp_int32) = 0;
  2668. int (**__kmp_indirect_test)(kmp_user_lock_p, kmp_int32) = 0;
  2669. // Lock index table.
  2670. kmp_indirect_lock_table_t __kmp_i_lock_table;
  2671. // Size of indirect locks.
  2672. static kmp_uint32 __kmp_indirect_lock_size[KMP_NUM_I_LOCKS] = {0};
  2673. // Jump tables for lock accessor/modifier.
  2674. void (*__kmp_indirect_set_location[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
  2675. const ident_t *) = {0};
  2676. void (*__kmp_indirect_set_flags[KMP_NUM_I_LOCKS])(kmp_user_lock_p,
  2677. kmp_lock_flags_t) = {0};
  2678. const ident_t *(*__kmp_indirect_get_location[KMP_NUM_I_LOCKS])(
  2679. kmp_user_lock_p) = {0};
  2680. kmp_lock_flags_t (*__kmp_indirect_get_flags[KMP_NUM_I_LOCKS])(
  2681. kmp_user_lock_p) = {0};
  2682. // Use different lock pools for different lock types.
  2683. static kmp_indirect_lock_t *__kmp_indirect_lock_pool[KMP_NUM_I_LOCKS] = {0};
  2684. // User lock allocator for dynamically dispatched indirect locks. Every entry of
  2685. // the indirect lock table holds the address and type of the allocated indirect
  2686. // lock (kmp_indirect_lock_t), and the size of the table doubles when it is
  2687. // full. A destroyed indirect lock object is returned to the reusable pool of
  2688. // locks, unique to each lock type.
  2689. kmp_indirect_lock_t *__kmp_allocate_indirect_lock(void **user_lock,
  2690. kmp_int32 gtid,
  2691. kmp_indirect_locktag_t tag) {
  2692. kmp_indirect_lock_t *lck;
  2693. kmp_lock_index_t idx, table_idx;
  2694. __kmp_acquire_lock(&__kmp_global_lock, gtid);
  2695. if (__kmp_indirect_lock_pool[tag] != NULL) {
  2696. // Reuse the allocated and destroyed lock object
  2697. lck = __kmp_indirect_lock_pool[tag];
  2698. if (OMP_LOCK_T_SIZE < sizeof(void *))
  2699. idx = lck->lock->pool.index;
  2700. __kmp_indirect_lock_pool[tag] = (kmp_indirect_lock_t *)lck->lock->pool.next;
  2701. KA_TRACE(20, ("__kmp_allocate_indirect_lock: reusing an existing lock %p\n",
  2702. lck));
  2703. } else {
  2704. kmp_uint32 row, col;
  2705. kmp_indirect_lock_table_t *lock_table = &__kmp_i_lock_table;
  2706. idx = 0;
  2707. // Find location in list of lock tables to put new lock
  2708. while (1) {
  2709. table_idx = lock_table->next; // index within this table
  2710. idx += lock_table->next; // global index within list of tables
  2711. if (table_idx < lock_table->nrow_ptrs * KMP_I_LOCK_CHUNK) {
  2712. row = table_idx / KMP_I_LOCK_CHUNK;
  2713. col = table_idx % KMP_I_LOCK_CHUNK;
  2714. // Allocate a new row of locks if necessary
  2715. if (!lock_table->table[row]) {
  2716. lock_table->table[row] = (kmp_indirect_lock_t *)__kmp_allocate(
  2717. sizeof(kmp_indirect_lock_t) * KMP_I_LOCK_CHUNK);
  2718. }
  2719. break;
  2720. }
  2721. // Allocate a new lock table if necessary with double the capacity
  2722. if (!lock_table->next_table) {
  2723. kmp_indirect_lock_table_t *next_table =
  2724. (kmp_indirect_lock_table_t *)__kmp_allocate(
  2725. sizeof(kmp_indirect_lock_table_t));
  2726. next_table->table = (kmp_indirect_lock_t **)__kmp_allocate(
  2727. sizeof(kmp_indirect_lock_t *) * 2 * lock_table->nrow_ptrs);
  2728. next_table->nrow_ptrs = 2 * lock_table->nrow_ptrs;
  2729. next_table->next = 0;
  2730. next_table->next_table = nullptr;
  2731. lock_table->next_table = next_table;
  2732. }
  2733. lock_table = lock_table->next_table;
  2734. KMP_ASSERT(lock_table);
  2735. }
  2736. lock_table->next++;
  2737. lck = &lock_table->table[row][col];
  2738. // Allocate a new base lock object
  2739. lck->lock = (kmp_user_lock_p)__kmp_allocate(__kmp_indirect_lock_size[tag]);
  2740. KA_TRACE(20,
  2741. ("__kmp_allocate_indirect_lock: allocated a new lock %p\n", lck));
  2742. }
  2743. __kmp_release_lock(&__kmp_global_lock, gtid);
  2744. lck->type = tag;
  2745. if (OMP_LOCK_T_SIZE < sizeof(void *)) {
  2746. *((kmp_lock_index_t *)user_lock) = idx
  2747. << 1; // indirect lock word must be even
  2748. } else {
  2749. *((kmp_indirect_lock_t **)user_lock) = lck;
  2750. }
  2751. return lck;
  2752. }
  2753. // User lock lookup for dynamically dispatched locks.
  2754. static __forceinline kmp_indirect_lock_t *
  2755. __kmp_lookup_indirect_lock(void **user_lock, const char *func) {
  2756. if (__kmp_env_consistency_check) {
  2757. kmp_indirect_lock_t *lck = NULL;
  2758. if (user_lock == NULL) {
  2759. KMP_FATAL(LockIsUninitialized, func);
  2760. }
  2761. if (OMP_LOCK_T_SIZE < sizeof(void *)) {
  2762. kmp_lock_index_t idx = KMP_EXTRACT_I_INDEX(user_lock);
  2763. lck = __kmp_get_i_lock(idx);
  2764. } else {
  2765. lck = *((kmp_indirect_lock_t **)user_lock);
  2766. }
  2767. if (lck == NULL) {
  2768. KMP_FATAL(LockIsUninitialized, func);
  2769. }
  2770. return lck;
  2771. } else {
  2772. if (OMP_LOCK_T_SIZE < sizeof(void *)) {
  2773. return __kmp_get_i_lock(KMP_EXTRACT_I_INDEX(user_lock));
  2774. } else {
  2775. return *((kmp_indirect_lock_t **)user_lock);
  2776. }
  2777. }
  2778. }
  2779. static void __kmp_init_indirect_lock(kmp_dyna_lock_t *lock,
  2780. kmp_dyna_lockseq_t seq) {
  2781. #if KMP_USE_ADAPTIVE_LOCKS
  2782. if (seq == lockseq_adaptive && !__kmp_cpuinfo.flags.rtm) {
  2783. KMP_WARNING(AdaptiveNotSupported, "kmp_lockseq_t", "adaptive");
  2784. seq = lockseq_queuing;
  2785. }
  2786. #endif
  2787. #if KMP_USE_TSX
  2788. if (seq == lockseq_rtm_queuing && !__kmp_cpuinfo.flags.rtm) {
  2789. seq = lockseq_queuing;
  2790. }
  2791. #endif
  2792. kmp_indirect_locktag_t tag = KMP_GET_I_TAG(seq);
  2793. kmp_indirect_lock_t *l =
  2794. __kmp_allocate_indirect_lock((void **)lock, __kmp_entry_gtid(), tag);
  2795. KMP_I_LOCK_FUNC(l, init)(l->lock);
  2796. KA_TRACE(
  2797. 20, ("__kmp_init_indirect_lock: initialized indirect lock with type#%d\n",
  2798. seq));
  2799. }
  2800. static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock) {
  2801. kmp_uint32 gtid = __kmp_entry_gtid();
  2802. kmp_indirect_lock_t *l =
  2803. __kmp_lookup_indirect_lock((void **)lock, "omp_destroy_lock");
  2804. KMP_I_LOCK_FUNC(l, destroy)(l->lock);
  2805. kmp_indirect_locktag_t tag = l->type;
  2806. __kmp_acquire_lock(&__kmp_global_lock, gtid);
  2807. // Use the base lock's space to keep the pool chain.
  2808. l->lock->pool.next = (kmp_user_lock_p)__kmp_indirect_lock_pool[tag];
  2809. if (OMP_LOCK_T_SIZE < sizeof(void *)) {
  2810. l->lock->pool.index = KMP_EXTRACT_I_INDEX(lock);
  2811. }
  2812. __kmp_indirect_lock_pool[tag] = l;
  2813. __kmp_release_lock(&__kmp_global_lock, gtid);
  2814. }
  2815. static int __kmp_set_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
  2816. kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
  2817. return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
  2818. }
  2819. static int __kmp_unset_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
  2820. kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
  2821. return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
  2822. }
  2823. static int __kmp_test_indirect_lock(kmp_dyna_lock_t *lock, kmp_int32 gtid) {
  2824. kmp_indirect_lock_t *l = KMP_LOOKUP_I_LOCK(lock);
  2825. return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
  2826. }
  2827. static int __kmp_set_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
  2828. kmp_int32 gtid) {
  2829. kmp_indirect_lock_t *l =
  2830. __kmp_lookup_indirect_lock((void **)lock, "omp_set_lock");
  2831. return KMP_I_LOCK_FUNC(l, set)(l->lock, gtid);
  2832. }
  2833. static int __kmp_unset_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
  2834. kmp_int32 gtid) {
  2835. kmp_indirect_lock_t *l =
  2836. __kmp_lookup_indirect_lock((void **)lock, "omp_unset_lock");
  2837. return KMP_I_LOCK_FUNC(l, unset)(l->lock, gtid);
  2838. }
  2839. static int __kmp_test_indirect_lock_with_checks(kmp_dyna_lock_t *lock,
  2840. kmp_int32 gtid) {
  2841. kmp_indirect_lock_t *l =
  2842. __kmp_lookup_indirect_lock((void **)lock, "omp_test_lock");
  2843. return KMP_I_LOCK_FUNC(l, test)(l->lock, gtid);
  2844. }
  2845. kmp_dyna_lockseq_t __kmp_user_lock_seq = lockseq_queuing;
  2846. // This is used only in kmp_error.cpp when consistency checking is on.
  2847. kmp_int32 __kmp_get_user_lock_owner(kmp_user_lock_p lck, kmp_uint32 seq) {
  2848. switch (seq) {
  2849. case lockseq_tas:
  2850. case lockseq_nested_tas:
  2851. return __kmp_get_tas_lock_owner((kmp_tas_lock_t *)lck);
  2852. #if KMP_USE_FUTEX
  2853. case lockseq_futex:
  2854. case lockseq_nested_futex:
  2855. return __kmp_get_futex_lock_owner((kmp_futex_lock_t *)lck);
  2856. #endif
  2857. case lockseq_ticket:
  2858. case lockseq_nested_ticket:
  2859. return __kmp_get_ticket_lock_owner((kmp_ticket_lock_t *)lck);
  2860. case lockseq_queuing:
  2861. case lockseq_nested_queuing:
  2862. #if KMP_USE_ADAPTIVE_LOCKS
  2863. case lockseq_adaptive:
  2864. #endif
  2865. return __kmp_get_queuing_lock_owner((kmp_queuing_lock_t *)lck);
  2866. case lockseq_drdpa:
  2867. case lockseq_nested_drdpa:
  2868. return __kmp_get_drdpa_lock_owner((kmp_drdpa_lock_t *)lck);
  2869. default:
  2870. return 0;
  2871. }
  2872. }
  2873. // Initializes data for dynamic user locks.
  2874. void __kmp_init_dynamic_user_locks() {
  2875. // Initialize jump table for the lock functions
  2876. if (__kmp_env_consistency_check) {
  2877. __kmp_direct_set = direct_set_check;
  2878. __kmp_direct_unset = direct_unset_check;
  2879. __kmp_direct_test = direct_test_check;
  2880. __kmp_direct_destroy = direct_destroy_check;
  2881. __kmp_indirect_set = indirect_set_check;
  2882. __kmp_indirect_unset = indirect_unset_check;
  2883. __kmp_indirect_test = indirect_test_check;
  2884. __kmp_indirect_destroy = indirect_destroy_check;
  2885. } else {
  2886. __kmp_direct_set = direct_set;
  2887. __kmp_direct_unset = direct_unset;
  2888. __kmp_direct_test = direct_test;
  2889. __kmp_direct_destroy = direct_destroy;
  2890. __kmp_indirect_set = indirect_set;
  2891. __kmp_indirect_unset = indirect_unset;
  2892. __kmp_indirect_test = indirect_test;
  2893. __kmp_indirect_destroy = indirect_destroy;
  2894. }
  2895. // If the user locks have already been initialized, then return. Allow the
  2896. // switch between different KMP_CONSISTENCY_CHECK values, but do not allocate
  2897. // new lock tables if they have already been allocated.
  2898. if (__kmp_init_user_locks)
  2899. return;
  2900. // Initialize lock index table
  2901. __kmp_i_lock_table.nrow_ptrs = KMP_I_LOCK_TABLE_INIT_NROW_PTRS;
  2902. __kmp_i_lock_table.table = (kmp_indirect_lock_t **)__kmp_allocate(
  2903. sizeof(kmp_indirect_lock_t *) * KMP_I_LOCK_TABLE_INIT_NROW_PTRS);
  2904. *(__kmp_i_lock_table.table) = (kmp_indirect_lock_t *)__kmp_allocate(
  2905. KMP_I_LOCK_CHUNK * sizeof(kmp_indirect_lock_t));
  2906. __kmp_i_lock_table.next = 0;
  2907. __kmp_i_lock_table.next_table = nullptr;
  2908. // Indirect lock size
  2909. __kmp_indirect_lock_size[locktag_ticket] = sizeof(kmp_ticket_lock_t);
  2910. __kmp_indirect_lock_size[locktag_queuing] = sizeof(kmp_queuing_lock_t);
  2911. #if KMP_USE_ADAPTIVE_LOCKS
  2912. __kmp_indirect_lock_size[locktag_adaptive] = sizeof(kmp_adaptive_lock_t);
  2913. #endif
  2914. __kmp_indirect_lock_size[locktag_drdpa] = sizeof(kmp_drdpa_lock_t);
  2915. #if KMP_USE_TSX
  2916. __kmp_indirect_lock_size[locktag_rtm_queuing] = sizeof(kmp_queuing_lock_t);
  2917. #endif
  2918. __kmp_indirect_lock_size[locktag_nested_tas] = sizeof(kmp_tas_lock_t);
  2919. #if KMP_USE_FUTEX
  2920. __kmp_indirect_lock_size[locktag_nested_futex] = sizeof(kmp_futex_lock_t);
  2921. #endif
  2922. __kmp_indirect_lock_size[locktag_nested_ticket] = sizeof(kmp_ticket_lock_t);
  2923. __kmp_indirect_lock_size[locktag_nested_queuing] = sizeof(kmp_queuing_lock_t);
  2924. __kmp_indirect_lock_size[locktag_nested_drdpa] = sizeof(kmp_drdpa_lock_t);
  2925. // Initialize lock accessor/modifier
  2926. #define fill_jumps(table, expand, sep) \
  2927. { \
  2928. table[locktag##sep##ticket] = expand(ticket); \
  2929. table[locktag##sep##queuing] = expand(queuing); \
  2930. table[locktag##sep##drdpa] = expand(drdpa); \
  2931. }
  2932. #if KMP_USE_ADAPTIVE_LOCKS
  2933. #define fill_table(table, expand) \
  2934. { \
  2935. fill_jumps(table, expand, _); \
  2936. table[locktag_adaptive] = expand(queuing); \
  2937. fill_jumps(table, expand, _nested_); \
  2938. }
  2939. #else
  2940. #define fill_table(table, expand) \
  2941. { \
  2942. fill_jumps(table, expand, _); \
  2943. fill_jumps(table, expand, _nested_); \
  2944. }
  2945. #endif // KMP_USE_ADAPTIVE_LOCKS
  2946. #define expand(l) \
  2947. (void (*)(kmp_user_lock_p, const ident_t *)) __kmp_set_##l##_lock_location
  2948. fill_table(__kmp_indirect_set_location, expand);
  2949. #undef expand
  2950. #define expand(l) \
  2951. (void (*)(kmp_user_lock_p, kmp_lock_flags_t)) __kmp_set_##l##_lock_flags
  2952. fill_table(__kmp_indirect_set_flags, expand);
  2953. #undef expand
  2954. #define expand(l) \
  2955. (const ident_t *(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_location
  2956. fill_table(__kmp_indirect_get_location, expand);
  2957. #undef expand
  2958. #define expand(l) \
  2959. (kmp_lock_flags_t(*)(kmp_user_lock_p)) __kmp_get_##l##_lock_flags
  2960. fill_table(__kmp_indirect_get_flags, expand);
  2961. #undef expand
  2962. __kmp_init_user_locks = TRUE;
  2963. }
  2964. // Clean up the lock table.
  2965. void __kmp_cleanup_indirect_user_locks() {
  2966. int k;
  2967. // Clean up locks in the pools first (they were already destroyed before going
  2968. // into the pools).
  2969. for (k = 0; k < KMP_NUM_I_LOCKS; ++k) {
  2970. kmp_indirect_lock_t *l = __kmp_indirect_lock_pool[k];
  2971. while (l != NULL) {
  2972. kmp_indirect_lock_t *ll = l;
  2973. l = (kmp_indirect_lock_t *)l->lock->pool.next;
  2974. KA_TRACE(20, ("__kmp_cleanup_indirect_user_locks: freeing %p from pool\n",
  2975. ll));
  2976. __kmp_free(ll->lock);
  2977. ll->lock = NULL;
  2978. }
  2979. __kmp_indirect_lock_pool[k] = NULL;
  2980. }
  2981. // Clean up the remaining undestroyed locks.
  2982. kmp_indirect_lock_table_t *ptr = &__kmp_i_lock_table;
  2983. while (ptr) {
  2984. for (kmp_uint32 row = 0; row < ptr->nrow_ptrs; ++row) {
  2985. if (!ptr->table[row])
  2986. continue;
  2987. for (kmp_uint32 col = 0; col < KMP_I_LOCK_CHUNK; ++col) {
  2988. kmp_indirect_lock_t *l = &ptr->table[row][col];
  2989. if (l->lock) {
  2990. // Locks not destroyed explicitly need to be destroyed here.
  2991. KMP_I_LOCK_FUNC(l, destroy)(l->lock);
  2992. KA_TRACE(20, ("__kmp_cleanup_indirect_user_locks: destroy/freeing %p "
  2993. "from table\n",
  2994. l));
  2995. __kmp_free(l->lock);
  2996. }
  2997. }
  2998. __kmp_free(ptr->table[row]);
  2999. }
  3000. kmp_indirect_lock_table_t *next_table = ptr->next_table;
  3001. if (ptr != &__kmp_i_lock_table)
  3002. __kmp_free(ptr);
  3003. ptr = next_table;
  3004. }
  3005. __kmp_init_user_locks = FALSE;
  3006. }
  3007. enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
  3008. int __kmp_num_locks_in_block = 1; // FIXME - tune this value
  3009. #else // KMP_USE_DYNAMIC_LOCK
  3010. static void __kmp_init_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  3011. __kmp_init_tas_lock(lck);
  3012. }
  3013. static void __kmp_init_nested_tas_lock_with_checks(kmp_tas_lock_t *lck) {
  3014. __kmp_init_nested_tas_lock(lck);
  3015. }
  3016. #if KMP_USE_FUTEX
  3017. static void __kmp_init_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  3018. __kmp_init_futex_lock(lck);
  3019. }
  3020. static void __kmp_init_nested_futex_lock_with_checks(kmp_futex_lock_t *lck) {
  3021. __kmp_init_nested_futex_lock(lck);
  3022. }
  3023. #endif
  3024. static int __kmp_is_ticket_lock_initialized(kmp_ticket_lock_t *lck) {
  3025. return lck == lck->lk.self;
  3026. }
  3027. static void __kmp_init_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  3028. __kmp_init_ticket_lock(lck);
  3029. }
  3030. static void __kmp_init_nested_ticket_lock_with_checks(kmp_ticket_lock_t *lck) {
  3031. __kmp_init_nested_ticket_lock(lck);
  3032. }
  3033. static int __kmp_is_queuing_lock_initialized(kmp_queuing_lock_t *lck) {
  3034. return lck == lck->lk.initialized;
  3035. }
  3036. static void __kmp_init_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  3037. __kmp_init_queuing_lock(lck);
  3038. }
  3039. static void
  3040. __kmp_init_nested_queuing_lock_with_checks(kmp_queuing_lock_t *lck) {
  3041. __kmp_init_nested_queuing_lock(lck);
  3042. }
  3043. #if KMP_USE_ADAPTIVE_LOCKS
  3044. static void __kmp_init_adaptive_lock_with_checks(kmp_adaptive_lock_t *lck) {
  3045. __kmp_init_adaptive_lock(lck);
  3046. }
  3047. #endif
  3048. static int __kmp_is_drdpa_lock_initialized(kmp_drdpa_lock_t *lck) {
  3049. return lck == lck->lk.initialized;
  3050. }
  3051. static void __kmp_init_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  3052. __kmp_init_drdpa_lock(lck);
  3053. }
  3054. static void __kmp_init_nested_drdpa_lock_with_checks(kmp_drdpa_lock_t *lck) {
  3055. __kmp_init_nested_drdpa_lock(lck);
  3056. }
  3057. /* user locks
  3058. * They are implemented as a table of function pointers which are set to the
  3059. * lock functions of the appropriate kind, once that has been determined. */
  3060. enum kmp_lock_kind __kmp_user_lock_kind = lk_default;
  3061. size_t __kmp_base_user_lock_size = 0;
  3062. size_t __kmp_user_lock_size = 0;
  3063. kmp_int32 (*__kmp_get_user_lock_owner_)(kmp_user_lock_p lck) = NULL;
  3064. int (*__kmp_acquire_user_lock_with_checks_)(kmp_user_lock_p lck,
  3065. kmp_int32 gtid) = NULL;
  3066. int (*__kmp_test_user_lock_with_checks_)(kmp_user_lock_p lck,
  3067. kmp_int32 gtid) = NULL;
  3068. int (*__kmp_release_user_lock_with_checks_)(kmp_user_lock_p lck,
  3069. kmp_int32 gtid) = NULL;
  3070. void (*__kmp_init_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
  3071. void (*__kmp_destroy_user_lock_)(kmp_user_lock_p lck) = NULL;
  3072. void (*__kmp_destroy_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
  3073. int (*__kmp_acquire_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
  3074. kmp_int32 gtid) = NULL;
  3075. int (*__kmp_test_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
  3076. kmp_int32 gtid) = NULL;
  3077. int (*__kmp_release_nested_user_lock_with_checks_)(kmp_user_lock_p lck,
  3078. kmp_int32 gtid) = NULL;
  3079. void (*__kmp_init_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
  3080. void (*__kmp_destroy_nested_user_lock_with_checks_)(kmp_user_lock_p lck) = NULL;
  3081. int (*__kmp_is_user_lock_initialized_)(kmp_user_lock_p lck) = NULL;
  3082. const ident_t *(*__kmp_get_user_lock_location_)(kmp_user_lock_p lck) = NULL;
  3083. void (*__kmp_set_user_lock_location_)(kmp_user_lock_p lck,
  3084. const ident_t *loc) = NULL;
  3085. kmp_lock_flags_t (*__kmp_get_user_lock_flags_)(kmp_user_lock_p lck) = NULL;
  3086. void (*__kmp_set_user_lock_flags_)(kmp_user_lock_p lck,
  3087. kmp_lock_flags_t flags) = NULL;
  3088. void __kmp_set_user_lock_vptrs(kmp_lock_kind_t user_lock_kind) {
  3089. switch (user_lock_kind) {
  3090. case lk_default:
  3091. default:
  3092. KMP_ASSERT(0);
  3093. case lk_tas: {
  3094. __kmp_base_user_lock_size = sizeof(kmp_base_tas_lock_t);
  3095. __kmp_user_lock_size = sizeof(kmp_tas_lock_t);
  3096. __kmp_get_user_lock_owner_ =
  3097. (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_tas_lock_owner);
  3098. if (__kmp_env_consistency_check) {
  3099. KMP_BIND_USER_LOCK_WITH_CHECKS(tas);
  3100. KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(tas);
  3101. } else {
  3102. KMP_BIND_USER_LOCK(tas);
  3103. KMP_BIND_NESTED_USER_LOCK(tas);
  3104. }
  3105. __kmp_destroy_user_lock_ =
  3106. (void (*)(kmp_user_lock_p))(&__kmp_destroy_tas_lock);
  3107. __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
  3108. __kmp_get_user_lock_location_ = (const ident_t *(*)(kmp_user_lock_p))NULL;
  3109. __kmp_set_user_lock_location_ =
  3110. (void (*)(kmp_user_lock_p, const ident_t *))NULL;
  3111. __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
  3112. __kmp_set_user_lock_flags_ =
  3113. (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
  3114. } break;
  3115. #if KMP_USE_FUTEX
  3116. case lk_futex: {
  3117. __kmp_base_user_lock_size = sizeof(kmp_base_futex_lock_t);
  3118. __kmp_user_lock_size = sizeof(kmp_futex_lock_t);
  3119. __kmp_get_user_lock_owner_ =
  3120. (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_futex_lock_owner);
  3121. if (__kmp_env_consistency_check) {
  3122. KMP_BIND_USER_LOCK_WITH_CHECKS(futex);
  3123. KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(futex);
  3124. } else {
  3125. KMP_BIND_USER_LOCK(futex);
  3126. KMP_BIND_NESTED_USER_LOCK(futex);
  3127. }
  3128. __kmp_destroy_user_lock_ =
  3129. (void (*)(kmp_user_lock_p))(&__kmp_destroy_futex_lock);
  3130. __kmp_is_user_lock_initialized_ = (int (*)(kmp_user_lock_p))NULL;
  3131. __kmp_get_user_lock_location_ = (const ident_t *(*)(kmp_user_lock_p))NULL;
  3132. __kmp_set_user_lock_location_ =
  3133. (void (*)(kmp_user_lock_p, const ident_t *))NULL;
  3134. __kmp_get_user_lock_flags_ = (kmp_lock_flags_t(*)(kmp_user_lock_p))NULL;
  3135. __kmp_set_user_lock_flags_ =
  3136. (void (*)(kmp_user_lock_p, kmp_lock_flags_t))NULL;
  3137. } break;
  3138. #endif // KMP_USE_FUTEX
  3139. case lk_ticket: {
  3140. __kmp_base_user_lock_size = sizeof(kmp_base_ticket_lock_t);
  3141. __kmp_user_lock_size = sizeof(kmp_ticket_lock_t);
  3142. __kmp_get_user_lock_owner_ =
  3143. (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_owner);
  3144. if (__kmp_env_consistency_check) {
  3145. KMP_BIND_USER_LOCK_WITH_CHECKS(ticket);
  3146. KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(ticket);
  3147. } else {
  3148. KMP_BIND_USER_LOCK(ticket);
  3149. KMP_BIND_NESTED_USER_LOCK(ticket);
  3150. }
  3151. __kmp_destroy_user_lock_ =
  3152. (void (*)(kmp_user_lock_p))(&__kmp_destroy_ticket_lock);
  3153. __kmp_is_user_lock_initialized_ =
  3154. (int (*)(kmp_user_lock_p))(&__kmp_is_ticket_lock_initialized);
  3155. __kmp_get_user_lock_location_ =
  3156. (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_location);
  3157. __kmp_set_user_lock_location_ = (void (*)(
  3158. kmp_user_lock_p, const ident_t *))(&__kmp_set_ticket_lock_location);
  3159. __kmp_get_user_lock_flags_ =
  3160. (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_ticket_lock_flags);
  3161. __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
  3162. &__kmp_set_ticket_lock_flags);
  3163. } break;
  3164. case lk_queuing: {
  3165. __kmp_base_user_lock_size = sizeof(kmp_base_queuing_lock_t);
  3166. __kmp_user_lock_size = sizeof(kmp_queuing_lock_t);
  3167. __kmp_get_user_lock_owner_ =
  3168. (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
  3169. if (__kmp_env_consistency_check) {
  3170. KMP_BIND_USER_LOCK_WITH_CHECKS(queuing);
  3171. KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(queuing);
  3172. } else {
  3173. KMP_BIND_USER_LOCK(queuing);
  3174. KMP_BIND_NESTED_USER_LOCK(queuing);
  3175. }
  3176. __kmp_destroy_user_lock_ =
  3177. (void (*)(kmp_user_lock_p))(&__kmp_destroy_queuing_lock);
  3178. __kmp_is_user_lock_initialized_ =
  3179. (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
  3180. __kmp_get_user_lock_location_ =
  3181. (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
  3182. __kmp_set_user_lock_location_ = (void (*)(
  3183. kmp_user_lock_p, const ident_t *))(&__kmp_set_queuing_lock_location);
  3184. __kmp_get_user_lock_flags_ =
  3185. (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
  3186. __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
  3187. &__kmp_set_queuing_lock_flags);
  3188. } break;
  3189. #if KMP_USE_ADAPTIVE_LOCKS
  3190. case lk_adaptive: {
  3191. __kmp_base_user_lock_size = sizeof(kmp_base_adaptive_lock_t);
  3192. __kmp_user_lock_size = sizeof(kmp_adaptive_lock_t);
  3193. __kmp_get_user_lock_owner_ =
  3194. (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_owner);
  3195. if (__kmp_env_consistency_check) {
  3196. KMP_BIND_USER_LOCK_WITH_CHECKS(adaptive);
  3197. } else {
  3198. KMP_BIND_USER_LOCK(adaptive);
  3199. }
  3200. __kmp_destroy_user_lock_ =
  3201. (void (*)(kmp_user_lock_p))(&__kmp_destroy_adaptive_lock);
  3202. __kmp_is_user_lock_initialized_ =
  3203. (int (*)(kmp_user_lock_p))(&__kmp_is_queuing_lock_initialized);
  3204. __kmp_get_user_lock_location_ =
  3205. (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_location);
  3206. __kmp_set_user_lock_location_ = (void (*)(
  3207. kmp_user_lock_p, const ident_t *))(&__kmp_set_queuing_lock_location);
  3208. __kmp_get_user_lock_flags_ =
  3209. (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_queuing_lock_flags);
  3210. __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
  3211. &__kmp_set_queuing_lock_flags);
  3212. } break;
  3213. #endif // KMP_USE_ADAPTIVE_LOCKS
  3214. case lk_drdpa: {
  3215. __kmp_base_user_lock_size = sizeof(kmp_base_drdpa_lock_t);
  3216. __kmp_user_lock_size = sizeof(kmp_drdpa_lock_t);
  3217. __kmp_get_user_lock_owner_ =
  3218. (kmp_int32(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_owner);
  3219. if (__kmp_env_consistency_check) {
  3220. KMP_BIND_USER_LOCK_WITH_CHECKS(drdpa);
  3221. KMP_BIND_NESTED_USER_LOCK_WITH_CHECKS(drdpa);
  3222. } else {
  3223. KMP_BIND_USER_LOCK(drdpa);
  3224. KMP_BIND_NESTED_USER_LOCK(drdpa);
  3225. }
  3226. __kmp_destroy_user_lock_ =
  3227. (void (*)(kmp_user_lock_p))(&__kmp_destroy_drdpa_lock);
  3228. __kmp_is_user_lock_initialized_ =
  3229. (int (*)(kmp_user_lock_p))(&__kmp_is_drdpa_lock_initialized);
  3230. __kmp_get_user_lock_location_ =
  3231. (const ident_t *(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_location);
  3232. __kmp_set_user_lock_location_ = (void (*)(
  3233. kmp_user_lock_p, const ident_t *))(&__kmp_set_drdpa_lock_location);
  3234. __kmp_get_user_lock_flags_ =
  3235. (kmp_lock_flags_t(*)(kmp_user_lock_p))(&__kmp_get_drdpa_lock_flags);
  3236. __kmp_set_user_lock_flags_ = (void (*)(kmp_user_lock_p, kmp_lock_flags_t))(
  3237. &__kmp_set_drdpa_lock_flags);
  3238. } break;
  3239. }
  3240. }
  3241. // ----------------------------------------------------------------------------
  3242. // User lock table & lock allocation
  3243. kmp_lock_table_t __kmp_user_lock_table = {1, 0, NULL};
  3244. kmp_user_lock_p __kmp_lock_pool = NULL;
  3245. // Lock block-allocation support.
  3246. kmp_block_of_locks *__kmp_lock_blocks = NULL;
  3247. int __kmp_num_locks_in_block = 1; // FIXME - tune this value
  3248. static kmp_lock_index_t __kmp_lock_table_insert(kmp_user_lock_p lck) {
  3249. // Assume that kmp_global_lock is held upon entry/exit.
  3250. kmp_lock_index_t index;
  3251. if (__kmp_user_lock_table.used >= __kmp_user_lock_table.allocated) {
  3252. kmp_lock_index_t size;
  3253. kmp_user_lock_p *table;
  3254. // Reallocate lock table.
  3255. if (__kmp_user_lock_table.allocated == 0) {
  3256. size = 1024;
  3257. } else {
  3258. size = __kmp_user_lock_table.allocated * 2;
  3259. }
  3260. table = (kmp_user_lock_p *)__kmp_allocate(sizeof(kmp_user_lock_p) * size);
  3261. KMP_MEMCPY(table + 1, __kmp_user_lock_table.table + 1,
  3262. sizeof(kmp_user_lock_p) * (__kmp_user_lock_table.used - 1));
  3263. table[0] = (kmp_user_lock_p)__kmp_user_lock_table.table;
  3264. // We cannot free the previous table now, since it may be in use by other
  3265. // threads. So save the pointer to the previous table in in the first
  3266. // element of the new table. All the tables will be organized into a list,
  3267. // and could be freed when library shutting down.
  3268. __kmp_user_lock_table.table = table;
  3269. __kmp_user_lock_table.allocated = size;
  3270. }
  3271. KMP_DEBUG_ASSERT(__kmp_user_lock_table.used <
  3272. __kmp_user_lock_table.allocated);
  3273. index = __kmp_user_lock_table.used;
  3274. __kmp_user_lock_table.table[index] = lck;
  3275. ++__kmp_user_lock_table.used;
  3276. return index;
  3277. }
  3278. static kmp_user_lock_p __kmp_lock_block_allocate() {
  3279. // Assume that kmp_global_lock is held upon entry/exit.
  3280. static int last_index = 0;
  3281. if ((last_index >= __kmp_num_locks_in_block) || (__kmp_lock_blocks == NULL)) {
  3282. // Restart the index.
  3283. last_index = 0;
  3284. // Need to allocate a new block.
  3285. KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
  3286. size_t space_for_locks = __kmp_user_lock_size * __kmp_num_locks_in_block;
  3287. char *buffer =
  3288. (char *)__kmp_allocate(space_for_locks + sizeof(kmp_block_of_locks));
  3289. // Set up the new block.
  3290. kmp_block_of_locks *new_block =
  3291. (kmp_block_of_locks *)(&buffer[space_for_locks]);
  3292. new_block->next_block = __kmp_lock_blocks;
  3293. new_block->locks = (void *)buffer;
  3294. // Publish the new block.
  3295. KMP_MB();
  3296. __kmp_lock_blocks = new_block;
  3297. }
  3298. kmp_user_lock_p ret = (kmp_user_lock_p)(&(
  3299. ((char *)(__kmp_lock_blocks->locks))[last_index * __kmp_user_lock_size]));
  3300. last_index++;
  3301. return ret;
  3302. }
  3303. // Get memory for a lock. It may be freshly allocated memory or reused memory
  3304. // from lock pool.
  3305. kmp_user_lock_p __kmp_user_lock_allocate(void **user_lock, kmp_int32 gtid,
  3306. kmp_lock_flags_t flags) {
  3307. kmp_user_lock_p lck;
  3308. kmp_lock_index_t index;
  3309. KMP_DEBUG_ASSERT(user_lock);
  3310. __kmp_acquire_lock(&__kmp_global_lock, gtid);
  3311. if (__kmp_lock_pool == NULL) {
  3312. // Lock pool is empty. Allocate new memory.
  3313. if (__kmp_num_locks_in_block <= 1) { // Tune this cutoff point.
  3314. lck = (kmp_user_lock_p)__kmp_allocate(__kmp_user_lock_size);
  3315. } else {
  3316. lck = __kmp_lock_block_allocate();
  3317. }
  3318. // Insert lock in the table so that it can be freed in __kmp_cleanup,
  3319. // and debugger has info on all allocated locks.
  3320. index = __kmp_lock_table_insert(lck);
  3321. } else {
  3322. // Pick up lock from pool.
  3323. lck = __kmp_lock_pool;
  3324. index = __kmp_lock_pool->pool.index;
  3325. __kmp_lock_pool = __kmp_lock_pool->pool.next;
  3326. }
  3327. // We could potentially differentiate between nested and regular locks
  3328. // here, and do the lock table lookup for regular locks only.
  3329. if (OMP_LOCK_T_SIZE < sizeof(void *)) {
  3330. *((kmp_lock_index_t *)user_lock) = index;
  3331. } else {
  3332. *((kmp_user_lock_p *)user_lock) = lck;
  3333. }
  3334. // mark the lock if it is critical section lock.
  3335. __kmp_set_user_lock_flags(lck, flags);
  3336. __kmp_release_lock(&__kmp_global_lock, gtid); // AC: TODO move this line upper
  3337. return lck;
  3338. }
  3339. // Put lock's memory to pool for reusing.
  3340. void __kmp_user_lock_free(void **user_lock, kmp_int32 gtid,
  3341. kmp_user_lock_p lck) {
  3342. KMP_DEBUG_ASSERT(user_lock != NULL);
  3343. KMP_DEBUG_ASSERT(lck != NULL);
  3344. __kmp_acquire_lock(&__kmp_global_lock, gtid);
  3345. lck->pool.next = __kmp_lock_pool;
  3346. __kmp_lock_pool = lck;
  3347. if (OMP_LOCK_T_SIZE < sizeof(void *)) {
  3348. kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
  3349. KMP_DEBUG_ASSERT(0 < index && index <= __kmp_user_lock_table.used);
  3350. lck->pool.index = index;
  3351. }
  3352. __kmp_release_lock(&__kmp_global_lock, gtid);
  3353. }
  3354. kmp_user_lock_p __kmp_lookup_user_lock(void **user_lock, char const *func) {
  3355. kmp_user_lock_p lck = NULL;
  3356. if (__kmp_env_consistency_check) {
  3357. if (user_lock == NULL) {
  3358. KMP_FATAL(LockIsUninitialized, func);
  3359. }
  3360. }
  3361. if (OMP_LOCK_T_SIZE < sizeof(void *)) {
  3362. kmp_lock_index_t index = *((kmp_lock_index_t *)user_lock);
  3363. if (__kmp_env_consistency_check) {
  3364. if (!(0 < index && index < __kmp_user_lock_table.used)) {
  3365. KMP_FATAL(LockIsUninitialized, func);
  3366. }
  3367. }
  3368. KMP_DEBUG_ASSERT(0 < index && index < __kmp_user_lock_table.used);
  3369. KMP_DEBUG_ASSERT(__kmp_user_lock_size > 0);
  3370. lck = __kmp_user_lock_table.table[index];
  3371. } else {
  3372. lck = *((kmp_user_lock_p *)user_lock);
  3373. }
  3374. if (__kmp_env_consistency_check) {
  3375. if (lck == NULL) {
  3376. KMP_FATAL(LockIsUninitialized, func);
  3377. }
  3378. }
  3379. return lck;
  3380. }
  3381. void __kmp_cleanup_user_locks(void) {
  3382. // Reset lock pool. Don't worry about lock in the pool--we will free them when
  3383. // iterating through lock table (it includes all the locks, dead or alive).
  3384. __kmp_lock_pool = NULL;
  3385. #define IS_CRITICAL(lck) \
  3386. ((__kmp_get_user_lock_flags_ != NULL) && \
  3387. ((*__kmp_get_user_lock_flags_)(lck)&kmp_lf_critical_section))
  3388. // Loop through lock table, free all locks.
  3389. // Do not free item [0], it is reserved for lock tables list.
  3390. //
  3391. // FIXME - we are iterating through a list of (pointers to) objects of type
  3392. // union kmp_user_lock, but we have no way of knowing whether the base type is
  3393. // currently "pool" or whatever the global user lock type is.
  3394. //
  3395. // We are relying on the fact that for all of the user lock types
  3396. // (except "tas"), the first field in the lock struct is the "initialized"
  3397. // field, which is set to the address of the lock object itself when
  3398. // the lock is initialized. When the union is of type "pool", the
  3399. // first field is a pointer to the next object in the free list, which
  3400. // will not be the same address as the object itself.
  3401. //
  3402. // This means that the check (*__kmp_is_user_lock_initialized_)(lck) will fail
  3403. // for "pool" objects on the free list. This must happen as the "location"
  3404. // field of real user locks overlaps the "index" field of "pool" objects.
  3405. //
  3406. // It would be better to run through the free list, and remove all "pool"
  3407. // objects from the lock table before executing this loop. However,
  3408. // "pool" objects do not always have their index field set (only on
  3409. // lin_32e), and I don't want to search the lock table for the address
  3410. // of every "pool" object on the free list.
  3411. while (__kmp_user_lock_table.used > 1) {
  3412. const ident *loc;
  3413. // reduce __kmp_user_lock_table.used before freeing the lock,
  3414. // so that state of locks is consistent
  3415. kmp_user_lock_p lck =
  3416. __kmp_user_lock_table.table[--__kmp_user_lock_table.used];
  3417. if ((__kmp_is_user_lock_initialized_ != NULL) &&
  3418. (*__kmp_is_user_lock_initialized_)(lck)) {
  3419. // Issue a warning if: KMP_CONSISTENCY_CHECK AND lock is initialized AND
  3420. // it is NOT a critical section (user is not responsible for destroying
  3421. // criticals) AND we know source location to report.
  3422. if (__kmp_env_consistency_check && (!IS_CRITICAL(lck)) &&
  3423. ((loc = __kmp_get_user_lock_location(lck)) != NULL) &&
  3424. (loc->psource != NULL)) {
  3425. kmp_str_loc_t str_loc = __kmp_str_loc_init(loc->psource, false);
  3426. KMP_WARNING(CnsLockNotDestroyed, str_loc.file, str_loc.line);
  3427. __kmp_str_loc_free(&str_loc);
  3428. }
  3429. #ifdef KMP_DEBUG
  3430. if (IS_CRITICAL(lck)) {
  3431. KA_TRACE(
  3432. 20,
  3433. ("__kmp_cleanup_user_locks: free critical section lock %p (%p)\n",
  3434. lck, *(void **)lck));
  3435. } else {
  3436. KA_TRACE(20, ("__kmp_cleanup_user_locks: free lock %p (%p)\n", lck,
  3437. *(void **)lck));
  3438. }
  3439. #endif // KMP_DEBUG
  3440. // Cleanup internal lock dynamic resources (for drdpa locks particularly).
  3441. __kmp_destroy_user_lock(lck);
  3442. }
  3443. // Free the lock if block allocation of locks is not used.
  3444. if (__kmp_lock_blocks == NULL) {
  3445. __kmp_free(lck);
  3446. }
  3447. }
  3448. #undef IS_CRITICAL
  3449. // delete lock table(s).
  3450. kmp_user_lock_p *table_ptr = __kmp_user_lock_table.table;
  3451. __kmp_user_lock_table.table = NULL;
  3452. __kmp_user_lock_table.allocated = 0;
  3453. while (table_ptr != NULL) {
  3454. // In the first element we saved the pointer to the previous
  3455. // (smaller) lock table.
  3456. kmp_user_lock_p *next = (kmp_user_lock_p *)(table_ptr[0]);
  3457. __kmp_free(table_ptr);
  3458. table_ptr = next;
  3459. }
  3460. // Free buffers allocated for blocks of locks.
  3461. kmp_block_of_locks_t *block_ptr = __kmp_lock_blocks;
  3462. __kmp_lock_blocks = NULL;
  3463. while (block_ptr != NULL) {
  3464. kmp_block_of_locks_t *next = block_ptr->next_block;
  3465. __kmp_free(block_ptr->locks);
  3466. // *block_ptr itself was allocated at the end of the locks vector.
  3467. block_ptr = next;
  3468. }
  3469. TCW_4(__kmp_init_user_locks, FALSE);
  3470. }
  3471. #endif // KMP_USE_DYNAMIC_LOCK