|
13 | 13 | #ifndef SCORE_MW_LIFECYCLE_LM_CONTROL_IMPL_HPP |
14 | 14 | #define SCORE_MW_LIFECYCLE_LM_CONTROL_IMPL_HPP |
15 | 15 |
|
| 16 | +#include <chrono> |
| 17 | +#include <condition_variable> |
16 | 18 | #include <cstddef> |
17 | 19 | #include <exception> |
18 | 20 | #include <memory> |
|
22 | 24 | #include <string_view> |
23 | 25 | #include <utility> |
24 | 26 |
|
| 27 | +#include "score/concurrency/condition_variable.h" |
25 | 28 | #include "score/language/safecpp/scoped_function/copyable_scoped_function.h" |
26 | 29 | #include "score/language/safecpp/scoped_function/scope.h" |
27 | 30 | #include "score/mw/launch_manager/common/log.hpp" |
@@ -245,6 +248,20 @@ class BasicLmControlImpl final : public ILmControl |
245 | 248 | return response.run_target; |
246 | 249 | } |
247 | 250 |
|
| 251 | + score::Result<void> wait_for_connection(std::chrono::milliseconds timeout, const score::cpp::stop_token& stop_token) |
| 252 | + override |
| 253 | + { |
| 254 | + std::unique_lock<std::mutex> lock{mutex_}; |
| 255 | + const bool connected = connection_cv_.wait_for(lock, stop_token, timeout, [this] { |
| 256 | + return proxy_.has_value(); |
| 257 | + }); |
| 258 | + if (!connected) |
| 259 | + { |
| 260 | + return score::MakeUnexpected(ExecErrc::kCommunicationError); |
| 261 | + } |
| 262 | + return {}; |
| 263 | + } |
| 264 | + |
248 | 265 | private: |
249 | 266 | using ProxyType = typename Traits::Proxy; |
250 | 267 | using HandleType = typename Traits::HandleType; |
@@ -282,9 +299,9 @@ class BasicLmControlImpl final : public ILmControl |
282 | 299 | return; |
283 | 300 | } |
284 | 301 |
|
285 | | - if (createProxy(handles.front())) |
| 302 | + if (createProxy(handles.front()) && subscribeToActivationResults()) |
286 | 303 | { |
287 | | - static_cast<void>(subscribeToActivationResults()); |
| 304 | + connection_cv_.notify_all(); |
288 | 305 | } |
289 | 306 | } |
290 | 307 |
|
@@ -430,6 +447,8 @@ class BasicLmControlImpl final : public ILmControl |
430 | 447 |
|
431 | 448 | // Guards proxy_ |
432 | 449 | std::mutex mutex_; |
| 450 | + // Signalled (under mutex_) once proxy_ is set up, wakes wait_for_connection() |
| 451 | + concurrency::InterruptibleConditionalVariableBasic<std::mutex, std::condition_variable> connection_cv_; |
433 | 452 | std::optional<ProxyType> proxy_; |
434 | 453 |
|
435 | 454 | // Handle of the running discovery search, stopped by the destructor. Written once by init() |
|
0 commit comments