Skip to content

Commit 96ac07d

Browse files
authored
Merge pull request #877 from al45tair/eng/PR-152301793
[Tests] Fix Dispatch tests to work in container environments.
2 parents 4029973 + 3ec2b84 commit 96ac07d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

tests/dispatch_apply.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <dispatch/dispatch.h>
2222
#include <stdio.h>
2323
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
24+
#include <pthread.h>
2425
#include <unistd.h>
2526
#ifdef __ANDROID__
2627
#include <linux/sysctl.h>
@@ -83,6 +84,14 @@ static void test_apply_contended(dispatch_queue_t dq)
8384
uint32_t activecpu;
8485
#if defined(__linux__) || defined(__OpenBSD__) || defined(__FreeBSD__)
8586
activecpu = (uint32_t)sysconf(_SC_NPROCESSORS_ONLN);
87+
88+
#if defined(__linux__) && __USE_GNU
89+
cpu_set_t cpuset;
90+
if (pthread_getaffinity_np(pthread_self(),
91+
sizeof(cpu_set_t),
92+
&cpuset) == 0)
93+
activecpu = (uint32_t)CPU_COUNT(&cpuset);
94+
#endif
8695
#elif defined(_WIN32)
8796
SYSTEM_INFO si;
8897
GetSystemInfo(&si);

tests/dispatch_workqueue.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include <bsdtests.h>
22
#include "dispatch_test.h"
33

4+
#if defined(__linux__)
5+
// For pthread_getaffinity_np()
6+
#include <pthread.h>
7+
#endif
8+
49
struct test_context {
510
uint32_t ncpu;
611
int flag;
@@ -35,6 +40,15 @@ activecpu(void)
3540
uint32_t activecpu;
3641
#if defined(__linux__) || defined(__OpenBSD__)
3742
activecpu = (uint32_t)sysconf(_SC_NPROCESSORS_ONLN);
43+
44+
#if defined(__linux__) && __USE_GNU
45+
cpu_set_t cpuset;
46+
if (pthread_getaffinity_np(pthread_self(),
47+
sizeof(cpu_set_t),
48+
&cpuset) == 0)
49+
activecpu = (uint32_t)CPU_COUNT(&cpuset);
50+
#endif
51+
3852
#elif defined(_WIN32)
3953
SYSTEM_INFO si;
4054
GetSystemInfo(&si);

0 commit comments

Comments
 (0)