]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
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 | ||
7 | #if defined(USE_COROUTINES) | |
8 | #include "folly/experimental/coro/Coroutine.h" | |
9 | #include "folly/experimental/coro/Task.h" | |
10 | #endif | |
11 | #include "rocksdb/rocksdb_namespace.h" | |
12 | ||
13 | // This file has two sctions. The first section applies to all instances of | |
14 | // header file inclusion and has an include guard. The second section is | |
15 | // meant for multiple inclusions in the same source file, and is idempotent. | |
16 | namespace ROCKSDB_NAMESPACE { | |
17 | ||
18 | #ifndef UTIL_CORO_UTILS_H_ | |
19 | #define UTIL_CORO_UTILS_H_ | |
20 | ||
21 | #if defined(USE_COROUTINES) | |
22 | ||
23 | // The follwoing macros expand to regular and coroutine function | |
24 | // declarations for a given function | |
25 | #define DECLARE_SYNC_AND_ASYNC(__ret_type__, __func_name__, ...) \ | |
26 | __ret_type__ __func_name__(__VA_ARGS__); \ | |
27 | folly::coro::Task<__ret_type__> __func_name__##Coroutine(__VA_ARGS__); | |
28 | ||
29 | #define DECLARE_SYNC_AND_ASYNC_OVERRIDE(__ret_type__, __func_name__, ...) \ | |
30 | __ret_type__ __func_name__(__VA_ARGS__) override; \ | |
31 | folly::coro::Task<__ret_type__> __func_name__##Coroutine(__VA_ARGS__) \ | |
32 | override; | |
33 | ||
34 | #define DECLARE_SYNC_AND_ASYNC_CONST(__ret_type__, __func_name__, ...) \ | |
35 | __ret_type__ __func_name__(__VA_ARGS__) const; \ | |
36 | folly::coro::Task<__ret_type__> __func_name__##Coroutine(__VA_ARGS__) const; | |
37 | ||
38 | constexpr bool using_coroutines() { return true; } | |
39 | #else // !USE_COROUTINES | |
40 | ||
41 | // The follwoing macros expand to a regular function declaration for a given | |
42 | // function | |
43 | #define DECLARE_SYNC_AND_ASYNC(__ret_type__, __func_name__, ...) \ | |
44 | __ret_type__ __func_name__(__VA_ARGS__); | |
45 | ||
46 | #define DECLARE_SYNC_AND_ASYNC_OVERRIDE(__ret_type__, __func_name__, ...) \ | |
47 | __ret_type__ __func_name__(__VA_ARGS__) override; | |
48 | ||
49 | #define DECLARE_SYNC_AND_ASYNC_CONST(__ret_type__, __func_name__, ...) \ | |
50 | __ret_type__ __func_name__(__VA_ARGS__) const; | |
51 | ||
52 | constexpr bool using_coroutines() { return false; } | |
53 | #endif // USE_COROUTINES | |
54 | #endif // UTIL_CORO_UTILS_H_ | |
55 | ||
56 | // The following section of the file is meant to be included twice in a | |
57 | // source file - once defining WITH_COROUTINES and once defining | |
58 | // WITHOUT_COROUTINES | |
59 | #undef DEFINE_SYNC_AND_ASYNC | |
60 | #undef CO_AWAIT | |
61 | #undef CO_RETURN | |
62 | ||
63 | #if defined(WITH_COROUTINES) && defined(USE_COROUTINES) | |
64 | ||
65 | // This macro should be used in the beginning of the function | |
66 | // definition. The declaration should have been done using one of the | |
67 | // DECLARE_SYNC_AND_ASYNC* macros. It expands to the return type and | |
68 | // the function name with the Coroutine suffix. For example - | |
69 | // DEFINE_SYNC_AND_ASYNC(int, foo)(bool bar) {} | |
70 | // would expand to - | |
71 | // folly::coro::Task<int> fooCoroutine(bool bar) {} | |
72 | #define DEFINE_SYNC_AND_ASYNC(__ret_type__, __func_name__) \ | |
73 | folly::coro::Task<__ret_type__> __func_name__##Coroutine | |
74 | ||
75 | // This macro should be used to call a function that might be a | |
76 | // coroutine. It expands to the correct function name and prefixes | |
77 | // the co_await operator if necessary. For example - | |
78 | // s = CO_AWAIT(foo)(true); | |
79 | // if the code is compiled WITH_COROUTINES, would expand to | |
80 | // s = co_await fooCoroutine(true); | |
81 | // if compiled WITHOUT_COROUTINES, would expand to | |
82 | // s = foo(true); | |
83 | #define CO_AWAIT(__func_name__) co_await __func_name__##Coroutine | |
84 | ||
85 | #define CO_RETURN co_return | |
86 | ||
87 | #elif defined(WITHOUT_COROUTINES) | |
88 | ||
89 | // This macro should be used in the beginning of the function | |
90 | // definition. The declaration should have been done using one of the | |
91 | // DECLARE_SYNC_AND_ASYNC* macros. It expands to the return type and | |
92 | // the function name without the Coroutine suffix. For example - | |
93 | // DEFINE_SYNC_AND_ASYNC(int, foo)(bool bar) {} | |
94 | // would expand to - | |
95 | // int foo(bool bar) {} | |
96 | #define DEFINE_SYNC_AND_ASYNC(__ret_type__, __func_name__) \ | |
97 | __ret_type__ __func_name__ | |
98 | ||
99 | // This macro should be used to call a function that might be a | |
100 | // coroutine. It expands to the correct function name and prefixes | |
101 | // the co_await operator if necessary. For example - | |
102 | // s = CO_AWAIT(foo)(true); | |
103 | // if the code is compiled WITH_COROUTINES, would expand to | |
104 | // s = co_await fooCoroutine(true); | |
105 | // if compiled WITHOUT_COROUTINES, would expand to | |
106 | // s = foo(true); | |
107 | #define CO_AWAIT(__func_name__) __func_name__ | |
108 | ||
109 | #define CO_RETURN return | |
110 | ||
111 | #endif // DO_NOT_USE_COROUTINES | |
112 | } // namespace ROCKSDB_NAMESPACE |