CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/Instance.cpp
Views: 1401
// Copyright (c) 2020 PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "ppsspp_config.h"18#include "Core/Instance.h"1920#if !PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(ANDROID) && !defined(__LIBRETRO__) && !PPSSPP_PLATFORM(SWITCH)21#include <unistd.h>22#include <sys/types.h>23#include <sys/mman.h>24#include <sys/stat.h>25#include <fcntl.h>26#endif2728#if PPSSPP_PLATFORM(WINDOWS)29#include "Common/CommonWindows.h"30#endif3132#include "Common/Log.h"33#include "Common/SysError.h"3435#include <cstdint>3637uint8_t PPSSPP_ID = 0;3839#if PPSSPP_PLATFORM(WINDOWS)40static HANDLE hIDMapFile = nullptr;41static HANDLE mapLock = nullptr;42#else43static int hIDMapFile = -1;44static long BUF_SIZE = 4096;45#endif4647struct InstanceInfo {48uint8_t pad[2];49uint8_t next;50uint8_t total;51};5253#define ID_SHM_NAME "/PPSSPP_ID"5455static bool UpdateInstanceCounter(void (*callback)(volatile InstanceInfo *)) {56#if PPSSPP_PLATFORM(WINDOWS)57if (!hIDMapFile) {58return false;59}60InstanceInfo *buf = (InstanceInfo *)MapViewOfFile(hIDMapFile, // handle to map object61FILE_MAP_ALL_ACCESS, // read/write permission620,630,64sizeof(InstanceInfo));6566if (!buf) {67auto err = GetLastError();68ERROR_LOG(Log::sceNet, "Could not map view of file %s, %08x %s", ID_SHM_NAME, (uint32_t)err, GetStringErrorMsg(err).c_str());69return false;70}7172bool result = false;73if (!mapLock || WaitForSingleObject(mapLock, INFINITE) == 0) {74callback(buf);75if (mapLock) {76ReleaseMutex(mapLock);77}78result = true;79}80UnmapViewOfFile(buf);8182return result;83#elif PPSSPP_PLATFORM(ANDROID) || defined(__LIBRETRO__) || PPSSPP_PLATFORM(SWITCH)84// TODO: replace shm_open & shm_unlink with ashmem or android-shmem85return false;86#else87if (hIDMapFile < 0) {88return false;89}9091InstanceInfo *buf = (InstanceInfo *)mmap(0, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, hIDMapFile, 0);92if (buf == MAP_FAILED) {93ERROR_LOG(Log::sceNet, "mmap(%s) failure.", ID_SHM_NAME);94return false;95}9697bool result = false;98if (mlock(buf, BUF_SIZE) == 0) {99callback(buf);100munlock(buf, BUF_SIZE);101result = true;102}103104munmap(buf, BUF_SIZE);105return result;106#endif107}108109int GetInstancePeerCount() {110static int c = 0;111UpdateInstanceCounter([](volatile InstanceInfo *buf) {112c = buf->total;113});114return c;115}116117// Get current number of instance of PPSSPP running.118// Must be called only once during init.119void InitInstanceCounter() {120#if PPSSPP_PLATFORM(WINDOWS)121uint32_t BUF_SIZE = 4096;122SYSTEM_INFO sysInfo;123124GetSystemInfo(&sysInfo);125int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000;126BUF_SIZE = (BUF_SIZE + gran - 1) & ~(gran - 1);127128mapLock = CreateMutex(nullptr, FALSE, L"PPSSPP_ID_mutex");129130hIDMapFile = CreateFileMapping(131INVALID_HANDLE_VALUE, // use paging file132NULL, // default security133PAGE_READWRITE, // read/write access1340, // maximum object size (high-order DWORD)135BUF_SIZE, // maximum object size (low-order DWORD)136TEXT(ID_SHM_NAME)); // name of mapping object137138DWORD lasterr = GetLastError();139if (!hIDMapFile) {140ERROR_LOG(Log::sceNet, "Could not create %s file mapping object, %08x %s", ID_SHM_NAME, (uint32_t)lasterr, GetStringErrorMsg(lasterr).c_str());141PPSSPP_ID = 1;142return;143}144#elif PPSSPP_PLATFORM(ANDROID) || defined(__LIBRETRO__) || PPSSPP_PLATFORM(SWITCH)145// TODO : replace shm_open & shm_unlink with ashmem or android-shmem146#else147// Create shared memory object148hIDMapFile = shm_open(ID_SHM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);149BUF_SIZE = (BUF_SIZE < sysconf(_SC_PAGE_SIZE)) ? sysconf(_SC_PAGE_SIZE) : BUF_SIZE;150151if (hIDMapFile < 0 || (ftruncate(hIDMapFile, BUF_SIZE)) == -1) { // Set the size152ERROR_LOG(Log::sceNet, "ftruncate(%s) failure.", ID_SHM_NAME);153PPSSPP_ID = 1;154return;155}156#endif157158bool success = UpdateInstanceCounter([](volatile InstanceInfo *buf) {159PPSSPP_ID = ++buf->next;160buf->total++;161});162if (!success) {163PPSSPP_ID = 1;164}165}166167void ShutdownInstanceCounter() {168UpdateInstanceCounter([](volatile InstanceInfo *buf) {169buf->total--;170});171172#if PPSSPP_PLATFORM(WINDOWS)173if (hIDMapFile) {174CloseHandle(hIDMapFile); // If program exited(or crashed?) or the last handle reference closed the shared memory object will be deleted.175hIDMapFile = nullptr;176}177if (mapLock) {178CloseHandle(mapLock);179mapLock = nullptr;180}181#elif PPSSPP_PLATFORM(ANDROID) || defined(__LIBRETRO__) || PPSSPP_PLATFORM(SWITCH)182// Do nothing183#else184if (hIDMapFile >= 0) {185close(hIDMapFile);186shm_unlink(ID_SHM_NAME); // If program exited or crashed before unlinked the shared memory object and it's contents will persist.187hIDMapFile = -1;188}189#endif190}191192193