kern_ctx.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // package kern_ctx -- контекст ядра.
  2. package kern_ctx
  3. import (
  4. "context"
  5. "gitp78su.ipnodns.ru/svi/kern/v4/d0"
  6. "gitp78su.ipnodns.ru/svi/kern/v4/d1"
  7. "gitp78su.ipnodns.ru/svi/kern/v4/d1/local_ctx"
  8. "gitp78su.ipnodns.ru/svi/kern/v4/d2/kernel_keeper"
  9. "gitp78su.ipnodns.ru/svi/kern/v4/d2/kspec"
  10. "gitp78su.ipnodns.ru/svi/kern/v4/d2/kwg"
  11. )
  12. // KernCtx -- контекст ядра.
  13. type KernCtx struct {
  14. *d1.LocalCtx
  15. log *d1.LogBuf
  16. ctxBg context.Context // Неотменяемый контекст ядра
  17. ctx context.Context // Отменяемый контекст ядра
  18. fnCancel func() // Функция отмены контекста ядра
  19. kernKeeper *kernel_keeper.KernKeeper // Встроенный сторож отмены контекста системным сигналом
  20. kernWg kspec.IKernelWg // Встроенный ожидатель потока
  21. }
  22. var (
  23. kernCtx *KernCtx // Глобальный объект контекста приложения
  24. ctxBg context.Context
  25. internalCtx context.Context
  26. internalFnCancel func()
  27. block *d0.MSafeMutex
  28. )
  29. func _init() {
  30. block = d0.MutSafeMutex()
  31. ctxBg = context.Background()
  32. internalCtx, internalFnCancel = context.WithCancel(ctxBg)
  33. }
  34. func init() {
  35. _init()
  36. }
  37. // GetIntenalCtx -- возвроащает внутренний контекст
  38. func GetInternalCtx() (context.Context, func()) {
  39. block.Lock()
  40. defer block.Unlock()
  41. return internalCtx, internalFnCancel
  42. }
  43. // GetKernCtx -- возвращает контекст ядра.
  44. func GetKernCtx() *KernCtx {
  45. block.Lock()
  46. defer block.Unlock()
  47. if kernCtx != nil {
  48. return kernCtx
  49. }
  50. owner := d0.LetOwner(1, "kern_ctx")
  51. sf := &KernCtx{
  52. LocalCtx: local_ctx.NewLocalCtx(owner, internalCtx),
  53. ctxBg: ctxBg,
  54. ctx: internalCtx,
  55. fnCancel: internalFnCancel,
  56. }
  57. sf.log = sf.Log()
  58. sf.kernWg = kwg.GetKernelWg(sf.ctx)
  59. kKeep := kernel_keeper.GetKernelKeeper(sf.ctx, sf.fnCancel, sf.kernWg)
  60. sf.kernKeeper = kKeep
  61. kernCtx = sf
  62. return kernCtx
  63. }
  64. // Keeper -- возвращает сторож системных сигналов.
  65. func (sf *KernCtx) Keeper() *kernel_keeper.KernKeeper {
  66. return sf.kernKeeper
  67. }
  68. // Wg -- возвращает ожидатель потоков.
  69. func (sf *KernCtx) Wg() kspec.IKernelWg {
  70. return sf.kernWg
  71. }
  72. // AllDone -- блокирующий вызов ожидания отмены контекста ядра.
  73. func (sf *KernCtx) AllDone() {
  74. <-sf.ctx.Done()
  75. sf.log.Debug("kCtx.Done()")
  76. }
  77. // CtxBg -- возвращает неотменяемый контекст ядра (лучше не использовать).
  78. func (sf *KernCtx) CtxBg() context.Context {
  79. return sf.ctxBg
  80. }
  81. // Cancel -- отменяет контекст ядра.
  82. func (sf *KernCtx) Cancel() {
  83. sf.fnCancel()
  84. sf.log.Debug("kCtx.Cancel()")
  85. }