1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
10 #include <condition_variable>
14 #include "details/ie_exception.hpp"
15 #include "cpp_interfaces/exception2status.hpp"
16 #include "cpp_interfaces/ie_task_synchronizer.hpp"
18 #include "ie_profiling.hpp"
20 namespace InferenceEngine {
22 class INFERENCE_ENGINE_API_CLASS(Task) {
24 typedef std::shared_ptr<Task> Ptr;
26 *@brief Enumeration to hold status of the Task
29 // Inference failed with some error
31 // Task was never started
33 // Task is ongoing: waiting in a queue of task or doing inference
35 // Task was finished, result is ready
37 // Task was started to executed but was interrupted
38 // can change status to BUSY or DONE
44 explicit Task(const std::function<void()> &function);
45 virtual ~Task() = default;
48 * @brief Executes the task with catching all exceptions. It doesn't check that task is running
49 * @note Not recommended to call from multiple threads without synchronizaion, because there's no check for task status
50 * @note If task throws exception, it returns TS_ERROR status. Call getExceptionPtr to get pointer to exception.
51 * @return Enumeration of the task status: TS_DONE(2) for success
53 virtual Status runNoThrowNoBusyCheck() noexcept;
56 * @brief Executes the task in turn, controlled by task synchronizer
57 * @note Can be called from multiple threads - will return TS_BUSY(1) status for the task which is currently running
58 * @param taskSynchronizer - shared pointer to the task synchronizer, which ensures thread-safe execution multiple tasks from multiple threads
59 * @return Enumeration of the task status: TS_DONE(2) for success
61 Status runWithSynchronizer(TaskSynchronizer::Ptr &taskSynchronizer);
64 * @brief Waits for the finishing task. Blocks until specified millis_timeout has elapsed or the task is done, whichever comes first.
65 * @param millis_timeout Maximum duration in milliseconds to block for
66 * @note if millis_timeout < 0 it blocks until task is done
67 * @return Enumeration of the task status: TS_DONE(2) for success
69 Status wait(int64_t millis_timeout);
72 * @brief Occupies task for launching. Makes busy status, if task is not running
73 * @return true if occupation succeed, otherwise - false
79 void checkException();
81 static StatusCode TaskStatus2StatusCode(Status status);
86 void setStatus(Status status);
89 std::function<void()> _function;
91 std::exception_ptr _exceptionPtr = nullptr;
92 std::mutex _taskStatusMutex;
93 std::condition_variable _isTaskDoneCondVar;
95 bool _isOnWait = false;
98 } // namespace InferenceEngine