1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | * \author guonix |
17 | * \date Jan 2021 |
18 | * \brief |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <bthread/bthread.h> |
24 | #include "task.h" |
25 | |
26 | namespace proxima { |
27 | namespace be { |
28 | namespace query { |
29 | |
30 | /*! |
31 | * Bthread Task interface |
32 | */ |
33 | class BthreadTask : public Task { |
34 | public: |
35 | //! Constructor |
36 | explicit BthreadTask(std::string name); |
37 | |
38 | //! Destructor |
39 | ~BthreadTask() override; |
40 | |
41 | public: |
42 | //! Retrieve task name |
43 | const std::string &name() const override; |
44 | |
45 | int exit_code() const override; |
46 | |
47 | //! Run task, 0 for success otherwise failed |
48 | int run() override; |
49 | |
50 | //! Retrieve the status of task, readonly |
51 | Status status() const override; |
52 | |
53 | //! Update status |
54 | void status(Status status) override; |
55 | |
56 | //! true for task in running stage, otherwise return false |
57 | bool running() const override; |
58 | |
59 | //! true for task has been finished, otherwise return false |
60 | bool finished() const override; |
61 | |
62 | //! Executes the task object exactly once, even if called concurrently, from |
63 | // several threads. return immediately if invoke run method after run_once. |
64 | int run_once() override; |
65 | |
66 | //! Wait until task has been finished, return value same with finished() |
67 | bool wait_finish() override; |
68 | |
69 | private: |
70 | //! Run interface, all derived class should implement this function |
71 | virtual int do_run() = 0; |
72 | |
73 | private: |
74 | //! task name |
75 | std::string name_{}; |
76 | |
77 | //! task status |
78 | std::atomic<Status> status_{Status::INITIALIZED}; |
79 | |
80 | //! Return code of run interface |
81 | int exit_code_{0}; |
82 | |
83 | //! Status mutex |
84 | bthread_mutex_t mutex_; |
85 | |
86 | //! Finished event condition |
87 | bthread_cond_t cond_; |
88 | }; |
89 | |
90 | |
91 | } // namespace query |
92 | } // namespace be |
93 | } // namespace proxima |
94 | |