And implemented the multithreading stuff on Unix and Darwin. We can finally continue re-adding stuff to libui!

This commit is contained in:
Pietro Gagliardi 2019-04-28 21:22:11 -04:00
parent df8eadb980
commit 74468bb38f
3 changed files with 56 additions and 2 deletions

View File

@ -157,7 +157,7 @@ static void queueOrderThread(void *data)
testingTest(QueueMain_DifferentThreadSequence)
{
testingThread *thread;
uint32_t flag;
uint32_t flag = 1; // make sure it's initialized just in case
thread = testingNewThread(queueOrderThread, &flag);
timeout_uiMain(t, 5 * testingNsecPerSec, 0);

View File

@ -20,7 +20,10 @@ else
]
endif
libui_test_deps = []
libui_test_deps = [
dependency('threads',
required: true),
]
if libui_OS == 'windows'
libui_test_manifest = 'test.manifest'
if libui_mode == 'static'

View File

@ -3,8 +3,11 @@
#include <inttypes.h>
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include "testing.h"
// TODO testingTimers after this fails are wrong on GTK+
@ -54,3 +57,51 @@ out:
if (failNowOnError)
testingTFailNow(t);
}
void testingSleep(int64_t nsec)
{
struct timespec rqtp;
// TODO check errors, possibly falling back to usleep, setitimer/pause, or even sleep
rqtp.tv_sec = nsec / testingNsecPerSec;
rqtp.tv_nsec = nsec % testingNsecPerSec;
nanosleep(&rqtp, NULL);
}
struct testingThread {
pthread_t thread;
void (*f)(void *data);
void *data;
};
static void *threadThreadProc(void *data)
{
testingThread *t = (testingThread *) data;
(*(t->f))(t->data);
return NULL;
}
testingThread *testingNewThread(void (*f)(void *data), void *data)
{
testingThread *t;
t = malloc(sizeof (testingThread));
// TODO check error
memset(t, 0, sizeof (testingThread));
t->f = f;
t->data = data;
// TODO check error
pthread_create(&(t->thread), NULL, threadThreadProc, t);
return t;
}
void testingThreadWaitAndFree(testingThread *t)
{
// TODO check errors
pthread_join(t->thread, NULL);
// TODO end check errors
// TODO do we need to free t->thread somehow?
free(t);
}