/// \warning call only from event loop thread, otherwise the behavior is undefined. void on_connect(task<std::error_code>::future_move_type future, std::shared_ptr<task<void>::promise_type> promise) { const auto ec = future.get(); if (ec) { switch (ec.value()) { case asio::error::already_started: queue->push_back(promise); break; case asio::error::already_connected: promise->set_value(); break; default: promise->set_exception(std::system_error(ec)); queue.apply([&](queue_type& queue) { for (auto it = queue.begin(); it != queue.end(); ++it) { (*it)->set_exception(std::system_error(ec)); } queue.clear(); }); } } else { promise->set_value(); queue.apply([&](queue_type& queue) { for (auto it = queue.begin(); it != queue.end(); ++it) { (*it)->set_value(); } queue.clear(); }); } }
void spool() { state.apply([&](state_type& state) { if (!state->stopped()) { throw std::logic_error("invalid state"); } COCAINE_LOG_TRACE(log, "app is spooling"); state.reset(new state::spooling_t( context, *loop, manifest(), profile, std::bind(&app_state_t::on_spool, shared_from_this(), ph::_1, ph::_2) )); }); }
void worker(int worker, int iterations, int dt) { for(int i=0; i<iterations; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(dt)); auto res = foo.data(); // we now have a unique_lock until the end of the scope std::cout << "Worker " << worker << " adding " << i << std::endl; res->push_back(i); } }
void publish() { try { state.synchronize()->reset( new state::running_t(context, manifest(), profile, log.get(), loop) ); } catch (const std::exception& err) { COCAINE_LOG_ERROR(log, "unable to publish app: %s", err.what()); cancel(err.what()); } // Attempt to finish node service's request. try { deferred.close(); } catch (const std::exception&) { // Ignore if the client has been disconnected. } }
void cancel(std::string cause = "manually stopped") { state.synchronize()->reset(new state::stopped_t(std::move(cause))); }
dynamic_t info(app_t::info_policy_t policy) const { return (*state.synchronize())->info(policy); }
void swap(synchronized<T, M>& a, synchronized<T, M>& b) { a.swap(b); }