2015-02-02 07:56:45 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2015-02-02 21:04:43 +00:00
|
|
|
|
2023-08-30 16:46:17 +00:00
|
|
|
#define test_assert(_expr) \
|
|
|
|
if (!(_expr)) { \
|
|
|
|
__assert(__func__, __FILE__, __LINE__, #_expr); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-02 21:04:43 +00:00
|
|
|
pthread_mutex_t mtx;
|
2015-02-02 21:13:59 +00:00
|
|
|
pthread_cond_t cnd;
|
2015-02-02 07:56:45 +00:00
|
|
|
|
|
|
|
void *
|
|
|
|
thread_simple(void *arg)
|
|
|
|
{
|
|
|
|
printf("thread_simple %p!\n", arg);
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2015-02-02 21:04:43 +00:00
|
|
|
void *
|
|
|
|
thread_lock(void *arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
status = pthread_mutex_lock(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
pthread_yield();
|
|
|
|
status = pthread_mutex_unlock(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-02 21:13:59 +00:00
|
|
|
void *
|
|
|
|
thread_cond(void *arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
status = pthread_cond_wait(&cnd, NULL);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
status = pthread_cond_signal(&cnd);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-02 07:56:45 +00:00
|
|
|
int
|
|
|
|
main(int argc, const char *argv[])
|
|
|
|
{
|
2015-02-02 21:04:43 +00:00
|
|
|
int i;
|
2015-02-02 07:56:45 +00:00
|
|
|
int status;
|
|
|
|
pthread_t thr;
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
printf("PThread Test\n");
|
|
|
|
|
2015-02-02 21:13:59 +00:00
|
|
|
// Simple thread Test
|
2023-08-30 16:46:17 +00:00
|
|
|
printf("simple test: ");
|
2015-02-02 07:56:45 +00:00
|
|
|
status = pthread_create(&thr, NULL, thread_simple, NULL);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 07:56:45 +00:00
|
|
|
status = pthread_join(thr, &result);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
|
|
|
test_assert(result == NULL);
|
|
|
|
printf("OK\n");
|
2015-02-02 07:56:45 +00:00
|
|
|
|
2015-02-02 21:13:59 +00:00
|
|
|
// Return value Test
|
2023-08-30 16:46:17 +00:00
|
|
|
printf("return value test: ");
|
2015-02-02 07:56:45 +00:00
|
|
|
status = pthread_create(&thr, NULL, thread_simple, (void *)1);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 07:56:45 +00:00
|
|
|
status = pthread_join(thr, &result);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
|
|
|
test_assert(result == (void *)1);
|
|
|
|
printf("OK\n");
|
2015-02-02 07:56:45 +00:00
|
|
|
|
2015-02-02 21:13:59 +00:00
|
|
|
// Mutex Test
|
2023-08-30 16:46:17 +00:00
|
|
|
printf("simple mutex lock test: ");
|
2015-02-02 21:04:43 +00:00
|
|
|
status = pthread_mutex_init(&mtx, NULL);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
status = pthread_mutex_lock(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
status = pthread_mutex_unlock(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
status = pthread_mutex_destroy(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
|
|
|
printf("OK\n");
|
2015-02-02 21:04:43 +00:00
|
|
|
|
|
|
|
// Mutex Contention Test
|
2023-08-30 16:46:17 +00:00
|
|
|
printf("contended mutex lock test: ");
|
2015-02-02 21:04:43 +00:00
|
|
|
pthread_mutex_init(&mtx, NULL);
|
2015-02-02 21:16:37 +00:00
|
|
|
status = pthread_create(&thr, NULL, thread_lock, (void *)1);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
status = pthread_mutex_lock(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
pthread_yield();
|
|
|
|
pthread_mutex_unlock(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
}
|
|
|
|
status = pthread_join(thr, &result);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:04:43 +00:00
|
|
|
status = pthread_mutex_destroy(&mtx);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
|
|
|
printf("OK\n");
|
2015-02-02 21:13:59 +00:00
|
|
|
|
|
|
|
// Condition Variable Test
|
2023-08-30 16:46:17 +00:00
|
|
|
printf("simple condition variable test: ");
|
2015-02-02 21:13:59 +00:00
|
|
|
status = pthread_cond_init(&cnd, NULL);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
status = pthread_cond_signal(&cnd);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
status = pthread_cond_wait(&cnd, NULL);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
status = pthread_cond_destroy(&cnd);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
|
|
|
printf("OK\n");
|
2015-02-02 21:13:59 +00:00
|
|
|
|
2023-08-30 16:46:17 +00:00
|
|
|
printf("threaded condition variable test: OK");
|
2015-02-02 21:13:59 +00:00
|
|
|
status = pthread_cond_init(&cnd, NULL);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
status = pthread_cond_signal(&cnd);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
status = pthread_cond_wait(&cnd, NULL);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
}
|
|
|
|
status = pthread_cond_destroy(&cnd);
|
2023-08-30 16:46:17 +00:00
|
|
|
test_assert(status == 0);
|
2015-02-02 21:13:59 +00:00
|
|
|
printf("\n");
|
2015-02-02 19:55:34 +00:00
|
|
|
|
2015-02-02 07:56:45 +00:00
|
|
|
printf("Success!\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|