EventUtil.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #pragma once
  17. #include <functional>
  18. #include <folly/portability/Event.h>
  19. namespace folly {
  20. #if LIBEVENT_VERSION_NUMBER <= 0x02010101
  21. #define FOLLY_LIBEVENT_COMPAT_PLUCK(name) ev_##name
  22. #else
  23. #define FOLLY_LIBEVENT_COMPAT_PLUCK(name) ev_evcallback.evcb_##name
  24. #endif
  25. #define FOLLY_LIBEVENT_DEF_ACCESSORS(name) \
  26. inline auto event_ref_##name(struct event* ev) \
  27. ->decltype(std::ref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name))) { \
  28. return std::ref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name)); \
  29. } \
  30. inline auto event_ref_##name(struct event const* ev) \
  31. ->decltype(std::cref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name))) { \
  32. return std::cref(ev->FOLLY_LIBEVENT_COMPAT_PLUCK(name)); \
  33. } \
  34. //
  35. FOLLY_LIBEVENT_DEF_ACCESSORS(flags)
  36. #undef FOLLY_LIBEVENT_COMPAT_PLUCK
  37. #undef FOLLY_LIBEVENT_DEF_ACCESSORS
  38. /**
  39. * low-level libevent utility functions
  40. */
  41. class EventUtil {
  42. public:
  43. static bool isEventRegistered(const struct event* ev) {
  44. // If any of these flags are set, the event is registered.
  45. enum {
  46. EVLIST_REGISTERED =
  47. (EVLIST_INSERTED | EVLIST_ACTIVE | EVLIST_TIMEOUT | EVLIST_SIGNAL)
  48. };
  49. return (event_ref_flags(ev) & EVLIST_REGISTERED);
  50. }
  51. };
  52. } // namespace folly