Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

remain: use C11 mutex and add EALREADY thread test #68

Merged
merged 1 commit into from
May 15, 2022
Merged
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
31 changes: 12 additions & 19 deletions src/remain.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

#include <string.h>
#include <re.h>
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include "test.h"


Expand All @@ -17,12 +14,9 @@
#include <re_dbg.h>


#ifdef HAVE_PTHREAD


struct data {
thrd_t tid;
pthread_mutex_t mutex;
mtx_t mutex;
bool thread_started;
bool thread_exited;
unsigned tmr_called;
Expand All @@ -35,7 +29,7 @@ static void tmr_handler(void *arg)
struct data *data = arg;
int err = 0;

pthread_mutex_lock(&data->mutex);
mtx_lock(&data->mutex);

/* verify that timer is called from the new thread */
TEST_ASSERT(0 != thrd_equal(data->tid, thrd_current()));
Expand All @@ -46,7 +40,7 @@ static void tmr_handler(void *arg)
if (err)
data->err = err;

pthread_mutex_unlock(&data->mutex);
mtx_unlock(&data->mutex);

re_cancel();
}
Expand All @@ -69,14 +63,18 @@ static int thread_handler(void *arg)
return 0;
}

err = re_thread_init();
TEST_EQUALS(EALREADY, err);

tmr_start(&tmr, 1, tmr_handler, data);

/* run the main loop now */
err = re_main(NULL);

out:
if (err) {
data->err = err;
}

tmr_cancel(&tmr);

/* cleanup */
Expand All @@ -96,7 +94,7 @@ static int test_remain_thread(void)

memset(&data, 0, sizeof(data));

pthread_mutex_init(&data.mutex, NULL);
mtx_init(&data.mutex, mtx_plain);

err = thrd_create(&data.tid, thread_handler, &data);
if (err)
Expand All @@ -105,14 +103,14 @@ static int test_remain_thread(void)
/* wait for timer to be called */
for (i=0; i<500; i++) {

pthread_mutex_lock(&data.mutex);
mtx_lock(&data.mutex);

if (data.tmr_called || data.err) {
pthread_mutex_unlock(&data.mutex);
mtx_unlock(&data.mutex);
break;
}

pthread_mutex_unlock(&data.mutex);
mtx_unlock(&data.mutex);

sys_msleep(1);
}
Expand All @@ -133,18 +131,13 @@ static int test_remain_thread(void)
}


#endif


int test_remain(void)
{
int err = 0;

#ifdef HAVE_PTHREAD
err = test_remain_thread();
if (err)
return err;
#endif

return err;
}