33#include "sys/thread.h"
44#include "sys/locker.h"
55#include "sys/pollfd.h"
6+ #include "port/socketpair.h"
67#include "sockutil.h"
78#include "list.h"
89#include <errno.h>
910#include <stdlib.h>
1011#include <assert.h>
1112
12- #define N_SOCKETS FD_SETSIZE
13+ #define N_SOCKETS ( FD_SETSIZE-1)
1314
1415struct aio_poll_socket_t
1516{
@@ -19,13 +20,14 @@ struct aio_poll_socket_t
1920 int events ;
2021 int revents ;
2122 uint64_t expire ;
22- aio_poll_callback callback ;
23+ aio_poll_onpoll callback ;
2324 void * param ;
2425};
2526
2627struct aio_poll_t
2728{
2829 locker_t locker ;
30+ socket_t pair [2 ];
2931 struct list_head root ;
3032 struct list_head idles ;
3133
@@ -79,6 +81,13 @@ struct aio_poll_t* aio_poll_create()
7981 {
8082 LIST_INIT_HEAD (& poll -> root );
8183 LIST_INIT_HEAD (& poll -> idles );
84+ aio_poll_init_idles (poll );
85+
86+ if (0 != socketpair (PF_INET , SOCK_DGRAM , 0 , poll -> pair ))
87+ {
88+ free (poll );
89+ return NULL ;
90+ }
8291
8392 poll -> running = 1 ;
8493 locker_create (& poll -> locker );
@@ -94,12 +103,16 @@ int aio_poll_destroy(struct aio_poll_t* poll)
94103
95104 assert (list_empty (& poll -> root ));
96105 poll -> running = 0 ;
106+ socket_send_all_by_time (poll -> pair [1 ], poll , 1 , 0 , 5000 );
107+
97108 thread_destroy (poll -> thread );
98109 locker_destroy (& poll -> locker );
110+ socket_close (poll -> pair [0 ]);
111+ socket_close (poll -> pair [1 ]);
99112 return 0 ;
100113}
101114
102- int aio_poll_poll (struct aio_poll_t * poll , socket_t socket , int flags , int timeout , aio_poll_callback callback , void * param )
115+ int aio_poll_poll (struct aio_poll_t * poll , socket_t socket , int flags , int timeout , aio_poll_onpoll callback , void * param )
103116{
104117 struct aio_poll_socket_t * s ;
105118
@@ -120,6 +133,9 @@ int aio_poll_poll(struct aio_poll_t* poll, socket_t socket, int flags, int timeo
120133 locker_lock (& poll -> locker );
121134 list_insert_after (& s -> link , poll -> root .prev );
122135 locker_unlock (& poll -> locker );
136+
137+ // notify
138+ socket_send_all_by_time (poll -> pair [1 ], s , 1 , 0 , 5000 );
123139 return 0 ;
124140}
125141
@@ -128,28 +144,34 @@ static int STDCALL aio_poll_worker(void* param)
128144{
129145 int i , n , r ;
130146 uint64_t now ;
147+ char buf [128 ];
131148 struct list_head * ptr ;
132149 struct aio_poll_t * poll ;
133- struct aio_poll_socket_t * links [N_SOCKETS ];
150+ struct aio_poll_socket_t link0 , * links [N_SOCKETS ];
134151
135152 poll = (struct aio_poll_t * )param ;
153+ link0 .fd = poll -> pair [0 ];
154+ link0 .events = AIO_POLL_IN ;
155+
136156 while (poll -> running )
137157 {
158+ socket_recv_by_time (poll -> pair [0 ], buf , sizeof (buf ), 0 , 0 );
159+
138160 n = 0 ;
161+ links [n ++ ] = & link0 ;
139162 locker_lock (& poll -> locker );
140163 list_for_each (ptr , & poll -> root )
141164 {
142165 links [n ++ ] = list_entry (ptr , struct aio_poll_socket_t , link );
143166 }
144167 locker_unlock (& poll -> locker );
145168
146- // TODO: timeout
147- r = aio_poll_do (links , n , 1000 );
169+ r = aio_poll_do (links , n , 120000 );
148170 if (r < 0 )
149171 break ;
150172
151173 now = system_clock ();
152- for (i = 0 ; i < n ; i ++ )
174+ for (i = 1 ; i < n ; i ++ )
153175 {
154176 if (0 != links [i ]-> revents )
155177 {
@@ -231,11 +253,11 @@ static int aio_poll_do(struct aio_poll_socket_t* s[], int n, int timeout)
231253
232254 for (r = i = 0 ; i < n && i < 64 ; i ++ )
233255 {
234- s [i ]-> oflags = 0 ;
256+ s [i ]-> revents = 0 ;
235257 if (fds [i ].revents & POLLIN )
236- s [i ]-> oflags |= AIO_POLL_IN ;
258+ s [i ]-> revents |= AIO_POLL_IN ;
237259 if (fds [i ].revents & POLLOUT )
238- s [i ]-> oflags |= AIO_POLL_OUT ;
260+ s [i ]-> revents |= AIO_POLL_OUT ;
239261 }
240262
241263 return r ;
0 commit comments