Add unit test for timeout=0
[folly.git] / folly / io / async / EventFDWrapper.h
1 /*
2  * Copyright 2017 Facebook, Inc.
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
17 /**
18  * Work around the lack of <sys/eventfd.h> on glibc 2.5.1 which we still
19  * need to support, sigh.
20  */
21
22 #pragma once
23
24 #ifndef FOLLY_NO_CONFIG
25 #include <folly/folly-config.h>
26 #endif
27
28 #ifdef FOLLY_HAVE_FEATURES_H
29 #include <features.h>
30 #endif
31
32 #if defined(__GLIBC__) && !defined(__APPLE__)
33 #if __GLIBC_PREREQ(2, 9)
34 # define FOLLY_GLIBC_2_9
35 #endif
36 #endif
37
38 // <sys/eventfd.h> doesn't exist on older glibc versions
39 #ifdef FOLLY_GLIBC_2_9
40 #include <sys/eventfd.h>
41 #else /* !def FOLLY_GLIBC_2_9 */
42
43 #include <fcntl.h>
44 #include <sys/syscall.h>
45 #include <unistd.h>
46
47 // Use existing __NR_eventfd2 if already defined
48 // Values from the Linux kernel source:
49 // arch/x86/include/asm/unistd_{32,64}.h
50 #ifndef __NR_eventfd2
51 #if FOLLY_X64
52 /* nolint */
53 #define __NR_eventfd2  290
54 #elif defined(__i386__)
55 /* nolint */
56 #define __NR_eventfd2  328
57 #else
58 #error "Can't define __NR_eventfd2 for your architecture."
59 #endif
60 #endif
61
62 enum
63   {
64     EFD_SEMAPHORE = 1,
65 #define EFD_SEMAPHORE EFD_SEMAPHORE
66     EFD_CLOEXEC = 02000000,
67 #define EFD_CLOEXEC EFD_CLOEXEC
68     EFD_NONBLOCK = 04000
69 #define EFD_NONBLOCK EFD_NONBLOCK
70   };
71
72 // http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html
73 // Use the eventfd2 system call, as in glibc 2.9+
74 // (requires kernel 2.6.30+)
75 #define eventfd(initval, flags) syscall(__NR_eventfd2,(initval),(flags))
76
77 #endif /* !(defined(__GLIBC__) && __GLIBC_PREREQ(2, 9)) */