1717
1818package net .openhft .affinity .impl ;
1919
20- import com .sun .jna .LastErrorException ;
21- import com .sun .jna .Library ;
22- import com .sun .jna .Native ;
23- import com .sun .jna .PointerType ;
20+ import com .sun .jna .*;
2421import com .sun .jna .platform .win32 .Kernel32 ;
2522import com .sun .jna .platform .win32 .WinDef ;
23+ import com .sun .jna .platform .win32 .WinNT ;
2624import com .sun .jna .ptr .LongByReference ;
2725import net .openhft .affinity .IAffinity ;
2826import org .jetbrains .annotations .Nullable ;
@@ -86,7 +84,7 @@ public void setAffinity(final BitSet affinity) {
8684
8785 int pid = getTid ();
8886 try {
89- lib .SetThreadAffinityMask (pid , aff );
87+ lib .SetThreadAffinityMask (handle ( pid ) , aff );
9088 } catch (LastErrorException e ) {
9189 throw new IllegalStateException ("SetThreadAffinityMask((" + pid + ") , &(" + affinity + ") ) errorNo=" + e .getErrorCode (), e );
9290 }
@@ -104,7 +102,7 @@ private BitSet getAffinity0() {
104102 final LongByReference cpuset2 = new LongByReference (0 );
105103 try {
106104
107- final int ret = lib .GetProcessAffinityMask (- 1 , cpuset1 , cpuset2 );
105+ final int ret = lib .GetProcessAffinityMask (handle (- 1 ) , cpuset1 , cpuset2 );
108106 // Successful result is positive, according to the docs
109107 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683213%28v=vs.85%29.aspx
110108 if (ret <= 0 ) {
@@ -120,6 +118,10 @@ private BitSet getAffinity0() {
120118 return null ;
121119 }
122120
121+ private WinNT .HANDLE handle (int pid ) {
122+ return new WinNT .HANDLE (new Pointer (pid ));
123+ }
124+
123125 public int getTid () {
124126 final CLibrary lib = CLibrary .INSTANCE ;
125127
@@ -152,11 +154,11 @@ public int getThreadId() {
152154 * @author BegemoT
153155 */
154156 private interface CLibrary extends Library {
155- CLibrary INSTANCE = ( CLibrary ) Native .loadLibrary ("kernel32" , CLibrary .class );
157+ CLibrary INSTANCE = Native .load ("kernel32" , CLibrary .class );
156158
157- int GetProcessAffinityMask (final int pid , final PointerType lpProcessAffinityMask , final PointerType lpSystemAffinityMask ) throws LastErrorException ;
159+ int GetProcessAffinityMask (final WinNT . HANDLE pid , final PointerType lpProcessAffinityMask , final PointerType lpSystemAffinityMask ) throws LastErrorException ;
158160
159- void SetThreadAffinityMask (final int pid , final WinDef .DWORD lpProcessAffinityMask ) throws LastErrorException ;
161+ void SetThreadAffinityMask (final WinNT . HANDLE pid , final WinDef .DWORD lpProcessAffinityMask ) throws LastErrorException ;
160162
161163 int GetCurrentThread () throws LastErrorException ;
162164 }
0 commit comments