2 * Copyright 2013 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Wrapper around the eventfd system call, as defined in <sys/eventfd.h>
21 * @author Tudor Bosman (tudorb@fb.com)
24 #ifndef FOLLY_BASE_EVENTFD_H_
25 #define FOLLY_BASE_EVENTFD_H_
28 #error This file may be compiled on Linux only.
31 #include <sys/syscall.h>
35 // Use existing __NR_eventfd2 if already defined
36 // Values from the Linux kernel source:
37 // arch/x86/include/asm/unistd_{32,64}.h
39 #if defined(__x86_64__)
40 #define __NR_eventfd2 290
41 #elif defined(__i386__)
42 #define __NR_eventfd2 328
44 #error "Can't define __NR_eventfd2 for your architecture."
49 #define EFD_SEMAPHORE 1
52 /* from linux/fcntl.h - this conflicts with fcntl.h so include just the #define
56 #define O_CLOEXEC 02000000 /* set close_on_exec */
60 #define EFD_CLOEXEC O_CLOEXEC
64 #define EFD_NONBLOCK O_NONBLOCK
69 // http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html
70 inline int eventfd(unsigned int initval, int flags) {
71 // Use the eventfd2 system call, as in glibc 2.9+
72 // (requires kernel 2.6.30+)
73 return syscall(__NR_eventfd2, initval, flags);
78 #endif /* FOLLY_BASE_EVENTFD_H_ */