Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1705 from nemanja-boric-sociomantic/addsched
Browse files Browse the repository at this point in the history
Add Linux-specific sched_setaffinity and sched_getaffinity
  • Loading branch information
Михаил Страшун authored Dec 13, 2016
2 parents 0595fd5 + ab4cc15 commit c3f9cd5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions mak/COPY
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ COPY=\
$(IMPDIR)\core\sys\linux\fcntl.d \
$(IMPDIR)\core\sys\linux\ifaddrs.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 \
Expand Down
80 changes: 80 additions & 0 deletions src/core/sys/linux/sched.d
Original file line number Diff line number Diff line change
@@ -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);

3 changes: 3 additions & 0 deletions win32.mak
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,9 @@ $(IMPDIR)\core\sys\linux\ifaddrs.d : src\core\sys\linux\ifaddrs.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 $** $@

Expand Down
3 changes: 3 additions & 0 deletions win64.mak
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,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 $** $@

Expand Down

0 comments on commit c3f9cd5

Please sign in to comment.