coro_utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. //
  3. // This source code is licensed under both the GPLv2 (found in the
  4. // COPYING file in the root directory) and Apache 2.0 License
  5. // (found in the LICENSE.Apache file in the root directory).
  6. #if defined(USE_COROUTINES)
  7. #include "folly/coro/Coroutine.h"
  8. #include "folly/coro/Task.h"
  9. #endif
  10. #include "rocksdb/rocksdb_namespace.h"
  11. // This file has two sctions. The first section applies to all instances of
  12. // header file inclusion and has an include guard. The second section is
  13. // meant for multiple inclusions in the same source file, and is idempotent.
  14. namespace ROCKSDB_NAMESPACE {
  15. #ifndef UTIL_CORO_UTILS_H_
  16. #define UTIL_CORO_UTILS_H_
  17. #if defined(USE_COROUTINES)
  18. // The follwoing macros expand to regular and coroutine function
  19. // declarations for a given function
  20. #define DECLARE_SYNC_AND_ASYNC(__ret_type__, __func_name__, ...) \
  21. __ret_type__ __func_name__(__VA_ARGS__); \
  22. folly::coro::Task<__ret_type__> __func_name__##Coroutine(__VA_ARGS__);
  23. #define DECLARE_SYNC_AND_ASYNC_OVERRIDE(__ret_type__, __func_name__, ...) \
  24. __ret_type__ __func_name__(__VA_ARGS__) override; \
  25. folly::coro::Task<__ret_type__> __func_name__##Coroutine(__VA_ARGS__) \
  26. override;
  27. #define DECLARE_SYNC_AND_ASYNC_CONST(__ret_type__, __func_name__, ...) \
  28. __ret_type__ __func_name__(__VA_ARGS__) const; \
  29. folly::coro::Task<__ret_type__> __func_name__##Coroutine(__VA_ARGS__) const;
  30. constexpr bool using_coroutines() { return true; }
  31. #else // !USE_COROUTINES
  32. // The follwoing macros expand to a regular function declaration for a given
  33. // function
  34. #define DECLARE_SYNC_AND_ASYNC(__ret_type__, __func_name__, ...) \
  35. __ret_type__ __func_name__(__VA_ARGS__);
  36. #define DECLARE_SYNC_AND_ASYNC_OVERRIDE(__ret_type__, __func_name__, ...) \
  37. __ret_type__ __func_name__(__VA_ARGS__) override;
  38. #define DECLARE_SYNC_AND_ASYNC_CONST(__ret_type__, __func_name__, ...) \
  39. __ret_type__ __func_name__(__VA_ARGS__) const;
  40. constexpr bool using_coroutines() { return false; }
  41. #endif // USE_COROUTINES
  42. #endif // UTIL_CORO_UTILS_H_
  43. // The following section of the file is meant to be included twice in a
  44. // source file - once defining WITH_COROUTINES and once defining
  45. // WITHOUT_COROUTINES
  46. #undef DEFINE_SYNC_AND_ASYNC
  47. #undef CO_AWAIT
  48. #undef CO_RETURN
  49. #if defined(WITH_COROUTINES) && defined(USE_COROUTINES)
  50. // This macro should be used in the beginning of the function
  51. // definition. The declaration should have been done using one of the
  52. // DECLARE_SYNC_AND_ASYNC* macros. It expands to the return type and
  53. // the function name with the Coroutine suffix. For example -
  54. // DEFINE_SYNC_AND_ASYNC(int, foo)(bool bar) {}
  55. // would expand to -
  56. // folly::coro::Task<int> fooCoroutine(bool bar) {}
  57. #define DEFINE_SYNC_AND_ASYNC(__ret_type__, __func_name__) \
  58. folly::coro::Task<__ret_type__> __func_name__##Coroutine
  59. // This macro should be used to call a function that might be a
  60. // coroutine. It expands to the correct function name and prefixes
  61. // the co_await operator if necessary. For example -
  62. // s = CO_AWAIT(foo)(true);
  63. // if the code is compiled WITH_COROUTINES, would expand to
  64. // s = co_await fooCoroutine(true);
  65. // if compiled WITHOUT_COROUTINES, would expand to
  66. // s = foo(true);
  67. #define CO_AWAIT(__func_name__) co_await __func_name__##Coroutine
  68. #define CO_RETURN co_return
  69. #elif defined(WITHOUT_COROUTINES)
  70. // This macro should be used in the beginning of the function
  71. // definition. The declaration should have been done using one of the
  72. // DECLARE_SYNC_AND_ASYNC* macros. It expands to the return type and
  73. // the function name without the Coroutine suffix. For example -
  74. // DEFINE_SYNC_AND_ASYNC(int, foo)(bool bar) {}
  75. // would expand to -
  76. // int foo(bool bar) {}
  77. #define DEFINE_SYNC_AND_ASYNC(__ret_type__, __func_name__) \
  78. __ret_type__ __func_name__
  79. // This macro should be used to call a function that might be a
  80. // coroutine. It expands to the correct function name and prefixes
  81. // the co_await operator if necessary. For example -
  82. // s = CO_AWAIT(foo)(true);
  83. // if the code is compiled WITH_COROUTINES, would expand to
  84. // s = co_await fooCoroutine(true);
  85. // if compiled WITHOUT_COROUTINES, would expand to
  86. // s = foo(true);
  87. #define CO_AWAIT(__func_name__) __func_name__
  88. #define CO_RETURN return
  89. #endif // DO_NOT_USE_COROUTINES
  90. } // namespace ROCKSDB_NAMESPACE