Files
graphene/Pal/regression/Thread.c
T
Michał Kowalczyk 61a9534921 [Pal] Fix broken Thread test
Almost everything in this test was wrong:
  - race on `count = 100` in the main thread and `count++` in the
    worker thread
  - using volatile as atomic
  - dead code after `DkThreadExit`, which is `noreturn`
  - checking if noreturn function didn't actually return
  - doing the above with 500 loop iterations
2020-06-16 16:18:30 +00:00

58 lines
1.3 KiB
C

#include <stdatomic.h>
#include "pal.h"
#include "pal_debug.h"
const char* private1 = "Hello World 1";
const char* private2 = "Hello World 2";
static atomic_int count = 0;
static void callback(void* args) {
pal_printf("Run in Child Thread: %s\n", (char*)args);
while (count < 10) {
while (!(count % 2)) {
DkThreadYieldExecution();
}
count++;
}
pal_printf("Threads Run in Parallel OK\n");
DkSegmentRegister(PAL_SEGMENT_FS, &private2);
const char* ptr2;
__asm__ volatile("mov %%fs:0, %0" : "=r"(ptr2)::"memory");
pal_printf("Private Message (FS Segment) 2: %s\n", ptr2);
count = 100;
DkThreadExit(/*clear_child_tid=*/NULL);
/* UNREACHABLE */
}
int main() {
DkSegmentRegister(PAL_SEGMENT_FS, &private1);
const char* ptr1;
__asm__ volatile("mov %%fs:0, %0" : "=r"(ptr1)::"memory");
pal_printf("Private Message (FS Segment) 1: %s\n", ptr1);
PAL_HANDLE thread1 = DkThreadCreate(callback, "Hello World");
if (!thread1)
return 1;
pal_printf("Child Thread Created\n");
while (count < 9) {
while (!!(count % 2)) {
DkThreadYieldExecution();
}
count++;
}
while (count != 100) {
DkThreadYieldExecution();
}
pal_printf("Child Thread Exited\n");
return 0;
}