flycast/core/stdclass.h

184 lines
3.5 KiB
C
Raw Normal View History

2013-12-19 17:10:14 +00:00
#pragma once
#include "types.h"
2019-09-07 12:37:39 +00:00
#include <cstdlib>
2013-12-19 17:10:14 +00:00
#include <vector>
2019-09-07 12:37:39 +00:00
#include <cstring>
2013-12-19 17:10:14 +00:00
#ifndef _WIN32
2013-12-19 17:10:14 +00:00
#include <pthread.h>
#else
#include <windows.h>
2013-12-19 17:10:14 +00:00
#endif
#ifdef __ANDROID__
2013-12-19 17:10:14 +00:00
#include <sys/mman.h>
#undef PAGE_MASK
#define PAGE_MASK (PAGE_SIZE-1)
#else
#define PAGE_SIZE 4096
#define PAGE_MASK (PAGE_SIZE-1)
#endif
//Threads
#if !defined(HOST_NO_THREADS)
2013-12-19 17:10:14 +00:00
typedef void* ThreadEntryFP(void* param);
class cThread {
2013-12-19 17:10:14 +00:00
private:
ThreadEntryFP* entry;
2013-12-19 17:10:14 +00:00
void* param;
public :
#ifdef _WIN32
HANDLE hThread;
#else
pthread_t *hThread;
#endif
cThread(ThreadEntryFP* function, void* param)
:entry(function), param(param), hThread(NULL) {}
~cThread() { WaitToEnd(); }
2013-12-19 17:10:14 +00:00
void Start();
void WaitToEnd();
};
#endif
2013-12-19 17:10:14 +00:00
//Wait Events
typedef void* EVENTHANDLE;
class cResetEvent
{
private:
#ifdef _WIN32
2013-12-19 17:10:14 +00:00
EVENTHANDLE hEvent;
#else
pthread_mutex_t mutx;
pthread_cond_t cond;
bool state;
2013-12-19 17:10:14 +00:00
#endif
public :
cResetEvent();
2013-12-19 17:10:14 +00:00
~cResetEvent();
void Set(); //Set state to signaled
void Reset(); //Set state to non signaled
bool Wait(u32 msec);//Wait for signal , then reset[if auto]. Returns false if timed out
2013-12-19 17:10:14 +00:00
void Wait(); //Wait for signal , then reset[if auto]
};
class cMutex
{
private:
#ifdef _WIN32
2013-12-19 17:10:14 +00:00
CRITICAL_SECTION cs;
#else
pthread_mutex_t mutx;
#endif
public :
bool state;
cMutex()
{
#ifdef _WIN32
2013-12-19 17:10:14 +00:00
InitializeCriticalSection(&cs);
#else
2015-05-08 15:44:27 +00:00
pthread_mutex_init ( &mutx, NULL);
2013-12-19 17:10:14 +00:00
#endif
}
~cMutex()
{
#ifdef _WIN32
2013-12-19 17:10:14 +00:00
DeleteCriticalSection(&cs);
#else
pthread_mutex_destroy(&mutx);
#endif
}
void Lock()
{
#ifdef _WIN32
2013-12-19 17:10:14 +00:00
EnterCriticalSection(&cs);
#else
pthread_mutex_lock(&mutx);
2018-09-02 13:49:23 +00:00
#endif
}
bool TryLock()
{
#ifdef _WIN32
2018-09-02 13:49:23 +00:00
return TryEnterCriticalSection(&cs);
#else
return pthread_mutex_trylock(&mutx)==0;
2013-12-19 17:10:14 +00:00
#endif
}
void Unlock()
{
#ifdef _WIN32
2013-12-19 17:10:14 +00:00
LeaveCriticalSection(&cs);
#else
pthread_mutex_unlock(&mutx);
#endif
}
// std::BasicLockable so we can use std::lock_guard
void lock() { Lock(); }
void unlock() { Unlock(); }
2013-12-19 17:10:14 +00:00
};
#if !defined(TARGET_IPHONE)
#define DATA_PATH "/data/"
#else
#define DATA_PATH "/"
#endif
2013-12-19 17:10:14 +00:00
//Set the path !
void set_user_config_dir(const string& dir);
void set_user_data_dir(const string& dir);
void add_system_config_dir(const string& dir);
void add_system_data_dir(const string& dir);
2013-12-19 17:10:14 +00:00
//subpath format: /data/fsca-table.bit
string get_writable_config_path(const string& filename);
string get_writable_data_path(const string& filename);
string get_readonly_config_path(const string& filename);
string get_readonly_data_path(const string& filename);
bool file_exists(const string& filename);
bool make_directory(const string& path);
2013-12-19 17:10:14 +00:00
string get_game_save_prefix();
2018-11-17 11:29:59 +00:00
string get_game_basename();
2018-11-23 17:46:21 +00:00
string get_game_dir();
2013-12-19 17:10:14 +00:00
bool mem_region_lock(void *start, size_t len);
bool mem_region_unlock(void *start, size_t len);
bool mem_region_set_exec(void *start, size_t len);
void *mem_region_reserve(void *start, size_t len);
bool mem_region_release(void *start, size_t len);
void *mem_region_map_file(void *file_handle, void *dest, size_t len, size_t offset, bool readwrite);
bool mem_region_unmap_file(void *start, size_t len);
2013-12-19 17:10:14 +00:00
2019-08-30 21:35:10 +00:00
class VArray2 {
public:
2013-12-19 17:10:14 +00:00
u8* data;
unsigned size;
2013-12-19 17:10:14 +00:00
void Zero() {
memset(data, 0, size);
}
INLINE u8& operator [](unsigned i) {
2013-12-19 17:10:14 +00:00
#ifdef MEM_BOUND_CHECK
if (i >= size)
2013-12-19 17:10:14 +00:00
{
2019-08-30 21:35:10 +00:00
ERROR_LOG(COMMON, "Error: VArray2 , index out of range (%d > %d)\n", i, size - 1);
2013-12-19 17:10:14 +00:00
MEM_DO_BREAK;
}
#endif
return data[i];
}
};
2020-01-31 22:51:12 +00:00
int msgboxf(const char* text,unsigned int type,...);
2013-12-19 17:10:14 +00:00
#define MBX_OK 0
#define MBX_ICONEXCLAMATION 0
#define MBX_ICONERROR 0