CRLF -> LF

This commit is contained in:
misson20000 2018-09-15 03:27:05 -07:00
parent bde5905eba
commit fa76a3cb60
5 changed files with 2133 additions and 2133 deletions

View File

@ -1,358 +1,358 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2018 - misson2000
* Copyright (C) 2018 - m4xw
* Copyright (C) 2018 - lifajucejo
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdint.h>
#include <sys/unistd.h>
#include <switch.h>
#include <queues/fifo_queue.h>
#include "../audio_driver.h"
#include "../../verbosity.h"
#include "../../tasks/tasks_internal.h"
#define THREAD_STACK_SIZE (1024 * 8)
#define AUDIO_THREAD_CPU 2
#define CHANNELCOUNT 2
#define BYTESPERSAMPLE sizeof(uint16_t)
#define SAMPLE_SIZE (CHANNELCOUNT * BYTESPERSAMPLE)
#define AUDIO_BUFFER_COUNT 2
static inline void lockMutex(Mutex* mtx)
{
mutexLock(mtx);
}
typedef struct
{
fifo_buffer_t* fifo;
Mutex fifoLock;
CondVar cond;
Mutex condLock;
size_t fifoSize;
volatile bool running;
bool nonblocking;
bool is_paused;
AudioOutBuffer buffer[AUDIO_BUFFER_COUNT];
Thread thread;
unsigned latency;
uint32_t sampleRate;
} switch_thread_audio_t;
static void mainLoop(void* data)
{
switch_thread_audio_t* swa = (switch_thread_audio_t*)data;
if (!swa)
return;
RARCH_LOG("[Audio]: start mainLoop cpu %u tid %u\n", svcGetCurrentProcessorNumber(), swa->thread.handle);
for (int i = 0; i < AUDIO_BUFFER_COUNT; i++)
{
swa->buffer[i].next = NULL; /* Unused */
swa->buffer[i].buffer_size = swa->fifoSize;
swa->buffer[i].buffer = memalign(0x1000, swa->buffer[i].buffer_size);
swa->buffer[i].data_size = swa->buffer[i].buffer_size;
swa->buffer[i].data_offset = 0;
memset(swa->buffer[i].buffer, 0, swa->buffer[i].buffer_size);
audoutAppendAudioOutBuffer(&swa->buffer[i]);
}
AudioOutBuffer* released_out_buffer = NULL;
u32 released_out_count;
Result rc;
while (swa->running)
{
if (!released_out_buffer)
{
rc = audoutWaitPlayFinish(&released_out_buffer, &released_out_count, U64_MAX);
if (R_FAILED(rc))
{
swa->running = false;
RARCH_LOG("[Audio]: audoutGetReleasedAudioOutBuffer failed: %d\n", (int)rc);
break;
}
released_out_buffer->data_size = 0;
}
size_t bufAvail = released_out_buffer->buffer_size - released_out_buffer->data_size;
lockMutex(&swa->fifoLock);
size_t avail = fifo_read_avail(swa->fifo);
size_t to_write = MIN(avail, bufAvail);
if (to_write > 0)
fifo_read(swa->fifo, ((u8*)released_out_buffer->buffer) + released_out_buffer->data_size, to_write);
mutexUnlock(&swa->fifoLock);
condvarWakeAll(&swa->cond);
released_out_buffer->data_size += to_write;
if (released_out_buffer->data_size >= released_out_buffer->buffer_size / 2)
{
rc = audoutAppendAudioOutBuffer(released_out_buffer);
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: audoutAppendAudioOutBuffer failed: %d\n", (int)rc);
}
released_out_buffer = NULL;
}
else
svcSleepThread(16000000); /* 16ms */
}
}
static void *switch_thread_audio_init(const char *device, unsigned rate, unsigned latency, unsigned block_frames, unsigned *new_rate)
{
(void)device;
switch_thread_audio_t *swa = (switch_thread_audio_t *)calloc(1, sizeof(switch_thread_audio_t));
if (!swa)
return NULL;
swa->running = true;
swa->nonblocking = true;
swa->is_paused = true;
swa->latency = MAX(latency, 8);
Result rc = audoutInitialize();
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: audio init failed %d\n", (int)rc);
return NULL;
}
rc = audoutStartAudioOut();
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: audio start init failed: %d\n", (int)rc);
return NULL;
}
swa->sampleRate = audoutGetSampleRate();
*new_rate = swa->sampleRate;
mutexInit(&swa->fifoLock);
swa->fifoSize = (swa->sampleRate * SAMPLE_SIZE * swa->latency) / 1000;
swa->fifo = fifo_new(swa->fifoSize);
condvarInit(&swa->cond);
RARCH_LOG("[Audio]: switch_thread_audio_init device %s requested rate %hu rate %hu latency %hu block_frames %hu fifoSize %lu\n",
device, rate, swa->sampleRate, swa->latency, block_frames, swa->fifoSize);
u32 prio;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
rc = threadCreate(&swa->thread, &mainLoop, (void*)swa, THREAD_STACK_SIZE, prio + 1, AUDIO_THREAD_CPU);
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: thread creation failed create %u\n", swa->thread.handle);
swa->running = false;
return NULL;
}
if (R_FAILED(threadStart(&swa->thread)))
{
RARCH_LOG("[Audio]: thread creation failed start %u\n", swa->thread.handle);
threadClose(&swa->thread);
swa->running = false;
return NULL;
}
return swa;
}
static bool switch_thread_audio_start(void *data, bool is_shutdown)
{
/* RARCH_LOG("[Audio]: switch_thread_audio_start\n"); */
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return false;
swa->is_paused = false;
return true;
}
static bool switch_thread_audio_stop(void *data)
{
switch_thread_audio_t* swa = (switch_thread_audio_t*)data;
if (!swa)
return false;
swa->is_paused = true;
return true;
}
static void switch_thread_audio_free(void *data)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return;
if (swa->running)
{
swa->running = false;
threadWaitForExit(&swa->thread);
threadClose(&swa->thread);
}
audoutStopAudioOut();
audoutExit();
if (swa->fifo)
{
fifo_free(swa->fifo);
swa->fifo = NULL;
}
for (int i = 0; i < AUDIO_BUFFER_COUNT; i++)
free(swa->buffer[i].buffer);
free(swa);
swa = NULL;
}
static ssize_t switch_thread_audio_write(void *data, const void *buf, size_t size)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa || !swa->running)
return 0;
size_t avail;
size_t written;
if (swa->nonblocking)
{
lockMutex(&swa->fifoLock);
avail = fifo_write_avail(swa->fifo);
written = MIN(avail, size);
if (written > 0)
{
fifo_write(swa->fifo, buf, written);
}
mutexUnlock(&swa->fifoLock);
}
else
{
written = 0;
while (written < size && swa->running)
{
lockMutex(&swa->fifoLock);
avail = fifo_write_avail(swa->fifo);
if (avail == 0)
{
mutexUnlock(&swa->fifoLock);
lockMutex(&swa->condLock);
if (swa->running)
condvarWait(&swa->cond, &swa->condLock);
mutexUnlock(&swa->condLock);
}
else
{
size_t write_amt = MIN(size - written, avail);
fifo_write(swa->fifo, (const char*)buf + written, write_amt);
mutexUnlock(&swa->fifoLock);
written += write_amt;
}
}
}
return written;
}
static bool switch_thread_audio_alive(void *data)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return false;
return !swa->is_paused;
}
static void switch_thread_audio_set_nonblock_state(void *data, bool state)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (swa)
swa->nonblocking = state;
}
static bool switch_thread_audio_use_float(void *data)
{
(void)data;
return false;
}
static size_t switch_thread_audio_write_avail(void *data)
{
switch_thread_audio_t* swa = (switch_thread_audio_t*)data;
lockMutex(&swa->fifoLock);
size_t val = fifo_write_avail(swa->fifo);
mutexUnlock(&swa->fifoLock);
return val;
}
size_t switch_thread_audio_buffer_size(void *data)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return 0;
return swa->fifoSize;
}
audio_driver_t audio_switch_thread = {
switch_thread_audio_init,
switch_thread_audio_write,
switch_thread_audio_stop,
switch_thread_audio_start,
switch_thread_audio_alive,
switch_thread_audio_set_nonblock_state,
switch_thread_audio_free,
switch_thread_audio_use_float,
"switch_thread",
NULL, /* device_list_new */
NULL, /* device_list_free */
switch_thread_audio_write_avail,
switch_thread_audio_buffer_size
};
/* vim: set ts=6 sw=6 sts=6: */
/* RetroArch - A frontend for libretro.
* Copyright (C) 2018 - misson2000
* Copyright (C) 2018 - m4xw
* Copyright (C) 2018 - lifajucejo
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdint.h>
#include <sys/unistd.h>
#include <switch.h>
#include <queues/fifo_queue.h>
#include "../audio_driver.h"
#include "../../verbosity.h"
#include "../../tasks/tasks_internal.h"
#define THREAD_STACK_SIZE (1024 * 8)
#define AUDIO_THREAD_CPU 2
#define CHANNELCOUNT 2
#define BYTESPERSAMPLE sizeof(uint16_t)
#define SAMPLE_SIZE (CHANNELCOUNT * BYTESPERSAMPLE)
#define AUDIO_BUFFER_COUNT 2
static inline void lockMutex(Mutex* mtx)
{
mutexLock(mtx);
}
typedef struct
{
fifo_buffer_t* fifo;
Mutex fifoLock;
CondVar cond;
Mutex condLock;
size_t fifoSize;
volatile bool running;
bool nonblocking;
bool is_paused;
AudioOutBuffer buffer[AUDIO_BUFFER_COUNT];
Thread thread;
unsigned latency;
uint32_t sampleRate;
} switch_thread_audio_t;
static void mainLoop(void* data)
{
switch_thread_audio_t* swa = (switch_thread_audio_t*)data;
if (!swa)
return;
RARCH_LOG("[Audio]: start mainLoop cpu %u tid %u\n", svcGetCurrentProcessorNumber(), swa->thread.handle);
for (int i = 0; i < AUDIO_BUFFER_COUNT; i++)
{
swa->buffer[i].next = NULL; /* Unused */
swa->buffer[i].buffer_size = swa->fifoSize;
swa->buffer[i].buffer = memalign(0x1000, swa->buffer[i].buffer_size);
swa->buffer[i].data_size = swa->buffer[i].buffer_size;
swa->buffer[i].data_offset = 0;
memset(swa->buffer[i].buffer, 0, swa->buffer[i].buffer_size);
audoutAppendAudioOutBuffer(&swa->buffer[i]);
}
AudioOutBuffer* released_out_buffer = NULL;
u32 released_out_count;
Result rc;
while (swa->running)
{
if (!released_out_buffer)
{
rc = audoutWaitPlayFinish(&released_out_buffer, &released_out_count, U64_MAX);
if (R_FAILED(rc))
{
swa->running = false;
RARCH_LOG("[Audio]: audoutGetReleasedAudioOutBuffer failed: %d\n", (int)rc);
break;
}
released_out_buffer->data_size = 0;
}
size_t bufAvail = released_out_buffer->buffer_size - released_out_buffer->data_size;
lockMutex(&swa->fifoLock);
size_t avail = fifo_read_avail(swa->fifo);
size_t to_write = MIN(avail, bufAvail);
if (to_write > 0)
fifo_read(swa->fifo, ((u8*)released_out_buffer->buffer) + released_out_buffer->data_size, to_write);
mutexUnlock(&swa->fifoLock);
condvarWakeAll(&swa->cond);
released_out_buffer->data_size += to_write;
if (released_out_buffer->data_size >= released_out_buffer->buffer_size / 2)
{
rc = audoutAppendAudioOutBuffer(released_out_buffer);
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: audoutAppendAudioOutBuffer failed: %d\n", (int)rc);
}
released_out_buffer = NULL;
}
else
svcSleepThread(16000000); /* 16ms */
}
}
static void *switch_thread_audio_init(const char *device, unsigned rate, unsigned latency, unsigned block_frames, unsigned *new_rate)
{
(void)device;
switch_thread_audio_t *swa = (switch_thread_audio_t *)calloc(1, sizeof(switch_thread_audio_t));
if (!swa)
return NULL;
swa->running = true;
swa->nonblocking = true;
swa->is_paused = true;
swa->latency = MAX(latency, 8);
Result rc = audoutInitialize();
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: audio init failed %d\n", (int)rc);
return NULL;
}
rc = audoutStartAudioOut();
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: audio start init failed: %d\n", (int)rc);
return NULL;
}
swa->sampleRate = audoutGetSampleRate();
*new_rate = swa->sampleRate;
mutexInit(&swa->fifoLock);
swa->fifoSize = (swa->sampleRate * SAMPLE_SIZE * swa->latency) / 1000;
swa->fifo = fifo_new(swa->fifoSize);
condvarInit(&swa->cond);
RARCH_LOG("[Audio]: switch_thread_audio_init device %s requested rate %hu rate %hu latency %hu block_frames %hu fifoSize %lu\n",
device, rate, swa->sampleRate, swa->latency, block_frames, swa->fifoSize);
u32 prio;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
rc = threadCreate(&swa->thread, &mainLoop, (void*)swa, THREAD_STACK_SIZE, prio + 1, AUDIO_THREAD_CPU);
if (R_FAILED(rc))
{
RARCH_LOG("[Audio]: thread creation failed create %u\n", swa->thread.handle);
swa->running = false;
return NULL;
}
if (R_FAILED(threadStart(&swa->thread)))
{
RARCH_LOG("[Audio]: thread creation failed start %u\n", swa->thread.handle);
threadClose(&swa->thread);
swa->running = false;
return NULL;
}
return swa;
}
static bool switch_thread_audio_start(void *data, bool is_shutdown)
{
/* RARCH_LOG("[Audio]: switch_thread_audio_start\n"); */
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return false;
swa->is_paused = false;
return true;
}
static bool switch_thread_audio_stop(void *data)
{
switch_thread_audio_t* swa = (switch_thread_audio_t*)data;
if (!swa)
return false;
swa->is_paused = true;
return true;
}
static void switch_thread_audio_free(void *data)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return;
if (swa->running)
{
swa->running = false;
threadWaitForExit(&swa->thread);
threadClose(&swa->thread);
}
audoutStopAudioOut();
audoutExit();
if (swa->fifo)
{
fifo_free(swa->fifo);
swa->fifo = NULL;
}
for (int i = 0; i < AUDIO_BUFFER_COUNT; i++)
free(swa->buffer[i].buffer);
free(swa);
swa = NULL;
}
static ssize_t switch_thread_audio_write(void *data, const void *buf, size_t size)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa || !swa->running)
return 0;
size_t avail;
size_t written;
if (swa->nonblocking)
{
lockMutex(&swa->fifoLock);
avail = fifo_write_avail(swa->fifo);
written = MIN(avail, size);
if (written > 0)
{
fifo_write(swa->fifo, buf, written);
}
mutexUnlock(&swa->fifoLock);
}
else
{
written = 0;
while (written < size && swa->running)
{
lockMutex(&swa->fifoLock);
avail = fifo_write_avail(swa->fifo);
if (avail == 0)
{
mutexUnlock(&swa->fifoLock);
lockMutex(&swa->condLock);
if (swa->running)
condvarWait(&swa->cond, &swa->condLock);
mutexUnlock(&swa->condLock);
}
else
{
size_t write_amt = MIN(size - written, avail);
fifo_write(swa->fifo, (const char*)buf + written, write_amt);
mutexUnlock(&swa->fifoLock);
written += write_amt;
}
}
}
return written;
}
static bool switch_thread_audio_alive(void *data)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return false;
return !swa->is_paused;
}
static void switch_thread_audio_set_nonblock_state(void *data, bool state)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (swa)
swa->nonblocking = state;
}
static bool switch_thread_audio_use_float(void *data)
{
(void)data;
return false;
}
static size_t switch_thread_audio_write_avail(void *data)
{
switch_thread_audio_t* swa = (switch_thread_audio_t*)data;
lockMutex(&swa->fifoLock);
size_t val = fifo_write_avail(swa->fifo);
mutexUnlock(&swa->fifoLock);
return val;
}
size_t switch_thread_audio_buffer_size(void *data)
{
switch_thread_audio_t *swa = (switch_thread_audio_t *)data;
if (!swa)
return 0;
return swa->fifoSize;
}
audio_driver_t audio_switch_thread = {
switch_thread_audio_init,
switch_thread_audio_write,
switch_thread_audio_stop,
switch_thread_audio_start,
switch_thread_audio_alive,
switch_thread_audio_set_nonblock_state,
switch_thread_audio_free,
switch_thread_audio_use_float,
"switch_thread",
NULL, /* device_list_new */
NULL, /* device_list_free */
switch_thread_audio_write_avail,
switch_thread_audio_buffer_size
};
/* vim: set ts=6 sw=6 sts=6: */

File diff suppressed because it is too large Load Diff

View File

@ -1,59 +1,59 @@
#ifndef SWITCH_COMMON_H__
#define SWITCH_COMMON_H__
#include <switch.h>
#include <gfx/scaler/scaler.h>
typedef struct
{
bool vsync;
bool rgb32;
bool smooth; // bilinear
unsigned width, height;
unsigned rotation;
struct video_viewport vp;
struct texture_image *overlay;
bool overlay_enabled;
bool in_menu;
struct
{
bool enable;
bool fullscreen;
uint32_t *pixels;
uint32_t width;
uint32_t height;
unsigned tgtw;
unsigned tgth;
struct scaler_ctx scaler;
} menu_texture;
struct
{
uint32_t width;
uint32_t height;
uint32_t x_offset;
} hw_scale;
uint32_t image[1280 * 720];
uint32_t tmp_image[1280 * 720];
u32 cnt;
struct scaler_ctx scaler;
uint32_t last_width;
uint32_t last_height;
bool keep_aspect;
bool should_resize;
bool need_clear;
bool is_threaded;
bool o_size;
uint32_t o_height;
uint32_t o_width;
} switch_video_t;
void gfx_slow_swizzling_blit(uint32_t *buffer, uint32_t *image, int w, int h, int tx, int ty, bool blend);
#endif
#ifndef SWITCH_COMMON_H__
#define SWITCH_COMMON_H__
#include <switch.h>
#include <gfx/scaler/scaler.h>
typedef struct
{
bool vsync;
bool rgb32;
bool smooth; // bilinear
unsigned width, height;
unsigned rotation;
struct video_viewport vp;
struct texture_image *overlay;
bool overlay_enabled;
bool in_menu;
struct
{
bool enable;
bool fullscreen;
uint32_t *pixels;
uint32_t width;
uint32_t height;
unsigned tgtw;
unsigned tgth;
struct scaler_ctx scaler;
} menu_texture;
struct
{
uint32_t width;
uint32_t height;
uint32_t x_offset;
} hw_scale;
uint32_t image[1280 * 720];
uint32_t tmp_image[1280 * 720];
u32 cnt;
struct scaler_ctx scaler;
uint32_t last_width;
uint32_t last_height;
bool keep_aspect;
bool should_resize;
bool need_clear;
bool is_threaded;
bool o_size;
uint32_t o_height;
uint32_t o_width;
} switch_video_t;
void gfx_slow_swizzling_blit(uint32_t *buffer, uint32_t *image, int w, int h, int tx, int ty, bool blend);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,207 +1,207 @@
/* Copyright (C) 2018 - M4xw <m4x@m4xw.net>, RetroArch Team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (switch_pthread.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 _SWITCH_PTHREAD_WRAP_
#define _SWITCH_PTHREAD_WRAP_
#include <time.h>
#include <stdio.h>
#include <switch.h>
#include <errno.h>
#include "../include/retro_inline.h"
#include "../../verbosity.h"
#define THREADVARS_MAGIC 0x21545624 // !TV$
// This structure is exactly 0x20 bytes, if more is needed modify getThreadVars() below
typedef struct
{
// Magic value used to check if the struct is initialized
u32 magic;
// Thread handle, for mutexes
Handle handle;
// Pointer to the current thread (if exists)
Thread *thread_ptr;
// Pointer to this thread's newlib state
struct _reent *reent;
// Pointer to this thread's thread-local segment
void *tls_tp; // !! Offset needs to be TLS+0x1F8 for __aarch64_read_tp !!
} ThreadVars;
static INLINE ThreadVars *getThreadVars(void)
{
return (ThreadVars *)((u8 *)armGetTls() + 0x1E0);
}
#define STACKSIZE (8 * 1024)
/* libnx threads return void but pthreads return void pointer */
static uint32_t threadCounter = 1;
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
{
u32 prio = 0;
Thread new_switch_thread;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
// Launch threads on Core 1
int rc = threadCreate(&new_switch_thread, (void (*)(void *))start_routine, arg, STACKSIZE, prio - 1, 1);
if (R_FAILED(rc))
{
return EAGAIN;
}
printf("[Threading]: Starting Thread(#%i)\n", threadCounter);
if (R_FAILED(threadStart(&new_switch_thread)))
{
threadClose(&new_switch_thread);
return -1;
}
*thread = new_switch_thread;
return 0;
}
void pthread_exit(void *retval)
{
(void)retval;
printf("[Threading]: Exiting Thread\n");
svcExitThread();
}
static INLINE Thread threadGetCurrent(void)
{
ThreadVars *tv = getThreadVars();
return *tv->thread_ptr;
}
static INLINE pthread_t pthread_self(void)
{
return threadGetCurrent();
}
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
mutexInit(mutex);
return 0;
}
INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
// Nothing
*mutex = 0;
return 0;
}
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
{
mutexLock(mutex);
return 0;
}
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
mutexUnlock(mutex);
return 0;
}
INLINE int pthread_detach(pthread_t thread)
{
(void)thread;
// Nothing for now
return 0;
}
static INLINE int pthread_join(pthread_t thread, void **retval)
{
printf("[Threading]: Waiting for Thread Exit\n");
threadWaitForExit(&thread);
threadClose(&thread);
return 0;
}
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
return mutexTryLock(mutex) ? 0 : 1;
}
static INLINE int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
condvarWait(cond, mutex);
return 0;
}
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
{
condvarWaitTimeout(cond, mutex, abstime->tv_nsec);
return 0;
}
static INLINE int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
{
condvarInit(cond);
return 0;
}
static INLINE int pthread_cond_signal(pthread_cond_t *cond)
{
condvarWakeOne(cond);
return 0;
}
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
{
condvarWakeAll(cond);
return 0;
}
INLINE int pthread_cond_destroy(pthread_cond_t *cond)
{
// Nothing
return 0;
}
INLINE int pthread_equal(pthread_t t1, pthread_t t2)
{
if (t1.handle == t2.handle)
return 1;
return 0;
}
#endif
/* Copyright (C) 2018 - M4xw <m4x@m4xw.net>, RetroArch Team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (switch_pthread.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 _SWITCH_PTHREAD_WRAP_
#define _SWITCH_PTHREAD_WRAP_
#include <time.h>
#include <stdio.h>
#include <switch.h>
#include <errno.h>
#include "../include/retro_inline.h"
#include "../../verbosity.h"
#define THREADVARS_MAGIC 0x21545624 // !TV$
// This structure is exactly 0x20 bytes, if more is needed modify getThreadVars() below
typedef struct
{
// Magic value used to check if the struct is initialized
u32 magic;
// Thread handle, for mutexes
Handle handle;
// Pointer to the current thread (if exists)
Thread *thread_ptr;
// Pointer to this thread's newlib state
struct _reent *reent;
// Pointer to this thread's thread-local segment
void *tls_tp; // !! Offset needs to be TLS+0x1F8 for __aarch64_read_tp !!
} ThreadVars;
static INLINE ThreadVars *getThreadVars(void)
{
return (ThreadVars *)((u8 *)armGetTls() + 0x1E0);
}
#define STACKSIZE (8 * 1024)
/* libnx threads return void but pthreads return void pointer */
static uint32_t threadCounter = 1;
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
{
u32 prio = 0;
Thread new_switch_thread;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
// Launch threads on Core 1
int rc = threadCreate(&new_switch_thread, (void (*)(void *))start_routine, arg, STACKSIZE, prio - 1, 1);
if (R_FAILED(rc))
{
return EAGAIN;
}
printf("[Threading]: Starting Thread(#%i)\n", threadCounter);
if (R_FAILED(threadStart(&new_switch_thread)))
{
threadClose(&new_switch_thread);
return -1;
}
*thread = new_switch_thread;
return 0;
}
void pthread_exit(void *retval)
{
(void)retval;
printf("[Threading]: Exiting Thread\n");
svcExitThread();
}
static INLINE Thread threadGetCurrent(void)
{
ThreadVars *tv = getThreadVars();
return *tv->thread_ptr;
}
static INLINE pthread_t pthread_self(void)
{
return threadGetCurrent();
}
static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
{
mutexInit(mutex);
return 0;
}
INLINE int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
// Nothing
*mutex = 0;
return 0;
}
static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex)
{
mutexLock(mutex);
return 0;
}
static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
mutexUnlock(mutex);
return 0;
}
INLINE int pthread_detach(pthread_t thread)
{
(void)thread;
// Nothing for now
return 0;
}
static INLINE int pthread_join(pthread_t thread, void **retval)
{
printf("[Threading]: Waiting for Thread Exit\n");
threadWaitForExit(&thread);
threadClose(&thread);
return 0;
}
static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex)
{
return mutexTryLock(mutex) ? 0 : 1;
}
static INLINE int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
condvarWait(cond, mutex);
return 0;
}
static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
{
condvarWaitTimeout(cond, mutex, abstime->tv_nsec);
return 0;
}
static INLINE int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
{
condvarInit(cond);
return 0;
}
static INLINE int pthread_cond_signal(pthread_cond_t *cond)
{
condvarWakeOne(cond);
return 0;
}
static INLINE int pthread_cond_broadcast(pthread_cond_t *cond)
{
condvarWakeAll(cond);
return 0;
}
INLINE int pthread_cond_destroy(pthread_cond_t *cond)
{
// Nothing
return 0;
}
INLINE int pthread_equal(pthread_t t1, pthread_t t2)
{
if (t1.handle == t2.handle)
return 1;
return 0;
}
#endif