Spicy
context.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <iostream>
6 #include <memory>
7 #include <optional>
8 #include <utility>
9 #include <vector>
10 
11 #include <hilti/rt/fiber.h>
12 #include <hilti/rt/threading.h>
13 
14 namespace hilti::rt {
15 
23 struct Context {
27  explicit Context(vthread::ID vid);
28  ~Context();
29 
30  Context() = delete;
31  Context(const Context&) = delete;
32  Context(Context&&) = delete;
33  Context& operator=(const Context&) = delete;
34  Context& operator=(Context&&) = delete;
35 
40  vthread::ID vid;
41 
47 
50 
56  std::vector<std::shared_ptr<void>> hilti_globals;
57 
59  void* cookie = nullptr;
60 
62  uint64_t debug_indent{};
63 
69  const char* source_location{};
70 };
71 
72 namespace context {
73 namespace detail {
74 
82 extern Context*& current();
83 
85 extern Context* master();
86 
88 inline auto get() {
89  assert(current());
90  return current();
91 }
92 
97 inline auto set(Context* ctx) {
98  auto old = current();
99  current() = ctx;
100  return old;
101 }
102 
107 public:
108  explicit ResumableSetter(resumable::Handle* r) {
109  old = get()->resumable;
110  get()->resumable = r;
111  }
112 
113  ~ResumableSetter() { get()->resumable = old; }
114 
115  ResumableSetter(const ResumableSetter&) = delete;
116  ResumableSetter(ResumableSetter&&) = delete;
117  ResumableSetter& operator=(const ResumableSetter&) = delete;
118  ResumableSetter& operator=(ResumableSetter&&) = delete;
119 
120  resumable::Handle* old;
121 };
122 
123 } // namespace detail
124 
126 inline void saveCookie(void* cookie) { detail::get()->cookie = cookie; }
127 
129 inline void* cookie() { return detail::get()->cookie; }
130 
132 inline void clearCookie() { detail::get()->cookie = nullptr; }
133 
138 public:
139  explicit CookieSetter(void* cookie) {
140  _old = detail::get()->cookie;
141  detail::get()->cookie = cookie;
142  }
143 
144  ~CookieSetter() { detail::get()->cookie = _old; }
145 
146  CookieSetter() = delete;
147  CookieSetter(const CookieSetter&) = delete;
148  CookieSetter(CookieSetter&&) = delete;
149  CookieSetter& operator=(const CookieSetter&) = delete;
150  CookieSetter& operator=(CookieSetter&&) = delete;
151 
152 private:
153  void* _old;
154 };
155 
163 template<typename Function, typename... Params>
164 Resumable execute(Function f, Params&&... params) {
165  auto cb = [&](resumable::Handle* r) {
166  auto _ = detail::ResumableSetter(r);
167  return f(std::forward<Params>(params)...);
168  };
169 
170  Resumable r(std::move(cb));
171  r.run();
172  return r;
173 }
174 
175 } // namespace context
176 } // namespace hilti::rt
detail::FiberContext fiber
Definition: context.h:49
void * cookie
Definition: context.h:59
Definition: any.h:7
vthread::ID vid
Definition: context.h:40
const char * source_location
Definition: context.h:69
Definition: fiber.h:43
std::vector< std::shared_ptr< void > > hilti_globals
Definition: context.h:56
Definition: function.h:44
void run()
Definition: fiber.cc:539
uint64_t debug_indent
Definition: context.h:62
resumable::Handle * resumable
Definition: context.h:46
Definition: fiber.h:274
Definition: context.h:23
Definition: fiber.h:130