Skip to content

Commit

Permalink
Windows JNI: refactorings
Browse files Browse the repository at this point in the history
In this change:

- rename //src/main/native:windows_jni_utils to
//src/main/native:windows_jni_lib and make it
visible to //src/main/cpp:__subpackages__ because
we will use some methods there from this library

- move AutoHandle into windows_util.h, we'll use
it from blaze_util_windows.cc / file_windows.cc
later

See bazelbuild#2107

--
PiperOrigin-RevId: 144946842
MOS_MIGRATED_REVID=144946842
  • Loading branch information
laszlocsomor authored and vladmos committed Jan 19, 2017
1 parent 97f4ac6 commit e5f4f92
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/main/native/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ cc_binary(
)

cc_library(
name = "windows_jni_utils",
name = "windows_jni_lib",
srcs = ["windows_util.cc"],
hdrs = ["windows_util.h"],
visibility = ["//src/test/native:__pkg__"],
visibility = [
"//src/main/cpp:__subpackages__",
"//src/test/native:__pkg__",
],
)

genrule(
Expand Down
18 changes: 4 additions & 14 deletions src/main/native/windows_processes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ struct NativeProcess {
error_("") {}
};

struct AutoHandle {
AutoHandle() : handle(INVALID_HANDLE_VALUE) {}
~AutoHandle() {
CloseHandle(handle); // handles INVALID_HANDLE_VALUE
handle = INVALID_HANDLE_VALUE;
}

HANDLE handle;
};

class JavaByteArray {
public:
JavaByteArray(JNIEnv* env, jbyteArray java_array)
Expand Down Expand Up @@ -196,10 +186,10 @@ Java_com_google_devtools_build_lib_windows_WindowsProcesses_nativeCreateProcess(
// created. If this was not so, operations on these file handles would not
// return immediately if the process is terminated.
// Therefore we make these handles auto-closing (by using AutoHandle).
AutoHandle stdin_process;
AutoHandle stdout_process;
AutoHandle stderr_process;
AutoHandle thread;
windows_util::AutoHandle stdin_process;
windows_util::AutoHandle stdout_process;
windows_util::AutoHandle stderr_process;
windows_util::AutoHandle thread;
PROCESS_INFORMATION process_info = {0};
STARTUPINFOA startup_info = {0};
JOBOBJECT_EXTENDED_LIMIT_INFORMATION job_info = {0};
Expand Down
16 changes: 14 additions & 2 deletions src/main/native/windows_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// INTERNAL header file for use by C++ code in this package.

#ifndef BAZEL_SRC_MAIN_NATIVE_WINDOWS_UTIL_H__
#define BAZEL_SRC_MAIN_NATIVE_WINDOWS_UTIL_H__

#include <windows.h>

#include <functional>
#include <memory>
#include <string>
Expand All @@ -28,6 +28,18 @@ using std::string;
using std::unique_ptr;
using std::wstring;

// A wrapper for the `HANDLE` type that calls CloseHandle in its d'tor.
struct AutoHandle {
AutoHandle(HANDLE _handle = INVALID_HANDLE_VALUE) : handle(_handle) {}

~AutoHandle() {
::CloseHandle(handle); // succeeds if handle == INVALID_HANDLE_VALUE
handle = INVALID_HANDLE_VALUE;
}

HANDLE handle;
};

string GetLastErrorString(const string& cause);

// Computes a path suitable as the executable part in CreateProcessA's cmdline.
Expand Down
4 changes: 2 additions & 2 deletions src/test/native/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ cc_test(
}),
deps = select({
"//src:windows": [
"//src/main/native:windows_jni_utils",
"//src/main/native:windows_jni_lib",
"//third_party:gtest",
],
"//src:windows_msvc": [
"//src/main/native:windows_jni_utils",
"//src/main/native:windows_jni_lib",
"//third_party:gtest",
],
"//conditions:default": [],
Expand Down

0 comments on commit e5f4f92

Please sign in to comment.