From 394221f3dfbc256d128d59cc02c6ebee29a81b72 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 21 Dec 2015 15:07:37 -0800 Subject: [PATCH] Use file locks to synchronize stdout instead of a pthread mutex --- app/src/main/jni/evdev_reader/evdev_reader.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/main/jni/evdev_reader/evdev_reader.c b/app/src/main/jni/evdev_reader/evdev_reader.c index 2001c73d..039ce769 100644 --- a/app/src/main/jni/evdev_reader/evdev_reader.c +++ b/app/src/main/jni/evdev_reader/evdev_reader.c @@ -30,7 +30,6 @@ struct DeviceEntry { static struct DeviceEntry *DeviceListHead; static int grabbing = 1; static pthread_mutex_t DeviceListLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t StdoutLock = PTHREAD_MUTEX_INITIALIZER; // This is a small executable that runs in a root shell. It reads input // devices and writes the evdev output packets to stdout. This allows @@ -57,13 +56,13 @@ static int hasKey(int fd, short key) { } static void outputEvdevData(char *data, int dataSize) { - // We need to hold our StdoutLock to avoid garbling - // input data when multiple threads try to write at once. - pthread_mutex_lock(&StdoutLock); + // We need to lock stdout before writing to prevent + // interleaving of data between threads. + flockfile(stdout); fwrite(&dataSize, sizeof(dataSize), 1, stdout); fwrite(data, dataSize, 1, stdout); fflush(stdout); - pthread_mutex_unlock(&StdoutLock); + funlockfile(stdout); } void* pollThreadFunc(void* context) {