blob: 23d2944ac2deb450ad2426b0dd66ed92c287063c [file] [log] [blame]
fei.dengb9a1a572023-09-13 01:33:57 +00001/*
2 * Copyright (C) 2021 Amlogic Corporation.
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#ifndef _TOOS_POLL_H_
17#define _TOOS_POLL_H_
18
19#include <stdint.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <list>
23#include <atomic>
24#include <poll.h>
25
26#include "Mutex.h"
27
28/**
29 * @brief Poll is a implement wrap about
30 * poll,but Poll can wakeup when poll is waiting
31 * for ever,if set flushing
32 * the sequence api is
33 * 1.poll = new Poll(true)
34 * 2.poll->addfd(fd)
35 * 3.poll->setFdReadable(fd,true)/poll->setFdWritable(fd,true)
36 * 4.poll->wait(waittime)
37 * ........
38 * 5.if (poll->isReadable(fd))
39 * 6. read data from fd
40 * 7.if (poll->isReadable(fd))
41 * 8. write data to fd
42 * if want to destroy poll,call
43 * 9.poll->setFlushing
44 * 10.delete poll
45 */
46namespace Tls {
47class Poll {
48 public:
49 Poll(bool controllable);
50 virtual ~Poll();
51 /**
52 * @brief add a fd to poll
53 *
54 * @param fd
55 * @return int
56 */
57 int addFd(int fd);
58 /**
59 * @brief remove fd from poll
60 *
61 * @param fd
62 * @return int
63 */
64 int removeFd(int fd);
65 /**
66 * @brief Set the Fd Readable ,poll will
67 * check readable event
68 * @param fd
69 * @param readable
70 * @return int 0 success, -1 fail
71 */
72 int setFdReadable(int fd, bool readable);
73 /**
74 * @brief Set the Fd Writable,poll will
75 * check writable event
76 * @param fd
77 * @param writable
78 * @return int 0 success, -1 fail
79 */
80 int setFdWritable(int fd, bool writable);
81 /**
82 * @brief wait fd event
83 *
84 * @param timeoutMs wait millisecond time, -1 will wait for ever
85 * otherwise wait the special nanasecond time
86 * @return int active fd count
87 */
88 int wait(int64_t timeoutMs /*millisecond*/);
89 /**
90 * @brief Set the Flushing if poll wait had called
91 * and waiting, this func will send a raise event to
92 * wakeup poll wait
93 * @param flushing
94 */
95 void setFlushing(bool flushing);
96 /**
97 * @brief check this fd if had data to read
98 *
99 * @param fd file fd
100 * @return true
101 * @return false
102 */
103 bool isReadable(int fd);
104 /**
105 * @brief check this fd if can write
106 *
107 * @param fd file fd
108 * @return true
109 * @return false
110 */
111 bool isWritable(int fd);
112 private:
113 struct pollfd * findFd(int fd);
114 bool wakeEvent();
115 bool releaseEvent();
116 bool raiseWakeup();
117 bool releaseWakeup();
118 bool releaseAllWakeup();
119 Tls::Mutex mMutex;
120 int mControlReadFd;
121 int mControlWriteFd;
122 struct pollfd * mFds;
123 int mFdsMaxCnt;
124 int mFdsCnt;
125 bool mControllable;
126 std::atomic<int> mControlPending;
127 std::atomic<int> mFlushing;
128 std::atomic<int> mWaiting; //store waiting thread count
129};
130
131}
132
133#endif /*_TOOS_POLL_H_*/