Move thrift/lib/cpp/async to folly.
[folly.git] / folly / io / async / EventFDWrapper.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 /**
20  * Work around the lack of <sys/eventfd.h> on glibc 2.5.1 which we still
21  * need to support, sigh.
22  */
23
24 #pragma once
25
26 #include <features.h>
27
28 // <sys/eventfd.h> doesn't exist on older glibc versions
29 #if (defined(__GLIBC__) && __GLIBC_PREREQ(2, 9))
30 #include <sys/eventfd.h>
31 #else /* !(defined(__GLIBC__) && __GLIBC_PREREQ(2, 9)) */
32
33 #include <sys/syscall.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36
37 // Use existing __NR_eventfd2 if already defined
38 // Values from the Linux kernel source:
39 // arch/x86/include/asm/unistd_{32,64}.h
40 #ifndef __NR_eventfd2
41 #if defined(__x86_64__)
42 #define __NR_eventfd2  290
43 #elif defined(__i386__)
44 #define __NR_eventfd2  328
45 #else
46 #error "Can't define __NR_eventfd2 for your architecture."
47 #endif
48 #endif
49
50 enum
51   {
52     EFD_SEMAPHORE = 1,
53 #define EFD_SEMAPHORE EFD_SEMAPHORE
54     EFD_CLOEXEC = 02000000,
55 #define EFD_CLOEXEC EFD_CLOEXEC
56     EFD_NONBLOCK = 04000
57 #define EFD_NONBLOCK EFD_NONBLOCK
58   };
59
60 // http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html
61 // Use the eventfd2 system call, as in glibc 2.9+
62 // (requires kernel 2.6.30+)
63 #define eventfd(initval, flags) syscall(__NR_eventfd2,(initval),(flags))
64
65 #endif /* !(defined(__GLIBC__) && __GLIBC_PREREQ(2, 9)) */