summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/cothread/amd64.c104
-rw-r--r--src/lib/cothread/doc/amd64.asm85
-rw-r--r--src/lib/cothread/doc/style.css8
-rw-r--r--src/lib/cothread/doc/targets.html89
-rw-r--r--src/lib/cothread/doc/usage.html107
-rw-r--r--src/lib/cothread/doc/x86.asm21
-rw-r--r--src/lib/cothread/fiber.c51
-rw-r--r--src/lib/cothread/libco.c23
-rw-r--r--src/lib/cothread/libco.h34
-rw-r--r--src/lib/cothread/ppc.c407
-rw-r--r--src/lib/cothread/sjlj.c102
-rw-r--r--src/lib/cothread/test/cc.bat4
-rw-r--r--src/lib/cothread/test/cc.sh5
-rw-r--r--src/lib/cothread/test/test.h6
-rw-r--r--src/lib/cothread/test/test_args.cpp76
-rw-r--r--src/lib/cothread/test/test_timing.cpp52
-rw-r--r--src/lib/cothread/ucontext.c67
-rw-r--r--src/lib/cothread/x86.c93
-rw-r--r--src/lib/lib.mak21
19 files changed, 1353 insertions, 2 deletions
diff --git a/src/lib/cothread/amd64.c b/src/lib/cothread/amd64.c
new file mode 100644
index 00000000000..5f1cfca97a6
--- /dev/null
+++ b/src/lib/cothread/amd64.c
@@ -0,0 +1,104 @@
+/*
+ libco.amd64 (2009-10-12)
+ author: byuu
+ license: public domain
+*/
+
+#define LIBCO_C
+#include "libco.h"
+#include <assert.h>
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static thread_local long long co_active_buffer[64];
+static thread_local cothread_t co_active_handle = 0;
+static void (*co_swap)(cothread_t, cothread_t) = 0;
+
+#ifdef _WIN32
+ //ABI: Win64
+ static unsigned char co_swap_function[] = {
+ 0x48, 0x89, 0x22, 0x48, 0x8B, 0x21, 0x58, 0x48, 0x89, 0x6A, 0x08, 0x48, 0x89, 0x72, 0x10, 0x48,
+ 0x89, 0x7A, 0x18, 0x48, 0x89, 0x5A, 0x20, 0x4C, 0x89, 0x62, 0x28, 0x4C, 0x89, 0x6A, 0x30, 0x4C,
+ 0x89, 0x72, 0x38, 0x4C, 0x89, 0x7A, 0x40, 0x48, 0x81, 0xC2, 0x80, 0x00, 0x00, 0x00, 0x48, 0x83,
+ 0xE2, 0xF0, 0x0F, 0x29, 0x32, 0x0F, 0x29, 0x7A, 0x10, 0x44, 0x0F, 0x29, 0x42, 0x20, 0x44, 0x0F,
+ 0x29, 0x4A, 0x30, 0x44, 0x0F, 0x29, 0x52, 0x40, 0x44, 0x0F, 0x29, 0x5A, 0x50, 0x44, 0x0F, 0x29,
+ 0x62, 0x60, 0x44, 0x0F, 0x29, 0x6A, 0x70, 0x44, 0x0F, 0x29, 0xB2, 0x80, 0x00, 0x00, 0x00, 0x44,
+ 0x0F, 0x29, 0xBA, 0x90, 0x00, 0x00, 0x00, 0x48, 0x8B, 0x69, 0x08, 0x48, 0x8B, 0x71, 0x10, 0x48,
+ 0x8B, 0x79, 0x18, 0x48, 0x8B, 0x59, 0x20, 0x4C, 0x8B, 0x61, 0x28, 0x4C, 0x8B, 0x69, 0x30, 0x4C,
+ 0x8B, 0x71, 0x38, 0x4C, 0x8B, 0x79, 0x40, 0x48, 0x81, 0xC1, 0x80, 0x00, 0x00, 0x00, 0x48, 0x83,
+ 0xE1, 0xF0, 0x0F, 0x29, 0x31, 0x0F, 0x29, 0x79, 0x10, 0x44, 0x0F, 0x29, 0x41, 0x20, 0x44, 0x0F,
+ 0x29, 0x49, 0x30, 0x44, 0x0F, 0x29, 0x51, 0x40, 0x44, 0x0F, 0x29, 0x59, 0x50, 0x44, 0x0F, 0x29,
+ 0x61, 0x60, 0x44, 0x0F, 0x29, 0x69, 0x70, 0x44, 0x0F, 0x29, 0xB1, 0x80, 0x00, 0x00, 0x00, 0x44,
+ 0x0F, 0x29, 0xB9, 0x90, 0x00, 0x00, 0x00, 0xFF, 0xE0,
+ };
+
+ #include <windows.h>
+
+ void co_init() {
+ DWORD old_privileges;
+ VirtualProtect(co_swap_function, sizeof co_swap_function, PAGE_EXECUTE_READWRITE, &old_privileges);
+ }
+#else
+ //ABI: SystemV
+ static unsigned char co_swap_function[] = {
+ 0x48, 0x89, 0x26, 0x48, 0x8B, 0x27, 0x58, 0x48, 0x89, 0x6E, 0x08, 0x48, 0x89, 0x5E, 0x10, 0x4C,
+ 0x89, 0x66, 0x18, 0x4C, 0x89, 0x6E, 0x20, 0x4C, 0x89, 0x76, 0x28, 0x4C, 0x89, 0x7E, 0x30, 0x48,
+ 0x8B, 0x6F, 0x08, 0x48, 0x8B, 0x5F, 0x10, 0x4C, 0x8B, 0x67, 0x18, 0x4C, 0x8B, 0x6F, 0x20, 0x4C,
+ 0x8B, 0x77, 0x28, 0x4C, 0x8B, 0x7F, 0x30, 0xFF, 0xE0,
+ };
+
+ #include <unistd.h>
+ #include <sys/mman.h>
+
+ void co_init() {
+ unsigned long long addr = (unsigned long long)co_swap_function;
+ unsigned long long base = addr - (addr % sysconf(_SC_PAGESIZE));
+ unsigned long long size = (addr - base) + sizeof co_swap_function;
+ mprotect((void*)base, size, PROT_READ | PROT_WRITE | PROT_EXEC);
+ }
+#endif
+
+static void crash() {
+ assert(0); /* called only if cothread_t entrypoint returns */
+}
+
+cothread_t co_active() {
+ if(!co_active_handle) co_active_handle = &co_active_buffer;
+ return co_active_handle;
+}
+
+cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
+ cothread_t handle;
+ if(!co_swap) {
+ co_init();
+ co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
+ }
+ if(!co_active_handle) co_active_handle = &co_active_buffer;
+ size += 512; /* allocate additional space for storage */
+ size &= ~15; /* align stack to 16-byte boundary */
+
+ 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 */
+ }
+
+ return handle;
+}
+
+void co_delete(cothread_t handle) {
+ free(handle);
+}
+
+void co_switch(cothread_t handle) {
+ register cothread_t co_previous_handle = co_active_handle;
+ co_swap(co_active_handle = handle, co_previous_handle);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/lib/cothread/doc/amd64.asm b/src/lib/cothread/doc/amd64.asm
new file mode 100644
index 00000000000..3d02d8b34a6
--- /dev/null
+++ b/src/lib/cothread/doc/amd64.asm
@@ -0,0 +1,85 @@
+; SystemV ABI:
+; return = rax
+; arguments = rdi,rsi,rdx,rcx,r8,r9
+; non-volatile registers = rbp,rbx,r12-r15
+
+co_swap_systemV(to = rdi, from = rsi):
+ mov [rsi],rsp
+ mov rsp,[rdi]
+ pop rax
+
+ mov [rsi+ 8],rbp
+ mov [rsi+16],rbx
+ mov [rsi+24],r12
+ mov [rsi+32],r13
+ mov [rsi+40],r14
+ mov [rsi+48],r15
+
+ mov rbp,[rdi+ 8]
+ mov rbx,[rdi+16]
+ mov r12,[rdi+24]
+ mov r13,[rdi+32]
+ mov r14,[rdi+40]
+ mov r15,[rdi+48]
+
+ jmp rax
+
+; Win64 ABI:
+; return = rax
+; arguments = rcx,rdx,r8,r9
+; non-volatile registers = rbp,rsi,rdi,rbx,r12-r15
+; non-volatile FPU registers = xmm6-xmm15
+
+co_swap_win64(to = rcx, from = rdx):
+ mov [rdx],rsp
+ mov rsp,[rcx]
+ pop rax
+
+ mov [rdx+ 8],rbp
+ mov [rdx+ 16],rsi
+ mov [rdx+ 24],rdi
+ mov [rdx+ 32],rbx
+ mov [rdx+ 40],r12
+ mov [rdx+ 48],r13
+ mov [rdx+ 56],r14
+ mov [rdx+ 64],r15
+
+ add rdx,128
+ and rdx,~15
+
+ movaps [rdx+ 0],xmm6
+ movaps [rdx+ 16],xmm7
+ movaps [rdx+ 32],xmm8
+ movaps [rdx+ 48],xmm9
+ movaps [rdx+ 64],xmm10
+ movaps [rdx+ 80],xmm11
+ movaps [rdx+ 96],xmm12
+ movaps [rdx+112],xmm13
+ movaps [rdx+128],xmm14
+ movaps [rdx+144],xmm15
+
+ mov rbp,[rcx+ 8]
+ mov rsi,[rcx+ 16]
+ mov rdi,[rcx+ 24]
+ mov rbx,[rcx+ 32]
+ mov r12,[rcx+ 40]
+ mov r13,[rcx+ 48]
+ mov r14,[rcx+ 56]
+ mov r15,[rcx+ 64]
+
+ add rcx,128
+ and rcx,~15
+
+ movaps [rcx+ 0],xmm6
+ movaps [rcx+ 16],xmm7
+ movaps [rcx+ 32],xmm8
+ movaps [rcx+ 48],xmm9
+ movaps [rcx+ 64],xmm10
+ movaps [rcx+ 80],xmm11
+ movaps [rcx+ 96],xmm12
+ movaps [rcx+112],xmm13
+ movaps [rcx+128],xmm14
+ movaps [rcx+144],xmm15
+
+ jmp rax
+
diff --git a/src/lib/cothread/doc/style.css b/src/lib/cothread/doc/style.css
new file mode 100644
index 00000000000..5181afde6c5
--- /dev/null
+++ b/src/lib/cothread/doc/style.css
@@ -0,0 +1,8 @@
+body {
+ background: #333;
+ color: #fff;
+}
+
+code {
+ background: #444;
+}
diff --git a/src/lib/cothread/doc/targets.html b/src/lib/cothread/doc/targets.html
new file mode 100644
index 00000000000..d6211a15d75
--- /dev/null
+++ b/src/lib/cothread/doc/targets.html
@@ -0,0 +1,89 @@
+<html>
+<head>
+ <title></title>
+ <link href="style.css" rel="stylesheet" type="text/css">
+</head>
+<body>
+
+<b>Supported targets:</b><br/><br/>
+
+Note that supported targets are only those that have been tested and confirmed
+working. It is quite possible that libco will work on more processors, compilers
+and operating systems than those listed below.
+<hr/>
+
+<b>libco.x86</b><br/>
+Overhead: ~5x<br/>
+Supported processor(s): 32-bit x86<br/>
+Supported compiler(s): any<br/>
+Supported operating system(s):<ul>
+<li>Windows</li>
+<li>Mac OS X</li>
+<li>Linux</li>
+<li>BSD</li>
+</ul>
+<hr/>
+
+<b>libco.amd64</b><br/>
+Overhead: ~10x (Windows), ~6x (all other platforms)<br/>
+Supported processor(s): 64-bit amd64<br/>
+Supported compiler(s): any<br/>
+Supported operating system(s):<ul>
+<li>Windows</li>
+<li>Mac OS X</li>
+<li>Linux</li>
+<li>BSD</li>
+</ul>
+<hr/>
+
+<b>libco.ppc</b><br/>
+Overhead: ~20x<br/>
+Supported processor(s): 32-bit PowerPC, 64-bit PowerPC<br/>
+Supported compiler(s): GNU GCC<br/>
+Supported operating system(s):<ul>
+</ul>
+<li>Mac OS X</li>
+<li>Linux</li>
+<li>BSD</li>
+<li>Playstation 3</li>
+</ul>
+<br/>
+
+Note: this module contains compiler flags to enable/disable FPU and Altivec
+support.
+
+<hr/>
+
+<b>libco.fiber</b><br/>
+Overhead: ~15x<br/>
+Supported processor(s): Processor independent<br/>
+Supported compiler(s): any<br/>
+Supported operating system(s):<ul>
+<li>Windows</li>
+</ul>
+<hr/>
+
+<b>libco.sjlj</b><br/>
+Overhead: ~30x<br/>
+Supported processor(s): Processor independent<br/>
+Supported compiler(s): any<br/>
+Supported operating system(s):<ul>
+<li>Mac OS X</li>
+<li>Linux</li>
+<li>BSD</li>
+<li>Solaris</li>
+</ul>
+<hr/>
+
+<b>libco.ucontext</b><br/>
+Overhead: <b><font color="#ff0000">~300x</font></b><br/>
+Supported processor(s): Processor independent<br/>
+Supported compiler(s): any<br/>
+Supported operating system(s):<ul>
+<li>Linux</li>
+<li>BSD</li>
+</ul>
+<hr/>
+
+</body>
+</html>
diff --git a/src/lib/cothread/doc/usage.html b/src/lib/cothread/doc/usage.html
new file mode 100644
index 00000000000..3f0d81ccd88
--- /dev/null
+++ b/src/lib/cothread/doc/usage.html
@@ -0,0 +1,107 @@
+<html>
+<head>
+ <title></title>
+ <link href="style.css" rel="stylesheet" type="text/css">
+</head>
+<body>
+
+<b>License:</b><br/><br/>
+libco is released to the public domain.
+<hr/>
+
+<b>Contact:</b><br/><br/>
+At present, you may contact me at setsunakun0 at hotmail dot com.<br/>
+I am interested in knowing of any projects that make use of this library,
+though this is only a courtesy.
+<hr/>
+
+<b>Foreword:</b><br/><br/>
+libco is a cross-platform, public domain implementation of
+cooperative-multithreading; a feature that is sorely lacking
+from the ISO C/C++ standard.<br/>
+The library is designed for maximum speed and portability, and
+not for safety or features. If safety or extra functionality is desired,
+a wrapper API can easily be written to encapsulate all library functions.<br/>
+Behavior of executing operations that are listed as not permitted
+below result in undefined behavior. They may work anyway, they
+may cause undesired / unknown behavior, or they may crash the
+program entirely.<br/>
+The goal of this library was to simplify the base API as much as possible,
+implementing only that which cannot be implemented using pure C. Additional
+functionality after this would only complicate ports of this library to new
+platforms.
+<hr/>
+
+<b>Porting:</b><br/><br/>
+This document is included as a reference for porting libco. Please submit any
+ports you create to me, so that libco can become more useful. Please note that
+since libco is public domain, you must submit your code as a work of the
+public domain in order for it to be included in the official distribution.
+Full credit will be given in the source code of the official release. Please
+do not bother submitting code to me under any other license -- including GPL,
+LGPL, BSD or CC -- I am not interested in creating a library with multiple
+different licenses depending on which targets are used.
+<hr/>
+
+<b>Synopsis:</b><br/><br/>
+<code>
+typedef void* cothread_t;<br/>
+<br/>
+cothread_t co_active();<br/>
+cothread_t co_create(unsigned int heapsize, void (*coentry)(void));<br/>
+void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co_delete(cothread_t cothread);<br/>
+void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co_switch(cothread_t cothread);<br/>
+</code>
+<hr/>
+
+<b>Usage:</b>
+<hr/>
+
+<code>typedef void* cothread_t;</code><br/><br/>
+Handle to cothread.<br/>
+Handle must be of type void*.<br/>
+A value of null (0) indicates an uninitialized or invalid
+handle, whereas a non-zero value indicates a valid handle.
+<hr/>
+
+<code>cothread_t co_active();</code><br/><br/>
+Return handle to current cothread. Always returns a valid handle, even when
+called from the main program thread.
+<hr/>
+
+<code>cothread_t co_create(unsigned int heapsize, void (*coentry)(void));</code><br/><br/>
+Create new cothread.<br/>
+Heapsize is the amount of memory allocated for the cothread stack, specified
+in bytes. This is unfortunately impossible to make fully portable. It is
+recommended to specify sizes using `n * sizeof(void*)'. It is better to err
+on the side of caution and allocate more memory than will be needed to ensure
+compatibility with other platforms, within reason. A typical heapsize for a
+32-bit architecture is ~1MB.<br/>
+When the new cothread is first called, program execution jumps to coentry.
+This function does not take any arguments, due to portability issues with
+passing function arguments. However, arguments can be simulated by the use
+of global variables, which can be set before the first call to each cothread.<br/>
+coentry() must not return, and should end with an appropriate co_switch()
+statement. Behavior is undefined if entry point returns normally.<br/>
+Library is responsible for allocating cothread stack memory, to free
+the user from needing to allocate special memory capable of being used
+as program stack memory on platforms where this is required.<br/>
+User is always responsible for deleting cothreads with co_delete().<br/>
+Return value of null (0) indicates cothread creation failed.
+<hr/>
+
+<code>void co_delete(cothread_t cothread);</code><br/><br/>
+Delete specified cothread.<br/>
+Null (0) or invalid cothread handle is not allowed.<br/>
+Passing handle of active cothread to this function is not allowed.<br/>
+Passing handle of primary cothread is not allowed.
+<hr/>
+
+<code>void co_switch(cothread_t cothread);</code><br/><br/>
+Switch to specified cothread.<br/>
+Null (0) or invalid cothread handle is not allowed.<br/>
+Passing handle of active cothread to this function is not allowed.
+<hr/>
+
+</body>
+</html>
diff --git a/src/lib/cothread/doc/x86.asm b/src/lib/cothread/doc/x86.asm
new file mode 100644
index 00000000000..c55c25252dd
--- /dev/null
+++ b/src/lib/cothread/doc/x86.asm
@@ -0,0 +1,21 @@
+; fastcall ABI:
+; return = eax
+; arguments = ecx,edx
+; non-volatile registers = ebp,esi,edi,ebx
+
+co_swap_fastcall(to = ecx, from = edx):
+ mov [edx],esp
+ mov esp,[ecx]
+ pop eax
+
+ mov [edx+ 4],ebp
+ mov [edx+ 8],esi
+ mov [edx+12],edi
+ mov [edx+16],ebx
+
+ mov ebp,[ecx+ 4]
+ mov esi,[ecx+ 8]
+ mov edi,[ecx+12]
+ mov ebx,[ecx+16]
+
+ jmp eax
diff --git a/src/lib/cothread/fiber.c b/src/lib/cothread/fiber.c
new file mode 100644
index 00000000000..02ef5bc7881
--- /dev/null
+++ b/src/lib/cothread/fiber.c
@@ -0,0 +1,51 @@
+/*
+ libco.win (2008-01-28)
+ authors: Nach, byuu
+ license: public domain
+*/
+
+#define LIBCO_C
+#include "libco.h"
+#define WINVER 0x0400
+#define _WIN32_WINNT 0x0400
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static thread_local cothread_t co_active_ = 0;
+
+static void __stdcall co_thunk(void *coentry) {
+ ((void (*)(void))coentry)();
+}
+
+cothread_t co_active() {
+ if(!co_active_) {
+ ConvertThreadToFiber(0);
+ co_active_ = GetCurrentFiber();
+ }
+ return co_active_;
+}
+
+cothread_t co_create(unsigned int heapsize, void (*coentry)(void)) {
+ if(!co_active_) {
+ ConvertThreadToFiber(0);
+ co_active_ = GetCurrentFiber();
+ }
+ return (cothread_t)CreateFiber(heapsize, co_thunk, (void*)coentry);
+}
+
+void co_delete(cothread_t cothread) {
+ DeleteFiber(cothread);
+}
+
+void co_switch(cothread_t cothread) {
+ co_active_ = cothread;
+ SwitchToFiber(cothread);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/lib/cothread/libco.c b/src/lib/cothread/libco.c
new file mode 100644
index 00000000000..5567626330d
--- /dev/null
+++ b/src/lib/cothread/libco.c
@@ -0,0 +1,23 @@
+/*
+ libco
+ auto-selection module
+ license: public domain
+*/
+
+#if defined(__GNUC__) && defined(__i386__)
+ #include "x86.c"
+#elif defined(__GNUC__) && defined(__amd64__)
+ #include "amd64.c"
+#elif defined(__GNUC__) && defined(_ARCH_PPC)
+ #include "ppc.c"
+#elif defined(__GNUC__)
+ #include "sjlj.c"
+#elif defined(_MSC_VER) && defined(_M_IX86)
+ #include "x86.c"
+#elif defined(_MSC_VER) && defined(_M_AMD64)
+ #include "amd64.c"
+#elif defined(_MSC_VER)
+ #include "fiber.c"
+#else
+ #error "libco: unsupported processor, compiler or operating system"
+#endif
diff --git a/src/lib/cothread/libco.h b/src/lib/cothread/libco.h
new file mode 100644
index 00000000000..deb954fb4d6
--- /dev/null
+++ b/src/lib/cothread/libco.h
@@ -0,0 +1,34 @@
+/*
+ libco
+ version: 0.16 (2010-12-24)
+ license: public domain
+*/
+
+#ifndef LIBCO_H
+#define LIBCO_H
+
+#ifdef LIBCO_C
+ #ifdef LIBCO_MP
+ #define thread_local __thread
+ #else
+ #define thread_local
+ #endif
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void* cothread_t;
+
+cothread_t co_active();
+cothread_t co_create(unsigned int, void (*)(void));
+void co_delete(cothread_t);
+void co_switch(cothread_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+/* ifndef LIBCO_H */
+#endif
diff --git a/src/lib/cothread/ppc.c b/src/lib/cothread/ppc.c
new file mode 100644
index 00000000000..a6028fdba24
--- /dev/null
+++ b/src/lib/cothread/ppc.c
@@ -0,0 +1,407 @@
+/*
+ libco.ppc (2010-10-17)
+ author: blargg
+ license: public domain
+*/
+
+/* PowerPC 32/64 using embedded or external asm, with optional
+floating-point and AltiVec save/restore */
+
+#define LIBCO_C
+#include "libco.h"
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#define LIBCO_MPROTECT (__unix__ && !LIBCO_PPC_ASM)
+
+#if LIBCO_MPROTECT
+ #include <unistd.h>
+ #include <sys/mman.h>
+#endif
+
+/* State format (offsets in 32-bit words)
+
++0 Pointer to swap code
+ Rest of function descriptor for entry function
++8 PC
++10 SP
+ Special regs
+ GPRs
+ FPRs
+ VRs
+ stack
+*/
+
+enum { state_size = 1024 };
+enum { above_stack = 2048 };
+enum { stack_align = 256 };
+
+static thread_local cothread_t co_active_handle = 0;
+
+/**** Determine environment ****/
+
+#define LIBCO_PPC64 (_ARCH_PPC64 || __PPC64__ || __ppc64__ || __powerpc64__)
+
+/* Whether function calls are indirect through a descriptor,
+or are directly to function */
+#ifndef LIBCO_PPCDESC
+ #if !_CALL_SYSV && (_CALL_AIX || _CALL_AIXDESC || LIBCO_PPC64)
+ #define LIBCO_PPCDESC 1
+ #endif
+#endif
+
+#ifdef LIBCO_PPC_ASM
+
+ #ifdef __cplusplus
+ extern "C"
+ #endif
+
+ /* Swap code is in ppc.S */
+ void co_swap_asm( cothread_t, cothread_t );
+ #define CO_SWAP_ASM( x, y ) co_swap_asm( x, y )
+
+#else
+
+/* Swap code is here in array. Please leave dieassembly comments,
+as they make it easy to see what it does, and reorder instructions
+if one wants to see whether that improves performance. */
+static const uint32_t libco_ppc_code [] = {
+#if LIBCO_PPC64
+ 0x7d000026, /* mfcr r8 */
+ 0xf8240028, /* std r1,40(r4) */
+ 0x7d2802a6, /* mflr r9 */
+ 0xf9c40048, /* std r14,72(r4) */
+ 0xf9e40050, /* std r15,80(r4) */
+ 0xfa040058, /* std r16,88(r4) */
+ 0xfa240060, /* std r17,96(r4) */
+ 0xfa440068, /* std r18,104(r4) */
+ 0xfa640070, /* std r19,112(r4) */
+ 0xfa840078, /* std r20,120(r4) */
+ 0xfaa40080, /* std r21,128(r4) */
+ 0xfac40088, /* std r22,136(r4) */
+ 0xfae40090, /* std r23,144(r4) */
+ 0xfb040098, /* std r24,152(r4) */
+ 0xfb2400a0, /* std r25,160(r4) */
+ 0xfb4400a8, /* std r26,168(r4) */
+ 0xfb6400b0, /* std r27,176(r4) */
+ 0xfb8400b8, /* std r28,184(r4) */
+ 0xfba400c0, /* std r29,192(r4) */
+ 0xfbc400c8, /* std r30,200(r4) */
+ 0xfbe400d0, /* std r31,208(r4) */
+ 0xf9240020, /* std r9,32(r4) */
+ 0xe8e30020, /* ld r7,32(r3) */
+ 0xe8230028, /* ld r1,40(r3) */
+ 0x48000009, /* bl 1 */
+ 0x7fe00008, /* trap */
+ 0x91040030,/*1:stw r8,48(r4) */
+ 0x80c30030, /* lwz r6,48(r3) */
+ 0x7ce903a6, /* mtctr r7 */
+ 0xe9c30048, /* ld r14,72(r3) */
+ 0xe9e30050, /* ld r15,80(r3) */
+ 0xea030058, /* ld r16,88(r3) */
+ 0xea230060, /* ld r17,96(r3) */
+ 0xea430068, /* ld r18,104(r3) */
+ 0xea630070, /* ld r19,112(r3) */
+ 0xea830078, /* ld r20,120(r3) */
+ 0xeaa30080, /* ld r21,128(r3) */
+ 0xeac30088, /* ld r22,136(r3) */
+ 0xeae30090, /* ld r23,144(r3) */
+ 0xeb030098, /* ld r24,152(r3) */
+ 0xeb2300a0, /* ld r25,160(r3) */
+ 0xeb4300a8, /* ld r26,168(r3) */
+ 0xeb6300b0, /* ld r27,176(r3) */
+ 0xeb8300b8, /* ld r28,184(r3) */
+ 0xeba300c0, /* ld r29,192(r3) */
+ 0xebc300c8, /* ld r30,200(r3) */
+ 0xebe300d0, /* ld r31,208(r3) */
+ 0x7ccff120, /* mtcr r6 */
+#else
+ 0x7d000026, /* mfcr r8 */
+ 0x90240028, /* stw r1,40(r4) */
+ 0x7d2802a6, /* mflr r9 */
+ 0x91a4003c, /* stw r13,60(r4) */
+ 0x91c40040, /* stw r14,64(r4) */
+ 0x91e40044, /* stw r15,68(r4) */
+ 0x92040048, /* stw r16,72(r4) */
+ 0x9224004c, /* stw r17,76(r4) */
+ 0x92440050, /* stw r18,80(r4) */
+ 0x92640054, /* stw r19,84(r4) */
+ 0x92840058, /* stw r20,88(r4) */
+ 0x92a4005c, /* stw r21,92(r4) */
+ 0x92c40060, /* stw r22,96(r4) */
+ 0x92e40064, /* stw r23,100(r4) */
+ 0x93040068, /* stw r24,104(r4) */
+ 0x9324006c, /* stw r25,108(r4) */
+ 0x93440070, /* stw r26,112(r4) */
+ 0x93640074, /* stw r27,116(r4) */
+ 0x93840078, /* stw r28,120(r4) */
+ 0x93a4007c, /* stw r29,124(r4) */
+ 0x93c40080, /* stw r30,128(r4) */
+ 0x93e40084, /* stw r31,132(r4) */
+ 0x91240020, /* stw r9,32(r4) */
+ 0x80e30020, /* lwz r7,32(r3) */
+ 0x80230028, /* lwz r1,40(r3) */
+ 0x48000009, /* bl 1 */
+ 0x7fe00008, /* trap */
+ 0x91040030,/*1:stw r8,48(r4) */
+ 0x80c30030, /* lwz r6,48(r3) */
+ 0x7ce903a6, /* mtctr r7 */
+ 0x81a3003c, /* lwz r13,60(r3) */
+ 0x81c30040, /* lwz r14,64(r3) */
+ 0x81e30044, /* lwz r15,68(r3) */
+ 0x82030048, /* lwz r16,72(r3) */
+ 0x8223004c, /* lwz r17,76(r3) */
+ 0x82430050, /* lwz r18,80(r3) */
+ 0x82630054, /* lwz r19,84(r3) */
+ 0x82830058, /* lwz r20,88(r3) */
+ 0x82a3005c, /* lwz r21,92(r3) */
+ 0x82c30060, /* lwz r22,96(r3) */
+ 0x82e30064, /* lwz r23,100(r3) */
+ 0x83030068, /* lwz r24,104(r3) */
+ 0x8323006c, /* lwz r25,108(r3) */
+ 0x83430070, /* lwz r26,112(r3) */
+ 0x83630074, /* lwz r27,116(r3) */
+ 0x83830078, /* lwz r28,120(r3) */
+ 0x83a3007c, /* lwz r29,124(r3) */
+ 0x83c30080, /* lwz r30,128(r3) */
+ 0x83e30084, /* lwz r31,132(r3) */
+ 0x7ccff120, /* mtcr r6 */
+#endif
+
+#ifndef LIBCO_PPC_NOFP
+ 0xd9c400e0, /* stfd f14,224(r4) */
+ 0xd9e400e8, /* stfd f15,232(r4) */
+ 0xda0400f0, /* stfd f16,240(r4) */
+ 0xda2400f8, /* stfd f17,248(r4) */
+ 0xda440100, /* stfd f18,256(r4) */
+ 0xda640108, /* stfd f19,264(r4) */
+ 0xda840110, /* stfd f20,272(r4) */
+ 0xdaa40118, /* stfd f21,280(r4) */
+ 0xdac40120, /* stfd f22,288(r4) */
+ 0xdae40128, /* stfd f23,296(r4) */
+ 0xdb040130, /* stfd f24,304(r4) */
+ 0xdb240138, /* stfd f25,312(r4) */
+ 0xdb440140, /* stfd f26,320(r4) */
+ 0xdb640148, /* stfd f27,328(r4) */
+ 0xdb840150, /* stfd f28,336(r4) */
+ 0xdba40158, /* stfd f29,344(r4) */
+ 0xdbc40160, /* stfd f30,352(r4) */
+ 0xdbe40168, /* stfd f31,360(r4) */
+ 0xc9c300e0, /* lfd f14,224(r3) */
+ 0xc9e300e8, /* lfd f15,232(r3) */
+ 0xca0300f0, /* lfd f16,240(r3) */
+ 0xca2300f8, /* lfd f17,248(r3) */
+ 0xca430100, /* lfd f18,256(r3) */
+ 0xca630108, /* lfd f19,264(r3) */
+ 0xca830110, /* lfd f20,272(r3) */
+ 0xcaa30118, /* lfd f21,280(r3) */
+ 0xcac30120, /* lfd f22,288(r3) */
+ 0xcae30128, /* lfd f23,296(r3) */
+ 0xcb030130, /* lfd f24,304(r3) */
+ 0xcb230138, /* lfd f25,312(r3) */
+ 0xcb430140, /* lfd f26,320(r3) */
+ 0xcb630148, /* lfd f27,328(r3) */
+ 0xcb830150, /* lfd f28,336(r3) */
+ 0xcba30158, /* lfd f29,344(r3) */
+ 0xcbc30160, /* lfd f30,352(r3) */
+ 0xcbe30168, /* lfd f31,360(r3) */
+#endif
+
+#ifdef __ALTIVEC__
+ 0x7ca042a6, /* mfvrsave r5 */
+ 0x39040180, /* addi r8,r4,384 */
+ 0x39240190, /* addi r9,r4,400 */
+ 0x70a00fff, /* andi. r0,r5,4095 */
+ 0x90a40034, /* stw r5,52(r4) */
+ 0x4182005c, /* beq- 2 */
+ 0x7e8041ce, /* stvx v20,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7ea049ce, /* stvx v21,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7ec041ce, /* stvx v22,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7ee049ce, /* stvx v23,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7f0041ce, /* stvx v24,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7f2049ce, /* stvx v25,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7f4041ce, /* stvx v26,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7f6049ce, /* stvx v27,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7f8041ce, /* stvx v28,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7fa049ce, /* stvx v29,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7fc041ce, /* stvx v30,r0,r8 */
+ 0x7fe049ce, /* stvx v31,r0,r9 */
+ 0x80a30034,/*2:lwz r5,52(r3) */
+ 0x39030180, /* addi r8,r3,384 */
+ 0x39230190, /* addi r9,r3,400 */
+ 0x70a00fff, /* andi. r0,r5,4095 */
+ 0x7ca043a6, /* mtvrsave r5 */
+ 0x4d820420, /* beqctr */
+ 0x7e8040ce, /* lvx v20,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7ea048ce, /* lvx v21,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7ec040ce, /* lvx v22,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7ee048ce, /* lvx v23,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7f0040ce, /* lvx v24,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7f2048ce, /* lvx v25,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7f4040ce, /* lvx v26,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7f6048ce, /* lvx v27,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7f8040ce, /* lvx v28,r0,r8 */
+ 0x39080020, /* addi r8,r8,32 */
+ 0x7fa048ce, /* lvx v29,r0,r9 */
+ 0x39290020, /* addi r9,r9,32 */
+ 0x7fc040ce, /* lvx v30,r0,r8 */
+ 0x7fe048ce, /* lvx v31,r0,r9 */
+#endif
+
+ 0x4e800420, /* bctr */
+};
+
+ #if LIBCO_PPCDESC
+ /* Function call goes through indirect descriptor */
+ #define CO_SWAP_ASM( x, y ) \
+ ((void (*)( cothread_t, cothread_t )) (uintptr_t) x)( x, y )
+ #else
+ /* Function call goes directly to code */
+ #define CO_SWAP_ASM( x, y ) \
+ ((void (*)( cothread_t, cothread_t )) (uintptr_t) libco_ppc_code)( x, y )
+ #endif
+
+#endif
+
+static uint32_t* co_create_( unsigned size, uintptr_t entry )
+{
+ uint32_t* t = (uint32_t*) malloc( size );
+
+ (void) entry;
+
+ #if LIBCO_PPCDESC
+ if ( t )
+ {
+ /* Copy entry's descriptor */
+ memcpy( t, (void*) entry, sizeof (void*) * 3 );
+
+ /* Set function pointer to swap routine */
+ #ifdef LIBCO_PPC_ASM
+ *(const void**) t = *(void**) &co_swap_asm;
+ #else
+ *(const void**) t = libco_ppc_code;
+ #endif
+ }
+ #endif
+
+ return t;
+}
+
+cothread_t co_create( unsigned int size, void (*entry_)( void ) )
+{
+ uintptr_t entry = (uintptr_t) entry_;
+ uint32_t* t = NULL;
+
+ /* Be sure main thread was successfully allocated */
+ if ( co_active() )
+ {
+ size += state_size + above_stack + stack_align;
+ t = co_create_( size, entry );
+ }
+
+ if ( t )
+ {
+ uintptr_t sp;
+ int shift;
+
+ /* Save current registers into new thread, so that any special ones will
+ have proper values when thread is begun */
+ CO_SWAP_ASM( t, t );
+
+ #if LIBCO_PPCDESC
+ /* Get real address */
+ entry = (uintptr_t) *(void**) entry;
+ #endif
+
+ /* Put stack near end of block, and align */
+ sp = (uintptr_t) t + size - above_stack;
+ sp -= sp % stack_align;
+
+ /* On PPC32, we save and restore GPRs as 32 bits. For PPC64, we
+ save and restore them as 64 bits, regardless of the size the ABI
+ uses. So, we manually write pointers at the proper size. We always
+ save and restore at the same address, and since PPC is big-endian,
+ we must put the low byte first on PPC32. */
+
+ /* If uintptr_t is 32 bits, >>32 is undefined behavior, so we do two shifts
+ and don't have to care how many bits uintptr_t is. */
+ #if LIBCO_PPC64
+ shift = 16;
+ #else
+ shift = 0;
+ #endif
+
+ /* Set up so entry will be called on next swap */
+ t [8] = (uint32_t) (entry >> shift >> shift);
+ t [9] = (uint32_t) entry;
+
+ t [10] = (uint32_t) (sp >> shift >> shift);
+ t [11] = (uint32_t) sp;
+ }
+
+ return t;
+}
+
+void co_delete( cothread_t t )
+{
+ free( t );
+}
+
+static void co_init_( void )
+{
+ #if LIBCO_MPROTECT
+ /* TODO: pre- and post-pad PPC code so that this doesn't make other
+ data executable and writable */
+ long page_size = sysconf( _SC_PAGESIZE );
+ if ( page_size > 0 )
+ {
+ uintptr_t align = page_size;
+ uintptr_t begin = (uintptr_t) libco_ppc_code;
+ uintptr_t end = begin + sizeof libco_ppc_code;
+
+ /* Align beginning and end */
+ end += align - 1;
+ end -= end % align;
+ begin -= begin % align;
+
+ mprotect( (void*) begin, end - begin, PROT_READ | PROT_WRITE | PROT_EXEC );
+ }
+ #endif
+
+ co_active_handle = co_create_( state_size, (uintptr_t) &co_switch );
+}
+
+cothread_t co_active()
+{
+ if ( !co_active_handle )
+ co_init_();
+
+ return co_active_handle;
+}
+
+void co_switch( cothread_t t )
+{
+ cothread_t old = co_active_handle;
+ co_active_handle = t;
+
+ CO_SWAP_ASM( t, old );
+}
diff --git a/src/lib/cothread/sjlj.c b/src/lib/cothread/sjlj.c
new file mode 100644
index 00000000000..8b72b614e38
--- /dev/null
+++ b/src/lib/cothread/sjlj.c
@@ -0,0 +1,102 @@
+/*
+ libco.sjlj (2008-01-28)
+ author: Nach
+ license: public domain
+*/
+
+/*
+ * Note this was designed for UNIX systems. Based on ideas expressed in a paper
+ * by Ralf Engelschall.
+ * For SJLJ on other systems, one would want to rewrite springboard() and
+ * co_create() and hack the jmb_buf stack pointer.
+ */
+
+#define LIBCO_C
+#include "libco.h"
+#include <stdlib.h>
+#include <signal.h>
+#include <setjmp.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ sigjmp_buf context;
+ void (*coentry)(void);
+ void *stack;
+} cothread_struct;
+
+static thread_local cothread_struct co_primary;
+static thread_local cothread_struct *creating, *co_running = 0;
+
+static void springboard(int ignored) {
+ if(sigsetjmp(creating->context, 0)) {
+ co_running->coentry();
+ }
+}
+
+cothread_t co_active() {
+ if(!co_running) co_running = &co_primary;
+ return (cothread_t)co_running;
+}
+
+cothread_t co_create(unsigned int size, void (*coentry)(void)) {
+ if(!co_running) co_running = &co_primary;
+
+ cothread_struct *thread = (cothread_struct*)malloc(sizeof(cothread_struct));
+ if(thread) {
+ struct sigaction handler;
+ struct sigaction old_handler;
+
+ stack_t stack;
+ stack_t old_stack;
+
+ thread->coentry = thread->stack = 0;
+
+ stack.ss_flags = 0;
+ stack.ss_size = size;
+ thread->stack = stack.ss_sp = malloc(size);
+ if(stack.ss_sp && !sigaltstack(&stack, &old_stack)) {
+ handler.sa_handler = springboard;
+ handler.sa_flags = SA_ONSTACK;
+ sigemptyset(&handler.sa_mask);
+ creating = thread;
+
+ if(!sigaction(SIGUSR1, &handler, &old_handler)) {
+ if(!raise(SIGUSR1)) {
+ thread->coentry = coentry;
+ }
+ sigaltstack(&old_stack, 0);
+ sigaction(SIGUSR1, &old_handler, 0);
+ }
+ }
+
+ if(thread->coentry != coentry) {
+ co_delete(thread);
+ thread = 0;
+ }
+ }
+
+ return (cothread_t)thread;
+}
+
+void co_delete(cothread_t cothread) {
+ if(cothread) {
+ if(((cothread_struct*)cothread)->stack) {
+ free(((cothread_struct*)cothread)->stack);
+ }
+ free(cothread);
+ }
+}
+
+void co_switch(cothread_t cothread) {
+ if(!sigsetjmp(co_running->context, 0)) {
+ co_running = (cothread_struct*)cothread;
+ siglongjmp(co_running->context, 1);
+ }
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/lib/cothread/test/cc.bat b/src/lib/cothread/test/cc.bat
new file mode 100644
index 00000000000..d436d6f2949
--- /dev/null
+++ b/src/lib/cothread/test/cc.bat
@@ -0,0 +1,4 @@
+mingw32-gcc -O3 -fomit-frame-pointer -o libco.o -c ../libco.c
+mingw32-g++ -O3 -fomit-frame-pointer -c test_timing.cpp
+mingw32-g++ -O3 -fomit-frame-pointer test_timing.o libco.o -o test_timing.exe
+@del *.o
diff --git a/src/lib/cothread/test/cc.sh b/src/lib/cothread/test/cc.sh
new file mode 100644
index 00000000000..c0e7846264f
--- /dev/null
+++ b/src/lib/cothread/test/cc.sh
@@ -0,0 +1,5 @@
+clear
+g++ -O3 -fomit-frame-pointer -c test_timing.cpp
+gcc -O3 -fomit-frame-pointer -o libco.o -c ../libco.c
+g++ -O3 -fomit-frame-pointer test_timing.o libco.o -o test_timing
+rm -f *.o
diff --git a/src/lib/cothread/test/test.h b/src/lib/cothread/test/test.h
new file mode 100644
index 00000000000..9d111c306b4
--- /dev/null
+++ b/src/lib/cothread/test/test.h
@@ -0,0 +1,6 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "../libco.h"
diff --git a/src/lib/cothread/test/test_args.cpp b/src/lib/cothread/test/test_args.cpp
new file mode 100644
index 00000000000..1e6e2bbb5a3
--- /dev/null
+++ b/src/lib/cothread/test/test_args.cpp
@@ -0,0 +1,76 @@
+/*****
+ * cothread parameterized function example
+ *****
+ * entry point to cothreads cannot take arguments.
+ * this is due to portability issues: each processor,
+ * operating system, programming language and compiler
+ * can use different parameter passing methods, so
+ * arguments to the cothread entry points were omitted.
+ *
+ * however, the behavior can easily be simulated by use
+ * of a specialized co_switch to set global parameters to
+ * be used as function arguments.
+ *
+ * in this way, with a bit of extra red tape, one gains
+ * even more flexibility than would be possible with a
+ * fixed argument list entry point, such as void (*)(void*),
+ * as any number of arguments can be used.
+ *
+ * this also eliminates race conditions where a pointer
+ * passed to co_create may have changed or become invalidated
+ * before call to co_switch, as said pointer would now be set
+ * when calling co_switch, instead.
+ *****/
+
+#include "test.h"
+
+cothread_t thread[3];
+
+namespace co_arg {
+ int param_x;
+ int param_y;
+};
+
+//one could also call this co_init or somesuch if they preferred ...
+void co_switch(cothread_t thread, int param_x, int param_y) {
+ co_arg::param_x = param_x;
+ co_arg::param_y = param_y;
+ co_switch(thread);
+}
+
+void co_entrypoint() {
+int param_x = co_arg::param_x;
+int param_y = co_arg::param_y;
+ printf("co_entrypoint(%d, %d)\n", param_x, param_y);
+ co_switch(thread[0]);
+
+//co_arg::param_x will change here (due to co_switch(cothread_t, int, int) call changing values),
+//however, param_x and param_y will persist as they are thread local
+
+ printf("co_entrypoint(%d, %d)\n", param_x, param_y);
+ co_switch(thread[0]);
+ throw;
+}
+
+int main() {
+ printf("cothread parameterized function example\n\n");
+
+ thread[0] = co_active();
+ thread[1] = co_create(65536, co_entrypoint);
+ thread[2] = co_create(65536, co_entrypoint);
+
+//use specialized co_switch(cothread_t, int, int) for initial co_switch call
+ co_switch(thread[1], 1, 2);
+ co_switch(thread[2], 4, 8);
+
+//after first call, entry point arguments have been initialized, standard
+//co_switch(cothread_t) can be used from now on
+ co_switch(thread[2]);
+ co_switch(thread[1]);
+
+ printf("\ndone\n");
+#if defined(_MSC_VER) || defined(__DJGPP__)
+ getch();
+#endif
+ return 0;
+}
diff --git a/src/lib/cothread/test/test_timing.cpp b/src/lib/cothread/test/test_timing.cpp
new file mode 100644
index 00000000000..0232e47e0d6
--- /dev/null
+++ b/src/lib/cothread/test/test_timing.cpp
@@ -0,0 +1,52 @@
+#include "test.h"
+enum { Iterations = 500000000 };
+
+namespace thread {
+ cothread_t x;
+ cothread_t y;
+ volatile int counter;
+}
+
+void co_timingtest() {
+ for(;;) {
+ thread::counter++;
+ co_switch(thread::x);
+ }
+}
+
+void sub_timingtest() {
+ thread::counter++;
+}
+
+int main() {
+ printf("context-switching timing test\n\n");
+ time_t start, end;
+ int i, t1, t2;
+
+ start = clock();
+ for(thread::counter = 0, i = 0; i < Iterations; i++) {
+ sub_timingtest();
+ }
+ end = clock();
+
+ t1 = (int)difftime(end, start);
+ printf("%2.3f seconds per 50 million subroutine calls (%d iterations)\n", (float)t1 / CLOCKS_PER_SEC, thread::counter);
+
+ thread::x = co_active();
+ thread::y = co_create(65536, co_timingtest);
+
+ start = clock();
+ for(thread::counter = 0, i = 0; i < Iterations; i++) {
+ co_switch(thread::y);
+ }
+ end = clock();
+
+ co_delete(thread::y);
+
+ t2 = (int)difftime(end, start);
+ printf("%2.3f seconds per 100 million co_switch calls (%d iterations)\n", (float)t2 / CLOCKS_PER_SEC, thread::counter);
+
+ printf("co_switch skew = %fx\n\n", (double)t2 / (double)t1);
+ return 0;
+}
+
diff --git a/src/lib/cothread/ucontext.c b/src/lib/cothread/ucontext.c
new file mode 100644
index 00000000000..17472f6bccf
--- /dev/null
+++ b/src/lib/cothread/ucontext.c
@@ -0,0 +1,67 @@
+/*
+ libco.ucontext (2008-01-28)
+ author: Nach
+ license: public domain
+*/
+
+/*
+ * WARNING: the overhead of POSIX ucontext is very high,
+ * assembly versions of libco or libco_sjlj should be much faster
+ *
+ * This library only exists for two reasons:
+ * 1 - as an initial test for the viability of a ucontext implementation
+ * 2 - to demonstrate the power and speed of libco over existing implementations,
+ * such as pth (which defaults to wrapping ucontext on unix targets)
+ *
+ * Use this library only as a *last resort*
+ */
+
+#define LIBCO_C
+#include "libco.h"
+#include <stdlib.h>
+#include <ucontext.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static thread_local ucontext_t co_primary;
+static thread_local ucontext_t *co_running = 0;
+
+cothread_t co_active() {
+ if(!co_running) co_running = &co_primary;
+ return (cothread_t)co_running;
+}
+
+cothread_t co_create(unsigned int heapsize, void (*coentry)(void)) {
+ if(!co_running) co_running = &co_primary;
+ ucontext_t *thread = (ucontext_t*)malloc(sizeof(ucontext_t));
+ if(thread) {
+ if((!getcontext(thread) && !(thread->uc_stack.ss_sp = 0)) && (thread->uc_stack.ss_sp = malloc(heapsize))) {
+ thread->uc_link = co_running;
+ thread->uc_stack.ss_size = heapsize;
+ makecontext(thread, coentry, 0);
+ } else {
+ co_delete((cothread_t)thread);
+ thread = 0;
+ }
+ }
+ return (cothread_t)thread;
+}
+
+void co_delete(cothread_t cothread) {
+ if(cothread) {
+ if(((ucontext_t*)cothread)->uc_stack.ss_sp) { free(((ucontext_t*)cothread)->uc_stack.ss_sp); }
+ free(cothread);
+ }
+}
+
+void co_switch(cothread_t cothread) {
+ ucontext_t *old_thread = co_running;
+ co_running = (ucontext_t*)cothread;
+ swapcontext(old_thread, co_running);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/lib/cothread/x86.c b/src/lib/cothread/x86.c
new file mode 100644
index 00000000000..d8f820b0665
--- /dev/null
+++ b/src/lib/cothread/x86.c
@@ -0,0 +1,93 @@
+/*
+ libco.x86 (2009-10-12)
+ author: byuu
+ license: public domain
+*/
+
+#define LIBCO_C
+#include "libco.h"
+#include <assert.h>
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(_MSC_VER)
+ #define fastcall __fastcall
+#elif defined(__GNUC__)
+ #define fastcall __attribute__((fastcall))
+#else
+ #error "libco: please define fastcall macro"
+#endif
+
+static thread_local long co_active_buffer[64];
+static thread_local cothread_t co_active_handle = 0;
+static void (fastcall *co_swap)(cothread_t, cothread_t) = 0;
+
+//ABI: fastcall
+static unsigned char co_swap_function[] = {
+ 0x89, 0x22, 0x8B, 0x21, 0x58, 0x89, 0x6A, 0x04, 0x89, 0x72, 0x08, 0x89, 0x7A, 0x0C, 0x89, 0x5A,
+ 0x10, 0x8B, 0x69, 0x04, 0x8B, 0x71, 0x08, 0x8B, 0x79, 0x0C, 0x8B, 0x59, 0x10, 0xFF, 0xE0,
+};
+
+#ifdef _WIN32
+ #include <windows.h>
+
+ void co_init() {
+ DWORD old_privileges;
+ VirtualProtect(co_swap_function, sizeof co_swap_function, PAGE_EXECUTE_READWRITE, &old_privileges);
+ }
+#else
+ #include <unistd.h>
+ #include <sys/mman.h>
+
+ void co_init() {
+ unsigned long addr = (unsigned long)co_swap_function;
+ unsigned long base = addr - (addr % sysconf(_SC_PAGESIZE));
+ unsigned long size = (addr - base) + sizeof co_swap_function;
+ mprotect((void*)base, size, PROT_READ | PROT_WRITE | PROT_EXEC);
+ }
+#endif
+
+static void crash() {
+ assert(0); /* called only if cothread_t entrypoint returns */
+}
+
+cothread_t co_active() {
+ if(!co_active_handle) co_active_handle = &co_active_buffer;
+ return co_active_handle;
+}
+
+cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
+ cothread_t handle;
+ if(!co_swap) {
+ co_init();
+ co_swap = (void (fastcall*)(cothread_t, cothread_t))co_swap_function;
+ }
+ if(!co_active_handle) co_active_handle = &co_active_buffer;
+ size += 256; /* allocate additional space for storage */
+ size &= ~15; /* align stack to 16-byte boundary */
+
+ if(handle = (cothread_t)malloc(size)) {
+ long *p = (long*)((char*)handle + size); /* seek to top of stack */
+ *--p = (long)crash; /* crash if entrypoint returns */
+ *--p = (long)entrypoint; /* start of function */
+ *(long*)handle = (long)p; /* stack pointer */
+ }
+
+ return handle;
+}
+
+void co_delete(cothread_t handle) {
+ free(handle);
+}
+
+void co_switch(cothread_t handle) {
+ register cothread_t co_previous_handle = co_active_handle;
+ co_swap(co_active_handle = handle, co_previous_handle);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/lib/lib.mak b/src/lib/lib.mak
index 433055258e1..8d49ecfe700 100644
--- a/src/lib/lib.mak
+++ b/src/lib/lib.mak
@@ -19,6 +19,7 @@ OBJDIRS += \
$(LIBOBJ)/formats \
$(LIBOBJ)/zlib \
$(LIBOBJ)/softfloat \
+ $(LIBOBJ)/cothread \
@@ -78,7 +79,7 @@ $(LIBOBJ)/expat/%.o: $(LIBSRC)/explat/%.c | $(OSPREBUILD)
# formats library objects
#-------------------------------------------------
-EXPATOBJS = \
+FORMATSOBJS = \
$(LIBOBJ)/formats/cassimg.o \
$(LIBOBJ)/formats/flopimg.o \
$(LIBOBJ)/formats/imageutl.o \
@@ -152,8 +153,8 @@ EXPATOBJS = \
$(LIBOBJ)/formats/z80ne_dsk.o \
$(LIBOBJ)/formats/zx81_p.o \
+$(OBJ)/libformats.a: $(FORMATSOBJS)
-$(OBJ)/libformats.a: $(EXPATOBJS)
#-------------------------------------------------
@@ -180,6 +181,8 @@ $(LIBOBJ)/zlib/%.o: $(LIBSRC)/zlib/%.c | $(OSPREBUILD)
@echo Compiling $<...
$(CC) $(CDEFS) $(CCOMFLAGS) $(CONLYFLAGS) -c $< -o $@
+
+
#-------------------------------------------------
# SoftFloat library objects
#-------------------------------------------------
@@ -194,3 +197,17 @@ $(OBJ)/libsoftfloat.a: $(SOFTFLOATOBJS)
$(LIBOBJ)/softfloat/softfloat.o: $(LIBSRC)/softfloat/softfloat.c $(LIBSRC)/softfloat/softfloat.h $(LIBSRC)/softfloat/softfloat-macros $(LIBSRC)/softfloat/softfloat-specialize
+
+
+#-------------------------------------------------
+# cothread library objects
+#-------------------------------------------------
+
+COTHREADOBJS = \
+ $(LIBOBJ)/cothread/libco.o
+
+$(OBJ)/libco.a: $(COTHREADOBJS)
+
+$(LIBOBJ)/cothread/%.o: $(LIBSRC)/cothread/%.c | $(OSPREBUILD)
+ @echo Compiling $<...
+ $(CC) -c -fomit-frame-pointer $< -o $@