3c947bf9d6485996d65b7dc5828db104abce4d11
[platform/upstream/grpc.git] / src / core / lib / iomgr / combiner.h
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #ifndef GRPC_CORE_LIB_IOMGR_COMBINER_H
20 #define GRPC_CORE_LIB_IOMGR_COMBINER_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <stddef.h>
25
26 #include <grpc/support/atm.h>
27 #include "src/core/lib/debug/trace.h"
28 #include "src/core/lib/gpr/mpscq.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30
31 // Provides serialized access to some resource.
32 // Each action queued on a combiner is executed serially in a borrowed thread.
33 // The actual thread executing actions may change over time (but there will only
34 // every be one at a time).
35
36 // Initialize the lock, with an optional workqueue to shift load to when
37 // necessary
38 grpc_combiner* grpc_combiner_create(void);
39
40 #ifndef NDEBUG
41 #define GRPC_COMBINER_DEBUG_ARGS \
42   , const char *file, int line, const char *reason
43 #define GRPC_COMBINER_REF(combiner, reason) \
44   grpc_combiner_ref((combiner), __FILE__, __LINE__, (reason))
45 #define GRPC_COMBINER_UNREF(combiner, reason) \
46   grpc_combiner_unref((combiner), __FILE__, __LINE__, (reason))
47 #else
48 #define GRPC_COMBINER_DEBUG_ARGS
49 #define GRPC_COMBINER_REF(combiner, reason) grpc_combiner_ref((combiner))
50 #define GRPC_COMBINER_UNREF(combiner, reason) grpc_combiner_unref((combiner))
51 #endif
52
53 // Ref/unref the lock, for when we're sharing the lock ownership
54 // Prefer to use the macros above
55 grpc_combiner* grpc_combiner_ref(grpc_combiner* lock GRPC_COMBINER_DEBUG_ARGS);
56 void grpc_combiner_unref(grpc_combiner* lock GRPC_COMBINER_DEBUG_ARGS);
57 // Fetch a scheduler to schedule closures against
58 grpc_closure_scheduler* grpc_combiner_scheduler(grpc_combiner* lock);
59 // Scheduler to execute \a action within the lock just prior to unlocking.
60 grpc_closure_scheduler* grpc_combiner_finally_scheduler(grpc_combiner* lock);
61
62 bool grpc_combiner_continue_exec_ctx();
63
64 extern grpc_core::DebugOnlyTraceFlag grpc_combiner_trace;
65
66 #endif /* GRPC_CORE_LIB_IOMGR_COMBINER_H */