Skip to content

Commit 16632df

Browse files
Remove nsync header files from tensorflow/core/platform/default/mutex.h
The implementation of tensorflow/core/platform/default/mutex.h uses the nsync library, so mutex.h has included nsync header files. This has been awkward, because each TensorFlow build (bazel, cmake, make, plus the instructions for compiling individual custom ops, on all the various platforms) has needed to handle the include paths correctly, reaching into a package that is downloaded separately from TensorFlow itself. This change avoids that awkwardness, instead taking on two different irritations: - mutex.h now defines two structs that are large enough and aligned enough to contain an nsync_mu and an nsync_cv. This is an abstraction violation, because TensorFlow's source should not need to know how big these data structures are. However, this is unlikely to cause problems because: 1) this is checked by a static assertion in mutex.cc, so we will notice immediately should a change be needed, and 2) this will likely never fail because we have no intent of allowing nsync's data strcutures to get bigger. - The methods of mutex and condition_variable can no longer be inlined, because that too would require mutex.h to include the nsync header files. (Or we'd need to declare the nsync functions directly in mutex.h, which would be another abstraction violation.) However, this is a small imposition because the overhead of a procedure call is typically small. The assumption behind this CL is that these irritations are less important than the ongoing frustration of maintaining the complex include path in multiple build systems. PiperOrigin-RevId: 189079523
1 parent 4ff81a7 commit 16632df

10 files changed

Lines changed: 121 additions & 58 deletions

File tree

‎tensorflow/compiler/xla/service/interpreter/BUILD‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ cc_library(
6363
name = "platform_id",
6464
srcs = ["platform_id.cc"],
6565
hdrs = ["platform_id.h"],
66-
deps = [
67-
"@nsync//:nsync_headers",
68-
"//tensorflow/core:stream_executor_headers_lib",
69-
] + if_static(
66+
deps = ["//tensorflow/core:stream_executor_headers_lib"] + if_static(
7067
["@protobuf_archive//:protobuf"],
7168
["@protobuf_archive//:protobuf_headers"],
7269
),

‎tensorflow/contrib/android/cmake/CMakeLists.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ target_link_libraries(tensorflow_inference
7575
include_directories(
7676
${PREBUILT_DIR}/proto
7777
${PREBUILT_DIR}/protobuf/include
78-
${PREBUILT_DIR}/nsync/public
7978
${TENSORFLOW_ROOT_DIR}/tensorflow/contrib/makefile/downloads/eigen
8079
${TENSORFLOW_ROOT_DIR}
8180
${CMAKE_CURRENT_SOURCE_DIR}/..)

‎tensorflow/contrib/cmake/tf_shared_lib.cmake‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ if(WIN32)
100100
endif(WIN32)
101101

102102
target_include_directories(tensorflow PUBLIC
103-
$<INSTALL_INTERFACE:include/>
104-
$<INSTALL_INTERFACE:include/external/nsync/public>)
103+
$<INSTALL_INTERFACE:include/>)
105104

106105
install(TARGETS tensorflow EXPORT tensorflow_export
107106
RUNTIME DESTINATION bin
@@ -133,10 +132,6 @@ install(DIRECTORY ${tensorflow_source_dir}/tensorflow/stream_executor/
133132
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/protobuf/src/protobuf/src/google/
134133
DESTINATION include/google
135134
FILES_MATCHING PATTERN "*.h")
136-
# nsync headers
137-
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/external/nsync/
138-
DESTINATION include/external/nsync
139-
FILES_MATCHING PATTERN "*.h")
140135
# Eigen directory
141136
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/eigen/src/eigen/Eigen/
142137
DESTINATION include/Eigen)

‎tensorflow/contrib/makefile/proto_text_cc_files.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tensorflow/core/platform/env_time.cc
1717
tensorflow/core/platform/setround.cc
1818
tensorflow/core/platform/denormal.cc
1919
tensorflow/core/platform/default/tracing.cc
20+
tensorflow/core/platform/default/mutex.cc
2021
tensorflow/core/platform/default/logging.cc
2122
tensorflow/core/platform/cpu_info.cc
2223
tensorflow/core/lib/wav/wav_io.cc

‎tensorflow/core/BUILD‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,6 @@ cc_header_only_library(
19581958
deps = [
19591959
":framework",
19601960
":reader_base",
1961-
"@nsync//:nsync_headers",
19621961
],
19631962
)
19641963

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

‎tensorflow/core/platform/default/mutex.h‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ limitations under the License.
2222
#include <chrono>
2323
#include <condition_variable>
2424
#include <mutex>
25-
#include "nsync_cv.h"
26-
#include "nsync_mu.h"
2725
#include "tensorflow/core/platform/thread_annotations.h"
26+
2827
namespace tensorflow {
2928

3029
#undef mutex_lock
@@ -38,26 +37,26 @@ class condition_variable;
3837
// lock.
3938
class LOCKABLE mutex {
4039
public:
41-
mutex() { nsync::nsync_mu_init(&mu_); }
42-
// The default implementation of nsync_mutex is safe to use after the linker
43-
// initializations
40+
mutex();
41+
// The default implementation of the underlying mutex is safe to use after
42+
// the linker initialization to zero.
4443
explicit mutex(LinkerInitialized x) {}
4544

46-
void lock() EXCLUSIVE_LOCK_FUNCTION() { nsync::nsync_mu_lock(&mu_); }
47-
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true) {
48-
return nsync::nsync_mu_trylock(&mu_) != 0;
49-
};
50-
void unlock() UNLOCK_FUNCTION() { nsync::nsync_mu_unlock(&mu_); }
45+
void lock() EXCLUSIVE_LOCK_FUNCTION();
46+
bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
47+
void unlock() UNLOCK_FUNCTION();
48+
49+
void lock_shared() SHARED_LOCK_FUNCTION();
50+
bool try_lock_shared() SHARED_TRYLOCK_FUNCTION(true);
51+
void unlock_shared() UNLOCK_FUNCTION();
5152

52-
void lock_shared() SHARED_LOCK_FUNCTION() { nsync::nsync_mu_rlock(&mu_); }
53-
bool try_lock_shared() SHARED_TRYLOCK_FUNCTION(true) {
54-
return nsync::nsync_mu_rtrylock(&mu_) != 0;
53+
struct external_mu_space {
54+
void* space[2];
5555
};
56-
void unlock_shared() UNLOCK_FUNCTION() { nsync::nsync_mu_runlock(&mu_); }
5756

5857
private:
5958
friend class condition_variable;
60-
nsync::nsync_mu mu_;
59+
external_mu_space mu_;
6160
};
6261

6362
// Mimic a subset of the std::unique_lock<tensorflow::mutex> functionality.
@@ -139,26 +138,29 @@ class SCOPED_LOCKABLE tf_shared_lock {
139138
// Mimic std::condition_variable.
140139
class condition_variable {
141140
public:
142-
condition_variable() { nsync::nsync_cv_init(&cv_); }
141+
condition_variable();
143142

144-
void wait(mutex_lock& lock) {
145-
nsync::nsync_cv_wait(&cv_, &lock.mutex()->mu_);
146-
}
143+
void wait(mutex_lock& lock);
147144
template <class Rep, class Period>
148145
std::cv_status wait_for(mutex_lock& lock,
149146
std::chrono::duration<Rep, Period> dur) {
150-
int r = nsync::nsync_cv_wait_with_deadline(
151-
&cv_, &lock.mutex()->mu_, std::chrono::system_clock::now() + dur,
152-
nullptr);
153-
return r ? std::cv_status::timeout : std::cv_status::no_timeout;
147+
return wait_until_system_clock(lock,
148+
std::chrono::system_clock::now() + dur);
154149
}
155-
void notify_one() { nsync::nsync_cv_signal(&cv_); }
156-
void notify_all() { nsync::nsync_cv_broadcast(&cv_); }
150+
void notify_one();
151+
void notify_all();
152+
153+
struct external_cv_space {
154+
void* space[2];
155+
};
157156

158157
private:
159158
friend ConditionResult WaitForMilliseconds(mutex_lock* mu,
160159
condition_variable* cv, int64 ms);
161-
nsync::nsync_cv cv_;
160+
std::cv_status wait_until_system_clock(
161+
mutex_lock& lock,
162+
const std::chrono::system_clock::time_point timeout_time);
163+
external_cv_space cv_;
162164
};
163165

164166
inline ConditionResult WaitForMilliseconds(mutex_lock* mu,

‎tensorflow/python/platform/sysconfig.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def get_compile_flags():
6868
"""
6969
flags = []
7070
flags.append('-I%s' % get_include())
71-
flags.append('-I%s/external/nsync/public' % get_include())
7271
flags.append('-D_GLIBCXX_USE_CXX11_ABI=%d' % _CXX11_ABI_FLAG)
7372
return flags
7473

‎tensorflow/tensorflow.bzl‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,22 +1158,6 @@ def transitive_hdrs(name, deps=[], **kwargs):
11581158
# the libraries in deps.
11591159
def cc_header_only_library(name, deps=[], includes=[], **kwargs):
11601160
_transitive_hdrs(name=name + "_gather", deps=deps)
1161-
1162-
# We could generalize the following, but rather than complicate things
1163-
# here, we'll do the minimal use case for now, and hope bazel comes up
1164-
# with a better solution before too long. We'd expect it to compute
1165-
# the right include path by itself, but it doesn't, possibly because
1166-
# _transitive_hdrs lost some information about the include path.
1167-
if "@nsync//:nsync_headers" in deps:
1168-
# Buiding tensorflow from @org_tensorflow finds this two up.
1169-
nsynch = "../../external/nsync/public"
1170-
# Building tensorflow from elsewhere finds it four up.
1171-
# Note that native.repository_name() is not yet available in TF's Kokoro.
1172-
if REPOSITORY_NAME != "@":
1173-
nsynch = "../../" + nsynch
1174-
includes = includes[:]
1175-
includes.append(nsynch)
1176-
11771161
native.cc_library(name=name,
11781162
hdrs=[":" + name + "_gather"],
11791163
includes=includes,
@@ -1182,7 +1166,6 @@ def cc_header_only_library(name, deps=[], includes=[], **kwargs):
11821166
def tf_custom_op_library_additional_deps():
11831167
return [
11841168
"@protobuf_archive//:protobuf_headers",
1185-
"@nsync//:nsync_headers",
11861169
clean_dep("//third_party/eigen3"),
11871170
clean_dep("//tensorflow/core:framework_headers_lib"),
11881171
]

‎tensorflow/tools/pip_package/setup.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ def find_files(pattern, root):
200200
list(find_files('*.h', 'tensorflow/stream_executor')) +
201201
list(find_files('*.h', 'google/protobuf_archive/src')) +
202202
list(find_files('*', 'third_party/eigen3')) +
203-
list(find_files('*', 'external/eigen_archive')) +
204-
list(find_files('*.h', 'external/nsync/public')))
203+
list(find_files('*', 'external/eigen_archive')))
205204

206205
setup(
207206
name=project_name,

0 commit comments

Comments
 (0)