• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FUTURE_FACTORY_H
17 #define FUTURE_FACTORY_H
18 
19 #include <atomic>
20 #include <map>
21 #include <mutex>
22 
23 #include "plugin/i_plugin_callback.h"
24 #include "protocol/data_channel/include/request.h"
25 #include "protocol/data_channel/include/response.h"
26 #include "server_executor/include/future.h"
27 #include "server_executor/include/i_future.h"
28 #include "server_executor/include/i_future_listener.h"
29 #include "utils/aie_macros.h"
30 
31 namespace OHOS {
32 namespace AI {
33 class FutureFactory {
34     FORBID_COPY_AND_ASSIGN(FutureFactory);
35     FORBID_CREATE_BY_SELF(FutureFactory);
36 public:
37     static FutureFactory *GetInstance();
38 
39     static void ReleaseInstance();
40 
41     /**
42      * Generate a future instance and add it to process queue.
43      *
44      * @param [in] request Task request information.
45      * @return Returns RETCODE_SUCCESS(0) if the operation is successful, returns a non-zero value otherwise.
46      */
47     int CreateFuture(IRequest *request);
48 
49     /**
50      * Release the future corresponding to sequenceId.
51      *
52      * @param [in] sequenceId The sequence number that uniquely identifies an asynchronous task.
53      */
54     void Release(long long sequenceId);
55 
56     /**
57      * Register callback for the transactionId.
58      *
59      * @param [in] listener Callback.
60      * @param [in] transactionId Transaction ID.
61      */
62     void RegisterListener(IFutureListener *listener, long long transactionId);
63 
64     /**
65      * Unregister the callback corresponding to transactionId.
66      *
67      * @param [in] transactionId Transaction ID.
68      */
69     void UnregisterListener(long long transactionId);
70 
71     /**
72      * Process the asynchronous task response and pass it to the upper level callback.
73      *
74      * @param [in] event Plugin event, {@code ON_PLUGIN_SUCCEED} or {@code ON_PLUGIN_FAIL}.
75      * @param [in] response Response of asynchronous task processing.
76      * @return Returns RETCODE_SUCCESS(0) if the operation is successful, returns a non-zero value otherwise.
77      */
78     int ProcessResponse(PluginEvent event, IResponse *response);
79 
80 private:
81     void AddFuture(long long sequenceId, Future* future);
82 
83     void DeleteFuture(long long sequenceId);
84 
85     Future* FetchFuture(Response *response);
86 
87     IFutureListener *FindListener(long long transactionId);
88 
89     long long FindSequenceId();
90 
91 private:
92     static std::mutex mutex_;
93     static FutureFactory *instance_;
94 
95 private:
96     std::mutex innerMutex_;
97     using Futures = std::map<long long, Future*>;
98     Futures futures_;
99     std::atomic<long long> sequenceId_;
100     using FutureListeners = std::map<long long, IFutureListener*>;
101     FutureListeners listeners_;
102 };
103 } // namespace AI
104 } // namespace OHOS
105 
106 #endif // FUTURE_FACTORY_H