1 //
2 //
3 // Copyright 2017 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 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/surface/completion_queue_factory.h"
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 
26 #include "src/core/lib/iomgr/exec_ctx.h"
27 #include "src/core/lib/surface/completion_queue.h"
28 
29 //
30 // == Default completion queue factory implementation ==
31 //
32 
default_create(const grpc_completion_queue_factory *,const grpc_completion_queue_attributes * attr)33 static grpc_completion_queue* default_create(
34     const grpc_completion_queue_factory* /*factory*/,
35     const grpc_completion_queue_attributes* attr) {
36   return grpc_completion_queue_create_internal(
37       attr->cq_completion_type, attr->cq_polling_type, attr->cq_shutdown_cb);
38 }
39 
40 static grpc_completion_queue_factory_vtable default_vtable = {default_create};
41 
42 static const grpc_completion_queue_factory g_default_cq_factory = {
43     "Default Factory", nullptr, &default_vtable};
44 
45 //
46 // == Completion queue factory APIs
47 //
48 
grpc_completion_queue_factory_lookup(const grpc_completion_queue_attributes * attributes)49 const grpc_completion_queue_factory* grpc_completion_queue_factory_lookup(
50     const grpc_completion_queue_attributes* attributes) {
51   GPR_ASSERT(attributes->version >= 1 &&
52              attributes->version <= GRPC_CQ_CURRENT_VERSION);
53 
54   // The default factory can handle version 1 of the attributes structure. We
55   // may have to change this as more fields are added to the structure
56   return &g_default_cq_factory;
57 }
58 
59 //
60 // == Completion queue creation APIs ==
61 //
62 
grpc_completion_queue_create_for_next(void * reserved)63 grpc_completion_queue* grpc_completion_queue_create_for_next(void* reserved) {
64   grpc_core::ExecCtx exec_ctx;
65   GPR_ASSERT(!reserved);
66   grpc_completion_queue_attributes attr = {1, GRPC_CQ_NEXT,
67                                            GRPC_CQ_DEFAULT_POLLING, nullptr};
68   return g_default_cq_factory.vtable->create(&g_default_cq_factory, &attr);
69 }
70 
grpc_completion_queue_create_for_pluck(void * reserved)71 grpc_completion_queue* grpc_completion_queue_create_for_pluck(void* reserved) {
72   grpc_core::ExecCtx exec_ctx;
73   GPR_ASSERT(!reserved);
74   grpc_completion_queue_attributes attr = {1, GRPC_CQ_PLUCK,
75                                            GRPC_CQ_DEFAULT_POLLING, nullptr};
76   return g_default_cq_factory.vtable->create(&g_default_cq_factory, &attr);
77 }
78 
grpc_completion_queue_create_for_callback(grpc_completion_queue_functor * shutdown_callback,void * reserved)79 grpc_completion_queue* grpc_completion_queue_create_for_callback(
80     grpc_completion_queue_functor* shutdown_callback, void* reserved) {
81   grpc_core::ExecCtx exec_ctx;
82   GPR_ASSERT(!reserved);
83   grpc_completion_queue_attributes attr = {
84       2, GRPC_CQ_CALLBACK, GRPC_CQ_DEFAULT_POLLING, shutdown_callback};
85   return g_default_cq_factory.vtable->create(&g_default_cq_factory, &attr);
86 }
87 
grpc_completion_queue_create(const grpc_completion_queue_factory * factory,const grpc_completion_queue_attributes * attr,void * reserved)88 grpc_completion_queue* grpc_completion_queue_create(
89     const grpc_completion_queue_factory* factory,
90     const grpc_completion_queue_attributes* attr, void* reserved) {
91   grpc_core::ExecCtx exec_ctx;
92   GPR_ASSERT(!reserved);
93   return factory->vtable->create(factory, attr);
94 }
95