Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
4 changes: 3 additions & 1 deletion minicoro.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@ typedef struct _mco_ctxbuf {
static void _mco_wrap_main(void) {
__asm__ __volatile__ (
"movq %r13, %rdi\n\t"
#if defined(__APPLE__)
"sub $8, %rsp\n\t"
#endif
Copy link
Copy Markdown
Owner

@edubart edubart Jan 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offsetting here will probably make the dummy return address endup in the wrong place. The dummy return address assists debugging as the last function in the backtrace when you put a breakpoint inside a coroutine. The offset should be really on _mco_makectx to overcome this, just like other platform ifdefs offsets there.

"jmpq *%r12");
}

Expand Down Expand Up @@ -658,7 +661,6 @@ static MCO_FORCE_INLINE void _mco_switch(_mco_ctxbuf* from, _mco_ctxbuf* to) {
: "rax", "rcx", "rdx", "r8", "r9", "r10", "r11", "memory", "cc");
}


static mco_result _mco_makectx(mco_coro* co, _mco_ctxbuf* ctx, void* stack_base, size_t stack_size) {
stack_size = stack_size - 128; /* Reserve 128 bytes for the Red Zone space (System V AMD64 ABI). */
void** stack_high_ptr = (void**)((size_t)stack_base + stack_size - sizeof(size_t));
Expand Down
28 changes: 15 additions & 13 deletions tests/mt-example.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#define MINICORO_IMPL
#include "minicoro.h"
#define C89THREAD_IMPLEMENTATION
#include "thirdparty/c89thread.h"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I am trying to support the c89thread (from David Reid the author of miniaudio) so I don't wish to change the library here, I told him the issue there about the Mac OS compilation and he fixed, and the library has been updated in a recent commit.


#define CUTE_SYNC_IMPLEMENTATION
#define CUTE_SYNC_POSIX
#include "thirdparty/cute_sync.h"
#include <stdio.h>

#define NUM_THREADS 4
#define NUM_TASKS 100
#define NUM_ITERATIONS 500
#define EXPECTED_RESULT 2396583362

static c89mtx_t mutex;
static c89thrd_t threads[NUM_THREADS];
static cute_mutex_t mutex;
static cute_thread_t* threads[NUM_THREADS];
static mco_coro* tasks[NUM_TASKS];

static void fail_mco(const char* message, mco_result res) {
Expand Down Expand Up @@ -61,7 +63,7 @@ int thread_worker(void* data) {
mco_coro* task = NULL;
int task_id = 0;

if(c89mtx_lock(&mutex) != c89thrd_success)
if(!cute_lock(&mutex))
fail("Unable to lock mutex");
for(int i=0;i<NUM_TASKS;++i) {
if(tasks[i] != NULL) {
Expand All @@ -71,7 +73,7 @@ int thread_worker(void* data) {
break;
}
}
if(c89mtx_unlock(&mutex) != c89thrd_success)
if(!cute_unlock(&mutex))
fail("Unable to unlock mutex");

if(!task) {
Expand All @@ -86,10 +88,10 @@ int thread_worker(void* data) {
switch(mco_status(task)) {
case MCO_SUSPENDED: { /* Task is not finished yet. */
/* Add it back to task list. */
if(c89mtx_lock(&mutex) != c89thrd_success)
if(!cute_lock(&mutex))
fail("Unable to lock mutex");
tasks[task_id] = task;
if(c89mtx_unlock(&mutex) != c89thrd_success)
if(!cute_unlock(&mutex))
fail("Unable to unlock mutex");
break;
}
Expand Down Expand Up @@ -121,8 +123,7 @@ int thread_worker(void* data) {

int main() {
/* Initialize mutex. */
if(c89mtx_init(&mutex, c89mtx_plain) != c89thrd_success)
fail("Failed to create mutex");
mutex = cute_mutex_create();

/* Create coroutine tasks. */
for(int i=0;i<NUM_TASKS;++i) {
Expand All @@ -131,13 +132,14 @@ int main() {

/* Create thread workers. */
for(size_t i=0;i<NUM_THREADS;++i) {
if(c89thrd_create(&threads[i], thread_worker, NULL) != c89thrd_success)
threads[i] = cute_thread_create(thread_worker, NULL, NULL);
if (!threads[i])
fail("Failed to create a thread");
}

/* Wait all threads to finish. */
for(size_t i=0;i<NUM_THREADS;++i) {
if(c89thrd_join(threads[i], NULL) != c89thrd_success)
if(!cute_thread_wait(threads[i]))
fail("Failed to join a thread!");
}

Expand All @@ -147,7 +149,7 @@ int main() {
}

/* Destroy mutex. */
c89mtx_destroy(&mutex);
cute_mutex_destroy(&mutex);

printf("Multithread test succeeded!\n");
return 0;
Expand Down
1 change: 1 addition & 0 deletions tests/simple.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define MINICORO_IMPL
#include "minicoro.h"
#include <stdio.h>
#include <assert.h>

// Coroutine entry function.
void coro_entry(mco_coro* co) {
Expand Down
Loading