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 Task(std::function<void()> function);
47 * @brief Executes the task with catching all exceptions. It doesn't check that task is running
48 * @note Not recommended to call from multiple threads without synchronizaion, because there's no check for task status
49 * @note If task throws exception, it returns TS_ERROR status. Call getExceptionPtr to get pointer to exception.
50 * @return Enumeration of the task status: TS_DONE(2) for success
52 virtual Status runNoThrowNoBusyCheck() noexcept;
55 * @brief Executes the task in turn, controlled by task synchronizer
56 * @note Can be called from multiple threads - will return TS_BUSY(1) status for the task which is currently running
57 * @param taskSynchronizer - shared pointer to the task synchronizer, which ensures thread-safe execution multiple tasks from multiple threads
58 * @return Enumeration of the task status: TS_DONE(2) for success
60 Status runWithSynchronizer(TaskSynchronizer::Ptr &taskSynchronizer);
63 * @brief Waits for the finishing task. Blocks until specified millis_timeout has elapsed or the task is done, whichever comes first.
64 * @param millis_timeout Maximum duration in milliseconds to block for
65 * @note if millis_timeout < 0 it blocks until task is done
66 * @return Enumeration of the task status: TS_DONE(2) for success
68 Status wait(int64_t millis_timeout);
71 * @brief Occupies task for launching. Makes busy status, if task is not running
72 * @return true if occupation succeed, otherwise - false
78 void checkException();
80 static StatusCode TaskStatus2StatusCode(Status status);
85 void setStatus(Status status);
88 std::function<void()> _function;
90 std::exception_ptr _exceptionPtr = nullptr;
91 std::mutex _taskStatusMutex;
92 std::condition_variable _isTaskDoneCondVar;
94 bool _isOnWait = false;
97 } // namespace InferenceEngine