1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_CORE_PLATFORM_DEFAULT_MUTEX_H_
17 #define TENSORFLOW_CORE_PLATFORM_DEFAULT_MUTEX_H_
18
19 // IWYU pragma: private, include "third_party/tensorflow/core/platform/mutex.h"
20 // IWYU pragma: friend third_party/tensorflow/core/platform/mutex.h
21
22 #include <chrono> // NOLINT
23 #include <condition_variable> // NOLINT
24 #include <mutex> // NOLINT
25
26 #include "absl/synchronization/mutex.h"
27 #include "tensorflow/core/platform/macros.h"
28 #include "tensorflow/core/platform/mutex.h"
29
30 namespace tensorflow {
31
mutex()32 inline mutex::mutex() {}
33
lock()34 inline void mutex::lock() TF_EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); }
35
try_lock()36 inline bool mutex::try_lock() TF_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
37 return mu_.TryLock();
38 };
39
unlock()40 inline void mutex::unlock() TF_UNLOCK_FUNCTION() { mu_.Unlock(); }
41
lock_shared()42 inline void mutex::lock_shared() TF_SHARED_LOCK_FUNCTION() { mu_.ReaderLock(); }
43
try_lock_shared()44 inline bool mutex::try_lock_shared() TF_SHARED_TRYLOCK_FUNCTION(true) {
45 return mu_.ReaderTryLock();
46 }
47
unlock_shared()48 inline void mutex::unlock_shared() TF_UNLOCK_FUNCTION() { mu_.ReaderUnlock(); }
49
Await(const Condition & cond)50 inline void mutex::Await(const Condition& cond) {
51 mu_.Await(absl::Condition(&cond, &Condition::Eval));
52 }
53
AwaitWithDeadline(const Condition & cond,uint64 abs_deadline_ns)54 inline bool mutex::AwaitWithDeadline(const Condition& cond,
55 uint64 abs_deadline_ns) {
56 return mu_.AwaitWithDeadline(absl::Condition(&cond, &Condition::Eval),
57 absl::FromUnixNanos(abs_deadline_ns));
58 }
59
condition_variable()60 inline condition_variable::condition_variable() {}
61
wait(mutex_lock & lock)62 inline void condition_variable::wait(mutex_lock& lock) {
63 cv_.Wait(&lock.mutex()->mu_);
64 }
65
notify_one()66 inline void condition_variable::notify_one() { cv_.Signal(); }
67
notify_all()68 inline void condition_variable::notify_all() { cv_.SignalAll(); }
69
70 template <class Rep, class Period>
wait_for(mutex_lock & lock,std::chrono::duration<Rep,Period> dur)71 std::cv_status condition_variable::wait_for(
72 mutex_lock& lock, std::chrono::duration<Rep, Period> dur) {
73 bool r = cv_.WaitWithTimeout(&lock.mutex()->mu_, ::absl::FromChrono(dur));
74 return r ? std::cv_status::timeout : std::cv_status::no_timeout;
75 }
76
77 } // namespace tensorflow
78
79 #endif // TENSORFLOW_CORE_PLATFORM_DEFAULT_MUTEX_H_
80