Compare commits

...

2 Commits

Author SHA1 Message Date
eb10385a2b Replace SDL_Atomic by stdatomic from C11
There is no reason to use SDL atomics.
2020-04-02 19:19:11 +02:00
3603939475 Do not log success on failure
If calling the private API does not work, an exception is printed. In
that case, do not log that the action succeeded.
2020-04-01 11:28:48 +02:00
7 changed files with 44 additions and 24 deletions

View File

@ -23,7 +23,7 @@ fps_counter_init(struct fps_counter *counter) {
} }
counter->thread = NULL; 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 // no need to initialize the other fields, they are unused until started
return true; return true;
@ -35,6 +35,16 @@ fps_counter_destroy(struct fps_counter *counter) {
SDL_DestroyMutex(counter->mutex); 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 // must be called with mutex locked
static void static void
display_fps(struct fps_counter *counter) { display_fps(struct fps_counter *counter) {
@ -70,10 +80,10 @@ run_fps_counter(void *data) {
mutex_lock(counter->mutex); mutex_lock(counter->mutex);
while (!counter->interrupted) { while (!counter->interrupted) {
while (!counter->interrupted && !SDL_AtomicGet(&counter->started)) { while (!counter->interrupted && !is_started(counter)) {
cond_wait(counter->state_cond, counter->mutex); 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(); uint32_t now = SDL_GetTicks();
check_interval_expired(counter, now); check_interval_expired(counter, now);
@ -96,7 +106,7 @@ fps_counter_start(struct fps_counter *counter) {
counter->nr_skipped = 0; counter->nr_skipped = 0;
mutex_unlock(counter->mutex); mutex_unlock(counter->mutex);
SDL_AtomicSet(&counter->started, 1); set_started(counter, true);
cond_signal(counter->state_cond); cond_signal(counter->state_cond);
// counter->thread is always accessed from the same thread, no need to lock // 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 void
fps_counter_stop(struct fps_counter *counter) { fps_counter_stop(struct fps_counter *counter) {
SDL_AtomicSet(&counter->started, 0); set_started(counter, false);
cond_signal(counter->state_cond); cond_signal(counter->state_cond);
} }
bool bool
fps_counter_is_started(struct fps_counter *counter) { fps_counter_is_started(struct fps_counter *counter) {
return SDL_AtomicGet(&counter->started); return is_started(counter);
} }
void void
@ -145,7 +155,7 @@ fps_counter_join(struct fps_counter *counter) {
void void
fps_counter_add_rendered_frame(struct fps_counter *counter) { fps_counter_add_rendered_frame(struct fps_counter *counter) {
if (!SDL_AtomicGet(&counter->started)) { if (!is_started(counter)) {
return; return;
} }
@ -158,7 +168,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
void void
fps_counter_add_skipped_frame(struct fps_counter *counter) { fps_counter_add_skipped_frame(struct fps_counter *counter) {
if (!SDL_AtomicGet(&counter->started)) { if (!is_started(counter)) {
return; return;
} }

View File

@ -1,9 +1,9 @@
#ifndef FPSCOUNTER_H #ifndef FPSCOUNTER_H
#define FPSCOUNTER_H #define FPSCOUNTER_H
#include <stdatomic.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <SDL2/SDL_atomic.h>
#include <SDL2/SDL_mutex.h> #include <SDL2/SDL_mutex.h>
#include <SDL2/SDL_thread.h> #include <SDL2/SDL_thread.h>
@ -16,7 +16,7 @@ struct fps_counter {
// atomic so that we can check without locking the mutex // atomic so that we can check without locking the mutex
// if the FPS counter is disabled, we don't want to lock unnecessarily // 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 // the following fields are protected by the mutex
bool interrupted; bool interrupted;

View File

@ -338,7 +338,7 @@ run_wait_server(void *data) {
// no need for synchronization, server_socket is initialized before this // no need for synchronization, server_socket is initialized before this
// thread was created // thread was created
if (server->server_socket != INVALID_SOCKET 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 // On Linux, accept() is unblocked by shutdown(), but on Windows, it is
// unblocked by closesocket(). Therefore, call both (close_socket()). // unblocked by closesocket(). Therefore, call both (close_socket()).
close_socket(server->server_socket); close_socket(server->server_socket);
@ -393,8 +393,10 @@ server_start(struct server *server, const char *serial,
error2: error2:
if (!server->tunnel_forward) { if (!server->tunnel_forward) {
// the wait server thread is not started, SDL_AtomicSet() is sufficient bool was_closed =
SDL_AtomicSet(&server->server_socket_closed, 1); atomic_flag_test_and_set(&server->server_socket_closed);
// the thread is not started, the flag could not be already set
assert(!was_closed);
close_socket(server->server_socket); close_socket(server->server_socket);
} }
disable_tunnel(server); disable_tunnel(server);
@ -418,7 +420,7 @@ server_connect_to(struct server *server) {
} }
// we don't need the server socket anymore // 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 it from here
close_socket(server->server_socket); close_socket(server->server_socket);
// otherwise, it is closed by run_wait_server() // otherwise, it is closed by run_wait_server()
@ -450,7 +452,7 @@ server_connect_to(struct server *server) {
void void
server_stop(struct server *server) { server_stop(struct server *server) {
if (server->server_socket != INVALID_SOCKET 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); close_socket(server->server_socket);
} }
if (server->video_socket != INVALID_SOCKET) { if (server->video_socket != INVALID_SOCKET) {

View File

@ -1,9 +1,9 @@
#ifndef SERVER_H #ifndef SERVER_H
#define SERVER_H #define SERVER_H
#include <stdatomic.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <SDL2/SDL_atomic.h>
#include <SDL2/SDL_thread.h> #include <SDL2/SDL_thread.h>
#include "config.h" #include "config.h"
@ -15,7 +15,7 @@ struct server {
char *serial; char *serial;
process_t process; process_t process;
SDL_Thread *wait_server_thread; 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 server_socket; // only used if !tunnel_forward
socket_t video_socket; socket_t video_socket;
socket_t control_socket; socket_t control_socket;
@ -29,7 +29,7 @@ struct server {
.serial = NULL, \ .serial = NULL, \
.process = PROCESS_NONE, \ .process = PROCESS_NONE, \
.wait_server_thread = NULL, \ .wait_server_thread = NULL, \
.server_socket_closed = {0}, \ .server_socket_closed = ATOMIC_FLAG_INIT, \
.server_socket = INVALID_SOCKET, \ .server_socket = INVALID_SOCKET, \
.video_socket = INVALID_SOCKET, \ .video_socket = INVALID_SOCKET, \
.control_socket = INVALID_SOCKET, \ .control_socket = INVALID_SOCKET, \

View File

@ -163,8 +163,10 @@ public final class Device {
} }
public void setClipboardText(String text) { public void setClipboardText(String text) {
serviceManager.getClipboardManager().setText(text); boolean ok = serviceManager.getClipboardManager().setText(text);
Ln.i("Device clipboard set"); if (ok) {
Ln.i("Device clipboard set");
}
} }
/** /**
@ -176,8 +178,10 @@ public final class Device {
Ln.e("Could not get built-in display"); Ln.e("Could not get built-in display");
return; return;
} }
SurfaceControl.setDisplayPowerMode(d, mode); boolean ok = SurfaceControl.setDisplayPowerMode(d, mode);
Ln.i("Device screen turned " + (mode == Device.POWER_MODE_OFF ? "off" : "on")); if (ok) {
Ln.i("Device screen turned " + (mode == Device.POWER_MODE_OFF ? "off" : "on"));
}
} }
/** /**

View File

@ -74,13 +74,15 @@ public class ClipboardManager {
} }
} }
public void setText(CharSequence text) { public boolean setText(CharSequence text) {
try { try {
Method method = getSetPrimaryClipMethod(); Method method = getSetPrimaryClipMethod();
ClipData clipData = ClipData.newPlainText(null, text); ClipData clipData = ClipData.newPlainText(null, text);
setPrimaryClip(method, manager, clipData); setPrimaryClip(method, manager, clipData);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false;
} }
} }
} }

View File

@ -121,12 +121,14 @@ public final class SurfaceControl {
return setDisplayPowerModeMethod; return setDisplayPowerModeMethod;
} }
public static void setDisplayPowerMode(IBinder displayToken, int mode) { public static boolean setDisplayPowerMode(IBinder displayToken, int mode) {
try { try {
Method method = getSetDisplayPowerModeMethod(); Method method = getSetDisplayPowerModeMethod();
method.invoke(null, displayToken, mode); method.invoke(null, displayToken, mode);
return true;
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) { } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
Ln.e("Could not invoke method", e); Ln.e("Could not invoke method", e);
return false;
} }
} }