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