| 120 | } |
| 121 | |
| 122 | WasmResult SharedQueue::enqueue(uint32_t token, std::string_view value) { |
| 123 | std::string vm_key; |
| 124 | uint32_t context_id; |
| 125 | CallOnThreadFunction call_on_thread; |
| 126 | |
| 127 | { |
| 128 | std::lock_guard<std::mutex> lock(mutex_); |
| 129 | auto it = queues_.find(token); |
| 130 | if (it == queues_.end()) { |
| 131 | return WasmResult::NotFound; |
| 132 | } |
| 133 | Queue *target_queue = &(it->second); |
| 134 | vm_key = target_queue->vm_key; |
| 135 | context_id = target_queue->context_id; |
| 136 | call_on_thread = target_queue->call_on_thread; |
| 137 | target_queue->queue.emplace_back(value); |
| 138 | } |
| 139 | |
| 140 | call_on_thread([vm_key, context_id, token] { |
| 141 | // This code may or may not execute in another thread. |
| 142 | // Make sure that the lock is no longer held here. |
| 143 | auto wasm = getThreadLocalWasm(vm_key); |
| 144 | if (wasm) { |
| 145 | auto *context = wasm->wasm()->getContext(context_id); |
| 146 | if (context != nullptr) { |
| 147 | context->onQueueReady(token); |
| 148 | } |
| 149 | } |
| 150 | }); |
| 151 | return WasmResult::Ok; |
| 152 | } |
| 153 | |
| 154 | } // namespace proxy_wasm |
no test coverage detected