Replace SDL_Atomic by stdatomic from C11

There is no reason to use SDL atomics.
This commit is contained in:
Romain Vimont 2020-04-02 19:16:33 +02:00
parent bea1c11f8e
commit 54ccccd883
4 changed files with 31 additions and 18 deletions

View File

@ -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;
@ -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) {
@ -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);
@ -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
@ -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
@ -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;
}
@ -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;
}

View File

@ -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>
@ -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;

View File

@ -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);
@ -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);
@ -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()
@ -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) {

View File

@ -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"
@ -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;
@ -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, \