1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-07-29 zmq810150896 first version
* 2024-03-26 TroyMitchelle Add comments for all functions, members within structure members and fix incorrect naming of triggered
* 2023-12-14 Shell When poll goes to sleep before the waitqueue has added a
* record and finished enumerating all the fd's, it may be
* incorrectly woken up. This is basically because the poll
* mechanism wakeup algorithm does not correctly distinguish
* the current wait state.
*/
#include <rtthread.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <dfs_file.h>
#include "sys/epoll.h"
#include "poll.h"
#include <lwp_signal.h>
#define EPOLL_MUTEX_NAME "EVENTEPOLL"
#define EFD_SHARED_EPOLL_TYPE (EPOLL_CTL_ADD | EPOLL_CTL_DEL | EPOLL_CTL_MOD)
#define EPOLLINOUT_BITS (EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM)
#define EPOLLEXCLUSIVE_BITS (EPOLLINOUT_BITS | EPOLLERR | EPOLLHUP | \
EPOLLET | EPOLLEXCLUSIVE)
struct rt_eventpoll;
struct rt_fd_list;
struct rt_epoll_waiter
{
struct rt_wqueue_node wqn;
struct rt_fd_list *fdlist;
struct rt_epoll_waiter *next;
rt_bool_t queued;
};
enum rt_epoll_status {
RT_EPOLL_STAT_INIT,
RT_EPOLL_STAT_TRIG,
RT_EPOLL_STAT_WAITING,
};
/* Monitor queue */
struct rt_fd_list
{
rt_uint32_t revents; /**< Monitored events */
struct epoll_event epev; /**< Epoll event structure */
rt_pollreq_t req; /**< Poll request structure */
struct rt_eventpoll *ep; /**< Pointer to the associated event poll */
struct rt_epoll_waiter *waiters; /**< Wait nodes registered by poll */
int exclusive; /**< Indicates if the event is exclusive */
rt_bool_t is_rdl_node; /**< Indicates if the node is in the ready list */
rt_bool_t waiter_oom; /**< Waiter allocation failed; report POLLERR */
int fd; /**< File descriptor */
struct rt_fd_list *next; /**< Pointer to the next file descriptor list */
rt_slist_t rdl_node; /**< Ready list node */
};
struct rt_eventpoll
{
rt_wqueue_t epoll_read; /**< Epoll read queue */
rt_thread_t polling_thread; /**< Polling thread */
struct rt_mutex lock; /**< Mutex lock */
struct rt_fd_list *fdlist; /**< Monitor list */
int eventpoll_num; /**< Number of ready lists */
rt_pollreq_t req; /**< Poll request structure */
struct rt_spinlock spinlock; /**< Spin lock */
rt_slist_t rdl_head; /**< Ready list head */
enum rt_epoll_status status; /* the waited thread whether triggered */
};
static int epoll_close(struct dfs_file *file);
static int epoll_poll(struct dfs_file *file, struct rt_pollreq *req);
static int epoll_get_event(struct rt_fd_list *fl, rt_pollreq_t *req);
static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event);
static void epoll_remove_waiters(struct rt_fd_list *fdlist)
{
struct rt_epoll_waiter *waiter;
while (fdlist->waiters != RT_NULL)
{
waiter = fdlist->waiters;
fdlist->waiters = waiter->next;
if (waiter->queued)
{
rt_wqueue_remove(&waiter->wqn);
waiter->queued = RT_FALSE;
}
rt_free(waiter);
}
}
static void epoll_set_polling_thread(struct rt_eventpoll *ep,
rt_thread_t thread)
{
struct rt_epoll_waiter *waiter;
struct rt_fd_list *fdlist;
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
ep->polling_thread = thread;
for (fdlist = ep->fdlist; fdlist != RT_NULL; fdlist = fdlist->next)
{
for (waiter = fdlist->waiters; waiter != RT_NULL;
waiter = waiter->next)
{
waiter->wqn.polling_thread = thread;
}
}
rt_mutex_release(&ep->lock);
}
static const struct dfs_file_ops epoll_fops =
{
.close = epoll_close,
.poll = epoll_poll,
};
/**
* @brief Closes the file descriptor list associated with epoll.
*
* This function closes the file descriptor list associated with epoll and frees the allocated memory.
*
* @param fdlist Pointer to the file descriptor list.
*
* @return Returns 0 on success.
*/
static int epoll_close_fdlist(struct rt_fd_list *fdlist)
{
struct rt_fd_list *fre_node, *list;
if (fdlist != RT_NULL)
{
list = fdlist;
while (list->next != RT_NULL)
{
fre_node = list->next;
epoll_remove_waiters(fre_node);
list->next = fre_node->next;
rt_free(fre_node);
}
rt_free(fdlist);
}
return 0;
}
/**
* @brief Closes the epoll file descriptor.
*
* This function closes the epoll file descriptor and cleans up associated resources.
*
* @param file Pointer to the file structure.
*
* @return Returns 0 on success.
*/
static int epoll_close(struct dfs_file *file)
{
struct rt_eventpoll *ep;
if (file->vnode->ref_count != 1)
return 0;
if (file->vnode)
{
if (file->vnode->data)
{
ep = file->vnode->data;
if (ep)
{
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
if (ep->fdlist)
{
epoll_close_fdlist(ep->fdlist);
}
rt_mutex_release(&ep->lock);
rt_mutex_detach(&ep->lock);
rt_free(ep);
}
}
}
return 0;
}
/**
* @brief Polls the epoll file descriptor for events.
*
* This function polls the epoll file descriptor for events and updates the poll request accordingly.
*
* @param file Pointer to the file structure.
* @param req Pointer to the poll request structure.
*
* @return Returns the events occurred on success.
*/
static int epoll_poll(struct dfs_file *file, struct rt_pollreq *req)
{
struct rt_eventpoll *ep;
int events = 0;
rt_base_t level;
if (file->vnode->data)
{
ep = file->vnode->data;
ep->req._key = req->_key;
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
rt_poll_add(&ep->epoll_read, req);
level = rt_spin_lock_irqsave(&ep->spinlock);
if (!rt_slist_isempty(&ep->rdl_head))
events |= POLLIN | EPOLLRDNORM | POLLOUT;
rt_spin_unlock_irqrestore(&ep->spinlock, level);
rt_mutex_release(&ep->lock);
}
return events;
}
/**
* @brief Callback function for the wait queue.
*
* This function is called when the file descriptor is ready for polling.
*
* @param wait Pointer to the wait queue node.
* @param key Key associated with the wait queue node.
*
* @return Returns 0 on success.
*/
static int epoll_wqueue_callback(struct rt_wqueue_node *wait, void *key)
{
struct rt_fd_list *fdlist;
struct rt_epoll_waiter *waiter;
struct rt_eventpoll *ep;
rt_base_t level;
int is_waiting = 0;
if (key && !((rt_ubase_t)key & wait->key))
return -1;
waiter = rt_container_of(wait, struct rt_epoll_waiter, wqn);
fdlist = waiter->fdlist;
ep = fdlist->ep;
if (ep)
{
level = rt_spin_lock_irqsave(&ep->spinlock);
if (fdlist->is_rdl_node == RT_FALSE)
{
rt_slist_append(&ep->rdl_head, &fdlist->rdl_node);
fdlist->exclusive = 0;
fdlist->is_rdl_node = RT_TRUE;
ep->eventpoll_num++;
is_waiting = (ep->status == RT_EPOLL_STAT_WAITING);
ep->status = RT_EPOLL_STAT_TRIG;
rt_wqueue_wakeup(&ep->epoll_read, (void *)POLLIN);
}
rt_spin_unlock_irqrestore(&ep->spinlock, level);
}
if (is_waiting)
{
return __wqueue_default_wake(wait, key);
}
return -1;
}
/**
* @brief Adds a callback function to the wait queue associated with epoll.
*
* This function adds a callback function to the wait queue associated with epoll.
*
* @param wq Pointer to the wait queue.
* @param req Pointer to the poll request structure.
*/
static void epoll_wqueue_add_callback(rt_wqueue_t *wq, rt_pollreq_t *req)
{
struct rt_fd_list *fdlist;
struct rt_eventpoll *ep;
struct rt_epoll_waiter *waiter;
fdlist = rt_container_of(req, struct rt_fd_list, req);
ep = fdlist->ep;
for (waiter = fdlist->waiters; waiter != RT_NULL; waiter = waiter->next)
{
if (waiter->wqn.wqueue == wq)
{
waiter->wqn.key |= req->_key;
return;
}
}
waiter = (struct rt_epoll_waiter *)rt_calloc(1, sizeof(*waiter));
if (waiter == RT_NULL)
{
rt_base_t level;
/*
* Do not leave the fd unwatched: mark it ready with POLLERR so
* epoll_wait() wakes instead of sleeping on an incomplete set.
*/
if (ep)
{
level = rt_spin_lock_irqsave(&ep->spinlock);
if (fdlist->is_rdl_node == RT_FALSE)
{
rt_slist_append(&ep->rdl_head, &fdlist->rdl_node);
fdlist->exclusive = 0;
fdlist->is_rdl_node = RT_TRUE;
ep->eventpoll_num++;
}
fdlist->waiter_oom = RT_TRUE;
fdlist->revents |= POLLERR;
fdlist->epev.events |= POLLERR;
ep->status = RT_EPOLL_STAT_TRIG;
rt_wqueue_wakeup(&ep->epoll_read, (void *)POLLIN);
rt_spin_unlock_irqrestore(&ep->spinlock, level);
}
return;
}
waiter->fdlist = fdlist;
waiter->wqn.key = req->_key;
waiter->wqn.polling_thread = ep->polling_thread;
waiter->wqn.wakeup = epoll_wqueue_callback;
rt_list_init(&waiter->wqn.list);
waiter->next = fdlist->waiters;
fdlist->waiters = waiter;
rt_wqueue_add(wq, &waiter->wqn);
waiter->queued = RT_TRUE;
}
/**
* @brief Installs a file descriptor list into the epoll control structure.
*
* This function installs a file descriptor list into the epoll control structure.
*
* @param fdlist Pointer to the file descriptor list.
* @param ep Pointer to the epoll control structure.
*/
static void epoll_ctl_install(struct rt_fd_list *fdlist, struct rt_eventpoll *ep)
{
rt_uint32_t mask = 0;
rt_base_t level;
fdlist->req._key = fdlist->revents;
mask = epoll_get_event(fdlist, &fdlist->req);
if (mask & fdlist->revents)
{
if (ep)
{
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
level = rt_spin_lock_irqsave(&ep->spinlock);
rt_slist_append(&ep->rdl_head, &fdlist->rdl_node);
fdlist->exclusive = 0;
fdlist->is_rdl_node = RT_TRUE;
ep->status = RT_EPOLL_STAT_TRIG;
ep->eventpoll_num ++;
rt_spin_unlock_irqrestore(&ep->spinlock, level);
rt_mutex_release(&ep->lock);
}
}
}
/**
* @brief Initializes the epoll control structure.
*
* This function initializes the epoll control structure.
*
* @param ep Pointer to the epoll control structure.
*/
static void epoll_member_init(struct rt_eventpoll *ep)
{
ep->status = RT_EPOLL_STAT_INIT;
ep->eventpoll_num = 0;
ep->polling_thread = rt_thread_self();
ep->fdlist = RT_NULL;
ep->req._key = 0;
rt_slist_init(&(ep->rdl_head));
rt_wqueue_init(&ep->epoll_read);
rt_mutex_init(&ep->lock, EPOLL_MUTEX_NAME, RT_IPC_FLAG_FIFO);
rt_spin_lock_init(&ep->spinlock);
}
/**
* @brief Initializes the epoll file descriptor.
*
* This function initializes the epoll file descriptor.
*
* @param fd File descriptor.
*
* @return Returns 0 on success.
*/
static int epoll_epf_init(int fd)
{
struct dfs_file *df;
struct rt_eventpoll *ep;
rt_err_t ret = 0;
df = fd_get(fd);
if (df)
{
ep = (struct rt_eventpoll *)rt_malloc(sizeof(struct rt_eventpoll));
if (ep)
{
epoll_member_init(ep);
#ifdef RT_USING_DFS_V2
df->fops = &epoll_fops;
#endif
df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
if (df->vnode)
{
ep->fdlist = (struct rt_fd_list *)rt_malloc(sizeof(struct rt_fd_list));
if (ep->fdlist)
{
ep->fdlist->next = RT_NULL;
ep->fdlist->fd = fd;
ep->fdlist->ep = ep;
ep->fdlist->waiters = RT_NULL;
ep->fdlist->exclusive = 0;
ep->fdlist->is_rdl_node = RT_FALSE;
ep->fdlist->waiter_oom = RT_FALSE;
dfs_vnode_init(df->vnode, FT_REGULAR, &epoll_fops);
df->vnode->data = ep;
rt_slist_init(&ep->fdlist->rdl_node);
}
else
{
ret = -ENOMEM;
rt_free(df->vnode);
rt_free(ep);
}
}
else
{
ret = -ENOMEM;
rt_free(ep);
}
}
else
{
ret = -ENOMEM;
}
}
return ret;
}
/**
* @brief Creates an epoll file descriptor.
*
* This function creates an epoll file descriptor.
*
* @param size Size of the epoll instance.
*
* @return Returns the file descriptor on success, or -1 on failure.
*/
static int epoll_do_create(int size)
{
rt_err_t ret = -1;
int status;
int fd;
if (size < 0)
{
rt_set_errno(EINVAL);
}
else
{
fd = fd_new();
if (fd >= 0)
{
ret = fd;
status = epoll_epf_init(fd);
if (status < 0)
{
fd_release(fd);
rt_set_errno(-status);
}
}
else
{
rt_set_errno(-fd);
}
}
return ret;
}
/**
* @brief Adds a file descriptor to the epoll instance.
*
* This function adds a file descriptor to the epoll instance.
*
* @param df Pointer to the file structure.
* @param fd File descriptor to add.
* @param event Pointer to the epoll event structure.
*
* @return Returns 0 on success, or an error code on failure.
*/
static int epoll_ctl_add(struct dfs_file *df, int fd, struct epoll_event *event)
{
struct rt_fd_list *fdlist;
struct rt_eventpoll *ep;
rt_err_t ret = -EINVAL;
if (df->vnode->data)
{
ep = df->vnode->data;
fdlist = ep->fdlist;
ret = 0;
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
while (fdlist->next != RT_NULL)
{
if (fdlist->next->fd == fd)
{
rt_mutex_release(&ep->lock);
return 0;
}
fdlist = fdlist->next;
}
rt_mutex_release(&ep->lock);
fdlist = (struct rt_fd_list *)rt_malloc(sizeof(struct rt_fd_list));
if (fdlist)
{
fdlist->fd = fd;
memcpy(&fdlist->epev.data, &event->data, sizeof(event->data));
fdlist->epev.events = 0;
fdlist->ep = ep;
fdlist->waiters = RT_NULL;
fdlist->exclusive = 0;
fdlist->is_rdl_node = RT_FALSE;
fdlist->waiter_oom = RT_FALSE;
fdlist->req._proc = epoll_wqueue_add_callback;
fdlist->revents = event->events;
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
fdlist->next = ep->fdlist->next;
ep->fdlist->next = fdlist;
rt_mutex_release(&ep->lock);
rt_slist_init(&fdlist->rdl_node);
epoll_ctl_install(fdlist, ep);
}
else
{
ret = -ENOMEM;
}
}
return ret;
}
/**
* @brief Removes a file descriptor from the epoll instance.
*
* This function removes a file descriptor from the epoll instance.
*
* @param df Pointer to the file structure.
* @param fd File descriptor to remove.
*
* @return Returns 0 on success, or an error code on failure.
*/
static int epoll_ctl_del(struct dfs_file *df, int fd)
{
struct rt_fd_list *fdlist, *fre_fd, *rdlist;
struct rt_eventpoll *ep = RT_NULL;
rt_slist_t *node = RT_NULL;
rt_err_t ret = -EINVAL;
rt_base_t level;
if (df->vnode->data)
{
ep = df->vnode->data;
if (ep)
{
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
level = rt_spin_lock_irqsave(&ep->spinlock);
rt_slist_for_each(node, &ep->rdl_head)
{
rdlist = rt_slist_entry(node, struct rt_fd_list, rdl_node);
if (rdlist->fd == fd)
rt_slist_remove(&ep->rdl_head, node);
}
rt_spin_unlock_irqrestore(&ep->spinlock, level);
fdlist = ep->fdlist;
while (fdlist->next != RT_NULL)
{
if (fdlist->next->fd == fd)
{
fre_fd = fdlist->next;
fdlist->next = fdlist->next->next;
epoll_remove_waiters(fre_fd);
rt_free(fre_fd);
break;
}
else
{
fdlist = fdlist->next;
}
}
rt_mutex_release(&ep->lock);
}
ret = 0;
}
return ret;
}
/**
* @brief Modifies the events associated with a file descriptor in the epoll instance.
*
* This function modifies the events associated with a file descriptor in the epoll instance.
*
* @param df Pointer to the file structure.
* @param fd File descriptor to modify.
* @param event Pointer to the epoll event structure.
*
* @return Returns 0 on success, or an error code on failure.
*/
static int epoll_ctl_mod(struct dfs_file *df, int fd, struct epoll_event *event)
{
struct rt_fd_list *fdlist;
struct rt_eventpoll *ep = RT_NULL;
rt_err_t ret = -EINVAL;
if (df->vnode->data)
{
ep = df->vnode->data;
fdlist = ep->fdlist;
while (fdlist->next != RT_NULL)
{
if (fdlist->next->fd == fd)
{
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
memcpy(&fdlist->next->epev.data, &event->data, sizeof(event->data));
fdlist->next->revents = event->events;
epoll_remove_waiters(fdlist->next);
rt_mutex_release(&ep->lock);
epoll_ctl_install(fdlist->next, ep);
break;
}
fdlist = fdlist->next;
}
ret = 0;
}
return ret;
}
/**
* @brief Controls an epoll instance.
*
* This function controls an epoll instance, performing operations such as adding,
* modifying, or removing file descriptors associated with the epoll instance.
*
* @param epfd File descriptor of the epoll instance.
* @param op Operation to perform (EPOLL_CTL_ADD, EPOLL_CTL_DEL, or EPOLL_CTL_MOD).
* @param fd File descriptor to add, modify, or remove.
* @param event Pointer to the epoll event structure.
*
* @return Returns 0 on success, or -1 on failure with errno set appropriately.
*/
static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
struct dfs_file *epdf;
struct rt_eventpoll *ep;
rt_err_t ret = 0;
if (op & ~EFD_SHARED_EPOLL_TYPE)
{
rt_set_errno(EINVAL);
return -1;
}
if ((epfd == fd) || (epfd < 0))
{
rt_set_errno(EINVAL);
return -1;
}
if (!(op & EPOLL_CTL_DEL))
{
if (!(event->events & EPOLLEXCLUSIVE_BITS))
{
rt_set_errno(EINVAL);
return -1;
}
event->events |= EPOLLERR | EPOLLHUP;
}
if (!fd_get(fd))
{
rt_set_errno(EBADF);
return -1;
}
epdf = fd_get(epfd);
if (epdf->vnode->data)
{
ep = epdf->vnode->data;
switch (op)
{
case EPOLL_CTL_ADD:
ret = epoll_ctl_add(epdf, fd, event);
break;
case EPOLL_CTL_DEL:
ret = epoll_ctl_del(epdf, fd);
break;
case EPOLL_CTL_MOD:
ret = epoll_ctl_mod(epdf, fd, event);
break;
default:
rt_set_errno(EINVAL);
break;
}
if (ret < 0)
{
rt_set_errno(-ret);
ret = -1;
}
else
{
ep->polling_thread = rt_thread_self();
}
}
return ret;
}
/**
* @brief Waits for events on an epoll instance with a specified timeout.
*
* This function waits for events on the specified epoll instance within the given timeout.
*
* @param ep Pointer to the epoll instance.
* @param msec Timeout in milliseconds.
*
* @return Returns 0 if no events occurred within the timeout, or 1 if events were triggered.
*/
static int epoll_wait_timeout(struct rt_eventpoll *ep, int msec)
{
rt_int32_t timeout;
struct rt_thread *thread;
rt_base_t level;
int ret = 0;
thread = ep->polling_thread;
timeout = rt_tick_from_millisecond(msec);
level = rt_spin_lock_irqsave(&ep->spinlock);
if (timeout != 0 && ep->status != RT_EPOLL_STAT_TRIG)
{
if (rt_thread_suspend_with_flag(thread, RT_KILLABLE) == RT_EOK)
{
if (timeout > 0)
{
rt_tick_t timeout_tick = timeout;
rt_timer_control(&(thread->thread_timer),
RT_TIMER_CTRL_SET_TIME,
&timeout_tick);
rt_timer_start(&(thread->thread_timer));
}
ep->status = RT_EPOLL_STAT_WAITING;
rt_spin_unlock_irqrestore(&ep->spinlock, level);
rt_schedule();
level = rt_spin_lock_irqsave(&ep->spinlock);
if (ep->status == RT_EPOLL_STAT_WAITING)
ep->status = RT_EPOLL_STAT_INIT;
}
}
ret = !(ep->status == RT_EPOLL_STAT_TRIG);
rt_spin_unlock_irqrestore(&ep->spinlock, level);
return ret;
}
/**
* @brief Gets events associated with a file descriptor in the epoll instance.
*
* This function gets events associated with a file descriptor in the epoll instance.
*
* @param fl Pointer to the file descriptor list structure.
* @param req Pointer to the poll request structure.
*
* @return Returns the bitmask of events associated with the file descriptor.
*/
static int epoll_get_event(struct rt_fd_list *fl, rt_pollreq_t *req)
{
struct dfs_file *df;
int mask = 0;
int fd = 0;
fd = fl->fd;
if (fd >= 0)
{
df = fd_get(fd);
if (df)
{
if (df->vnode->fops->poll)
{
req->_key = fl->revents | POLLERR | POLLHUP;
mask = df->vnode->fops->poll(df, req);
if (mask < 0)
return mask;
}
mask &= fl->revents | EPOLLOUT | POLLERR;
}
}
if (fl->waiter_oom)
{
mask |= POLLERR;
}
return mask;
}
/**
* @brief Performs epoll operation to get triggered events.
*
* This function performs epoll operation to get triggered events.
*
* @param ep Pointer to the epoll instance.
* @param events Pointer to the array to store triggered events.
* @param maxevents Maximum number of events to store in the array.
* @param timeout Timeout value in milliseconds.
*
* @return Returns the number of triggered events.
*/
static int epoll_do(struct rt_eventpoll *ep, struct epoll_event *events, int maxevents, int timeout)
{
struct rt_fd_list *rdlist;
rt_slist_t *node = RT_NULL;
int event_num = 0;
int istimeout = 0;
int isn_add = 0;
int isfree = 0;
int mask = 0;
rt_base_t level;
while (1)
{
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
level = rt_spin_lock_irqsave(&ep->spinlock);
if (ep->eventpoll_num > 0)
{
rt_slist_for_each(node,&ep->rdl_head)
{
rdlist = rt_slist_entry(node, struct rt_fd_list, rdl_node);
ep->eventpoll_num --;
rt_slist_remove(&ep->rdl_head, &rdlist->rdl_node);
rdlist->is_rdl_node = RT_FALSE;
rt_spin_unlock_irqrestore(&ep->spinlock, level);
isfree = 0;
isn_add = 0;
if (event_num < maxevents)
{
epoll_remove_waiters(rdlist);
mask = epoll_get_event(rdlist, &rdlist->req);
if (mask & rdlist->revents)
{
rdlist->epev.events = mask & rdlist->revents;
}
else
{
isfree = 1;
isn_add = 1;
}
if (rdlist->revents & EPOLLONESHOT)
{
rdlist->revents = 0;
isfree = 1;
epoll_remove_waiters(rdlist);
}
else
{
if (rdlist->revents & EPOLLET)
{
isfree = 1;
}
else
{
level = rt_spin_lock_irqsave(&ep->spinlock);
if (rdlist->exclusive != 1)
{
rdlist->exclusive = 1;
}
rt_spin_unlock_irqrestore(&ep->spinlock, level);
}
}
if (!isn_add)
{
memcpy(&events[event_num], &rdlist->epev, sizeof(rdlist->epev));
event_num ++;
}
if (!isfree)
{
if (rdlist->is_rdl_node == RT_FALSE)
{
level = rt_spin_lock_irqsave(&ep->spinlock);
ep->eventpoll_num ++;
rt_slist_append(&ep->rdl_head, &rdlist->rdl_node);
rdlist->is_rdl_node = RT_TRUE;
rt_spin_unlock_irqrestore(&ep->spinlock, level);
}
else
{
level = rt_spin_lock_irqsave(&ep->spinlock);
if (rdlist->waiters == RT_NULL)
{
epoll_get_event(rdlist, &rdlist->req);
}
rt_spin_unlock_irqrestore(&ep->spinlock, level);
}
}
}
else
{
level = rt_spin_lock_irqsave(&ep->spinlock);
break;
}
level = rt_spin_lock_irqsave(&ep->spinlock);
}
}
rt_spin_unlock_irqrestore(&ep->spinlock, level);
rt_mutex_release(&ep->lock);
if (event_num || istimeout)
{
level = rt_spin_lock_irqsave(&ep->spinlock);
ep->status = RT_EPOLL_STAT_INIT;
rt_spin_unlock_irqrestore(&ep->spinlock, level);
if ((timeout >= 0) || (event_num > 0))
break;
}
if (epoll_wait_timeout(ep, timeout))
{
istimeout = 1;
}
}
return event_num;
}
/**
* @brief Waits for events on an epoll instance with specified parameters.
*
* This function waits for events on the specified epoll instance within the given timeout, optionally blocking signals based on the provided signal set.
*
* @param epfd File descriptor referring to the epoll instance.
* @param events Pointer to the array to store triggered events.
* @param maxevents Maximum number of events to store in the array.
* @param timeout Timeout value in milliseconds.
* @param ss Pointer to the signal set indicating signals to block during the wait operation. Pass NULL if no signals need to be blocked.
*
* @return Returns the number of triggered events on success, or -1 on failure.
*/
static int epoll_do_wait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
{
struct rt_eventpoll *ep;
struct dfs_file *df;
lwp_sigset_t old_sig, new_sig;
rt_err_t ret = 0;
if (ss)
{
memcpy(&new_sig, ss, sizeof(lwp_sigset_t));
lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_BLOCK, &new_sig, &old_sig);
}
if ((maxevents > 0) && (epfd >= 0))
{
df = fd_get(epfd);
if (df && df->vnode)
{
ep = (struct rt_eventpoll *)df->vnode->data;
if (ep)
{
epoll_set_polling_thread(ep, rt_thread_self());
ret = epoll_do(ep, events, maxevents, timeout);
}
}
}
if (ss)
{
lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_SET_MASK, &old_sig, RT_NULL);
}
if (ret < 0)
{
rt_set_errno(-ret);
ret = -1;
}
return ret;
}
/**
* @brief Creates an epoll instance.
*
* This function creates an epoll instance with the specified size.
*
* @param size Size of the epoll instance.
*
* @return Returns the file descriptor referring to the created epoll instance on success, or -1 on failure.
*/
int epoll_create(int size)
{
return epoll_do_create(size);
}
/**
* @brief Modifies an epoll instance.
*
* This function modifies the epoll instance referred to by 'epfd' according to the specified operation 'op', associated with the file descriptor 'fd', and the event structure 'event'.
*
* @param epfd File descriptor referring to the epoll instance.
* @param op Operation to perform (EPOLL_CTL_ADD, EPOLL_CTL_DEL, or EPOLL_CTL_MOD).
* @param fd File descriptor to associate with the epoll instance or remove from it.
* @param event Pointer to the event structure containing the events to modify.
*
* @return Returns 0 on success, or -1 on failure.
*/
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
return epoll_do_ctl(epfd, op, fd, event);
}
/**
* @brief Waits for events on an epoll instance.
*
* This function waits for events on the epoll instance referred to by 'epfd' within the given timeout.
*
* @param epfd File descriptor referring to the epoll instance.
* @param events Pointer to the array to store triggered events.
* @param maxevents Maximum number of events to store in the array.
* @param timeout Timeout value in milliseconds.
*
* @return Returns the number of triggered events on success, or -1 on failure.
*/
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
{
return epoll_do_wait(epfd, events, maxevents, timeout, RT_NULL);
}
/**
* @brief Waits for events on an epoll instance, blocking signals.
*
* This function waits for events on the epoll instance referred to by 'epfd' within the given timeout, blocking signals based on the provided signal set 'ss'.
*
* @param epfd File descriptor referring to the epoll instance.
* @param events Pointer to the array to store triggered events.
* @param maxevents Maximum number of events to store in the array.
* @param timeout Timeout value in milliseconds.
* @param ss Pointer to the signal set indicating signals to block during the wait operation.
*
* @return Returns the number of triggered events on success, or -1 on failure.
*/
int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
{
return epoll_do_wait(epfd, events, maxevents, timeout, ss);
}
/**
* @brief Waits for events on an epoll instance, blocking signals.
*
* This function waits for events on the epoll instance referred to by 'epfd' within the given timeout, blocking signals based on the provided signal set 'ss'.
*
* @param epfd File descriptor referring to the epoll instance.
* @param events Pointer to the array to store triggered events.
* @param maxevents Maximum number of events to store in the array.
* @param timeout Timeout value in milliseconds.
* @param ss Pointer to the signal set indicating signals to block during the wait operation.
*
* @return Returns the number of triggered events on success, or -1 on failure.
*/
int epoll_pwait2(int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *ss)
{
return epoll_do_wait(epfd, events, maxevents, timeout, ss);
}
|