mirror of
https://github.com/libretro/beetle-lynx-libretro.git
synced 2024-11-23 16:21:03 +00:00
Take out threading
This commit is contained in:
parent
7c5852d5f6
commit
8619e54c64
45
libretro.cpp
45
libretro.cpp
@ -683,48 +683,3 @@ void MDFND_Sleep(unsigned int time)
|
||||
{
|
||||
retro_sleep(time);
|
||||
}
|
||||
|
||||
#ifdef WANT_THREADING
|
||||
MDFN_Thread *MDFND_CreateThread(int (*fn)(void *), void *data)
|
||||
{
|
||||
return (MDFN_Thread*)sthread_create((void (*)(void*))fn, data);
|
||||
}
|
||||
|
||||
void MDFND_WaitThread(MDFN_Thread *thr, int *val)
|
||||
{
|
||||
sthread_join((sthread_t*)thr);
|
||||
|
||||
if (val)
|
||||
{
|
||||
*val = 0;
|
||||
fprintf(stderr, "WaitThread relies on return value.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void MDFND_KillThread(MDFN_Thread *)
|
||||
{
|
||||
fprintf(stderr, "Killing a thread is a BAD IDEA!\n");
|
||||
}
|
||||
|
||||
MDFN_Mutex *MDFND_CreateMutex()
|
||||
{
|
||||
return (MDFN_Mutex*)slock_new();
|
||||
}
|
||||
|
||||
void MDFND_DestroyMutex(MDFN_Mutex *lock)
|
||||
{
|
||||
slock_free((slock_t*)lock);
|
||||
}
|
||||
|
||||
int MDFND_LockMutex(MDFN_Mutex *lock)
|
||||
{
|
||||
slock_lock((slock_t*)lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MDFND_UnlockMutex(MDFN_Mutex *lock)
|
||||
{
|
||||
slock_unlock((slock_t*)lock);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
@ -31,11 +31,6 @@
|
||||
#include "file.h"
|
||||
#include "FileWrapper.h"
|
||||
|
||||
#ifdef NEED_CD
|
||||
#include "cdrom/cdromif.h"
|
||||
#include "cdrom/CDUtility.h"
|
||||
#endif
|
||||
|
||||
#include "mempatcher.h"
|
||||
#include "md5.h"
|
||||
#include "clamp.h"
|
||||
@ -97,172 +92,12 @@ void MDFN_ResetMessages(void)
|
||||
}
|
||||
|
||||
|
||||
#ifdef NEED_CD
|
||||
static void ReadM3U(std::vector<std::string> &file_list, std::string path, unsigned depth = 0)
|
||||
{
|
||||
std::vector<std::string> ret;
|
||||
FileWrapper m3u_file(path.c_str(), FileWrapper::MODE_READ, _("M3U CD Set"));
|
||||
std::string dir_path;
|
||||
char linebuf[2048];
|
||||
|
||||
MDFN_GetFilePathComponents(path, &dir_path);
|
||||
|
||||
while(m3u_file.get_line(linebuf, sizeof(linebuf)))
|
||||
{
|
||||
std::string efp;
|
||||
|
||||
if(linebuf[0] == '#') continue;
|
||||
MDFN_rtrim(linebuf);
|
||||
if(linebuf[0] == 0) continue;
|
||||
|
||||
efp = MDFN_EvalFIP(dir_path, std::string(linebuf));
|
||||
|
||||
if(efp.size() >= 4 && efp.substr(efp.size() - 4) == ".m3u")
|
||||
{
|
||||
if(efp == path)
|
||||
throw(MDFN_Error(0, _("M3U at \"%s\" references self."), efp.c_str()));
|
||||
|
||||
if(depth == 99)
|
||||
throw(MDFN_Error(0, _("M3U load recursion too deep!")));
|
||||
|
||||
ReadM3U(file_list, efp, depth++);
|
||||
}
|
||||
else
|
||||
file_list.push_back(efp);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef NEED_CD
|
||||
static std::vector<CDIF *> CDInterfaces; // FIXME: Cleanup on error out.
|
||||
#endif
|
||||
// TODO: LoadCommon()
|
||||
|
||||
MDFNGI *MDFNI_LoadCD(const char *force_module, const char *devicename)
|
||||
{
|
||||
uint8 LayoutMD5[16];
|
||||
|
||||
MDFN_printf(_("Loading %s...\n\n"), devicename ? devicename : _("PHYSICAL CD"));
|
||||
|
||||
try
|
||||
{
|
||||
if(devicename && strlen(devicename) > 4 && !strcasecmp(devicename + strlen(devicename) - 4, ".m3u"))
|
||||
{
|
||||
std::vector<std::string> file_list;
|
||||
|
||||
ReadM3U(file_list, devicename);
|
||||
|
||||
for(unsigned i = 0; i < file_list.size(); i++)
|
||||
{
|
||||
CDInterfaces.push_back(CDIF_Open(file_list[i].c_str(), false, false /* cdimage_memcache */));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CDInterfaces.push_back(CDIF_Open(devicename, false, false /* cdimage_memcache */));
|
||||
}
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
MDFND_PrintError(e.what());
|
||||
MDFN_PrintError(_("Error opening CD."));
|
||||
return(0);
|
||||
}
|
||||
|
||||
//
|
||||
// Print out a track list for all discs.
|
||||
//
|
||||
MDFN_indent(1);
|
||||
for(unsigned i = 0; i < CDInterfaces.size(); i++)
|
||||
{
|
||||
CDUtility::TOC toc;
|
||||
|
||||
CDInterfaces[i]->ReadTOC(&toc);
|
||||
|
||||
MDFN_printf(_("CD %d Layout:\n"), i + 1);
|
||||
MDFN_indent(1);
|
||||
|
||||
for(int32 track = toc.first_track; track <= toc.last_track; track++)
|
||||
{
|
||||
MDFN_printf(_("Track %2d, LBA: %6d %s\n"), track, toc.tracks[track].lba, (toc.tracks[track].control & 0x4) ? "DATA" : "AUDIO");
|
||||
}
|
||||
|
||||
MDFN_printf("Leadout: %6d\n", toc.tracks[100].lba);
|
||||
MDFN_indent(-1);
|
||||
MDFN_printf("\n");
|
||||
}
|
||||
MDFN_indent(-1);
|
||||
|
||||
// Calculate layout MD5. The system emulation LoadCD() code is free to ignore this value and calculate
|
||||
// its own, or to use it to look up a game in its database.
|
||||
{
|
||||
md5_context layout_md5;
|
||||
|
||||
layout_md5.starts();
|
||||
|
||||
for(unsigned i = 0; i < CDInterfaces.size(); i++)
|
||||
{
|
||||
CD_TOC toc;
|
||||
|
||||
CDInterfaces[i]->ReadTOC(&toc);
|
||||
|
||||
layout_md5.update_u32_as_lsb(toc.first_track);
|
||||
layout_md5.update_u32_as_lsb(toc.last_track);
|
||||
layout_md5.update_u32_as_lsb(toc.tracks[100].lba);
|
||||
|
||||
for(uint32 track = toc.first_track; track <= toc.last_track; track++)
|
||||
{
|
||||
layout_md5.update_u32_as_lsb(toc.tracks[track].lba);
|
||||
layout_md5.update_u32_as_lsb(toc.tracks[track].control & 0x4);
|
||||
}
|
||||
}
|
||||
|
||||
layout_md5.finish(LayoutMD5);
|
||||
}
|
||||
|
||||
// This if statement will be true if force_module references a system without CDROM support.
|
||||
if(!MDFNGameInfo->LoadCD)
|
||||
{
|
||||
MDFN_PrintError(_("Specified system \"%s\" doesn't support CDs!"), force_module);
|
||||
return(0);
|
||||
}
|
||||
|
||||
MDFN_printf(_("Using module: %s(%s)\n\n"), MDFNGameInfo->shortname, MDFNGameInfo->fullname);
|
||||
|
||||
// TODO: include module name in hash
|
||||
memcpy(MDFNGameInfo->MD5, LayoutMD5, 16);
|
||||
|
||||
if(!(MDFNGameInfo->LoadCD(&CDInterfaces)))
|
||||
{
|
||||
for(unsigned i = 0; i < CDInterfaces.size(); i++)
|
||||
delete CDInterfaces[i];
|
||||
CDInterfaces.clear();
|
||||
|
||||
MDFNGameInfo = NULL;
|
||||
return(0);
|
||||
}
|
||||
|
||||
//MDFNI_SetLayerEnableMask(~0ULL);
|
||||
|
||||
MDFN_ResetMessages(); // Save state, status messages, etc.
|
||||
|
||||
MDFN_LoadGameCheats(NULL);
|
||||
MDFNMP_InstallReadPatches();
|
||||
|
||||
return(MDFNGameInfo);
|
||||
}
|
||||
#endif
|
||||
|
||||
MDFNGI *MDFNI_LoadGame(const char *force_module, const char *name)
|
||||
{
|
||||
MDFNFILE GameFile;
|
||||
std::vector<FileExtensionSpecStruct> valid_iae;
|
||||
MDFNGameInfo = MDFNGI_CORE;
|
||||
|
||||
#ifdef NEED_CD
|
||||
if(strlen(name) > 4 && (!strcasecmp(name + strlen(name) - 4, ".cue") || !strcasecmp(name + strlen(name) - 4, ".ccd") || !strcasecmp(name + strlen(name) - 4, ".toc") || !strcasecmp(name + strlen(name) - 4, ".m3u")))
|
||||
return(MDFNI_LoadCD(force_module, name));
|
||||
#endif
|
||||
|
||||
MDFN_printf(_("Loading %s...\n"),name);
|
||||
|
||||
MDFN_indent(1);
|
||||
@ -342,21 +177,11 @@ void MDFNI_CloseGame(void)
|
||||
MDFNMP_Kill();
|
||||
|
||||
MDFNGameInfo = NULL;
|
||||
|
||||
#ifdef NEED_CD
|
||||
for(unsigned i = 0; i < CDInterfaces.size(); i++)
|
||||
delete CDInterfaces[i];
|
||||
CDInterfaces.clear();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool MDFNI_InitializeModule(void)
|
||||
{
|
||||
|
||||
#ifdef NEED_CD
|
||||
CDUtility::CDUtility_Init();
|
||||
#endif
|
||||
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
362
threads.c
362
threads.c
@ -1,362 +0,0 @@
|
||||
/* RetroArch - A frontend for libretro.
|
||||
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
||||
* Copyright (C) 2011-2014 - Daniel De Matteis
|
||||
*
|
||||
* 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 "thread.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#ifdef _XBOX
|
||||
#include <xtl.h>
|
||||
#else
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#elif defined(GEKKO)
|
||||
#include "thread/gx_pthread.h"
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#ifdef __MACH__
|
||||
#include <mach/clock.h>
|
||||
#include <mach/mach.h>
|
||||
#endif
|
||||
|
||||
struct thread_data
|
||||
{
|
||||
void (*func)(void*);
|
||||
void *userdata;
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
struct sthread
|
||||
{
|
||||
HANDLE thread;
|
||||
};
|
||||
|
||||
static DWORD CALLBACK thread_wrap(void *data_)
|
||||
{
|
||||
struct thread_data *data = (struct thread_data*)data_;
|
||||
data->func(data->userdata);
|
||||
free(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
sthread_t *sthread_create(void (*thread_func)(void*), void *userdata)
|
||||
{
|
||||
sthread_t *thread = (sthread_t*)calloc(1, sizeof(*thread));
|
||||
if (!thread)
|
||||
return NULL;
|
||||
|
||||
struct thread_data *data = (struct thread_data*)calloc(1, sizeof(*data));
|
||||
if (!data)
|
||||
{
|
||||
free(thread);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data->func = thread_func;
|
||||
data->userdata = userdata;
|
||||
|
||||
thread->thread = CreateThread(NULL, 0, thread_wrap, data, 0, NULL);
|
||||
if (!thread->thread)
|
||||
{
|
||||
free(data);
|
||||
free(thread);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return thread;
|
||||
}
|
||||
|
||||
int sthread_detach(sthread_t *thread)
|
||||
{
|
||||
CloseHandle(thread->thread);
|
||||
free(thread);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sthread_join(sthread_t *thread)
|
||||
{
|
||||
WaitForSingleObject(thread->thread, INFINITE);
|
||||
CloseHandle(thread->thread);
|
||||
free(thread);
|
||||
}
|
||||
|
||||
struct slock
|
||||
{
|
||||
HANDLE lock;
|
||||
};
|
||||
|
||||
slock_t *slock_new(void)
|
||||
{
|
||||
slock_t *lock = (slock_t*)calloc(1, sizeof(*lock));
|
||||
if (!lock)
|
||||
return NULL;
|
||||
|
||||
lock->lock = CreateMutex(NULL, FALSE, "");
|
||||
if (!lock->lock)
|
||||
{
|
||||
free(lock);
|
||||
return NULL;
|
||||
}
|
||||
return lock;
|
||||
}
|
||||
|
||||
void slock_free(slock_t *lock)
|
||||
{
|
||||
CloseHandle(lock->lock);
|
||||
free(lock);
|
||||
}
|
||||
|
||||
void slock_lock(slock_t *lock)
|
||||
{
|
||||
WaitForSingleObject(lock->lock, INFINITE);
|
||||
}
|
||||
|
||||
void slock_unlock(slock_t *lock)
|
||||
{
|
||||
ReleaseMutex(lock->lock);
|
||||
}
|
||||
|
||||
struct scond
|
||||
{
|
||||
HANDLE event;
|
||||
};
|
||||
|
||||
scond_t *scond_new(void)
|
||||
{
|
||||
scond_t *cond = (scond_t*)calloc(1, sizeof(*cond));
|
||||
if (!cond)
|
||||
return NULL;
|
||||
|
||||
cond->event = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||||
if (!cond->event)
|
||||
{
|
||||
free(cond);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cond;
|
||||
}
|
||||
|
||||
void scond_wait(scond_t *cond, slock_t *lock)
|
||||
{
|
||||
WaitForSingleObject(cond->event, 0);
|
||||
|
||||
SignalObjectAndWait(lock->lock, cond->event, INFINITE, FALSE);
|
||||
|
||||
slock_lock(lock);
|
||||
}
|
||||
|
||||
bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us)
|
||||
{
|
||||
WaitForSingleObject(cond->event, 0);
|
||||
|
||||
DWORD res = SignalObjectAndWait(lock->lock, cond->event, (DWORD)(timeout_us) / 1000, FALSE);
|
||||
|
||||
slock_lock(lock);
|
||||
return res == WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
void scond_signal(scond_t *cond)
|
||||
{
|
||||
SetEvent(cond->event);
|
||||
}
|
||||
|
||||
/* FIXME - check how this function should differ from scond_signal implementation */
|
||||
int scond_broadcast(scond_t *cond)
|
||||
{
|
||||
SetEvent(cond->event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void scond_free(scond_t *cond)
|
||||
{
|
||||
CloseHandle(cond->event);
|
||||
free(cond);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
struct sthread
|
||||
{
|
||||
pthread_t id;
|
||||
};
|
||||
|
||||
static void *thread_wrap(void *data_)
|
||||
{
|
||||
struct thread_data *data = (struct thread_data*)data_;
|
||||
data->func(data->userdata);
|
||||
free(data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sthread_t *sthread_create(void (*thread_func)(void*), void *userdata)
|
||||
{
|
||||
sthread_t *thr = (sthread_t*)calloc(1, sizeof(*thr));
|
||||
if (!thr)
|
||||
return NULL;
|
||||
|
||||
struct thread_data *data = (struct thread_data*)calloc(1, sizeof(*data));
|
||||
if (!data)
|
||||
{
|
||||
free(thr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data->func = thread_func;
|
||||
data->userdata = userdata;
|
||||
|
||||
if (pthread_create(&thr->id, NULL, thread_wrap, data) < 0)
|
||||
{
|
||||
free(data);
|
||||
free(thr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return thr;
|
||||
}
|
||||
|
||||
int sthread_detach(sthread_t *thread)
|
||||
{
|
||||
return pthread_detach(thread->id);
|
||||
}
|
||||
|
||||
void sthread_join(sthread_t *thread)
|
||||
{
|
||||
pthread_join(thread->id, NULL);
|
||||
free(thread);
|
||||
}
|
||||
|
||||
struct slock
|
||||
{
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
slock_t *slock_new(void)
|
||||
{
|
||||
slock_t *lock = (slock_t*)calloc(1, sizeof(*lock));
|
||||
if (!lock)
|
||||
return NULL;
|
||||
|
||||
if (pthread_mutex_init(&lock->lock, NULL) < 0)
|
||||
{
|
||||
free(lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return lock;
|
||||
}
|
||||
|
||||
void slock_free(slock_t *lock)
|
||||
{
|
||||
pthread_mutex_destroy(&lock->lock);
|
||||
free(lock);
|
||||
}
|
||||
|
||||
void slock_lock(slock_t *lock)
|
||||
{
|
||||
pthread_mutex_lock(&lock->lock);
|
||||
}
|
||||
|
||||
void slock_unlock(slock_t *lock)
|
||||
{
|
||||
pthread_mutex_unlock(&lock->lock);
|
||||
}
|
||||
|
||||
struct scond
|
||||
{
|
||||
pthread_cond_t cond;
|
||||
};
|
||||
|
||||
scond_t *scond_new(void)
|
||||
{
|
||||
scond_t *cond = (scond_t*)calloc(1, sizeof(*cond));
|
||||
if (!cond)
|
||||
return NULL;
|
||||
|
||||
if (pthread_cond_init(&cond->cond, NULL) < 0)
|
||||
{
|
||||
free(cond);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cond;
|
||||
}
|
||||
|
||||
void scond_free(scond_t *cond)
|
||||
{
|
||||
pthread_cond_destroy(&cond->cond);
|
||||
free(cond);
|
||||
}
|
||||
|
||||
void scond_wait(scond_t *cond, slock_t *lock)
|
||||
{
|
||||
pthread_cond_wait(&cond->cond, &lock->lock);
|
||||
}
|
||||
|
||||
int scond_broadcast(scond_t *cond)
|
||||
{
|
||||
return pthread_cond_broadcast(&cond->cond);
|
||||
}
|
||||
|
||||
bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us)
|
||||
{
|
||||
struct timespec now = {0};
|
||||
|
||||
#ifdef __MACH__ // OSX doesn't have clock_gettime ... :(
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t mts;
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||
clock_get_time(cclock, &mts);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
now.tv_sec = mts.tv_sec;
|
||||
now.tv_nsec = mts.tv_nsec;
|
||||
#elif defined(__CELLOS_LV2__)
|
||||
sys_time_sec_t s;
|
||||
sys_time_nsec_t n;
|
||||
sys_time_get_current_time(&s, &n);
|
||||
now.tv_sec = s;
|
||||
now.tv_nsec = n;
|
||||
#elif defined(__mips__)
|
||||
struct timeval tm;
|
||||
gettimeofday(&tm, NULL);
|
||||
now.tv_sec = tm.tv_sec;
|
||||
now.tv_nsec = tm.tv_usec * 1000;
|
||||
#elif !defined(GEKKO) // timeout on libogc is duration, not end time
|
||||
clock_gettime(CLOCK_REALTIME, &now);
|
||||
#endif
|
||||
|
||||
now.tv_sec += timeout_us / 1000000LL;
|
||||
now.tv_nsec += timeout_us * 1000LL;
|
||||
|
||||
now.tv_sec += now.tv_nsec / 1000000000LL;
|
||||
now.tv_nsec = now.tv_nsec % 1000000000LL;
|
||||
|
||||
int ret = pthread_cond_timedwait(&cond->cond, &lock->lock, &now);
|
||||
return ret == 0;
|
||||
}
|
||||
|
||||
void scond_signal(scond_t *cond)
|
||||
{
|
||||
pthread_cond_signal(&cond->cond);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user