Skip to content

Commit

Permalink
Merge pull request #1167 from dbwiddis/affinity
Browse files Browse the repository at this point in the history
Add Kernel32#GetProcessAffinityMask
  • Loading branch information
matthiasblaesing authored Mar 3, 2020
2 parents 9c40da4 + 95c1e06 commit 9cc7c06
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next Release (5.6.0)
Features
--------
* [#1160](https://github.com/java-native-access/jna/issues/1160): Make FileUtils#moveToTrash a varargs method - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1167](https://github.com/java-native-access/jna/pull/1167): Add `c.s.j.p.win32.Kernel32.GetProcessAffinityMask` - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
Expand Down
38 changes: 36 additions & 2 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,48 @@ boolean ReadFile(HANDLE hFile, byte[] lpBuffer, int nNumberOfBytesToRead,
*/
int GetProcessVersion(int processId);

/**
* Retrieves the process affinity mask for the specified process and the system
* affinity mask for the system.
*
* @param hProcess
* A handle to the process whose affinity mask is desired.
* <p>
* This handle must have the {@link WinNT#PROCESS_QUERY_INFORMATION}
* or {@link WinNT#PROCESS_QUERY_LIMITED_INFORMATION} access right.
* @param lpProcessAffinityMask
* A pointer to a variable that receives the affinity mask for the
* specified process.
* @param lpSystemAffinityMask
* A pointer to a variable that receives the affinity mask for the
* system.
* @return If the function succeeds, returns {@code true} and the function sets
* the variables pointed to by {@code lpProcessAffinityMask} and
* {@code lpSystemAffinityMask} to the appropriate affinity masks.
* <p>
* On a system with more than 64 processors, if the threads of the
* calling process are in a single processor group, the function sets
* the variables pointed to by {@code lpProcessAffinityMask} and
* {@code lpSystemAffinityMask} to the process affinity mask and the
* processor mask of active logical processors for that group. If the
* calling process contains threads in multiple groups, the function
* returns zero for both affinity masks.
* <p>
* If the function fails, the return value is {@code false}, and the
* values of the variables pointed to by {@code lpProcessAffinityMask}
* and {@code lpSystemAffinityMask} are undefined. To get extended error
* information, call {@link #GetLastError()}.
*/
boolean GetProcessAffinityMask(HANDLE hProcess, ULONG_PTRByReference lpProcessAffinityMask,
ULONG_PTRByReference lpSystemAffinityMask);

/**
* Retrieves the termination status of the specified process.
*
* @param hProcess
* A handle to the process.
* @param lpExitCode
* A pointer to a variable to receive the process termination
* status.
* A pointer to a variable to receive the process termination status.
* @return If the function succeeds, the return value is nonzero.
*
* If the function fails, the return value is zero. To get extended
Expand Down
19 changes: 19 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTRByReference;
import com.sun.jna.platform.win32.Ntifs.REPARSE_DATA_BUFFER;
import com.sun.jna.platform.win32.Ntifs.SymbolicLinkReparseBuffer;
import com.sun.jna.platform.win32.WinBase.FILETIME;
Expand Down Expand Up @@ -463,6 +464,24 @@ public void testGetProcessIoCounters() {
}
}

public void testGetProcessAffinityMask() {
int myPid = Kernel32.INSTANCE.GetCurrentProcessId();
HANDLE pHandle = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION, false, myPid);
assertNotNull(pHandle);

ULONG_PTRByReference pProcessAffinity = new ULONG_PTRByReference();
ULONG_PTRByReference pSystemAffinity = new ULONG_PTRByReference();
assertTrue(Kernel32.INSTANCE.GetProcessAffinityMask(pHandle, pProcessAffinity, pSystemAffinity));

long processAffinity = pProcessAffinity.getValue().longValue();
long systemAffinity = pSystemAffinity.getValue().longValue();

assertEquals("Process affinity must be a subset of system affinity", processAffinity,
processAffinity & systemAffinity);
assertEquals("System affinity must be a superset of process affinity", systemAffinity,
processAffinity | systemAffinity);
}

public void testGetTempPath() {
char[] buffer = new char[WinDef.MAX_PATH];
assertTrue(Kernel32.INSTANCE.GetTempPath(new DWORD(WinDef.MAX_PATH), buffer).intValue() > 0);
Expand Down

0 comments on commit 9cc7c06

Please sign in to comment.