1 /*
2  * Copyright (C) 2022 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 FILLP_EPOLL_H
17 #define FILLP_EPOLL_H
18 #include "rb_tree.h"
19 #include "hlist.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /*
26  * This structure is stored inside the "private_data" member of the file
27  * structure and represent the main data structure for the eventpoll
28  * interface.
29  */
30 struct EventPoll {
31     struct Hlist rdList; /* epitem(ready fd) list */
32     /* RB-Tree root used to store checked fd structs */
33     struct RbRoot rbr; /* epitem storage. epitem will be storaged here if added by epoll_ctl */
34 
35     SYS_ARCH_SEM appSem;     /* protect data from multiple app thread */
36     SYS_ARCH_SEM waitSem;    /* Notify the ep_wait */
37     SYS_ARCH_SEM appCoreSem; /* protect data from app thread and core thread */
38 
39     /** don't signal the same semaphore twice: set to 1 when signalled */
40     SysArchAtomic semSignalled;
41 
42 #ifdef FILLP_64BIT_ALIGN
43     FILLP_UINT8 padd[4];
44 #endif
45 };
46 
47 /*
48  * Each file descriptor added to the eventpoll interface will
49  * have an entry of this type linked to the hash.
50  */
51 struct EpItem {
52     /* RB-Tree node used to link this structure to the eventpoll rb-tree */
53     struct RbNode rbn; /* Will be added to eventpoll->rbr */
54 
55     /* List header used to link this structure to the eventpoll ready list */
56     struct HlistNode rdlNode; /* Will be added to eventpoll->rdllist */
57 
58     struct HlistNode sockWaitNode; /* Will be added to ftSock->epoll_taskList */
59 
60     /* The "container" of this item -- this is core pointer always */
61     struct EventPoll *ep;
62 
63     /* The structure that describe the interested events and the source fd */
64     struct SpungeEpollEvent event;
65     FILLP_UINT32 revents;
66 
67     FILLP_INT fileDespcriptor; /* The file descriptor information this item refers to */
68 
69     void *next;
70 };
71 
EpItemEntryRbNode(struct RbNode * node)72 static __inline struct EpItem *EpItemEntryRbNode(struct RbNode *node)
73 {
74     return (struct EpItem *)((char *)(node) - (uintptr_t)(&(((struct EpItem *)0)->rbn)));
75 }
76 
EpItemEntryRdlNode(struct HlistNode * node)77 static __inline struct EpItem *EpItemEntryRdlNode(struct HlistNode *node)
78 {
79     return (struct EpItem *)((char *)(node) - (uintptr_t)(&(((struct EpItem *)0)->rdlNode)));
80 }
81 
EpitemEntrySockWaitNode(struct HlistNode * node)82 static __inline struct EpItem *EpitemEntrySockWaitNode(struct HlistNode *node)
83 {
84     return (struct EpItem *)((char *)(node) - (uintptr_t)(&(((struct EpItem *)0)->sockWaitNode)));
85 }
86 
87 void EpSocketReady(struct EventPoll *scb, struct EpItem *epiItem);
88 void EpDelRdlnode(struct EventPoll *ep, struct EpItem *epi);
89 void EpollUpdateEpEvent(struct EpItem *epi);
90 
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #endif /* FILLP_EPOLL_H */
97