Allow waitForEvent on main thread; fixes #60

also use target_panic() instead of assert
This commit is contained in:
Michal Moskal 2017-11-29 19:54:17 -03:00
parent da8de1e31e
commit cb8c14fbb1
2 changed files with 20 additions and 7 deletions

View File

@ -6,7 +6,6 @@
#include <time.h> #include <time.h>
#include <cstdarg> #include <cstdarg>
#include <pthread.h> #include <pthread.h>
#include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <signal.h> #include <signal.h>
@ -15,6 +14,8 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#define THREAD_DBG(...)
void *operator new(size_t size) { void *operator new(size_t size) {
return malloc(size); return malloc(size);
} }
@ -229,6 +230,8 @@ static void runAct(Thread *thr) {
disposeThread(thr); disposeThread(thr);
} }
static void mainThread(Thread *) {}
void setupThread(Action a, TValue arg = 0, void (*runner)(Thread *) = NULL, TValue d0 = 0, void setupThread(Action a, TValue arg = 0, void (*runner)(Thread *) = NULL, TValue d0 = 0,
TValue d1 = 0) { TValue d1 = 0) {
if (runner == NULL) if (runner == NULL)
@ -242,8 +245,13 @@ void setupThread(Action a, TValue arg = 0, void (*runner)(Thread *) = NULL, TVal
thr->data0 = incr(d0); thr->data0 = incr(d0);
thr->data1 = incr(d1); thr->data1 = incr(d1);
pthread_cond_init(&thr->waitCond, NULL); pthread_cond_init(&thr->waitCond, NULL);
if (runner == mainThread) {
thr->pid = pthread_self();
} else {
pthread_create(&thr->pid, NULL, (void *(*)(void *))runner, thr); pthread_create(&thr->pid, NULL, (void *(*)(void *))runner, thr);
THREAD_DBG("setup thread: %p (pid %p)", thr, thr->pid);
pthread_detach(thr->pid); pthread_detach(thr->pid);
}
} }
void runInBackground(Action a) { void runInBackground(Action a) {
@ -263,8 +271,10 @@ void runForever(Action a) {
} }
void waitForEvent(int source, int value) { void waitForEvent(int source, int value) {
THREAD_DBG("waitForEv: %d %d", source, value);
auto self = pthread_self(); auto self = pthread_self();
for (auto t = allThreads; t; t = t->next) { for (auto t = allThreads; t; t = t->next) {
THREAD_DBG("t: %p", t);
if (t->pid == self) { if (t->pid == self) {
pthread_mutex_lock(&eventMutex); pthread_mutex_lock(&eventMutex);
t->waitSource = source; t->waitSource = source;
@ -279,7 +289,8 @@ void waitForEvent(int source, int value) {
return; return;
} }
} }
assert(0); DMESG("current thread not registered!");
target_panic(901);
} }
static void dispatchEvent(Event &e) { static void dispatchEvent(Event &e) {
@ -342,7 +353,8 @@ void raiseEvent(int id, int event) {
auto e = mkEvent(id, event); auto e = mkEvent(id, event);
pthread_mutex_lock(&eventMutex); pthread_mutex_lock(&eventMutex);
if (eventTail == NULL) { if (eventTail == NULL) {
assert(eventHead == NULL); if (eventHead != NULL)
target_panic(902);
eventHead = eventTail = e; eventHead = eventTail = e;
} else { } else {
eventTail->next = e; eventTail->next = e;
@ -467,6 +479,7 @@ void initRuntime() {
pthread_t disp; pthread_t disp;
pthread_create(&disp, NULL, evtDispatcher, NULL); pthread_create(&disp, NULL, evtDispatcher, NULL);
pthread_detach(disp); pthread_detach(disp);
setupThread(0, 0, mainThread);
target_init(); target_init();
screen_init(); screen_init();
startUser(); startUser();
@ -508,4 +521,4 @@ void dmesg(const char *format, ...) {
fflush(dmesgFile); fflush(dmesgFile);
fdatasync(fileno(dmesgFile)); fdatasync(fileno(dmesgFile));
} }
} } // namespace pxt

View File

@ -180,7 +180,7 @@ void init() {
mappedFrameBuffer = (uint8_t *)mmap(NULL, FB_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); mappedFrameBuffer = (uint8_t *)mmap(NULL, FB_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
DMESG("map %p", mappedFrameBuffer); DMESG("map %p", mappedFrameBuffer);
if (mappedFrameBuffer == MAP_FAILED) { if (mappedFrameBuffer == MAP_FAILED) {
target_panic(111); target_panic(903);
} }
clear(); clear();