|
| 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 | +#include <chrono> |
| 18 | +#include <condition_variable> |
| 19 | +#include "nsync_cv.h" |
| 20 | +#include "nsync_mu.h" |
| 21 | + |
| 22 | +namespace tensorflow { |
| 23 | + |
| 24 | +// Check that the external_mu_space struct used to reserve space for the mutex |
| 25 | +// in tensorflow::mutex is big enough. |
| 26 | +static_assert(sizeof(nsync::nsync_mu) <= sizeof(mutex::external_mu_space), |
| 27 | + "tensorflow::mutex::external_mu_space needs to be bigger"); |
| 28 | + |
| 29 | +// Cast a pointer to mutex::external_mu_space to a pointer to the mutex mutex |
| 30 | +// representation. This is done so that the header files for nsync_mu do not |
| 31 | +// need to be included in every file that uses tensorflow's mutex. |
| 32 | +static inline nsync::nsync_mu *mu_cast(mutex::external_mu_space *mu) { |
| 33 | + return reinterpret_cast<nsync::nsync_mu *>(mu); |
| 34 | +} |
| 35 | + |
| 36 | +mutex::mutex() { nsync::nsync_mu_init(mu_cast(&mu_)); } |
| 37 | + |
| 38 | +void mutex::lock() { nsync::nsync_mu_lock(mu_cast(&mu_)); } |
| 39 | + |
| 40 | +bool mutex::try_lock() { return nsync::nsync_mu_trylock(mu_cast(&mu_)) != 0; }; |
| 41 | + |
| 42 | +void mutex::unlock() { nsync::nsync_mu_unlock(mu_cast(&mu_)); } |
| 43 | + |
| 44 | +void mutex::lock_shared() { nsync::nsync_mu_rlock(mu_cast(&mu_)); } |
| 45 | + |
| 46 | +bool mutex::try_lock_shared() { |
| 47 | + return nsync::nsync_mu_rtrylock(mu_cast(&mu_)) != 0; |
| 48 | +}; |
| 49 | + |
| 50 | +void mutex::unlock_shared() { nsync::nsync_mu_runlock(mu_cast(&mu_)); } |
| 51 | + |
| 52 | +// Check that the external_cv_space struct used to reserve space for the |
| 53 | +// condition variable in tensorflow::condition_variable is big enough. |
| 54 | +static_assert( |
| 55 | + sizeof(nsync::nsync_cv) <= sizeof(condition_variable::external_cv_space), |
| 56 | + "tensorflow::condition_variable::external_cv_space needs to be bigger"); |
| 57 | + |
| 58 | +// Cast a pointer to mutex::external_cv_space to a pointer to the condition |
| 59 | +// variable representation. This is done so that the header files for nsync_mu |
| 60 | +// do not need to be included in every file that uses tensorflow's |
| 61 | +// condition_variable. |
| 62 | +static inline nsync::nsync_cv *cv_cast( |
| 63 | + condition_variable::external_cv_space *cv) { |
| 64 | + return reinterpret_cast<nsync::nsync_cv *>(cv); |
| 65 | +} |
| 66 | + |
| 67 | +condition_variable::condition_variable() { |
| 68 | + nsync::nsync_cv_init(cv_cast(&cv_)); |
| 69 | +} |
| 70 | + |
| 71 | +void condition_variable::wait(mutex_lock &lock) { |
| 72 | + nsync::nsync_cv_wait(cv_cast(&cv_), mu_cast(&lock.mutex()->mu_)); |
| 73 | +} |
| 74 | + |
| 75 | +std::cv_status condition_variable::wait_until_system_clock( |
| 76 | + mutex_lock &lock, |
| 77 | + const std::chrono::system_clock::time_point timeout_time) { |
| 78 | + int r = nsync::nsync_cv_wait_with_deadline( |
| 79 | + cv_cast(&cv_), mu_cast(&lock.mutex()->mu_), timeout_time, nullptr); |
| 80 | + return r ? std::cv_status::timeout : std::cv_status::no_timeout; |
| 81 | +} |
| 82 | + |
| 83 | +void condition_variable::notify_one() { nsync::nsync_cv_signal(cv_cast(&cv_)); } |
| 84 | + |
| 85 | +void condition_variable::notify_all() { |
| 86 | + nsync::nsync_cv_broadcast(cv_cast(&cv_)); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace tensorflow |
0 commit comments