sync_abseil.cc
Go to the documentation of this file.
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 
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 
37 
38 #ifdef GPR_LOW_LEVEL_COUNTERS
39 gpr_atm gpr_mu_locks = 0;
40 gpr_atm gpr_counter_atm_cas = 0;
41 gpr_atm gpr_counter_atm_add = 0;
42 #endif
43 
44 void gpr_mu_init(gpr_mu* mu) {
45  static_assert(sizeof(gpr_mu) == sizeof(absl::Mutex),
46  "gpr_mu and Mutex must be the same size");
47  new (mu) absl::Mutex;
48 }
49 
50 void gpr_mu_destroy(gpr_mu* mu) {
51  reinterpret_cast<absl::Mutex*>(mu)->~Mutex();
52 }
53 
55  GPR_TIMER_SCOPE("gpr_mu_lock", 0);
56  reinterpret_cast<absl::Mutex*>(mu)->Lock();
57 }
58 
60  GPR_TIMER_SCOPE("gpr_mu_unlock", 0);
61  reinterpret_cast<absl::Mutex*>(mu)->Unlock();
62 }
63 
64 int gpr_mu_trylock(gpr_mu* mu) {
65  GPR_TIMER_SCOPE("gpr_mu_trylock", 0);
66  return reinterpret_cast<absl::Mutex*>(mu)->TryLock();
67 }
68 
69 /*----------------------------------------*/
70 
71 void gpr_cv_init(gpr_cv* cv) {
72  static_assert(sizeof(gpr_cv) == sizeof(absl::CondVar),
73  "gpr_cv and CondVar must be the same size");
74  new (cv) absl::CondVar;
75 }
76 
77 void gpr_cv_destroy(gpr_cv* cv) {
78  reinterpret_cast<absl::CondVar*>(cv)->~CondVar();
79 }
80 
81 int gpr_cv_wait(gpr_cv* cv, gpr_mu* mu, gpr_timespec abs_deadline) {
82  GPR_TIMER_SCOPE("gpr_cv_wait", 0);
83  if (gpr_time_cmp(abs_deadline, gpr_inf_future(abs_deadline.clock_type)) ==
84  0) {
85  reinterpret_cast<absl::CondVar*>(cv)->Wait(
86  reinterpret_cast<absl::Mutex*>(mu));
87  return 0;
88  }
89  abs_deadline = gpr_convert_clock_type(abs_deadline, GPR_CLOCK_REALTIME);
90  timespec ts = {static_cast<decltype(ts.tv_sec)>(abs_deadline.tv_sec),
91  static_cast<decltype(ts.tv_nsec)>(abs_deadline.tv_nsec)};
92  return reinterpret_cast<absl::CondVar*>(cv)->WaitWithDeadline(
93  reinterpret_cast<absl::Mutex*>(mu), absl::TimeFromTimespec(ts));
94 }
95 
96 void gpr_cv_signal(gpr_cv* cv) {
97  GPR_TIMER_MARK("gpr_cv_signal", 0);
98  reinterpret_cast<absl::CondVar*>(cv)->Signal();
99 }
100 
101 void gpr_cv_broadcast(gpr_cv* cv) {
102  GPR_TIMER_MARK("gpr_cv_broadcast", 0);
103  reinterpret_cast<absl::CondVar*>(cv)->SignalAll();
104 }
105 
106 /*----------------------------------------*/
107 
108 void gpr_once_init(gpr_once* once, void (*init_function)(void)) {
109  static_assert(sizeof(gpr_once) == sizeof(absl::once_flag),
110  "gpr_once and absl::once_flag must be the same size");
111  absl::call_once(*reinterpret_cast<absl::once_flag*>(once), init_function);
112 }
113 
114 #endif /* defined(GPR_ABSEIL_SYNC) && !defined(GPR_CUSTOM_SYNC) */
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition: gpr_types.h:52
gpr_timespec::tv_sec
int64_t tv_sec
Definition: gpr_types.h:51
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
log.h
timers.h
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
GPR_TIMER_SCOPE
#define GPR_TIMER_SCOPE(tag, important)
Definition: src/core/lib/profiling/timers.h:43
absl::Mutex
Definition: abseil-cpp/absl/synchronization/mutex.h:131
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
time.h
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
absl::call_once
void call_once(absl::once_flag &flag, Callable &&fn, Args &&... args)
Definition: abseil-cpp/absl/base/call_once.h:206
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
gpr_time_cmp
GPRAPI int gpr_time_cmp(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:30
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
gpr_timespec::clock_type
gpr_clock_type clock_type
Definition: gpr_types.h:55
gpr_atm
intptr_t gpr_atm
Definition: impl/codegen/atm_gcc_atomic.h:32
once
absl::once_flag once
Definition: bm_opencensus_plugin.cc:38
cv
unsigned cv
Definition: cxa_demangle.cpp:4908
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
absl::CondVar
Definition: abseil-cpp/absl/synchronization/mutex.h:798
absl::TimeFromTimespec
absl::Time TimeFromTimespec(timespec ts)
Definition: third_party/abseil-cpp/absl/time/time.cc:288
gpr_convert_clock_type
GPRAPI gpr_timespec gpr_convert_clock_type(gpr_timespec t, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:241
gpr_mu_trylock
GPRAPI int gpr_mu_trylock(gpr_mu *mu)
alloc.h
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
gpr_cv_broadcast
GPRAPI void gpr_cv_broadcast(gpr_cv *cv)
gpr_timespec
Definition: gpr_types.h:50
absl::once_flag
Definition: abseil-cpp/absl/base/call_once.h:85
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
sync.h
GPR_TIMER_MARK
#define GPR_TIMER_MARK(tag, important)
Definition: src/core/lib/profiling/timers.h:39
errno.h
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)
ABSL_NO_THREAD_SAFETY_ANALYSIS
#define ABSL_NO_THREAD_SAFETY_ANALYSIS
Definition: abseil-cpp/absl/base/thread_annotations.h:280
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:24