• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #include "tensorflow/core/platform/mutex.h"
17 
18 #include <time.h>
19 
20 #include "nsync_cv.h"       // NOLINT
21 #include "nsync_mu.h"       // NOLINT
22 #include "nsync_mu_wait.h"  // NOLINT
23 #include "nsync_time.h"     // NOLINT
24 
25 namespace tensorflow {
26 
27 // Check that the MuData struct used to reserve space for the mutex
28 // in tensorflow::mutex is big enough.
29 static_assert(sizeof(nsync::nsync_mu) <= sizeof(internal::MuData),
30               "tensorflow::internal::MuData needs to be bigger");
31 
32 // Cast a pointer to internal::MuData to a pointer to the mutex
33 // representation.  This is done so that the header files for nsync_mu do not
34 // need to be included in every file that uses tensorflow's mutex.
mu_cast(internal::MuData * mu)35 static inline nsync::nsync_mu *mu_cast(internal::MuData *mu) {
36   return reinterpret_cast<nsync::nsync_mu *>(mu);
37 }
38 
mutex()39 mutex::mutex() { nsync::nsync_mu_init(mu_cast(&mu_)); }
40 
mutex(LinkerInitialized x)41 mutex::mutex(LinkerInitialized x) {}
42 
lock()43 void mutex::lock() { nsync::nsync_mu_lock(mu_cast(&mu_)); }
44 
try_lock()45 bool mutex::try_lock() { return nsync::nsync_mu_trylock(mu_cast(&mu_)) != 0; };
46 
unlock()47 void mutex::unlock() { nsync::nsync_mu_unlock(mu_cast(&mu_)); }
48 
lock_shared()49 void mutex::lock_shared() { nsync::nsync_mu_rlock(mu_cast(&mu_)); }
50 
try_lock_shared()51 bool mutex::try_lock_shared() {
52   return nsync::nsync_mu_rtrylock(mu_cast(&mu_)) != 0;
53 };
54 
unlock_shared()55 void mutex::unlock_shared() { nsync::nsync_mu_runlock(mu_cast(&mu_)); }
56 
57 // A callback suitable for nsync_mu_wait() that calls Condition::Eval().
EvaluateCondition(const void * vcond)58 static int EvaluateCondition(const void *vcond) {
59   return static_cast<int>(static_cast<const Condition *>(vcond)->Eval());
60 }
61 
Await(const Condition & cond)62 void mutex::Await(const Condition &cond) {
63   nsync::nsync_mu_wait(mu_cast(&mu_), &EvaluateCondition, &cond, nullptr);
64 }
65 
AwaitWithDeadline(const Condition & cond,uint64 abs_deadline_ns)66 bool mutex::AwaitWithDeadline(const Condition &cond, uint64 abs_deadline_ns) {
67   time_t seconds = abs_deadline_ns / (1000 * 1000 * 1000);
68   nsync::nsync_time abs_time = nsync::nsync_time_s_ns(
69       seconds, abs_deadline_ns - seconds * (1000 * 1000 * 1000));
70   return nsync::nsync_mu_wait_with_deadline(mu_cast(&mu_), &EvaluateCondition,
71                                             &cond, nullptr, abs_time,
72                                             nullptr) == 0;
73 }
74 
75 // Check that the CVData struct used to reserve space for the
76 // condition variable in tensorflow::condition_variable is big enough.
77 static_assert(sizeof(nsync::nsync_cv) <= sizeof(internal::CVData),
78               "tensorflow::internal::CVData needs to be bigger");
79 
80 // Cast a pointer to internal::CVData to a pointer to the condition
81 // variable representation.  This is done so that the header files for nsync_cv
82 // do not need to be included in every file that uses tensorflow's
83 // condition_variable.
cv_cast(internal::CVData * cv)84 static inline nsync::nsync_cv *cv_cast(internal::CVData *cv) {
85   return reinterpret_cast<nsync::nsync_cv *>(cv);
86 }
87 
condition_variable()88 condition_variable::condition_variable() {
89   nsync::nsync_cv_init(cv_cast(&cv_));
90 }
91 
wait(mutex_lock & lock)92 void condition_variable::wait(mutex_lock &lock) {
93   nsync::nsync_cv_wait(cv_cast(&cv_), mu_cast(&lock.mutex()->mu_));
94 }
95 
notify_one()96 void condition_variable::notify_one() { nsync::nsync_cv_signal(cv_cast(&cv_)); }
97 
notify_all()98 void condition_variable::notify_all() {
99   nsync::nsync_cv_broadcast(cv_cast(&cv_));
100 }
101 
102 namespace internal {
wait_until_system_clock(CVData * cv_data,MuData * mu_data,const std::chrono::system_clock::time_point timeout_time)103 std::cv_status wait_until_system_clock(
104     CVData *cv_data, MuData *mu_data,
105     const std::chrono::system_clock::time_point timeout_time) {
106   int r = nsync::nsync_cv_wait_with_deadline(cv_cast(cv_data), mu_cast(mu_data),
107                                              timeout_time, nullptr);
108   return r ? std::cv_status::timeout : std::cv_status::no_timeout;
109 }
110 }  // namespace internal
111 
112 }  // namespace tensorflow
113