blob: d4fc1fba9f06614e70b862e88bba13fe75e163ca [file] [log] [blame]
James Kuszmaul82f6c042021-01-17 11:30:16 -08001/**
2 * @file method.c Polling methods
3 *
4 * Copyright (C) 2010 Creytiv.com
5 */
6#include <re_types.h>
7#include <re_fmt.h>
8#include <re_mbuf.h>
9#include <re_main.h>
10#include "main.h"
11
12
13static const char str_poll[] = "poll"; /**< POSIX.1-2001 poll */
14static const char str_select[] = "select"; /**< POSIX.1-2001 select */
15static const char str_epoll[] = "epoll"; /**< Linux epoll */
16static const char str_kqueue[] = "kqueue";
17
18
19/**
20 * Choose the best async I/O polling method
21 *
22 * @return Polling method
23 */
24enum poll_method poll_method_best(void)
25{
26 enum poll_method m = METHOD_NULL;
27
28#ifdef HAVE_EPOLL
29 /* Supported from Linux 2.5.66 */
30 if (METHOD_NULL == m) {
31 if (epoll_check())
32 m = METHOD_EPOLL;
33 }
34#endif
35
36#ifdef HAVE_KQUEUE
37 if (METHOD_NULL == m) {
38 m = METHOD_KQUEUE;
39 }
40#endif
41
42#ifdef HAVE_POLL
43 if (METHOD_NULL == m) {
44 m = METHOD_POLL;
45 }
46#endif
47#ifdef HAVE_SELECT
48 if (METHOD_NULL == m) {
49 m = METHOD_SELECT;
50 }
51#endif
52
53 return m;
54}
55
56
57/**
58 * Get the name of the polling method
59 *
60 * @param method Polling method
61 *
62 * @return Polling name string
63 */
64const char *poll_method_name(enum poll_method method)
65{
66 switch (method) {
67
68 case METHOD_POLL: return str_poll;
69 case METHOD_SELECT: return str_select;
70 case METHOD_EPOLL: return str_epoll;
71 case METHOD_KQUEUE: return str_kqueue;
72 default: return "???";
73 }
74}
75
76
77/**
78 * Get the polling method type from a string
79 *
80 * @param method Returned polling method
81 * @param name Polling method name string
82 *
83 * @return 0 if success, otherwise errorcode
84 */
85int poll_method_type(enum poll_method *method, const struct pl *name)
86{
87 if (!method || !name)
88 return EINVAL;
89
90 if (0 == pl_strcasecmp(name, str_poll))
91 *method = METHOD_POLL;
92 else if (0 == pl_strcasecmp(name, str_select))
93 *method = METHOD_SELECT;
94 else if (0 == pl_strcasecmp(name, str_epoll))
95 *method = METHOD_EPOLL;
96 else if (0 == pl_strcasecmp(name, str_kqueue))
97 *method = METHOD_KQUEUE;
98 else
99 return ENOENT;
100
101 return 0;
102}