test/platform/pthread/thread/test_thread_cond_signal_wait.cpp
TEST(wait_signal_one)
{
    allocator* alloc = nullptr;
    thread* one = nullptr;
    thread* two = nullptr;
    thread_mutex* mut = nullptr;
    thread_cond* cond = nullptr;
    threadstuff ts;
    int waiters;
    int val;

    /* clear thread context. */
    memset(&ts, 0, sizeof(ts));

    /* we should be able to create a malloc allocator. */
    TEST_ASSERT(STATUS_SUCCESS == malloc_allocator_create(&alloc));

    /* we should be able to create a thread cond. */
    TEST_ASSERT(STATUS_SUCCESS == thread_cond_create(&cond, alloc));

    /* we should be able to create a thread mutex. */
    TEST_ASSERT(STATUS_SUCCESS == thread_mutex_create(&mut, alloc));

    /* copy these resources to ts. */
    ts.mut = mut;
    ts.cond = cond;

    /* precondition: val is 0. */
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(0 == val);

    /* precondition: there are no waiters. */
    TEST_ASSERT(STATUS_SUCCESS == read_waiters(&ts, &waiters));
    TEST_ASSERT(0 == waiters);

    /* create thread one. */
    TEST_ASSERT(
        STATUS_SUCCESS == thread_create(&one, alloc, 16384, &ts, &incr));

    /* wait until the thread is blocking. */
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 1));

    /* val is still zero. */
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(0 == val);

    /* create thread two. */
    TEST_ASSERT(
        STATUS_SUCCESS == thread_create(&two, alloc, 16384, &ts, &incr));

    /* wait until the thread is blocking. */
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 2));

    /* val is still zero. */
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(0 == val);

    /* signal a thread. */
    TEST_ASSERT(
        STATUS_SUCCESS == signal_one_thread(&ts));

    /* wait until the thread is unblocked. */
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 1));

    /* val is one. */
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(1 == val);

    /* signal a thread. */
    TEST_ASSERT(
        STATUS_SUCCESS == signal_one_thread(&ts));

    /* wait until the thread is unblocked. */
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 0));

    /* val is two. */
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(2 == val);

    /* release (join) thread one. */
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_resource_handle(one)));

    /* release (join) thread two. */
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_resource_handle(two)));

    /* release the mutex. */
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_mutex_resource_handle(mut)));

    /* release the condition variable. */
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_cond_resource_handle(cond)));

    /* clean up. */
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(allocator_resource_handle(alloc)));
}
/**
 * Create two threads. Signal one, then the other. Verify that a counter
 * increments each time.
 */
Create two threads. Signal one, then the other. Verify that a counter
increments each time.
Create two threads . Signal one then the other . Verify that a counter increments each time .
TEST(wait_signal_one)
{
    allocator* alloc = nullptr;
    thread* one = nullptr;
    thread* two = nullptr;
    thread_mutex* mut = nullptr;
    thread_cond* cond = nullptr;
    threadstuff ts;
    int waiters;
    int val;
    memset(&ts, 0, sizeof(ts));
    TEST_ASSERT(STATUS_SUCCESS == malloc_allocator_create(&alloc));
    TEST_ASSERT(STATUS_SUCCESS == thread_cond_create(&cond, alloc));
    TEST_ASSERT(STATUS_SUCCESS == thread_mutex_create(&mut, alloc));
    ts.mut = mut;
    ts.cond = cond;
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(0 == val);
    TEST_ASSERT(STATUS_SUCCESS == read_waiters(&ts, &waiters));
    TEST_ASSERT(0 == waiters);
    TEST_ASSERT(
        STATUS_SUCCESS == thread_create(&one, alloc, 16384, &ts, &incr));
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 1));
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(0 == val);
    TEST_ASSERT(
        STATUS_SUCCESS == thread_create(&two, alloc, 16384, &ts, &incr));
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 2));
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(0 == val);
    TEST_ASSERT(
        STATUS_SUCCESS == signal_one_thread(&ts));
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 1));
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(1 == val);
    TEST_ASSERT(
        STATUS_SUCCESS == signal_one_thread(&ts));
    TEST_ASSERT(STATUS_SUCCESS == wait_on_waiters(&ts, 0));
    TEST_ASSERT(STATUS_SUCCESS == read_val(&ts, &val));
    TEST_ASSERT(2 == val);
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_resource_handle(one)));
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_resource_handle(two)));
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_mutex_resource_handle(mut)));
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(thread_cond_resource_handle(cond)));
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(allocator_resource_handle(alloc)));
}
TEST ( wait_signal_one ) { allocator * alloc = nullptr ; thread * one = nullptr ; thread * two = nullptr ; thread_mutex * mut = nullptr ; thread_cond * cond = nullptr ; threadstuff ts ; int waiters ; int val ; memset ( & ts , 0 , sizeof ( ts ) ) ; TEST_ASSERT ( STATUS_SUCCESS == malloc_allocator_create ( & alloc ) ) ; TEST_ASSERT ( STATUS_SUCCESS == thread_cond_create ( & cond , alloc ) ) ; TEST_ASSERT ( STATUS_SUCCESS == thread_mutex_create ( & mut , alloc ) ) ; ts . mut = mut ; ts . cond = cond ; TEST_ASSERT ( STATUS_SUCCESS == read_val ( & ts , & val ) ) ; TEST_ASSERT ( 0 == val ) ; TEST_ASSERT ( STATUS_SUCCESS == read_waiters ( & ts , & waiters ) ) ; TEST_ASSERT ( 0 == waiters ) ; TEST_ASSERT ( STATUS_SUCCESS == thread_create ( & one , alloc , 16384 , & ts , & incr ) ) ; TEST_ASSERT ( STATUS_SUCCESS == wait_on_waiters ( & ts , 1 ) ) ; TEST_ASSERT ( STATUS_SUCCESS == read_val ( & ts , & val ) ) ; TEST_ASSERT ( 0 == val ) ; TEST_ASSERT ( STATUS_SUCCESS == thread_create ( & two , alloc , 16384 , & ts , & incr ) ) ; TEST_ASSERT ( STATUS_SUCCESS == wait_on_waiters ( & ts , 2 ) ) ; TEST_ASSERT ( STATUS_SUCCESS == read_val ( & ts , & val ) ) ; TEST_ASSERT ( 0 == val ) ; TEST_ASSERT ( STATUS_SUCCESS == signal_one_thread ( & ts ) ) ; TEST_ASSERT ( STATUS_SUCCESS == wait_on_waiters ( & ts , 1 ) ) ; TEST_ASSERT ( STATUS_SUCCESS == read_val ( & ts , & val ) ) ; TEST_ASSERT ( 1 == val ) ; TEST_ASSERT ( STATUS_SUCCESS == signal_one_thread ( & ts ) ) ; TEST_ASSERT ( STATUS_SUCCESS == wait_on_waiters ( & ts , 0 ) ) ; TEST_ASSERT ( STATUS_SUCCESS == read_val ( & ts , & val ) ) ; TEST_ASSERT ( 2 == val ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( thread_resource_handle ( one ) ) ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( thread_resource_handle ( two ) ) ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( thread_mutex_resource_handle ( mut ) ) ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( thread_cond_resource_handle ( cond ) ) ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( allocator_resource_handle ( alloc ) ) ) ; }
Create two threads.
Create two threads .
/* clear thread context. */ /* we should be able to create a malloc allocator. */ /* we should be able to create a thread cond. */ /* we should be able to create a thread mutex. */ /* copy these resources to ts. */ /* precondition: val is 0. */ /* precondition: there are no waiters. */ /* create thread one. */ /* wait until the thread is blocking. */ /* val is still zero. */ /* create thread two. */ /* wait until the thread is blocking. */ /* val is still zero. */ /* signal a thread. */ /* wait until the thread is unblocked. */ /* val is one. */ /* signal a thread. */ /* wait until the thread is unblocked. */ /* val is two. */ /* release (join) thread one. */ /* release (join) thread two. */ /* release the mutex. */ /* release the condition variable. */ /* clean up. */