Skip to content

Commit

Permalink
FileSystem: Don't use POSIX locks on Android
Browse files Browse the repository at this point in the history
Requires SDK 24, and it's pointless anyway.
  • Loading branch information
stenzek committed Dec 9, 2024
1 parent b814666 commit 2e6deca
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/common/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,7 @@ bool FileSystem::SetPathCompression(const char* path, bool enable)

#endif

#ifndef _WIN32
#ifdef HAS_POSIX_FILE_LOCK

static bool SetLock(int fd, bool lock, bool block, Error* error)
{
Expand Down
8 changes: 7 additions & 1 deletion src/common/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ bool CommitAtomicRenamedFile(AtomicRenamedFile& file, Error* error);
void DiscardAtomicRenamedFile(AtomicRenamedFile& file);

/// Abstracts a POSIX file lock.
#ifndef _WIN32
#if !defined(_WIN32) && !defined(__ANDROID__)
#define HAS_POSIX_FILE_LOCK 1
#endif

#ifdef HAS_POSIX_FILE_LOCK

class POSIXLock
{
public:
Expand All @@ -175,6 +180,7 @@ class POSIXLock
private:
int m_fd;
};

#endif

std::optional<DynamicHeapArray<u8>> ReadBinaryFile(const char* path, Error* error = nullptr);
Expand Down
10 changes: 5 additions & 5 deletions src/core/game_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ void GameList::Refresh(bool invalidate_cache, bool only_cache, ProgressCallback*
if (!cache_file)
ERROR_LOG("Failed to open game list cache: {}", error.GetDescription());

#ifndef _WIN32
#ifdef HAS_POSIX_FILE_LOCK
// Lock cache file for multi-instance on Linux. Implicitly done on Windows.
std::optional<FileSystem::POSIXLock> cache_file_lock;
if (cache_file)
Expand Down Expand Up @@ -1122,7 +1122,7 @@ GameList::PlayedTimeMap GameList::LoadPlayedTimeMap(const std::string& path)
return ret;
}

#ifndef _WIN32
#ifdef HAS_POSIX_FILE_LOCK
FileSystem::POSIXLock flock(fp.get());
#endif

Expand Down Expand Up @@ -1159,7 +1159,7 @@ GameList::PlayedTimeEntry GameList::UpdatePlayedTimeFile(const std::string& path
return new_entry;
}

#ifndef _WIN32
#ifdef HAS_POSIX_FILE_LOCK
FileSystem::POSIXLock flock(fp.get());
#endif

Expand Down Expand Up @@ -1726,7 +1726,7 @@ void GameList::ReloadMemcardTimestampCache()
if (!fp)
return;

#ifndef _WIN32
#ifdef HAS_POSIX_FILE_LOCK
FileSystem::POSIXLock lock(fp.get());
#endif

Expand Down Expand Up @@ -1856,7 +1856,7 @@ bool GameList::UpdateMemcardTimestampCache(const MemcardTimestampCacheEntry& ent
if (!fp)
return false;

#ifndef _WIN32
#ifdef HAS_POSIX_FILE_LOCK
FileSystem::POSIXLock lock(fp.get());
#endif

Expand Down
8 changes: 1 addition & 7 deletions src/util/opengl_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
#include <string_view>
#include <tuple>

// Unix doesn't prevent concurrent write access, need to explicitly lock the pipeline cache.
// Don't worry about Android, it's not like you can run one more than one instance of the app there...
#if !defined(_WIN32) && !defined(__ANDROID__)
#define OPENGL_PIPELINE_CACHE_NEEDS_LOCK 1
#endif

class OpenGLPipeline;
class OpenGLStreamBuffer;
class OpenGLTexture;
Expand Down Expand Up @@ -239,7 +233,7 @@ class OpenGLDevice final : public GPUDevice
bool m_timestamp_query_started = false;

std::FILE* m_pipeline_disk_cache_file = nullptr;
#ifdef OPENGL_PIPELINE_CACHE_NEEDS_LOCK
#ifdef HAS_POSIX_FILE_LOCK
FileSystem::POSIXLock m_pipeline_disk_cache_file_lock;
#endif
u32 m_pipeline_disk_cache_data_end = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/util/opengl_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error)
if (!fp)
return false;

#ifdef OPENGL_PIPELINE_CACHE_NEEDS_LOCK
#ifdef HAS_POSIX_FILE_LOCK
// Unix doesn't prevent concurrent write access, need to explicitly lock it.
FileSystem::POSIXLock fp_lock(fp.get(), true, error);
if (!fp_lock.IsLocked())
Expand Down Expand Up @@ -847,15 +847,15 @@ bool OpenGLDevice::OpenPipelineCache(const std::string& path, Error* error)

VERBOSE_LOG("Read {} programs from disk cache.", m_program_cache.size());
m_pipeline_disk_cache_file = fp.release();
#ifdef OPENGL_PIPELINE_CACHE_NEEDS_LOCK
#ifdef HAS_POSIX_FILE_LOCK
m_pipeline_disk_cache_file_lock = std::move(fp_lock);
#endif
return true;
}

bool OpenGLDevice::CreatePipelineCache(const std::string& path, Error* error)
{
#ifndef OPENGL_PIPELINE_CACHE_NEEDS_LOCK
#ifndef HAS_POSIX_FILE_LOCK
m_pipeline_disk_cache_file = FileSystem::OpenCFile(path.c_str(), "w+b", error);
if (!m_pipeline_disk_cache_file)
return false;
Expand Down Expand Up @@ -1015,7 +1015,7 @@ bool OpenGLDevice::DiscardPipelineCache()
if (!FileSystem::FTruncate64(m_pipeline_disk_cache_file, 0, &error))
{
ERROR_LOG("Failed to truncate pipeline cache: {}", error.GetDescription());
#ifdef OPENGL_PIPELINE_CACHE_NEEDS_LOCK
#ifdef HAS_POSIX_FILE_LOCK
m_pipeline_disk_cache_file_lock.Unlock();
#endif
std::fclose(m_pipeline_disk_cache_file);
Expand All @@ -1031,7 +1031,7 @@ bool OpenGLDevice::DiscardPipelineCache()
bool OpenGLDevice::ClosePipelineCache(const std::string& filename, Error* error)
{
const auto close_cache = [this]() {
#ifdef OPENGL_PIPELINE_CACHE_NEEDS_LOCK
#ifdef HAS_POSIX_FILE_LOCK
m_pipeline_disk_cache_file_lock.Unlock();
#endif
std::fclose(m_pipeline_disk_cache_file);
Expand Down

0 comments on commit 2e6deca

Please sign in to comment.