EventFDWrapper.h 1.9 KB

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