Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/lifecycle1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
#include "test.h"
#include "implement.h"

static pthread_key_t implicit_key;
static int implicit_destroyed;

static void destroy_implicit(void *arg)
{
assert(arg == &implicit_destroyed);
assert(implicit_destroyed == 0);
++implicit_destroyed;

/* POSIX permits deleting a key from its destructor. Release the key
* itself as well as its value before the runtime discards OSAL TLS. */
assert(pthread_key_delete(implicit_key) == 0);
implicit_key = NULL;
}

static void *worker(void *arg)
{
return arg;
Expand Down Expand Up @@ -38,14 +53,20 @@ int pthread_test_lifecycle1(void)
/* The joined thread populates the reuse pool. Teardown must discard
* it and the calling native thread's implicit pthread record. */
assert(pte_threadReuseTop != PTE_THREAD_REUSE_EMPTY);
implicit_destroyed = 0;
assert(pthread_key_create(&implicit_key, destroy_implicit) == 0);
assert(pthread_setspecific(implicit_key, &implicit_destroyed) == 0);
pthread_terminate();
assert(implicit_destroyed == 1);
assert(implicit_key == NULL);
assert(!pte_processInitialized);
assert(pte_selfThreadKey == NULL);
assert(pte_cleanupKey == NULL);
assert(pte_threadReuseTop == PTE_THREAD_REUSE_EMPTY);
assert(pte_threadReuseBottom == PTE_THREAD_REUSE_EMPTY);
pthread_terminate();
assert(!pte_processInitialized);
assert(implicit_destroyed == 1);

assert(pthread_init());
assert(pte_osThreadGetHandle() == native);
Expand Down
8 changes: 4 additions & 4 deletions tests/once4.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ int pthread_test_once4()
numThreads.i = 0;

// pte_osMutexCreate(&print_lock);
// pte_osMutexCreate(&numThreads.cs);
// pte_osMutexCreate(&numOnce.cs);
assert(pte_osMutexCreate(&numThreads.cs) == PTE_OS_OK);
assert(pte_osMutexCreate(&numOnce.cs) == PTE_OS_OK);

/*
* Set the priority class to realtime - otherwise normal
Expand Down Expand Up @@ -180,8 +180,8 @@ int pthread_test_once4()
assert(numOnce.i == NUM_ONCE * NUM_THREADS);
assert(numThreads.i == 0);

// pte_osMutexDelete(&numOnce.cs);
// pte_osMutexDelete(&numThreads.cs);
assert(pte_osMutexDelete(numOnce.cs) == PTE_OS_OK);
assert(pte_osMutexDelete(numThreads.cs) == PTE_OS_OK);
// pte_osMutexDelete(&print_lock);

return 0;
Expand Down
51 changes: 49 additions & 2 deletions tests/tsd1.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
* - keys are created for each existing thread including the main thread
* - keys are created for newly created threads
* - keys are thread specific
* - destroy routine is called on each thread exit including the main thread
* - destroy routine is called on each worker thread exit
* - main-thread TSD remains valid until the key is deleted
*
* Features Tested:
* -
Expand Down Expand Up @@ -149,6 +150,44 @@ mythread(void * arg)
/* Exiting the thread will call the key destructor. */
}

/* A deleted key's slot must be NULL when reused, even in threads that
* remain alive across deletion and recreation. No destructor is needed. */
static void *reuse_thread(void *arg)
{
assert(pthread_setspecific(key, arg) == 0);
(void) pthread_barrier_wait(&startBarrier);
(void) pthread_barrier_wait(&startBarrier);
assert(pthread_getspecific(key) == NULL);
assert(pthread_setspecific(key, arg) == 0);
assert(pthread_getspecific(key) == arg);
return NULL;
}

static void test_key_reuse(void)
{
pthread_t threads[2];
int values[3];
int i;

assert(pthread_barrier_init(&startBarrier, NULL, 3) == 0);
assert(pthread_key_create(&key, NULL) == 0);
assert(pthread_setspecific(key, &values[0]) == 0);
for (i = 0; i < 2; ++i)
assert(pthread_create(&threads[i], NULL, reuse_thread, &values[i + 1]) == 0);
(void) pthread_barrier_wait(&startBarrier);

assert(pthread_key_delete(key) == 0);
assert(pthread_key_create(&key, NULL) == 0);
assert(pthread_getspecific(key) == NULL);
(void) pthread_barrier_wait(&startBarrier);
for (i = 0; i < 2; ++i)
assert(pthread_join(threads[i], NULL) == 0);

assert(pthread_key_delete(key) == 0);
key = NULL;
assert(pthread_barrier_destroy(&startBarrier) == 0);
}

int pthread_test_tsd1()
{
int i;
Expand Down Expand Up @@ -202,7 +241,14 @@ int pthread_test_tsd1()
assert(pthread_join(thread[i], (void **) &result) == 0);
}

// assert(pthread_key_delete(key) == 0);
/* Do not leave a key associated with accesscount[0] across suite runs.
* Key deletion must not call its destructor. The implicit main-thread
* destructor is exercised separately by lifecycle1. */
assert(accesscount[0] == 1);
assert(pthread_getspecific(key) == &accesscount[0]);
assert(pthread_key_delete(key) == 0);
assert(accesscount[0] == 1);
key = NULL;

assert(pthread_barrier_destroy(&startBarrier) == 0);

Expand All @@ -222,6 +268,7 @@ int pthread_test_tsd1()
}

assert(fail == 0);
test_key_reuse();

return (fail);
}