Skip to content

Commit

Permalink
Replace SDL_Atomic by stdatomic from C11
Browse files Browse the repository at this point in the history
There is no reason to use SDL atomics.
  • Loading branch information
rom1v committed Apr 2, 2020
1 parent bea1c11 commit 54ccccd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
26 changes: 18 additions & 8 deletions app/src/fps_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fps_counter_init(struct fps_counter *counter) {
}

counter->thread = NULL;
SDL_AtomicSet(&counter->started, 0);
atomic_init(&counter->started, 0);
// no need to initialize the other fields, they are unused until started

return true;
Expand All @@ -35,6 +35,16 @@ fps_counter_destroy(struct fps_counter *counter) {
SDL_DestroyMutex(counter->mutex);
}

static inline bool
is_started(struct fps_counter *counter) {
return atomic_load_explicit(&counter->started, memory_order_acquire);
}

static inline void
set_started(struct fps_counter *counter, bool started) {
atomic_store_explicit(&counter->started, started, memory_order_release);
}

// must be called with mutex locked
static void
display_fps(struct fps_counter *counter) {
Expand Down Expand Up @@ -70,10 +80,10 @@ run_fps_counter(void *data) {

mutex_lock(counter->mutex);
while (!counter->interrupted) {
while (!counter->interrupted && !SDL_AtomicGet(&counter->started)) {
while (!counter->interrupted && !is_started(counter)) {
cond_wait(counter->state_cond, counter->mutex);
}
while (!counter->interrupted && SDL_AtomicGet(&counter->started)) {
while (!counter->interrupted && is_started(counter)) {
uint32_t now = SDL_GetTicks();
check_interval_expired(counter, now);

Expand All @@ -96,7 +106,7 @@ fps_counter_start(struct fps_counter *counter) {
counter->nr_skipped = 0;
mutex_unlock(counter->mutex);

SDL_AtomicSet(&counter->started, 1);
set_started(counter, true);
cond_signal(counter->state_cond);

// counter->thread is always accessed from the same thread, no need to lock
Expand All @@ -114,13 +124,13 @@ fps_counter_start(struct fps_counter *counter) {

void
fps_counter_stop(struct fps_counter *counter) {
SDL_AtomicSet(&counter->started, 0);
set_started(counter, false);
cond_signal(counter->state_cond);
}

bool
fps_counter_is_started(struct fps_counter *counter) {
return SDL_AtomicGet(&counter->started);
return is_started(counter);
}

void
Expand All @@ -145,7 +155,7 @@ fps_counter_join(struct fps_counter *counter) {

void
fps_counter_add_rendered_frame(struct fps_counter *counter) {
if (!SDL_AtomicGet(&counter->started)) {
if (!is_started(counter)) {
return;
}

Expand All @@ -158,7 +168,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {

void
fps_counter_add_skipped_frame(struct fps_counter *counter) {
if (!SDL_AtomicGet(&counter->started)) {
if (!is_started(counter)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/fps_counter.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef FPSCOUNTER_H
#define FPSCOUNTER_H

#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <SDL2/SDL_atomic.h>
#include <SDL2/SDL_mutex.h>
#include <SDL2/SDL_thread.h>

Expand All @@ -16,7 +16,7 @@ struct fps_counter {

// atomic so that we can check without locking the mutex
// if the FPS counter is disabled, we don't want to lock unnecessarily
SDL_atomic_t started;
atomic_bool started;

// the following fields are protected by the mutex
bool interrupted;
Expand Down
13 changes: 8 additions & 5 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ run_wait_server(void *data) {
// no need for synchronization, server_socket is initialized before this
// thread was created
if (server->server_socket != INVALID_SOCKET
&& SDL_AtomicCAS(&server->server_socket_closed, 0, 1)) {
&& !atomic_flag_test_and_set(&server->server_socket_closed)) {
// On Linux, accept() is unblocked by shutdown(), but on Windows, it is
// unblocked by closesocket(). Therefore, call both (close_socket()).
close_socket(server->server_socket);
Expand Down Expand Up @@ -393,8 +393,11 @@ server_start(struct server *server, const char *serial,

error2:
if (!server->tunnel_forward) {
// the wait server thread is not started, SDL_AtomicSet() is sufficient
SDL_AtomicSet(&server->server_socket_closed, 1);
bool was_closed =
atomic_flag_test_and_set(&server->server_socket_closed);
// the thread is not started, the flag could not be already set
assert(!was_closed);
(void) was_closed;
close_socket(server->server_socket);
}
disable_tunnel(server);
Expand All @@ -418,7 +421,7 @@ server_connect_to(struct server *server) {
}

// we don't need the server socket anymore
if (SDL_AtomicCAS(&server->server_socket_closed, 0, 1)) {
if (!atomic_flag_test_and_set(&server->server_socket_closed)) {
// close it from here
close_socket(server->server_socket);
// otherwise, it is closed by run_wait_server()
Expand Down Expand Up @@ -450,7 +453,7 @@ server_connect_to(struct server *server) {
void
server_stop(struct server *server) {
if (server->server_socket != INVALID_SOCKET
&& SDL_AtomicCAS(&server->server_socket_closed, 0, 1)) {
&& !atomic_flag_test_and_set(&server->server_socket_closed)) {
close_socket(server->server_socket);
}
if (server->video_socket != INVALID_SOCKET) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/server.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef SERVER_H
#define SERVER_H

#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <SDL2/SDL_atomic.h>
#include <SDL2/SDL_thread.h>

#include "config.h"
Expand All @@ -15,7 +15,7 @@ struct server {
char *serial;
process_t process;
SDL_Thread *wait_server_thread;
SDL_atomic_t server_socket_closed;
atomic_flag server_socket_closed;
socket_t server_socket; // only used if !tunnel_forward
socket_t video_socket;
socket_t control_socket;
Expand All @@ -29,7 +29,7 @@ struct server {
.serial = NULL, \
.process = PROCESS_NONE, \
.wait_server_thread = NULL, \
.server_socket_closed = {0}, \
.server_socket_closed = ATOMIC_FLAG_INIT, \
.server_socket = INVALID_SOCKET, \
.video_socket = INVALID_SOCKET, \
.control_socket = INVALID_SOCKET, \
Expand Down

0 comments on commit 54ccccd

Please sign in to comment.