Skip to content

Commit

Permalink
tests: posix: fix semaphore.h test
Browse files Browse the repository at this point in the history
Previous implementation was buggy, destroying
resources with pending thread and race conditions.

New version tests basic functionality with 2
threads and basic synchronization.

Signed-off-by: Juan Manuel Torres Palma <[email protected]>
  • Loading branch information
jmtorrespalma authored and andrewboie committed Mar 21, 2018
1 parent 342da7a commit 4aa4905
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions tests/posix/semaphore/src/sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ static K_THREAD_STACK_DEFINE(stack, STACK_SIZE);

static void *foo_func(void *p1)
{
zassert_false(sem_wait(&sema), "sem_wait failed\n");
printk("Print me after taking semaphore!\n");
printk("Child thread running\n");
zassert_false(sem_post(&sema), "sem_post failed\n");
return NULL;
}
Expand All @@ -39,26 +38,19 @@ static void test_sema(void)
pthread_attr_setschedpolicy(&attr, schedpolicy);
pthread_attr_setschedparam(&attr, &schedparam);

zassert_equal(sem_init(&sema, 1, 1), -1, "pshared is not 0\n");
zassert_equal(errno, ENOSYS, NULL);

zassert_equal(sem_init(&sema, 0, (CONFIG_SEM_VALUE_MAX + 1)), -1,
"value larger than %d\n", CONFIG_SEM_VALUE_MAX);
zassert_equal(errno, EINVAL, NULL);

zassert_false(sem_init(&sema, 0, 1), "sem_init failed\n");
zassert_false(sem_init(&sema, 0, 0), "sem_init failed\n");

zassert_equal(sem_getvalue(&sema, &val), 0, NULL);
zassert_equal(val, 1, NULL);
zassert_equal(val, 0, NULL);

pthread_create(&newthread, &attr, foo_func, NULL);
zassert_false(sem_wait(&sema), "sem_wait failed\n");

printk("after releasing sem\n");
zassert_false(sem_trywait(&sema), "sem_wait failed\n");
printk("After taking semaphore second time\n");

zassert_false(sem_post(&sema), "sem_post failed\n");

printk("Parent thread unlocked\n");
zassert_false(sem_destroy(&sema), "sema is not destroyed\n");
}

Expand Down

0 comments on commit 4aa4905

Please sign in to comment.