proxygen
EventFDWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present 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 
22 #pragma once
23 
24 #ifndef FOLLY_NO_CONFIG
25 #include <folly/folly-config.h>
26 #endif
27 
28 #if __has_include(<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 {
64 #define EFD_SEMAPHORE EFD_SEMAPHORE
65  EFD_CLOEXEC = 02000000,
66 #define EFD_CLOEXEC EFD_CLOEXEC
67  EFD_NONBLOCK = 04000
68 #define EFD_NONBLOCK EFD_NONBLOCK
69 };
70 
71 // http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html
72 // Use the eventfd2 system call, as in glibc 2.9+
73 // (requires kernel 2.6.30+)
74 #define eventfd(initval, flags) syscall(__NR_eventfd2, (initval), (flags))
75 
76 #endif /* !(defined(__GLIBC__) && __GLIBC_PREREQ(2, 9)) */
#define EFD_SEMAPHORE
#define EFD_CLOEXEC
#define EFD_NONBLOCK