Skip to content

Commit b6ccb24

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

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

ext/pcntl/pcntl.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,33 @@
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) ((void)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+
php_error_docref(NULL, E_WARNING, "cpuset_create: Insufficient memory"); \
73+
RETURN_FALSE; \
74+
} \
75+
cpuset_zero(mask); \
76+
} while(0)
77+
#define PCNTL_CPU_DESTROY(mask) cpuset_destroy(mask)
78+
#define HAVE_SCHED_SETAFFINITY 1
5279
#endif
5380

5481
#if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
@@ -1533,9 +1560,10 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
15331560
// 0 == getpid in this context, we're just saving a syscall
15341561
pid = pid_is_null ? 0 : pid;
15351562

1536-
CPU_ZERO(&mask);
1563+
PCNTL_CPU_ZERO(mask);
15371564

1538-
if (sched_getaffinity(pid, sizeof(mask), &mask) != 0) {
1565+
if (sched_getaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1566+
PCNTL_CPU_DESTROY(mask);
15391567
PCNTL_G(last_error) = errno;
15401568
switch (errno) {
15411569
case ESRCH:
@@ -1558,10 +1586,11 @@ PHP_FUNCTION(pcntl_getcpuaffinity)
15581586
array_init(return_value);
15591587

15601588
for (zend_ulong i = 0; i < maxcpus; i ++) {
1561-
if (CPU_ISSET(i, &mask)) {
1589+
if (PCNTL_CPU_ISSET(i, mask)) {
15621590
add_next_index_long(return_value, i);
15631591
}
15641592
}
1593+
PCNTL_CPU_DESTROY(mask);
15651594
}
15661595

15671596
PHP_FUNCTION(pcntl_setcpuaffinity)
@@ -1585,7 +1614,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15851614
// 0 == getpid in this context, we're just saving a syscall
15861615
pid = pid_is_null ? 0 : pid;
15871616
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
1588-
CPU_ZERO(&mask);
1617+
PCNTL_CPU_ZERO(mask);
15891618

15901619
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
15911620
ZVAL_DEREF(ncpu);
@@ -1595,6 +1624,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
15951624
zend_ulong tmp;
15961625
if (!ZEND_HANDLE_NUMERIC(Z_STR_P(ncpu), tmp)) {
15971626
zend_argument_value_error(2, "cpu id invalid value (%s)", ZSTR_VAL(Z_STR_P(ncpu)));
1627+
PCNTL_CPU_DESTROY(mask);
15981628
RETURN_THROWS();
15991629
}
16001630

@@ -1603,6 +1633,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
16031633
zend_string *wcpu = zval_get_string_func(ncpu);
16041634
zend_argument_value_error(2, "cpu id invalid type (%s)", ZSTR_VAL(wcpu));
16051635
zend_string_release(wcpu);
1636+
PCNTL_CPU_DESTROY(mask);
16061637
RETURN_THROWS();
16071638
}
16081639
} else {
@@ -1614,12 +1645,13 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
16141645
RETURN_THROWS();
16151646
}
16161647

1617-
if (!CPU_ISSET(cpu, &mask)) {
1618-
CPU_SET(cpu, &mask);
1648+
if (!PCNTL_CPU_ISSET(cpu, mask)) {
1649+
PCNTL_CPU_SET(cpu, mask);
16191650
}
16201651
} ZEND_HASH_FOREACH_END();
16211652

1622-
if (sched_setaffinity(pid, sizeof(mask), &mask) != 0) {
1653+
if (sched_setaffinity(pid, PCNTL_CPUSET_SIZE(mask), PCNTL_CPUSET(mask)) != 0) {
1654+
PCNTL_CPU_DESTROY(mask);
16231655
PCNTL_G(last_error) = errno;
16241656
switch (errno) {
16251657
case ESRCH:
@@ -1636,6 +1668,7 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
16361668
}
16371669
RETURN_FALSE;
16381670
} else {
1671+
PCNTL_CPU_DESTROY(mask);
16391672
RETURN_TRUE;
16401673
}
16411674
}

0 commit comments

Comments
 (0)