ccgthreadvars.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Thread var support for crappy architectures that lack native support for
  10. ## thread local storage. (**Thank you Mac OS X!**)
  11. # included from cgen.nim
  12. proc emulatedThreadVars(): bool =
  13. result = {optThreads, optTlsEmulation} <= gGlobalOptions
  14. proc accessThreadLocalVar(p: BProc, s: PSym) =
  15. if emulatedThreadVars() and not p.threadVarAccessed:
  16. p.threadVarAccessed = true
  17. incl p.module.flags, usesThreadVars
  18. addf(p.procSec(cpsLocals), "\tNimThreadVars* NimTV_;$n", [])
  19. add(p.procSec(cpsInit),
  20. ropecg(p.module, "\tNimTV_ = (NimThreadVars*) #GetThreadLocalVars();$n"))
  21. var
  22. nimtv: Rope # Nim thread vars; the struct body
  23. nimtvDeps: seq[PType] = @[] # type deps: every module needs whole struct
  24. nimtvDeclared = initIntSet() # so that every var/field exists only once
  25. # in the struct
  26. # 'nimtv' is incredibly hard to modularize! Best effort is to store all thread
  27. # vars in a ROD section and with their type deps and load them
  28. # unconditionally...
  29. # nimtvDeps is VERY hard to cache because it's not a list of IDs nor can it be
  30. # made to be one.
  31. proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
  32. if emulatedThreadVars():
  33. # we gather all thread locals var into a struct; we need to allocate
  34. # storage for that somehow, can't use the thread local storage
  35. # allocator for it :-(
  36. if not containsOrIncl(nimtvDeclared, s.id):
  37. nimtvDeps.add(s.loc.t)
  38. addf(nimtv, "$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r])
  39. else:
  40. if isExtern: add(m.s[cfsVars], "extern ")
  41. if optThreads in gGlobalOptions: add(m.s[cfsVars], "NIM_THREADVAR ")
  42. add(m.s[cfsVars], getTypeDesc(m, s.loc.t))
  43. addf(m.s[cfsVars], " $1;$n", [s.loc.r])
  44. proc generateThreadLocalStorage(m: BModule) =
  45. if nimtv != nil and (usesThreadVars in m.flags or sfMainModule in m.module.flags):
  46. for t in items(nimtvDeps): discard getTypeDesc(m, t)
  47. addf(m.s[cfsSeqTypes], "typedef struct {$1} NimThreadVars;$n", [nimtv])
  48. proc generateThreadVarsSize(m: BModule) =
  49. if nimtv != nil:
  50. let externc = if gCmd == cmdCompileToCpp or
  51. sfCompileToCpp in m.module.flags: "extern \"C\" "
  52. else: ""
  53. addf(m.s[cfsProcs],
  54. "$#NI NimThreadVarsSize(){return (NI)sizeof(NimThreadVars);}$n",
  55. [externc.rope])