Browse Source

tests: expand semaphore test for multiple semaphores

cc430
Martine Lenders 8 years ago committed by Martine Lenders
parent
commit
4b92961f82
  1. 84
      tests/posix_semaphore/main.c

84
tests/posix_semaphore/main.c

@ -34,7 +34,8 @@ static char test2_thread_stack[SEMAPHORE_TEST_THREADS][THREAD_STACKSIZE_MAIN];
static msg_t main_msg_queue[SEMAPHORE_MSG_QUEUE_SIZE];
static msg_t test1_msg_queue[SEMAPHORE_MSG_QUEUE_SIZE];
static sem_t s;
static sem_t s1;
static sem_t s2;
static void *test1_second_thread(void *arg)
{
@ -42,7 +43,7 @@ static void *test1_second_thread(void *arg)
msg_init_queue(test1_msg_queue, SEMAPHORE_MSG_QUEUE_SIZE);
puts("second: sem_trywait");
if (sem_trywait(&s) == 0) {
if (sem_trywait(&s1) == 0) {
puts("second: sem_trywait failed");
}
@ -50,7 +51,7 @@ static void *test1_second_thread(void *arg)
puts("second: wait for post");
if (sem_wait(&s) != 1) {
if (sem_wait(&s1) != 1) {
puts("second: sem_wait failed");
}
@ -64,7 +65,7 @@ static void test1(void)
{
puts("first: sem_init");
if (sem_init(&s, 0, 0) != 0) {
if (sem_init(&s1, 0, 0) != 0) {
puts("first: sem_init failed");
}
@ -84,7 +85,7 @@ static void test1(void)
puts("first: sem_getvalue");
int val;
if (sem_getvalue(&s, &val) != 0 || val != 0) {
if (sem_getvalue(&s1, &val) != 0 || val != 0) {
puts("first: sem_getvalue FAILED");
}
@ -98,7 +99,7 @@ static void test1(void)
puts("first: sem_trywait");
if (sem_trywait(&s) != -1) {
if (sem_trywait(&s1) != -1) {
puts("first: sem_trywait FAILED");
}
@ -106,7 +107,7 @@ static void test1(void)
puts("first: sem_post");
if (sem_post(&s) != 1) {
if (sem_post(&s1) != 1) {
puts("first: sem_post FAILED");
}
@ -116,7 +117,7 @@ static void test1(void)
puts("first: sem_destroy");
if (sem_destroy(&s) != 0) {
if (sem_destroy(&s1) != 0) {
puts("first: sem_destroy FAILED");
}
@ -127,7 +128,7 @@ static void *priority_sema_thread(void *name)
{
msg_t msg_queue[SEMAPHORE_MSG_QUEUE_SIZE];
msg_init_queue(msg_queue, SEMAPHORE_MSG_QUEUE_SIZE);
sem_wait(&s);
sem_wait(&s1);
printf("Thread '%s' woke up.\n", (const char *) name);
return NULL;
}
@ -137,7 +138,7 @@ void test2(void)
{
puts("first: sem_init");
if (sem_init(&s, 0, 0) != 0) {
if (sem_init(&s1, 0, 0) != 0) {
puts("first: sem_init FAILED");
}
@ -165,11 +166,70 @@ void test2(void)
for (int i = 0; i < SEMAPHORE_TEST_THREADS; i++) {
printf("post no. %d\n", i);
sem_post(&s);
sem_post(&s1);
puts("Back in main thread.");
}
}
static void *test3_one_two_thread(void *arg)
{
msg_t msg_queue[SEMAPHORE_MSG_QUEUE_SIZE];
(void)arg;
msg_init_queue(msg_queue, SEMAPHORE_MSG_QUEUE_SIZE);
sem_wait(&s1);
puts("Thread 1 woke up after waiting for s1.");
sem_wait(&s2);
puts("Thread 1 woke up after waiting for s2.");
return NULL;
}
static void *test3_two_one_thread(void *arg)
{
msg_t msg_queue[SEMAPHORE_MSG_QUEUE_SIZE];
(void)arg;
msg_init_queue(msg_queue, SEMAPHORE_MSG_QUEUE_SIZE);
sem_wait(&s2);
puts("Thread 2 woke up after waiting for s2.");
sem_wait(&s1);
puts("Thread 2 woke up after waiting for s1.");
return NULL;
}
void test3(void)
{
puts("first: sem_init s1");
if (sem_init(&s1, 0, 0) != 0) {
puts("first: sem_init FAILED");
}
puts("first: sem_init s2");
if (sem_init(&s2, 0, 0) != 0) {
puts("first: sem_init FAILED");
}
puts("first: create thread 1");
if (thread_create(test2_thread_stack[0], sizeof(test2_thread_stack[0]),
THREAD_PRIORITY_MAIN - 1, CREATE_STACKTEST,
test3_one_two_thread, NULL, "thread 1") < 0) {
puts("first: thread create FAILED");
return;
}
puts("first: create thread 2");
if (thread_create(test2_thread_stack[1], sizeof(test2_thread_stack[1]),
THREAD_PRIORITY_MAIN - 1, CREATE_STACKTEST,
test3_two_one_thread, NULL, "thread 2") < 0) {
puts("first: thread create FAILED");
return;
}
puts("------------------------------------------");
puts("post s1");
sem_post(&s1);
puts("post s2");
sem_post(&s2);
puts("post s2");
sem_post(&s2);
puts("post s1");
sem_post(&s1);
}
int main(void)
{
msg_init_queue(main_msg_queue, SEMAPHORE_MSG_QUEUE_SIZE);
@ -177,6 +237,8 @@ int main(void)
test1();
puts("######################### TEST2:");
test2();
puts("######################### TEST3:");
test3();
puts("######################### DONE");
return 0;
}

Loading…
Cancel
Save