tests/posix: test pthread_condattr
@@ -36,8 +36,13 @@ static void test_broadcast_wakes_all() {
}
static void test_timedwait_timedout() {
+ // Use CLOCK_MONOTONIC.
+ pthread_condattr_t attr;
+ pthread_condattr_init(&attr);
+ assert(!pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
+
struct timespec before_now;
- assert(!clock_gettime(CLOCK_REALTIME, &before_now));
+ assert(!clock_gettime(CLOCK_MONOTONIC, &before_now));
before_now.tv_nsec -= 10000;
pthread_mutex_lock(&mtx);
@@ -47,7 +52,7 @@ static void test_timedwait_timedout() {
long nanos_per_second = 1000000000;
struct timespec after_now;
- assert(!clock_gettime(CLOCK_REALTIME, &after_now));
+ assert(!clock_gettime(CLOCK_MONOTONIC, &after_now));
after_now.tv_nsec += nanos_per_second / 10; // 100ms
if (after_now.tv_nsec >= nanos_per_second) {
after_now.tv_nsec -= nanos_per_second;
@@ -66,7 +71,36 @@ static void test_timedwait_timedout() {
pthread_mutex_unlock(&mtx);
}
+static void test_attr() {
+ pthread_condattr_t attr;
+ pthread_condattr_init(&attr);
+
+ clockid_t clock;
+ assert(!pthread_condattr_getclock(&attr, &clock));
+ assert(clock == CLOCK_REALTIME);
+ assert(!pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
+ assert(!pthread_condattr_getclock(&attr, &clock));
+ assert(clock == CLOCK_MONOTONIC);
+
+ int pshared;
+ assert(!pthread_condattr_getpshared(&attr, &pshared));
+ assert(pshared == PTHREAD_PROCESS_PRIVATE);
+ assert(!pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
+ assert(!pthread_condattr_getpshared(&attr, &pshared));
+ assert(pshared == PTHREAD_PROCESS_SHARED);
+
+ pthread_condattr_destroy(&attr);
+ pthread_condattr_init(&attr);
+
+ // Make sure that we can create a pthread_cond_t with an attr.
+ pthread_cond_t cond;
+ pthread_cond_init(&cond, &attr);
+ pthread_cond_destroy(&cond);
+ pthread_condattr_destroy(&attr);
+}
+
int main() {
+ test_attr();
test_broadcast_wakes_all();
test_timedwait_timedout();
}
