1 module photon.freebsd.support; 2 version(FreeBSD): 3 import core.sys.posix.unistd; 4 import core.stdc.errno; 5 import core.stdc.stdlib; 6 import core.thread; 7 import core.stdc.config; 8 import core.sys.posix.pthread; 9 10 enum int MSG_DONTWAIT = 0x80; 11 enum int SOCK_NONBLOCK = 0x800; 12 13 extern(C) int eventfd(uint initial, int flags) nothrow; 14 extern(C) void perror(const(char) *s) nothrow; 15 16 T checked(T: ssize_t)(T value, const char* msg="unknown place") nothrow { 17 if (value < 0) { 18 perror(msg); 19 _exit(cast(int)-value); 20 } 21 return value; 22 } 23 24 ssize_t withErrorno(ssize_t resp) nothrow { 25 if(resp < 0) { 26 //logf("Syscall ret %d", resp); 27 errno = cast(int)-resp; 28 return -1; 29 } 30 else { 31 return resp; 32 } 33 } 34 35 void logf(string file = __FILE__, int line = __LINE__, T...)(string msg, T args) 36 { 37 debug(photon) { 38 try { 39 import std.stdio; 40 stderr.writefln(msg, args); 41 stderr.writefln("\tat %s:%s:[LWP:%s]", file, line, pthread_self()); 42 } 43 catch(Throwable t) { 44 abort(); 45 } 46 } 47 } 48 49 50 @nogc: 51 nothrow: 52 53 54 private // helpers 55 { 56 57 /* Size definition for CPU sets. */ 58 enum 59 { 60 __CPU_SETSIZE = 1024, 61 __NCPUBITS = 8 * cpu_mask.sizeof, 62 } 63 64 /* Macros */ 65 66 /* Basic access functions. */ 67 size_t __CPUELT()(size_t cpu) pure 68 { 69 return cpu / __NCPUBITS; 70 } 71 cpu_mask __CPUMASK()(size_t cpu) pure 72 { 73 return 1UL << (cpu % __NCPUBITS); 74 } 75 76 cpu_mask __CPU_SET_S()(size_t cpu, size_t setsize, cpu_set_t* cpusetp) pure 77 { 78 if (cpu < 8 * setsize) 79 { 80 cpusetp.__bits[__CPUELT(cpu)] |= __CPUMASK(cpu); 81 return __CPUMASK(cpu); 82 } 83 84 return 0; 85 } 86 } 87 88 /// Type for array elements in 'cpu_set_t'. 89 alias c_ulong cpu_mask; 90 91 /// Data structure to describe CPU mask. 92 struct cpu_set_t 93 { 94 cpu_mask[__CPU_SETSIZE / __NCPUBITS] __bits; 95 } 96 97 /// Access macros for 'cpu_set' (missing a lot of them) 98 99 cpu_mask CPU_SET()(size_t cpu, cpu_set_t* cpusetp) pure 100 { 101 return __CPU_SET_S(cpu, cpu_set_t.sizeof, cpusetp); 102 } 103 104 /* Functions */ 105 extern(C): 106 int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask); 107 int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask); 108