Publishing 2019 R3 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     explicit Task(const std::function<void()> &function);
45     virtual ~Task() = default;
46
47     /**
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
52      */
53     virtual Status runNoThrowNoBusyCheck() noexcept;
54
55     /**
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
60      */
61     Status runWithSynchronizer(TaskSynchronizer::Ptr &taskSynchronizer);
62
63     /**
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
68      */
69     Status wait(int64_t millis_timeout);
70
71     /**
72      * @brief Occupies task for launching. Makes busy status, if task is not running
73      * @return true if occupation succeed, otherwise - false
74      */
75     bool occupy();
76
77     Status getStatus();
78
79     void checkException();
80
81     static StatusCode TaskStatus2StatusCode(Status status);
82
83     bool isOnWait();
84
85 protected:
86     void setStatus(Status status);
87
88 protected:
89     std::function<void()> _function;
90     Status _status;
91     std::exception_ptr _exceptionPtr = nullptr;
92     std::mutex _taskStatusMutex;
93     std::condition_variable _isTaskDoneCondVar;
94
95     bool _isOnWait = false;
96 };
97
98 }  // namespace InferenceEngine