I need to come up with the P/Invoke signature for the Linux sched_setaffinity function, to be called from C# code running on Mono.
int sched_setaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
The ThreadAffinity class in the "open-hardware-monitor" project defines it as:
[DllImport(LIBC)]
public static extern int sched_setaffinity(int pid, IntPtr maskSize,
ref ulong mask);
However, the Mono.LibRt project uses a different signature:
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="sched_setaffinity")]
protected static extern int sched_setaffinity (long pid, int num_bytes,
byte[] affinity);
Are both signatures correct, or is one erroneous? Are they portable across 32- and 64-bit architectures?
I assume that the first argument should be int, not long, since pid_t is a signed 32-bit integer on both 32- and 64-bit platforms (per Joe Shaw). Similarly, the second should be UIntPtr or IntPtr per Patrick McDonald. However, I don't know how to handle the third parameter.
Edit: Building on Simon Mourier's comment: I gather, from the cpu_set_t * type, that the third parameter should be a pointer. Does ref ulong qualify? I'm tempted to use that type because it makes the macros easier to implement; for example, a mask for a single processor id can be computed as:
ulong mask = 1UL << id;
I've found a definition for cpu_set_t in this sched.h, where it's implemented as an array of unsigned long int.