#include #include #include #include #include #include #define PTHREAD_REQUIRE_STATUS(a, b) if ((errno = a) != b) \ err(1, "failed %s", # a) #define PTHREAD_REQUIRE(a) if ((errno = a) != 0) \ err(1, "failed %s", # a) #define ATF_REQUIRE_EQ(a, b) if (a != b) \ errx(1, "%s != %s", # a, # b) #define ATF_REQUIRE(a) if (!(a)) \ errx(1, "%s failed", # a) static pthread_mutex_t mutex; static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER; static int global_x; /* This code is used for verifying non-timed specific code */ static struct timespec ts_lengthy = { .tv_sec = UINT16_MAX, .tv_nsec = 0 }; /* This code is used for verifying timed-only specific code */ static struct timespec ts_shortlived = { .tv_sec = 0, .tv_nsec = 120 }; int main(void) { PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); printf("Before acquiring timed-mutex with timedlock\n"); PTHREAD_REQUIRE(pthread_mutex_timedlock(&mutex, &ts_lengthy)); printf("Before endeavor to reacquire timed-mutex (timeout expected)\n"); PTHREAD_REQUIRE_STATUS(pthread_mutex_timedlock(&mutex, &ts_shortlived), ETIMEDOUT); printf("Unlocking timed-mutex\n"); PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); return 0; }