xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/tests/test_task_service.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * test_task_service.h
3  *
4  *  Copyright (c) 2021 Rockchip Eletronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <signal.h>
20 
21 #include <cassert>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <memory>
25 
26 #include "task_service.h"
27 
28 using namespace XCam;
29 
30 class MockParam {};
31 
32 struct BarTask : public ServiceTask<MockParam> {
operator ()BarTask33     virtual TaskResult operator()(ServiceParam<MockParam>& p) override final {
34         std::cout << "run id " << p.payload.get() << "\n" << std::flush;
35         int time = std::rand() % 50 + 1;
36         std::this_thread::sleep_for(std::chrono::milliseconds(time));
37         return TaskResult::kSuccess;
38     }
39 };
40 
41 bool stop = false;
42 
worker_thread(std::shared_ptr<TaskService<MockParam>> arg)43 void worker_thread(std::shared_ptr<TaskService<MockParam>> arg) {
44     std::shared_ptr<TaskService<MockParam>> svc = arg;
45 
46     while (!stop) {
47         auto a = svc->dequeue();
48         std::cout << "worker dequed type " << std::to_string(static_cast<int>(a.state))
49                   << " handler " << static_cast<void*>(a.payload.get()) << "\n"
50                   << std::flush;
51         if (a.state != ParamState::kNull) {
52             svc->enqueue(std::move(a));
53         }
54         std::this_thread::sleep_for(std::chrono::milliseconds(1));
55     }
56 }
57 
58 std::shared_ptr<TaskService<MockParam>> svc;
59 
signal_handle(int signo)60 static void signal_handle(int signo) {
61     stop = true;
62     svc->stop();
63 }
64 
main()65 int main() {
66     signal(SIGINT, signal_handle);
67     signal(SIGQUIT, signal_handle);
68     signal(SIGTERM, signal_handle);
69 
70     auto task = std::unique_ptr<ServiceTask<MockParam>>(new BarTask);
71     svc = std::make_shared<TaskService<MockParam>>(std::move(task), false);
72 
73     svc->setMaxProceedTime(TaskDuration(10));
74     svc->setMaxProceedTimeByFps(40);
75     svc->start();
76 
77     std::thread thr(worker_thread, svc);
78 
79     while (!stop) {
80         auto p = svc->dequeue();
81         std::cout << "main dequed type " << std::to_string(static_cast<int>(p.state)) << " handler "
82                   << static_cast<void*>(p.payload.get()) << "\n"
83                   << std::flush;
84         if (p.state != ParamState::kNull) {
85             svc->enqueue(p);
86         }
87         std::this_thread::sleep_for(std::chrono::milliseconds(10));
88     }
89 
90     svc->stop();
91     thr.join();
92 
93     return 0;
94 }
95