Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / cpp_interfaces / ie_task.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <vector>
8 #include <mutex>
9 #include <memory>
10 #include <condition_variable>
11 #include <thread>
12 #include <queue>
13 #include "ie_api.h"
14 #include "details/ie_exception.hpp"
15 #include "cpp_interfaces/exception2status.hpp"
16 #include "cpp_interfaces/ie_task_synchronizer.hpp"
17 #include <iostream>
18 #include "ie_profiling.hpp"
19
20 namespace InferenceEngine {
21
22 class INFERENCE_ENGINE_API_CLASS(Task) {
23 public:
24     typedef std::shared_ptr<Task> Ptr;
25     /**
26      *@brief Enumeration to hold status of the Task
27      */
28     typedef enum {
29         // Inference failed with some error
30                 TS_ERROR = -1,
31         // Task was never started
32                 TS_INITIAL = 0,
33         // Task is ongoing: waiting in a queue of task or doing inference
34                 TS_BUSY,
35         // Task was finished, result is ready
36                 TS_DONE,
37         // Task was started to executed but was interrupted
38         // can change status to BUSY or DONE
39                 TS_POSTPONED
40     } Status;
41
42     Task();
43
44     Task(std::function<void()> function);
45
46     /**
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
51      */
52     virtual Status runNoThrowNoBusyCheck() noexcept;
53
54     /**
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
59      */
60     Status runWithSynchronizer(TaskSynchronizer::Ptr &taskSynchronizer);
61
62     /**
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
67      */
68     Status wait(int64_t millis_timeout);
69
70     /**
71      * @brief Occupies task for launching. Makes busy status, if task is not running
72      * @return true if occupation succeed, otherwise - false
73      */
74     bool occupy();
75
76     Status getStatus();
77
78     void checkException();
79
80     static StatusCode TaskStatus2StatusCode(Status status);
81
82     bool isOnWait();
83
84 protected:
85     void setStatus(Status status);
86
87 protected:
88     std::function<void()> _function;
89     Status _status;
90     std::exception_ptr _exceptionPtr = nullptr;
91     std::mutex _taskStatusMutex;
92     std::condition_variable _isTaskDoneCondVar;
93
94     bool _isOnWait = false;
95 };
96
97 }  // namespace InferenceEngine