Skip to content

Commit

Permalink
Merge pull request #87 from ehmry/genode
Browse files Browse the repository at this point in the history
Libco Genode support
  • Loading branch information
inactive123 committed Sep 23, 2018
2 parents 08ecd04 + 9f8bf3b commit 797ec45
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libco/amd64.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,34 @@ cothread_t co_create(unsigned int size, void (*entrypoint)(void))
size += 512; /* allocate additional space for storage */
size &= ~15; /* align stack to 16-byte boundary */

#ifdef __GENODE__
if((handle = (cothread_t)genode_alloc_secondary_stack(size)))
{
long long *p = (long long*)((char*)handle); /* OS returns top of stack */
*--p = (long long)crash; /* crash if entrypoint returns */
*--p = (long long)entrypoint; /* start of function */
*(long long*)handle = (long long)p; /* stack pointer */
}
#else
if((handle = (cothread_t)malloc(size)))
{
long long *p = (long long*)((char*)handle + size); /* seek to top of stack */
*--p = (long long)crash; /* crash if entrypoint returns */
*--p = (long long)entrypoint; /* start of function */
*(long long*)handle = (long long)p; /* stack pointer */
}
#endif

return handle;
}

void co_delete(cothread_t handle)
{
#ifdef __GENODE__
genode_free_secondary_stack(handle);
#else
free(handle);
#endif
}

#ifndef CO_USE_INLINE_ASM
Expand Down
29 changes: 29 additions & 0 deletions libco/genode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
libco.genode_secondary_stack (2018-09-15)
author: Emery Hemingway
license: public domain
*/

/* Genode include */
#include <base/thread.h>

/* Libco include */
#include <libco.h>

extern "C"
void *genode_alloc_secondary_stack(unsigned long stack_size)
{
try {
return Genode::Thread::myself()->alloc_secondary_stack("libco", stack_size); }
catch (...) {
Genode::error("libco: failed to allocate ", stack_size, " byte secondary stack");
return nullptr;
}

}

extern "C"
void genode_free_secondary_stack(void *stack)
{
Genode::Thread::myself()->free_secondary_stack(stack);
}
5 changes: 5 additions & 0 deletions libco/libco.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
license: public domain
*/

#ifdef __GENODE__
void *genode_alloc_secondary_stack(unsigned long stack_size);
void genode_free_secondary_stack(void *stack);
#endif

#if defined _MSC_VER
#include <Windows.h>
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
Expand Down

0 comments on commit 797ec45

Please sign in to comment.