move assignment operators for folly::Synchronized
[folly.git] / folly / eventfd.h
1 /*
2  * Copyright 2013 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  * Wrapper around the eventfd system call, as defined in <sys/eventfd.h>
19  * in glibc 2.9+.
20  *
21  * @author Tudor Bosman (tudorb@fb.com)
22  */
23
24 #ifndef FOLLY_BASE_EVENTFD_H_
25 #define FOLLY_BASE_EVENTFD_H_
26
27 #ifndef __linux__
28 #error This file may be compiled on Linux only.
29 #endif
30
31 #include <sys/syscall.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34
35 // Use existing __NR_eventfd2 if already defined
36 // Values from the Linux kernel source:
37 // arch/x86/include/asm/unistd_{32,64}.h
38 #ifndef __NR_eventfd2
39 #if defined(__x86_64__)
40 #define __NR_eventfd2  290
41 #elif defined(__i386__)
42 #define __NR_eventfd2  328
43 #else
44 #error "Can't define __NR_eventfd2 for your architecture."
45 #endif
46 #endif
47
48 #ifndef EFD_SEMAPHORE
49 #define EFD_SEMAPHORE 1
50 #endif
51
52 /* from linux/fcntl.h - this conflicts with fcntl.h so include just the #define
53  * we need
54  */
55 #ifndef O_CLOEXEC
56 #define O_CLOEXEC 02000000 /* set close_on_exec */
57 #endif
58
59 #ifndef EFD_CLOEXEC
60 #define EFD_CLOEXEC O_CLOEXEC
61 #endif
62
63 #ifndef EFD_NONBLOCK
64 #define EFD_NONBLOCK O_NONBLOCK
65 #endif
66
67 namespace folly {
68
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);
74 }
75
76 }  // namespace folly
77
78 #endif /* FOLLY_BASE_EVENTFD_H_ */
79