Debian Codegraph @

src/core/lib/gpr/sync_abseil.cc

raw
1 /*
2 *
3 * Copyright 2020 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 #if defined(GPR_ABSEIL_SYNC) && !defined(GPR_CUSTOM_SYNC)
22
23 #include <errno.h>
24 #include <time.h>
25
26 #include "absl/base/call_once.h"
27 #include "absl/synchronization/mutex.h"
28 #include "absl/time/clock.h"
29 #include "absl/time/time.h"
30
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
34 #include <grpc/support/time.h>
35
36 void gpr_mu_init(gpr_mu* mu) {
37 static_assert(sizeof(gpr_mu) == sizeof(absl::Mutex),
38 "gpr_mu and Mutex must be the same size");
39 new (mu) absl::Mutex;
40 }
41
42 void gpr_mu_destroy(gpr_mu* mu) {
43 reinterpret_cast<absl::Mutex*>(mu)->~Mutex();
44 }
45
46 void gpr_mu_lock(gpr_mu* mu) ABSL_NO_THREAD_SAFETY_ANALYSIS {
47 reinterpret_cast<absl::Mutex*>(mu)->Lock();
48 }
49
50 void gpr_mu_unlock(gpr_mu* mu) ABSL_NO_THREAD_SAFETY_ANALYSIS {
51 reinterpret_cast<absl::Mutex*>(mu)->Unlock();
52 }
53
54 int gpr_mu_trylock(gpr_mu* mu) {
55 return reinterpret_cast<absl::Mutex*>(mu)->TryLock();
56 }
57
58 /*----------------------------------------*/
59
60 void gpr_cv_init(gpr_cv* cv) {
61 static_assert(sizeof(gpr_cv) == sizeof(absl::CondVar),
62 "gpr_cv and CondVar must be the same size");
63 new (cv) absl::CondVar;
64 }
65
66 void gpr_cv_destroy(gpr_cv* cv) {
67 reinterpret_cast<absl::CondVar*>(cv)->~CondVar();
68 }
69
70 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
71 if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
72 0) {
73 reinterpret_cast<absl::CondVar*>(cv)->Wait(
74 reinterpret_cast<absl::Mutex*>(mu));
75 return 0;
76 }
77 abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
78 timespec ts = {static_cast<decltype(ts.tv_sec)>(abs_deadline.tv_sec),
79 static_cast<decltype(ts.tv_nsec)>(abs_deadline.tv_nsec)};
80 return reinterpret_cast<absl::CondVar*>(cv)->WaitWithDeadline(
81 reinterpret_cast<absl::Mutex*>(mu), absl::TimeFromTimespec(ts));
82 }
83
84 void gpr_cv_signal(gpr_cv* cv) {
85 reinterpret_cast<absl::CondVar*>(cv)->Signal();
86 }
87
88 void gpr_cv_broadcast(gpr_cv* cv) {
89 reinterpret_cast<absl::CondVar*>(cv)->SignalAll();
90 }
91
92 /*----------------------------------------*/
93
94 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
95 static_assert(sizeof(gpr_once) == sizeof(absl::once_flag),
96 "gpr_once and absl::once_flag must be the same size");
97 absl::call_once(*reinterpret_cast<absl::once_flag*>(once), init_function);
98 }
99
100 #endif /* defined(GPR_ABSEIL_SYNC) && !defined(GPR_CUSTOM_SYNC) */