Skip to content

Commit ed31aac

Browse files
committed
ext/pcntl: porting pcntl cpu affinity api to netbsd.
1 parent 8e4363d commit ed31aac

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

ext/pcntl/pcntl.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@
4949
#include <sys/cpuset.h>
5050
typedef cpuset_t cpu_set_t;
5151
#endif
52+
#define PCNTL_CPUSET(mask) &mask
53+
#define PCNTL_CPUSET_SIZE(mask) sizeof(mask)
54+
#define PCNTL_CPU_ISSET(i, mask) CPU_ISSET(i, &mask)
55+
#define PCNTL_CPU_SET(i, mask) CPU_SET(i, &mask)
56+
#define PCNTL_CPU_ZERO(mask) CPU_ZERO(&mask)
57+
#define PCNTL_CPU_DESTROY(mask) (0)
58+
#elif defined(__NetBSD__)
59+
#include <sys/syscall.h>
60+
#include <sched.h>
61+
typedef cpuset_t *cpu_set_t;
62+
#define sched_getaffinity(p, c, m) syscall(SYS__sched_getaffinity, p, 0, c, m)
63+
#define sched_setaffinity(p, c, m) syscall(SYS__sched_setaffinity, p, 0, c, m)
64+
#define PCNTL_CPUSET(mask) mask
65+
#define PCNTL_CPUSET_SIZE(mask) cpuset_size(mask)
66+
#define PCNTL_CPU_ISSET(i, mask) cpuset_isset((cpuid_t)i, mask)
67+
#define PCNTL_CPU_SET(i, mask) cpuset_set((cpuid_t)i, mask)
68+
#define PCNTL_CPU_ZERO(mask) \
69+
do { \
70+
mask = cpuset_create(); \
71+
if (UNEXPECTED(!mask)) { \
72+
RETURN_FALSE; \
73+
} \
74+
cpuset_zero(mask); \
75+
} while(0)
76+
#define PCNTL_CPU_DESTROY(mask) cpuset_destroy(mask)
77+
#define HAVE_SCHED_SETAFFINITY 1
5278
#endif
5379

5480
#if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
@@ -1533,9 +1559,10 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
15331559
// 0 == getpid in this context, we're just saving a syscall
15341560
pid = pid_is_null ? 0 : pid;
15351561

1536-
CPU_ZERO(&mask);
1562+
PCNTL_CPU_ZERO(mask);
15371563

1538-
if (sched_getaffinity(pid, sizeof(mask), &mask) != 0) {
1564+
if (sched_getaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1565+
PCNTL_CPU_DESTROY(mask);
15391566
PCNTL_G(last_error) = errno;
15401567
switch (errno) {
15411568
case ESRCH:
@@ -1558,10 +1585,11 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
15581585
array_init(return_value);
15591586

15601587
for (zend_ulong i = 0; i < maxcpus; i ++) {
1561-
if (CPU_ISSET(i, &mask)) {
1588+
if (PCNTL_CPU_ISSET(i, mask)) {
15621589
add_next_index_long(return_value, i);
15631590
}
15641591
}
1592+
PCNTL_CPU_DESTROY(mask);
15651593
}
15661594

15671595
PHP_FUNCTION(pcntl_setcpuaffinity)
@@ -1585,7 +1613,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15851613
// 0 == getpid in this context, we're just saving a syscall
15861614
pid = pid_is_null ? 0 : pid;
15871615
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
1588-
CPU_ZERO(&mask);
1616+
PCNTL_CPU_ZERO(mask);
15891617

15901618
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
15911619
ZVAL_DEREF(ncpu);
@@ -1595,6 +1623,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15951623
zend_ulong tmp;
15961624
if (!ZEND_HANDLE_NUMERIC(Z_STR_P(ncpu), tmp)) {
15971625
zend_argument_value_error(2, "cpu id invalid value (%s)", ZSTR_VAL(Z_STR_P(ncpu)));
1626+
PCNTL_CPU_DESTROY(mask);
15981627
RETURN_THROWS();
15991628
}
16001629

@@ -1603,6 +1632,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
16031632
zend_string *wcpu = zval_get_string_func(ncpu);
16041633
zend_argument_value_error(2, "cpu id invalid type (%s)", ZSTR_VAL(wcpu));
16051634
zend_string_release(wcpu);
1635+
PCNTL_CPU_DESTROY(mask);
16061636
RETURN_THROWS();
16071637
}
16081638
} else {
@@ -1614,12 +1644,13 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
16141644
RETURN_THROWS();
16151645
}
16161646

1617-
if (!CPU_ISSET(cpu, &mask)) {
1618-
CPU_SET(cpu, &mask);
1647+
if (!PCNTL_CPU_ISSET(cpu, mask)) {
1648+
PCNTL_CPU_SET(cpu, mask);
16191649
}
16201650
} ZEND_HASH_FOREACH_END();
16211651

1622-
if (sched_setaffinity(pid, sizeof(mask), &mask) != 0) {
1652+
if (sched_setaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1653+
PCNTL_CPU_DESTROY(mask);
16231654
PCNTL_G(last_error) = errno;
16241655
switch (errno) {
16251656
case ESRCH:
@@ -1636,6 +1667,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
16361667
}
16371668
RETURN_FALSE;
16381669
} else {
1670+
PCNTL_CPU_DESTROY(mask);
16391671
RETURN_TRUE;
16401672
}
16411673
}

0 commit comments

Comments
 (0)