@geastack/host 0.1.30 → 0.1.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,67 +16,117 @@
16
16
 
17
17
  namespace gea::platform {
18
18
 
19
+ #ifdef ESP_PLATFORM
20
+ namespace internal_stack_detail {
21
+
22
+ inline constexpr unsigned kWorkerStackBytes = 10240;
23
+
24
+ struct Worker {
25
+ SemaphoreHandle_t ready = nullptr;
26
+ SemaphoreHandle_t done = nullptr;
27
+ const std::function<void()> *action = nullptr;
28
+ std::exception_ptr error;
29
+ TaskHandle_t task = nullptr;
30
+ };
31
+
32
+ inline std::mutex &workerMutex() {
33
+ static std::mutex mutex;
34
+ return mutex;
35
+ }
36
+
37
+ inline Worker &worker() {
38
+ static Worker instance;
39
+ return instance;
40
+ }
41
+
42
+ // Creates the worker task once. Caller holds workerMutex(). Returns an error
43
+ // message, or nullptr when the worker exists.
44
+ inline const char *ensureWorkerLocked() {
45
+ Worker &w = worker();
46
+ if (w.task) return nullptr;
47
+ w.ready = xSemaphoreCreateBinary();
48
+ w.done = xSemaphoreCreateBinary();
49
+ if (!w.ready || !w.done) {
50
+ if (w.ready) vSemaphoreDelete(w.ready);
51
+ if (w.done) vSemaphoreDelete(w.done);
52
+ w.ready = w.done = nullptr;
53
+ return "Internal-stack semaphore allocation failed";
54
+ }
55
+ const auto run = [](void *argument) {
56
+ auto &w = *static_cast<Worker *>(argument);
57
+ for (;;) {
58
+ xSemaphoreTake(w.ready, portMAX_DELAY);
59
+ try {
60
+ (*w.action)();
61
+ } catch (...) {
62
+ w.error = std::current_exception();
63
+ }
64
+ w.action = nullptr;
65
+ xSemaphoreGive(w.done);
66
+ }
67
+ };
68
+ if (xTaskCreateWithCaps(run, "gea_internal", kWorkerStackBytes, &w, 5, &w.task,
69
+ MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) != pdPASS) {
70
+ vSemaphoreDelete(w.ready);
71
+ vSemaphoreDelete(w.done);
72
+ w.ready = w.done = nullptr;
73
+ w.task = nullptr;
74
+ return "Internal-stack worker allocation failed";
75
+ }
76
+ return nullptr;
77
+ }
78
+
79
+ } // namespace internal_stack_detail
80
+ #endif
81
+
82
+ // Create the internal-stack worker now, from any task. Boards whose internal
83
+ // RAM is nearly all claimed during bring-up (BLE controller, then display DMA
84
+ // buffers that size themselves to whatever is left) call this before those
85
+ // claims, so flash I/O from a PSRAM-stack app task still has its stack later.
86
+ // Returns false when the worker could not be created (it is retried on use).
87
+ inline bool reserveInternalStackWorker() {
88
+ #ifdef ESP_PLATFORM
89
+ std::lock_guard<std::mutex> lock(internal_stack_detail::workerMutex());
90
+ return internal_stack_detail::ensureWorkerLocked() == nullptr;
91
+ #else
92
+ return true;
93
+ #endif
94
+ }
95
+
96
+ // True when the internal-stack worker already exists. Code that needs an
97
+ // internal stack only once (BLE bring-up) borrows the worker when an app
98
+ // already paid for it, instead of allocating a second internal stack, and
99
+ // never creates it for an app that did not.
100
+ inline bool internalStackWorkerReady() {
101
+ #ifdef ESP_PLATFORM
102
+ std::lock_guard<std::mutex> lock(internal_stack_detail::workerMutex());
103
+ return internal_stack_detail::worker().task != nullptr;
104
+ #else
105
+ return true;
106
+ #endif
107
+ }
108
+
19
109
  // Flash-backed VFS and radio configuration can disable caches. Run these
20
110
  // operations on an internal stack when a compiled application uses PSRAM.
21
111
  inline void onInternalStack(const std::function<void()> &action, unsigned stackBytes = 6144) {
22
112
  #ifdef ESP_PLATFORM
23
113
  int marker;
24
114
  if (esp_ptr_external_ram(&marker)) {
25
- static std::mutex mutex;
26
- std::lock_guard<std::mutex> lock(mutex);
27
-
28
- struct Worker {
29
- SemaphoreHandle_t ready = nullptr;
30
- SemaphoreHandle_t done = nullptr;
31
- const std::function<void()> *action = nullptr;
32
- std::exception_ptr error;
33
- TaskHandle_t task = nullptr;
34
- };
35
-
36
- static Worker worker;
37
- if (stackBytes > 10240) {
115
+ using namespace internal_stack_detail;
116
+ std::lock_guard<std::mutex> lock(workerMutex());
117
+ if (stackBytes > kWorkerStackBytes) {
38
118
  throw std::runtime_error("Internal-stack action exceeds worker capacity");
39
119
  }
40
- if (!worker.task) {
41
- worker.ready = xSemaphoreCreateBinary();
42
- worker.done = xSemaphoreCreateBinary();
43
- if (!worker.ready || !worker.done) {
44
- if (worker.ready) {
45
- vSemaphoreDelete(worker.ready);
46
- }
47
- if (worker.done) {
48
- vSemaphoreDelete(worker.done);
49
- }
50
- worker.ready = worker.done = nullptr;
51
- throw std::runtime_error("Internal-stack semaphore allocation failed");
52
- }
53
- const auto run = [](void *argument) {
54
- auto &worker = *static_cast<Worker *>(argument);
55
- for (;;) {
56
- xSemaphoreTake(worker.ready, portMAX_DELAY);
57
- try {
58
- (*worker.action)();
59
- } catch (...) {
60
- worker.error = std::current_exception();
61
- }
62
- worker.action = nullptr;
63
- xSemaphoreGive(worker.done);
64
- }
65
- };
66
- if (xTaskCreateWithCaps(run, "gea_internal", 10240, &worker, 5, &worker.task,
67
- MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) != pdPASS) {
68
- vSemaphoreDelete(worker.ready);
69
- vSemaphoreDelete(worker.done);
70
- worker.ready = worker.done = nullptr;
71
- throw std::runtime_error("Internal-stack worker allocation failed");
72
- }
120
+ if (const char *error = ensureWorkerLocked()) {
121
+ throw std::runtime_error(error);
73
122
  }
74
- worker.error = {};
75
- worker.action = &action;
76
- xSemaphoreGive(worker.ready);
77
- xSemaphoreTake(worker.done, portMAX_DELAY);
78
- if (worker.error) {
79
- std::rethrow_exception(worker.error);
123
+ Worker &w = worker();
124
+ w.error = {};
125
+ w.action = &action;
126
+ xSemaphoreGive(w.ready);
127
+ xSemaphoreTake(w.done, portMAX_DELAY);
128
+ if (w.error) {
129
+ std::rethrow_exception(w.error);
80
130
  }
81
131
  return;
82
132
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/host",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
4
4
  "description": "GeaStack host bindings and platform service sources.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {