From ab4cc15bd7452d11705fa57c426d054be4494704 Mon Sep 17 00:00:00 2001 From: Nemanja Boric Date: Tue, 29 Nov 2016 10:37:13 +0100 Subject: [PATCH] Add Linux-specific sched_setaffinity and sched_getaffinity --- mak/COPY | 1 + src/core/sys/linux/sched.d | 80 ++++++++++++++++++++++++++++++++++++++ win32.mak | 3 ++ win64.mak | 3 ++ 4 files changed, 87 insertions(+) create mode 100644 src/core/sys/linux/sched.d diff --git a/mak/COPY b/mak/COPY index 58ed65e7b1..346d761048 100644 --- a/mak/COPY +++ b/mak/COPY @@ -86,6 +86,7 @@ COPY=\ $(IMPDIR)\core\sys\linux\execinfo.d \ $(IMPDIR)\core\sys\linux\fcntl.d \ $(IMPDIR)\core\sys\linux\link.d \ + $(IMPDIR)\core\sys\linux\sched.d \ $(IMPDIR)\core\sys\linux\termios.d \ $(IMPDIR)\core\sys\linux\time.d \ $(IMPDIR)\core\sys\linux\timerfd.d \ diff --git a/src/core/sys/linux/sched.d b/src/core/sys/linux/sched.d new file mode 100644 index 0000000000..6dc6e33655 --- /dev/null +++ b/src/core/sys/linux/sched.d @@ -0,0 +1,80 @@ +/******************************************************************************* + + D binding for Linux specific scheduler control methods. + + Defines functions sched_setaffinity and sched_getaffinity and the data + types they operate on. + + Copyright: Copyright (c) 2016 Sociomantic Labs. All rights reserved. + License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0). + Authors: Nemanja Boric + +*******************************************************************************/ + + +module core.sys.linux.sched; + +import core.sys.posix.sched; +import core.sys.posix.config; +import core.sys.posix.sys.types; + +version (Linux): +extern (C): +@nogc: +nothrow: + + +private // helpers +{ + + /* Size definition for CPU sets. */ + enum + { + __CPU_SETSIZE = 1024, + __NCPUBITS = 8 * cpu_mask.sizeof, + } + + /* Macros */ + + /* Basic access functions. */ + size_t __CPUELT(size_t cpu) pure + { + return cpu / __NCPUBITS; + } + cpu_mask __CPUMASK(size_t cpu) pure + { + return 1UL << (cpu % __NCPUBITS); + } + + cpu_mask __CPU_SET_S(size_t cpu, size_t setsize, cpu_set_t* cpusetp) pure + { + if (cpu < 8 * setsize) + { + cpusetp.__bits[__CPUELT(cpu)] |= __CPUMASK(cpu); + return __CPUMASK(cpu); + } + + return 0; + } +} + +/// Type for array elements in 'cpu_set_t'. +alias c_ulong cpu_mask; + +/// Data structure to describe CPU mask. +struct cpu_set_t +{ + cpu_mask[__CPU_SETSIZE / __NCPUBITS] __bits; +} + +/// Access macros for `cpu_set' (missing a lot of them) + +cpu_mask CPU_SET(size_t cpu, cpu_set_t* cpusetp) pure +{ + return __CPU_SET_S(cpu, cpu_set_t.sizeof, cpusetp); +} + +/* Functions */ +int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask); +int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask); + diff --git a/win32.mak b/win32.mak index 109a44adc8..45b4c5af13 100644 --- a/win32.mak +++ b/win32.mak @@ -449,6 +449,9 @@ $(IMPDIR)\core\sys\linux\fcntl.d : src\core\sys\linux\fcntl.d $(IMPDIR)\core\sys\linux\link.d : src\core\sys\linux\link.d copy $** $@ +$(IMPDIR)\core\sys\linux\sched.d : src\core\sys\linux\sched.d + copy $** $@ + $(IMPDIR)\core\sys\linux\termios.d : src\core\sys\linux\termios.d copy $** $@ diff --git a/win64.mak b/win64.mak index ddded63a08..05b86558bf 100644 --- a/win64.mak +++ b/win64.mak @@ -457,6 +457,9 @@ $(IMPDIR)\core\sys\linux\fcntl.d : src\core\sys\linux\fcntl.d $(IMPDIR)\core\sys\linux\link.d : src\core\sys\linux\link.d copy $** $@ +$(IMPDIR)\core\sys\linux\sched.d : src\core\sys\linux\sched.d + copy $** $@ + $(IMPDIR)\core\sys\linux\termios.d : src\core\sys\linux\termios.d copy $** $@