Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

only new up kernel32 instance once per jvm #329

Merged
merged 3 commits into from

2 participants

@rdp
rdp commented

as requested by Wayne.

@rdp

ok check out a2dee63
is it possible to fail to load kernel32 here?
Thanks!

@rdp
@rdp

ok @headius I believe the https://github.com/rdp/jruby/tree/static_kernel32 branch is ready to merge, see jruby#329

@BanzaiMan BanzaiMan merged commit 2b6f396 into from
@BanzaiMan
Owner

@wmeissner Did you try merging? I checked the permissions, and I believe you can.

@rdp

thanks @wmeissner and @BanzaiMan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 1, 2012
  1. @rdp
Commits on Oct 2, 2012
  1. @rdp
Commits on Oct 3, 2012
  1. @rdp
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 14 deletions.
  1. +21 −14 src/org/jruby/RubyProcess.java
View
35 src/org/jruby/RubyProcess.java
@@ -890,6 +890,14 @@ private static int parseSignalString(Ruby runtime, String value) {
int TerminateProcess(jnr.ffi.Pointer hProcess, int uExitCode);
}
+ private static class Kernel32Holder {
+ static Kernel32 kernel32Instance = Library.loadLibrary("kernel32", Kernel32.class); // instantiated lazily
+ }
+
+ static Kernel32 kernel32() {
+ return Kernel32Holder.kernel32Instance;
+ }
+
@Deprecated
public static IRubyObject kill(IRubyObject recv, IRubyObject[] args) {
return kill(recv.getRuntime(), args);
@@ -928,15 +936,14 @@ public static IRubyObject kill(Ruby runtime, IRubyObject[] args) {
int ERROR_INVALID_PARAMETER = 0x57;
int PROCESS_TERMINATE = 0x0001;
int STILL_ACTIVE = 259;
- Kernel32 libc = Library.loadLibrary("kernel32", Kernel32.class);
- jnr.ffi.Pointer status = Memory.allocate(Library.getRuntime(libc), 4);
+ jnr.ffi.Pointer status = Memory.allocate(Library.getRuntime(kernel32()), 4);
for (int i = 1; i < args.length; i++) {
- int pid = RubyNumeric.num2int(args[i]);
- if (signal == 0) {
- jnr.ffi.Pointer ptr = libc.OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
- if(ptr != null && ptr.address() != -1) {
+ int pid = RubyNumeric.num2int(args[i]);
+ if (signal == 0) {
+ jnr.ffi.Pointer ptr = kernel32().OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
+ if(ptr != null && ptr.address() != -1) {
try {
- if(libc.GetExitCodeProcess(ptr, status) == 0) {
+ if(kernel32().GetExitCodeProcess(ptr, status) == 0) {
throw runtime.newErrnoEPERMError("unable to call GetExitCodeProcess " + pid);
} else {
if(status.getInt(0) != STILL_ACTIVE) {
@@ -944,35 +951,35 @@ public static IRubyObject kill(Ruby runtime, IRubyObject[] args) {
}
}
} finally {
- libc.CloseHandle(ptr);
+ kernel32().CloseHandle(ptr);
}
} else {
- if (libc.GetLastError() == ERROR_INVALID_PARAMETER) {
+ if (kernel32().GetLastError() == ERROR_INVALID_PARAMETER) {
throw runtime.newErrnoESRCHError();
} else {
throw runtime.newErrnoEPERMError("Process does not exist " + pid);
}
}
} else if (signal == 9) { //SIGKILL
- jnr.ffi.Pointer ptr = libc.OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, 0, pid);
+ jnr.ffi.Pointer ptr = kernel32().OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, 0, pid);
if(ptr != null && ptr.address() != -1) {
try {
- if(libc.GetExitCodeProcess(ptr, status) == 0) {
+ if(kernel32().GetExitCodeProcess(ptr, status) == 0) {
throw runtime.newErrnoEPERMError("unable to call GetExitCodeProcess " + pid); // todo better error messages
} else {
if (status.getInt(0) == STILL_ACTIVE) {
- if (libc.TerminateProcess(ptr, 0) == 0) {
+ if (kernel32().TerminateProcess(ptr, 0) == 0) {
throw runtime.newErrnoEPERMError("unable to call TerminateProcess " + pid);
}
// success
}
}
} finally {
- libc.CloseHandle(ptr);
+ kernel32().CloseHandle(ptr);
}
} else {
- if (libc.GetLastError() == ERROR_INVALID_PARAMETER) {
+ if (kernel32().GetLastError() == ERROR_INVALID_PARAMETER) {
throw runtime.newErrnoESRCHError();
} else {
throw runtime.newErrnoEPERMError("Process does not exist " + pid);
Something went wrong with that request. Please try again.