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