Commit 5d0ae2be authored by Libretro-Admin's avatar Libretro-Admin
Browse files

libco is not used - remove confusion by removing it

parent 7b16293e
Pipeline #119604 passed with stages
in 29 minutes and 12 seconds
/* Copyright (C) 2010-2018 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (libco.h).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef LIBCO_H
#define LIBCO_H
#include <retro_common_api.h>
#ifdef LIBCO_C
#ifdef LIBCO_MP
#define thread_local __thread
#else
#define thread_local
#endif
#endif
RETRO_BEGIN_DECLS
typedef void* cothread_t;
/**
* co_active:
*
* Gets the currently active context.
*
* Returns: active context.
**/
cothread_t co_active(void);
/**
* co_create:
* @int : stack size
* @funcptr : thread entry function callback
*
* Create a co_thread.
*
* Returns: cothread if successful, otherwise NULL.
*/
cothread_t co_create(unsigned int, void (*)(void));
/**
* co_delete:
* @cothread : cothread object
*
* Frees a co_thread.
*/
void co_delete(cothread_t cothread);
/**
* co_switch:
* @cothread : cothread object to switch to
*
* Do a context switch to @cothread.
*/
void co_switch(cothread_t cothread);
RETRO_END_DECLS
/* ifndef LIBCO_H */
#endif
/*
libco.aarch64 (2017-06-26)
author: webgeek1234
license: public domain
*/
#define LIBCO_C
#include "libco.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#ifndef IOS
#include <malloc.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
static thread_local uint64_t co_active_buffer[64];
static thread_local cothread_t co_active_handle;
asm (
".globl co_switch_aarch64\n"
".globl _co_switch_aarch64\n"
"co_switch_aarch64:\n"
"_co_switch_aarch64:\n"
" stp x8, x9, [x1]\n"
" stp x10, x11, [x1, #16]\n"
" stp x12, x13, [x1, #32]\n"
" stp x14, x15, [x1, #48]\n"
" str x19, [x1, #72]\n"
" stp x20, x21, [x1, #80]\n"
" stp x22, x23, [x1, #96]\n"
" stp x24, x25, [x1, #112]\n"
" stp x26, x27, [x1, #128]\n"
" stp x28, x29, [x1, #144]\n"
" mov x16, sp\n"
" stp x16, x30, [x1, #160]\n"
" ldp x8, x9, [x0]\n"
" ldp x10, x11, [x0, #16]\n"
" ldp x12, x13, [x0, #32]\n"
" ldp x14, x15, [x0, #48]\n"
" ldr x19, [x0, #72]\n"
" ldp x20, x21, [x0, #80]\n"
" ldp x22, x23, [x0, #96]\n"
" ldp x24, x25, [x0, #112]\n"
" ldp x26, x27, [x0, #128]\n"
" ldp x28, x29, [x0, #144]\n"
" ldp x16, x17, [x0, #160]\n"
" mov sp, x16\n"
" br x17\n"
);
/* ASM */
void co_switch_aarch64(cothread_t handle, cothread_t current);
static void crash(void)
{
/* Called only if cothread_t entrypoint returns. */
assert(0);
}
cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
size = (size + 1023) & ~1023;
cothread_t handle = 0;
#if HAVE_POSIX_MEMALIGN >= 1
if (posix_memalign(&handle, 1024, size + 512) < 0)
return 0;
#else
handle = memalign(1024, size + 512);
#endif
if (!handle)
return handle;
uint64_t *ptr = (uint64_t*)handle;
/* Non-volatiles. */
ptr[0] = 0; /* x8 */
ptr[1] = 0; /* x9 */
ptr[2] = 0; /* x10 */
ptr[3] = 0; /* x11 */
ptr[4] = 0; /* x12 */
ptr[5] = 0; /* x13 */
ptr[6] = 0; /* x14 */
ptr[7] = 0; /* x15 */
ptr[8] = 0; /* padding */
ptr[9] = 0; /* x19 */
ptr[10] = 0; /* x20 */
ptr[11] = 0; /* x21 */
ptr[12] = 0; /* x22 */
ptr[13] = 0; /* x23 */
ptr[14] = 0; /* x24 */
ptr[15] = 0; /* x25 */
ptr[16] = 0; /* x26 */
ptr[17] = 0; /* x27 */
ptr[18] = 0; /* x28 */
ptr[20] = (uintptr_t)ptr + size + 512 - 16; /* x30, stack pointer */
ptr[19] = ptr[20]; /* x29, frame pointer */
ptr[21] = (uintptr_t)entrypoint; /* PC (link register x31 gets saved here). */
return handle;
}
cothread_t co_active(void)
{
if (!co_active_handle)
co_active_handle = co_active_buffer;
return co_active_handle;
}
void co_delete(cothread_t handle)
{
free(handle);
}
void co_switch(cothread_t handle)
{
cothread_t co_previous_handle = co_active();
co_switch_aarch64(co_active_handle = handle, co_previous_handle);
}
#ifdef __cplusplus
}
#endif
/*
libco.amd64 (2009-10-12)
author: byuu
license: public domain
*/
#define LIBCO_C
#include <libco.h>
#include <assert.h>
#include <stdlib.h>
#if defined(__GNUC__) && !defined(_WIN32) && !defined(__cplusplus)
#define CO_USE_INLINE_ASM
#endif
#ifdef __cplusplus
extern "C" {
#endif
static thread_local long long co_active_buffer[64];
static thread_local cothread_t co_active_handle = 0;
#ifndef CO_USE_INLINE_ASM
static void (*co_swap)(cothread_t, cothread_t) = 0;
#endif
#ifdef _WIN32
/* ABI: Win64 */
static unsigned char co_swap_function[] = {
0x48, 0x89, 0x22, /* mov [rdx],rsp */
0x48, 0x8b, 0x21, /* mov rsp,[rcx] */
0x58, /* pop rax */
0x48, 0x89, 0x6a, 0x08, /* mov [rdx+0x8],rbp */
0x48, 0x89, 0x72, 0x10, /* mov [rdx+0x10],rsi */
0x48, 0x89, 0x7a, 0x18, /* mov [rdx+0x18],rdi */
0x48, 0x89, 0x5a, 0x20, /* mov [rdx+0x20],rbx */
0x4c, 0x89, 0x62, 0x28, /* mov [rdx+0x28],r12 */
0x4c, 0x89, 0x6a, 0x30, /* mov [rdx+0x30],r13 */
0x4c, 0x89, 0x72, 0x38, /* mov [rdx+0x38],r14 */
0x4c, 0x89, 0x7a, 0x40, /* mov [rdx+0x40],r15 */
0x48, 0x81, 0xc2, 0x80, 0x00, 0x00, 0x00, /* add rdx,0x80 */
0x48, 0x83, 0xe2, 0xf0, /* and rdx,-0x10 */
0x0f, 0x29, 0x32, /* movaps [rdx],xmm6 */
0x0f, 0x29, 0x7a, 0x10, /* movaps [rdx+0x10],xmm7 */
0x44, 0x0f, 0x29, 0x42, 0x20, /* movaps [rdx+0x20],xmm8 */
0x44, 0x0f, 0x29, 0x4a, 0x30, /* movaps [rdx+0x30],xmm9 */
0x44, 0x0f, 0x29, 0x52, 0x40, /* movaps [rdx+0x40],xmm10 */
0x44, 0x0f, 0x29, 0x5a, 0x50, /* movaps [rdx+0x50],xmm11 */
0x44, 0x0f, 0x29, 0x62, 0x60, /* movaps [rdx+0x60],xmm12 */
0x44, 0x0f, 0x29, 0x6a, 0x70, /* movaps [rdx+0x70],xmm13 */
0x44, 0x0f, 0x29, 0xb2, 0x80, 0x00, 0x00, 0x00, /* movaps [rdx+0x80],xmm14 */
0x44, 0x0f, 0x29, 0xba, 0x90, 0x00, 0x00, 0x00, /* movaps [rdx+0x90],xmm15 */
0x48, 0x8b, 0x69, 0x08, /* mov rbp,[rcx+0x8] */
0x48, 0x8b, 0x71, 0x10, /* mov rsi,[rcx+0x10] */
0x48, 0x8b, 0x79, 0x18, /* mov rdi,[rcx+0x18] */
0x48, 0x8b, 0x59, 0x20, /* mov rbx,[rcx+0x20] */
0x4c, 0x8b, 0x61, 0x28, /* mov r12,[rcx+0x28] */
0x4c, 0x8b, 0x69, 0x30, /* mov r13,[rcx+0x30] */
0x4c, 0x8b, 0x71, 0x38, /* mov r14,[rcx+0x38] */
0x4c, 0x8b, 0x79, 0x40, /* mov r15,[rcx+0x40] */
0x48, 0x81, 0xc1, 0x80, 0x00, 0x00, 0x00, /* add rcx,0x80 */
0x48, 0x83, 0xe1, 0xf0, /* and rcx,-0x10 */
0x0f, 0x29, 0x31, /* movaps [rcx],xmm6 */
0x0f, 0x29, 0x79, 0x10, /* movaps [rcx+0x10],xmm7 */
0x44, 0x0f, 0x29, 0x41, 0x20, /* movaps [rcx+0x20],xmm8 */
0x44, 0x0f, 0x29, 0x49, 0x30, /* movaps [rcx+0x30],xmm9 */
0x44, 0x0f, 0x29, 0x51, 0x40, /* movaps [rcx+0x40],xmm10 */
0x44, 0x0f, 0x29, 0x59, 0x50, /* movaps [rcx+0x50],xmm11 */
0x44, 0x0f, 0x29, 0x61, 0x60, /* movaps [rcx+0x60],xmm12 */
0x44, 0x0f, 0x29, 0x69, 0x70, /* movaps [rcx+0x70],xmm13 */
0x44, 0x0f, 0x29, 0xb1, 0x80, 0x00, 0x00, 0x00, /* movaps [rcx+0x80],xmm14 */
0x44, 0x0f, 0x29, 0xb9, 0x90, 0x00, 0x00, 0x00, /* movaps [rcx+0x90],xmm15 */
0xff, 0xe0, /* jmp rax */
};
#include <windows.h>
static void co_init(void)
{
DWORD old_privileges;
VirtualProtect(co_swap_function,
sizeof(co_swap_function), PAGE_EXECUTE_READWRITE, &old_privileges);
}
#else
/* ABI: SystemV */
#ifndef CO_USE_INLINE_ASM
static unsigned char co_swap_function[] = {
0x48, 0x89, 0x26, /* mov [rsi],rsp */
0x48, 0x8b, 0x27, /* mov rsp,[rdi] */
0x58, /* pop rax */
0x48, 0x89, 0x6e, 0x08, /* mov [rsi+0x08],rbp */
0x48, 0x89, 0x5e, 0x10, /* mov [rsi+0x10],rbx */
0x4c, 0x89, 0x66, 0x18, /* mov [rsi+0x18],r12 */
0x4c, 0x89, 0x6e, 0x20, /* mov [rsi+0x20],r13 */
0x4c, 0x89, 0x76, 0x28, /* mov [rsi+0x28],r14 */
0x4c, 0x89, 0x7e, 0x30, /* mov [rsi+0x30],r15 */
0x48, 0x8b, 0x6f, 0x08, /* mov rbp,[rdi+0x08] */
0x48, 0x8b, 0x5f, 0x10, /* mov rbx,[rdi+0x10] */
0x4c, 0x8b, 0x67, 0x18, /* mov r12,[rdi+0x18] */
0x4c, 0x8b, 0x6f, 0x20, /* mov r13,[rdi+0x20] */
0x4c, 0x8b, 0x77, 0x28, /* mov r14,[rdi+0x28] */
0x4c, 0x8b, 0x7f, 0x30, /* mov r15,[rdi+0x30] */
0xff, 0xe0, /* jmp rax */
};
#include <unistd.h>
#include <sys/mman.h>
static void co_init(void)
{
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);
}
#else
static void co_init(void) {}
#endif
#endif
static void crash(void)
{
assert(0); /* called only if cothread_t entrypoint returns */
}
cothread_t co_active(void)
{
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;
#ifndef CO_USE_INLINE_ASM
if(!co_swap)
{
co_init();
co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
}
#endif
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);
}
#ifndef CO_USE_INLINE_ASM
void co_switch(cothread_t handle)
{
register cothread_t co_previous_handle = co_active_handle;
co_swap(co_active_handle = handle, co_previous_handle);
}
#else
#ifdef __APPLE__
#define ASM_PREFIX "_"
#else
#define ASM_PREFIX ""
#endif
__asm__(
".intel_syntax noprefix \n"
".globl " ASM_PREFIX "co_switch \n"
ASM_PREFIX "co_switch: \n"
"mov rsi, [rip+" ASM_PREFIX "co_active_handle]\n"
"mov [rsi],rsp \n"
"mov [rsi+0x08],rbp \n"
"mov [rsi+0x10],rbx \n"
"mov [rsi+0x18],r12 \n"
"mov [rsi+0x20],r13 \n"
"mov [rsi+0x28],r14 \n"
"mov [rsi+0x30],r15 \n"
"mov [rip+" ASM_PREFIX "co_active_handle], rdi\n"
"mov rsp,[rdi] \n"
"mov rbp,[rdi+0x08] \n"
"mov rbx,[rdi+0x10] \n"
"mov r12,[rdi+0x18] \n"
"mov r13,[rdi+0x20] \n"
"mov r14,[rdi+0x28] \n"
"mov r15,[rdi+0x30] \n"
"ret \n"
".att_syntax \n"
);
#endif
#ifdef __cplusplus
}
#endif
/*
libco.armeabi (2013-04-05)
author: Themaister
license: public domain
*/
#define LIBCO_C
#include <libco.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
static thread_local uint32_t co_active_buffer[64];
static thread_local cothread_t co_active_handle;
__asm__ (
#if defined(__thumb2__)
".thumb\n"
".align 2\n"
".globl co_switch_arm\n"
".globl _co_switch_arm\n"
"co_switch_arm:\n"
"_co_switch_arm:\n"
" mov r3, sp\n"
" stmia r1!, {r4, r5, r6, r7, r8, r9, r10, r11}\n"
" stmia r1!, {r3, lr}\n"
" ldmia r0!, {r4, r5, r6, r7, r8, r9, r10, r11}\n"
" ldmfd r0!, { r3 }\n"
" mov sp, r3\n"
" ldmfd r0!, { r3 }\n"
" mov pc, r3\n"
#else
".arm\n"
".align 4\n"
".globl co_switch_arm\n"
".globl _co_switch_arm\n"
"co_switch_arm:\n"
"_co_switch_arm:\n"
" stmia r1!, {r4, r5, r6, r7, r8, r9, r10, r11, sp, lr}\n"
" ldmia r0!, {r4, r5, r6, r7, r8, r9, r10, r11, sp, pc}\n"
#endif
);
/* ASM */
void co_switch_arm(cothread_t handle, cothread_t current);
cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
size = (size + 1023) & ~1023;
cothread_t handle = 0;
#if defined(__APPLE__) || HAVE_POSIX_MEMALIGN >= 1
if (posix_memalign(&handle, 1024, size + 256) < 0)
return 0;
#else
handle = memalign(1024, size + 256);
#endif
if (!handle)
return handle;
uint32_t *ptr = (uint32_t*)handle;
/* Non-volatiles. */
ptr[0] = 0; /* r4 */
ptr[1] = 0; /* r5 */
ptr[2] = 0; /* r6 */
ptr[3] = 0; /* r7 */
ptr[4] = 0; /* r8 */
ptr[5] = 0; /* r9 */
ptr[6] = 0; /* r10 */
ptr[7] = 0; /* r11 */
/* Align stack to 64-bit */
ptr[8] = (uintptr_t)ptr + size + 256 - 8; /* r13, stack pointer */
ptr[9] = (uintptr_t)entrypoint; /* r15, PC (link register r14 gets saved here). */
return handle;
}
cothread_t co_active(void)
{
if (!co_active_handle)
co_active_handle = co_active_buffer;
return co_active_handle;
}
void co_delete(cothread_t handle)
{
free(handle);
}
void co_switch(cothread_t handle)
{
cothread_t co_previous_handle = co_active();
co_switch_arm(co_active_handle = handle, co_previous_handle);
}
#ifdef __cplusplus
}
#endif
/*
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(void)
{
if(!co_active_)
{
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
ConvertThreadToFiberEx(0, FIBER_FLAG_FLOAT_SWITCH);
#else
ConvertThreadToFiber(0);
#endif
co_active_ = GetCurrentFiber();
}
return co_active_;
}
cothread_t co_create(unsigned int heapsize, void (*coentry)(void))
{
if(!co_active_)
{
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
ConvertThreadToFiberEx(0, FIBER_FLAG_FLOAT_SWITCH);
#else
ConvertThreadToFiber(0);
#endif
co_active_ = GetCurrentFiber();
}
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
return (cothread_t)CreateFiberEx(heapsize, heapsize, FIBER_FLAG_FLOAT_SWITCH, co_thunk, (void*)coentry);
#else
return (cothread_t)CreateFiber(heapsize, co_thunk, (void*)coentry);
#endif
}
void co_delete(cothread_t cothread)
{
DeleteFiber(cothread);
}
void co_switch(cothread_t cothread)
{
co_active_ = cothread;
SwitchToFiber(cothread);
}
#ifdef __cplusplus
}
#endif
/*
libco
auto-selection module
license: public domain
*/
#if defined _MSC_VER
#include <Windows.h>
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
#include "fiber.c"
#elif defined _M_IX86
#include "x86.c"
#elif defined _M_AMD64