ccgthreadvars.nim 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 architectures that lack native support for
  10. ## thread local storage.
  11. # included from cgen.nim
  12. proc emulatedThreadVars(conf: ConfigRef): bool =
  13. result = {optThreads, optTlsEmulation} <= conf.globalOptions
  14. proc accessThreadLocalVar(p: BProc, s: PSym) =
  15. if emulatedThreadVars(p.config) and threadVarAccessed notin p.flags:
  16. p.flags.incl threadVarAccessed
  17. incl p.module.flags, usesThreadVars
  18. p.procSec(cpsLocals).addf("\tNimThreadVars* NimTV_;$n", [])
  19. p.procSec(cpsInit).add(
  20. ropecg(p.module, "\tNimTV_ = (NimThreadVars*) #GetThreadLocalVars();$n", []))
  21. proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
  22. if emulatedThreadVars(m.config):
  23. # we gather all thread locals var into a struct; we need to allocate
  24. # storage for that somehow, can't use the thread local storage
  25. # allocator for it :-(
  26. if not containsOrIncl(m.g.nimtvDeclared, s.id):
  27. m.g.nimtvDeps.add(s.loc.t)
  28. m.g.nimtv.addf("$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.snippet])
  29. else:
  30. if isExtern: m.s[cfsVars].add("extern ")
  31. elif lfExportLib in s.loc.flags: m.s[cfsVars].add("N_LIB_EXPORT_VAR ")
  32. else: m.s[cfsVars].add("N_LIB_PRIVATE ")
  33. if optThreads in m.config.globalOptions:
  34. let sym = s.typ.sym
  35. if sym != nil and sfCppNonPod in sym.flags:
  36. m.s[cfsVars].add("NIM_THREAD_LOCAL ")
  37. else: m.s[cfsVars].add("NIM_THREADVAR ")
  38. m.s[cfsVars].add(getTypeDesc(m, s.loc.t))
  39. m.s[cfsVars].addf(" $1;$n", [s.loc.snippet])
  40. proc generateThreadLocalStorage(m: BModule) =
  41. if m.g.nimtv != "" and (usesThreadVars in m.flags or sfMainModule in m.module.flags):
  42. for t in items(m.g.nimtvDeps): discard getTypeDesc(m, t)
  43. finishTypeDescriptions(m)
  44. var typedef = newBuilder("")
  45. typedef.addTypedef(name = "NimThreadVars"):
  46. typedef.addSimpleStruct(m, name = "", baseType = ""):
  47. typedef.add(m.g.nimtv)
  48. m.s[cfsSeqTypes].add(typedef)
  49. proc generateThreadVarsSize(m: BModule) =
  50. if m.g.nimtv != "":
  51. let externc = if m.config.backend == backendCpp or
  52. sfCompileToCpp in m.module.flags: "extern \"C\" "
  53. else: ""
  54. m.s[cfsProcs].addf(
  55. "$#NI NimThreadVarsSize(){return (NI)sizeof(NimThreadVars);}$n",
  56. [externc.rope])