• 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 REACTOR_H
17 #define REACTOR_H
18 
19 #include <stdint.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef struct Reactor Reactor;
26 typedef struct ReactorItemCbs ReactorItemCbs;
27 typedef struct ReactorItem ReactorItem;
28 
29 typedef enum { REACTOR_STATUS_STOP, REACTOR_STATUS_ERROR, REACTOR_STATUS_DONE } ReactorStatus;
30 
31 /**
32  * @brief Perform instantiation of the Reactor.
33  *        Succeed return Reactor instantiation, failed return NULL.
34  *
35  * @param name
36  * @return Reactor pointer.
37  * @since 6
38  */
39 Reactor *ReactorCreate();
40 
41 /**
42  * @brief Destroy instantiation of the Reactor.
43  *
44  * @param reactor Reactor pointer.
45  * @since 6
46  */
47 void ReactorDelete(Reactor *reactor);
48 
49 /**
50  * @brief Reactor start to monitor fds.
51  *
52  * @param reactor Reactor pointer.
53  * @return Succeed return 0, failed return -1.
54  * @since 6
55  */
56 int32_t ReactorStart(Reactor *reactor);
57 
58 /**
59  * @brief Reactor stop monitor fds.
60  *
61  * @param reactor Reactor pointer.
62  * @since 6
63  */
64 void ReactorStop(const Reactor *reactor);
65 
66 /**
67  * @brief Reactor Set its corresponding thread ID.
68  *
69  * @param reactor Reactor pointer.
70  * @param threadId Corresponding Thread ID.
71  * @since 6
72  */
73 void ReactorSetThreadId(Reactor *reactor, unsigned long threadId);
74 
75 /**
76  * @brief Register item into reactor
77  *
78  * @param reactor Reactor pointer.
79  * @param fd Monitor fd.
80  * @param context Callback context.
81  * @param onReadReady Callback function while fd is readready.
82  * @param onWriteReady Callback function while fd is writeready.
83  * @return Success return ReactorItem pointer. Failed return NULL.
84  * @since 6
85  */
86 ReactorItem *ReactorRegister(Reactor *reactor, int32_t fd, void *context, void (*onReadReady)(void *context),
87     void (*onWriteReady)(void *context));
88 
89 /**
90  * @brief UnRegist item from reactor. As well as delete it.
91  *
92  * @param item ReactorItem pointer.
93  * @since 6
94  */
95 void ReactorUnregister(ReactorItem *item);
96 
97 #ifdef __cplusplus
98 }
99 #endif
100 
101 #endif  // REACTOR_H
102