|
6 | 6 |
|
7 | 7 | #include "Common/Cpp/Exceptions.h" |
8 | 8 | #include "Common/Cpp/CancellableScope.h" |
| 9 | +#include "Common/Cpp/Concurrency/AsyncDispatcher.h" |
| 10 | +#include "CommonFramework/Tools/ProgramEnvironment.h" |
9 | 11 | #include "InferenceSession.h" |
10 | 12 | #include "InferenceRoutines.h" |
11 | 13 |
|
@@ -59,80 +61,50 @@ int run_until( |
59 | 61 | ); |
60 | 62 |
|
61 | 63 | try{ |
62 | | - if (command){ |
63 | | - command(subscope); |
64 | | - } |
| 64 | + command(subscope); |
65 | 65 | }catch (OperationCancelledException&){} |
66 | 66 |
|
67 | 67 | subscope.throw_if_cancelled_with_exception(); |
68 | 68 | scope.throw_if_cancelled(); |
69 | 69 |
|
70 | 70 | return session.triggered_index(); |
71 | 71 | } |
72 | | -#if 0 |
73 | | -int run_until( |
74 | | - VideoStream& stream, BotBaseContext& context, |
75 | | - std::function<void(BotBaseContext& context)>&& command, |
76 | | - const std::vector<PeriodicInferenceCallback>& callbacks, |
77 | | - std::chrono::milliseconds default_video_period, |
78 | | - std::chrono::milliseconds default_audio_period |
79 | | -){ |
80 | | - BotBaseContext subcontext(context, context.botbase()); |
81 | | - InferenceSession session( |
82 | | - subcontext, stream, |
83 | | - callbacks, |
84 | | - default_video_period, default_audio_period |
85 | | - ); |
86 | | - |
87 | | - try{ |
88 | | - if (command){ |
89 | | - command(subcontext); |
90 | | - } |
91 | | - subcontext.wait_for_all_requests(); |
92 | | - }catch (OperationCancelledException&){} |
93 | | - |
94 | | - subcontext.throw_if_cancelled_with_exception(); |
95 | | - context.throw_if_cancelled(); |
96 | | - |
97 | | - return session.triggered_index(); |
98 | | -} |
99 | | -#endif |
100 | 72 |
|
101 | 73 |
|
102 | 74 |
|
103 | | -#if 0 |
| 75 | +#if 1 |
104 | 76 | int run_until_with_time_limit( |
105 | | - ProgramEnvironment& env, VideoStream& stream, BotBaseContext& context, |
| 77 | + ProgramEnvironment& env, VideoStream& stream, CancellableScope& scope, |
106 | 78 | WallClock deadline, |
107 | | - std::function<void(BotBaseContext& context)>&& command, |
| 79 | + std::function<void(CancellableScope& scope)>&& command, |
108 | 80 | const std::vector<PeriodicInferenceCallback>& callbacks, |
109 | 81 | std::chrono::milliseconds default_video_period, |
110 | 82 | std::chrono::milliseconds default_audio_period |
111 | 83 | ){ |
112 | | - BotBaseContext subcontext(context, context.botbase()); |
| 84 | + CancellableHolder<CancellableScope> subscope(scope); |
113 | 85 | InferenceSession session( |
114 | | - subcontext, stream, |
| 86 | + subscope, stream, |
115 | 87 | callbacks, |
116 | 88 | default_video_period, default_audio_period |
117 | 89 | ); |
118 | 90 |
|
119 | 91 | bool timed_out = false; |
120 | 92 | std::unique_ptr<AsyncTask> timer = env.realtime_dispatcher().dispatch([&]{ |
121 | | - subcontext.wait_until(deadline); |
| 93 | + subscope.wait_until(deadline); |
122 | 94 | timed_out = true; |
123 | | - subcontext.cancel_now(); |
| 95 | + subscope.cancel(nullptr); |
124 | 96 | }); |
125 | 97 |
|
126 | 98 | try{ |
127 | 99 | if (command){ |
128 | | - command(subcontext); |
| 100 | + command(subscope); |
129 | 101 | } |
130 | | - subcontext.wait_for_all_requests(); |
| 102 | +// subscope.wait_for_all_requests(); |
131 | 103 | }catch (OperationCancelledException&){} |
132 | 104 |
|
133 | 105 | timer->wait_and_rethrow_exceptions(); |
134 | | - subcontext.throw_if_cancelled_with_exception(); |
135 | | - context.throw_if_cancelled(); |
| 106 | + subscope.throw_if_cancelled_with_exception(); |
| 107 | + scope.throw_if_cancelled(); |
136 | 108 |
|
137 | 109 | return timed_out ? -2 : session.triggered_index(); |
138 | 110 | } |
|
0 commit comments