Skip to content
This repository

only new up kernel32 instance once per jvm #329

Merged
merged 3 commits into from over 1 year ago

2 participants

Roger Pack Hiro Asari
Roger Pack
rdp commented October 01, 2012

as requested by Wayne.

Roger Pack

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

Roger Pack
Roger Pack

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

Hiro Asari BanzaiMan merged commit 2b6f396 into from October 04, 2012
Hiro Asari BanzaiMan closed this October 04, 2012
Hiro Asari
Owner

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

Roger Pack
rdp commented October 11, 2012

thanks @wmeissner and @BanzaiMan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 21 additions and 14 deletions. Show diff stats Hide diff stats

  1. 35  src/org/jruby/RubyProcess.java
35  src/org/jruby/RubyProcess.java
@@ -890,6 +890,14 @@ private static int parseSignalString(Ruby runtime, String value) {
890 890
 		int TerminateProcess(jnr.ffi.Pointer hProcess, int uExitCode);
891 891
     }
892 892
 	
  893
+	private static class Kernel32Holder {
  894
+		static Kernel32 kernel32Instance = Library.loadLibrary("kernel32", Kernel32.class); // instantiated lazily
  895
+	}
  896
+
  897
+	static Kernel32 kernel32() {
  898
+		return Kernel32Holder.kernel32Instance;
  899
+	}
  900
+
893 901
     @Deprecated
894 902
     public static IRubyObject kill(IRubyObject recv, IRubyObject[] args) {
895 903
         return kill(recv.getRuntime(), args);
@@ -928,15 +936,14 @@ public static IRubyObject kill(Ruby runtime, IRubyObject[] args) {
928 936
 			int ERROR_INVALID_PARAMETER = 0x57;
929 937
 			int PROCESS_TERMINATE  = 0x0001;
930 938
 			int STILL_ACTIVE = 259;
931  
-			Kernel32 libc = Library.loadLibrary("kernel32", Kernel32.class);
932  
- 		    jnr.ffi.Pointer status = Memory.allocate(Library.getRuntime(libc), 4);
  939
+			jnr.ffi.Pointer status = Memory.allocate(Library.getRuntime(kernel32()), 4);
933 940
 		    for (int i = 1; i < args.length; i++) {
934  
-			    int pid = RubyNumeric.num2int(args[i]);
935  
-		        if (signal == 0) {	                
936  
-				    jnr.ffi.Pointer ptr = libc.OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
937  
-				    if(ptr != null && ptr.address() != -1) {
  941
+				int pid = RubyNumeric.num2int(args[i]);
  942
+				if (signal == 0) {
  943
+					jnr.ffi.Pointer ptr = kernel32().OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
  944
+					if(ptr != null && ptr.address() != -1) {
938 945
 					   try {
939  
-					       if(libc.GetExitCodeProcess(ptr, status) == 0) {
  946
+					       if(kernel32().GetExitCodeProcess(ptr, status) == 0) {
940 947
 					          throw runtime.newErrnoEPERMError("unable to call GetExitCodeProcess " + pid);
941 948
 					       } else {
942 949
 					           if(status.getInt(0) != STILL_ACTIVE) {
@@ -944,35 +951,35 @@ public static IRubyObject kill(Ruby runtime, IRubyObject[] args) {
944 951
                                }
945 952
 					       }
946 953
 					   } finally {
947  
-					     libc.CloseHandle(ptr);
  954
+					     kernel32().CloseHandle(ptr);
948 955
 					   }
949 956
 					   
950 957
 					} else {
951  
-					    if (libc.GetLastError() == ERROR_INVALID_PARAMETER) {
  958
+					    if (kernel32().GetLastError() == ERROR_INVALID_PARAMETER) {
952 959
 					        throw runtime.newErrnoESRCHError();
953 960
 					    } else {
954 961
 					        throw runtime.newErrnoEPERMError("Process does not exist " + pid);
955 962
 					    }
956 963
 					}
957 964
 			    } else if (signal == 9) { //SIGKILL
958  
-				    jnr.ffi.Pointer ptr = libc.OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, 0, pid);			      
  965
+				    jnr.ffi.Pointer ptr = kernel32().OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, 0, pid);
959 966
                     if(ptr != null && ptr.address() != -1) {
960 967
 					    try {
961  
-					        if(libc.GetExitCodeProcess(ptr, status) == 0) {
  968
+					        if(kernel32().GetExitCodeProcess(ptr, status) == 0) {
962 969
 					            throw runtime.newErrnoEPERMError("unable to call GetExitCodeProcess " + pid); // todo better error messages
963 970
 					        } else {
964 971
 					            if (status.getInt(0) == STILL_ACTIVE) {
965  
-						            if (libc.TerminateProcess(ptr, 0) == 0) {
  972
+						            if (kernel32().TerminateProcess(ptr, 0) == 0) {
966 973
 						               throw runtime.newErrnoEPERMError("unable to call TerminateProcess " + pid);
967 974
 						             }
968 975
                                      // success									 
969 976
 						        }
970 977
 					        }
971 978
 						} finally {						   
972  
-					       libc.CloseHandle(ptr);
  979
+					       kernel32().CloseHandle(ptr);
973 980
 					    }
974 981
 					} else {
975  
-					    if (libc.GetLastError() == ERROR_INVALID_PARAMETER) {
  982
+					    if (kernel32().GetLastError() == ERROR_INVALID_PARAMETER) {
976 983
 					        throw runtime.newErrnoESRCHError();
977 984
 					    } else {
978 985
 					        throw runtime.newErrnoEPERMError("Process does not exist " + pid);
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.