7 #include <TargetConditionals.h>
9 #include <pthread/pthread_spis.h>
11 #include <sys/sysctl.h>
13 #include "darwintest_defaults.h"
14 #include <darwintest_multiprocess.h>
17 pthread_mutex_t mutex
;
22 static void *test_thread(void *ptr
) {
25 struct context
*context
= ptr
;
34 str
= "pthread_mutex_lock";
35 res
= pthread_mutex_lock(&context
->mutex
);
37 str
= "pthread_mutex_trylock";
38 res
= pthread_mutex_trylock(&context
->mutex
);
41 if (try && res
== EBUSY
) {
44 T_ASSERT_POSIX_ZERO(res
, "[%ld] %s", context
->count
, str
);
47 old
= __sync_fetch_and_or(&context
->value
, 1);
49 T_FAIL("[%ld] OR %lx\n", context
->count
, old
);
52 old
= __sync_fetch_and_and(&context
->value
, 0);
54 T_FAIL("[%ld] AND %lx\n", context
->count
, old
);
57 res
= pthread_mutex_unlock(&context
->mutex
);
59 T_ASSERT_POSIX_ZERO(res
, "[%ld] pthread_mutex_lock", context
->count
);
61 } while (__sync_fetch_and_sub(&context
->count
, 1) > 0);
63 T_PASS("thread completed successfully");
68 T_DECL(mutex
, "pthread_mutex",
69 T_META_ALL_VALID_ARCHS(YES
))
71 struct context context
= {
72 .mutex
= PTHREAD_MUTEX_INITIALIZER
,
80 for (i
= 0; i
< threads
; ++i
) {
81 res
= pthread_create(&p
[i
], NULL
, test_thread
, &context
);
82 T_ASSERT_POSIX_ZERO(res
, "pthread_create()");
84 for (i
= 0; i
< threads
; ++i
) {
85 res
= pthread_join(p
[i
], NULL
);
86 T_ASSERT_POSIX_ZERO(res
, "pthread_join()");
91 check_process_default_mutex_policy(int expected_policy
)
93 pthread_mutexattr_t mattr
;
94 T_EXPECT_POSIX_ZERO(pthread_mutexattr_init(&mattr
), "pthread_mutexattr_init()");
97 T_EXPECT_POSIX_ZERO(pthread_mutexattr_getpolicy_np(&mattr
, &policy
),
98 "pthread_mutexattr_getpolicy_np()");
99 T_LOG("policy was %d", policy
);
100 T_EXPECT_EQ(policy
, expected_policy
, "Saw the expected default policy");
102 T_EXPECT_POSIX_ZERO(pthread_mutexattr_destroy(&mattr
), "pthread_mutexattr_destroy()");
105 T_DECL(mutex_default_policy
,
106 "Tests that the default mutex policy is fairshare")
108 check_process_default_mutex_policy(_PTHREAD_MUTEX_POLICY_FIRSTFIT
);
111 T_DECL(mutex_default_policy_sysctl
,
112 "Tests that setting the policy sysctl changes the default policy")
114 int firstfit_default
= _PTHREAD_MUTEX_POLICY_FIRSTFIT
;
116 sysctlbyname("kern.pthread_mutex_default_policy", NULL
, NULL
, &firstfit_default
, sizeof(firstfit_default
)),
117 "Changed the default policy sysctl to firstfit");
119 dt_helper_t helper
= dt_child_helper("mutex_default_policy_sysctl_helper");
120 dt_run_helpers(&helper
, 1, 5);
123 T_HELPER_DECL(mutex_default_policy_sysctl_helper
, "sysctl helper")
125 check_process_default_mutex_policy(_PTHREAD_MUTEX_POLICY_FIRSTFIT
);
127 int default_default
= _PTHREAD_MUTEX_POLICY_FAIRSHARE
;
129 sysctlbyname("kern.pthread_mutex_default_policy", NULL
, NULL
, &default_default
, sizeof(default_default
)),
130 "Restored the default policy to fairshare");
135 T_DECL(mutex_default_policy_envvar
,
136 "Tests that setting the policy environment variable changes the default policy",
137 T_META_ENVVAR("PTHREAD_MUTEX_DEFAULT_POLICY=3"))
139 check_process_default_mutex_policy(_PTHREAD_MUTEX_POLICY_FIRSTFIT
);