Skip to content

Commit

Permalink
use atomic wait instead of semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkautarch committed Jan 1, 2024
1 parent 538fc83 commit 84cacc3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 62 deletions.
37 changes: 18 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "sdlwindow.hpp"
#include "wlserver.hpp"
#include "gpuvis_trace_utils.h"
#include "steamcompmgr_shared.hpp"

#if HAVE_OPENVR
#include "vr_session.hpp"
Expand Down Expand Up @@ -763,8 +762,8 @@ int main(int argc, char **argv)
{
fprintf( stderr, "Failed to initialize OpenVR runtime\n" );
if ( BIsSDLSession() ) {
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();
}
return 1;
}
Expand All @@ -775,8 +774,8 @@ int main(int argc, char **argv)
{
fprintf( stderr, "Failed to initialize output\n" );
if ( BIsSDLSession() ) {
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();
}
return 1;
}
Expand All @@ -786,8 +785,8 @@ int main(int argc, char **argv)
if ( !SDL_Vulkan_CreateSurface( g_SDLWindow, instance, &surface ) )
{
fprintf(stderr, "SDL_Vulkan_CreateSurface failed: %s", SDL_GetError() );
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();
return 1;
}
}
Expand All @@ -798,8 +797,8 @@ int main(int argc, char **argv)
{
fprintf( stderr, "Failed to initialize Vulkan\n" );
if ( BIsSDLSession() ) {
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();
}
return 1;
}
Expand All @@ -808,8 +807,8 @@ int main(int argc, char **argv)
{
fprintf( stderr, "vulkan_init_formats failed\n" );
if ( BIsSDLSession() ) {
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();
}
return 1;
}
Expand All @@ -818,8 +817,8 @@ int main(int argc, char **argv)
{
fprintf( stderr, "vulkan_make_output failed\n" );
if ( BIsSDLSession() ) {
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();
}
return 1;
}
Expand Down Expand Up @@ -851,8 +850,8 @@ int main(int argc, char **argv)
{
fprintf( stderr, "Cannot specify -w without -h\n" );
if ( BIsSDLSession() ) {
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();
}
return 1;
}
Expand All @@ -865,16 +864,16 @@ int main(int argc, char **argv)
{
if ( !wlserver_init() )
{
sdl_latch = -1;
sdlLatchSem.signal();
g_sdlLatch = 1;
g_sdlLatch.notify_one();

fprintf( stderr, "Failed to initialize wlserver\n" );
return 1;
}
else
sdl_latch |= 1;
g_sdlLatch = 1;

sdlLatchSem.signal();
g_sdlLatch.notify_one();
}

#if HAVE_OPENVR
Expand Down
18 changes: 6 additions & 12 deletions src/sdlwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ extern bool g_bForceRelativeMouse;

static std::string gamescope_str = DEFAULT_TITLE;

std::atomic<int64_t> sdl_latch = {0}; //sdl_latch to notify sdl thread when wl-server is fully initialized
sem sdlLatchSem;
std::atomic<bool> g_sdlLatch = {0}; //g_sdlLatch to notify sdl thread when wl-server is fully initialized

void inputSDLThreadRun( void )
{
Expand Down Expand Up @@ -184,28 +183,23 @@ void inputSDLThreadRun( void )
SDL_Surface *icon_surface = nullptr;
SDL_Cursor *cursor = nullptr;

bool latch_cleared_successfully = true;

{
int64_t sdl_latch_cached = sdl_latch.load(std::memory_order_relaxed);
bool sdlLatch_cached = g_sdlLatch.load(std::memory_order_relaxed);

bool loop_done = false;
while (!loop_done)
{
if ( sdl_latch_cached == 0 && (sdl_latch_cached=sdl_latch.load()) == 0)
sdlLatchSem.wait();
if ( sdlLatch_cached == 0 && (sdlLatch_cached=g_sdlLatch.load(std::memory_order_acq_rel)) == 0)
g_sdlLatch.wait(0);


if ( (sdl_latch_cached = sdl_latch.load(std::memory_order_relaxed)) < 0 ) {
latch_cleared_successfully = false;
loop_done=true;
}
if ( sdl_latch_cached != 0 && sdl_latch.load() > 0)
if ( sdlLatch_cached != 0)
loop_done=true;
}
}

while( latch_cleared_successfully && SDL_WaitEvent( &event ) )
while( SDL_WaitEvent( &event ) )
{
fake_timestamp++;

Expand Down
3 changes: 1 addition & 2 deletions src/sdlwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ void sdlwindow_cursor(std::shared_ptr<std::vector<uint32_t>> pixels, uint32_t wi

extern SDL_Window *g_SDLWindow;

extern std::atomic<int64_t> sdl_latch; //sdl_latch to notify sdl thread when wl-server is fully initialized
extern sem sdlLatchSem;
extern std::atomic<bool> g_sdlLatch; //sdl_latch to notify sdl thread when wl-server is fully initialized
25 changes: 25 additions & 0 deletions src/steamcompmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,32 @@ static std::atomic<bool> g_bForceRepaint{false};

static int g_nCursorScaleHeight = -1;

// poor man's semaphore
class sem
{
public:
void wait( void )
{
std::unique_lock<std::mutex> lock(mtx);

while(count == 0){
cv.wait(lock);
}
count--;
}

void signal( void )
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}

private:
std::mutex mtx;
std::condition_variable cv;
int count = 0;
};

sem statsThreadSem;
std::mutex statsEventQueueLock;
Expand Down
29 changes: 0 additions & 29 deletions src/steamcompmgr_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,10 @@
#include <variant>
#include <string>
#include "gamescope-control-protocol.h"
#include <condition_variable>
#include <mutex>

struct commit_t;
struct wlserver_vk_swapchain_feedback;

// poor man's semaphore
class sem
{
public:
void wait( void )
{
std::unique_lock<std::mutex> lock(mtx);

while(count == 0){
cv.wait(lock);
}
count--;
}

void signal( void )
{
std::unique_lock<std::mutex> lock(mtx);
count++;
cv.notify_one();
}

private:
std::mutex mtx;
std::condition_variable cv;
int count = 0;
};

struct motif_hints_t
{
unsigned long flags;
Expand Down

0 comments on commit 84cacc3

Please sign in to comment.