summaryrefslogtreecommitdiffstats
path: root/src/device.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2014-11-12 21:58:30 +0100
committerGravatar Nikias Bassen2014-12-02 19:22:25 +0100
commita25009872304f17514089afb48a3e860e7cb9cfe (patch)
tree5a431f341c94b5de628fac67cbdc43de244fe5a7 /src/device.c
parent34e6eeef656a205d4f65f128cb17bc81cca3b4d3 (diff)
downloadusbmuxd-a25009872304f17514089afb48a3e860e7cb9cfe.tar.gz
usbmuxd-a25009872304f17514089afb48a3e860e7cb9cfe.tar.bz2
common: Add thread+mutex implementation and use it where applicablerefactor
Diffstat (limited to 'src/device.c')
-rw-r--r--src/device.c66
1 files changed, 33 insertions, 33 deletions
diff --git a/src/device.c b/src/device.c
index ddd1d4a..4188b95 100644
--- a/src/device.c
+++ b/src/device.c
@@ -31,12 +31,12 @@
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
-#include <pthread.h>
#include "device.h"
#include "client.h"
#include "preflight.h"
#include "usb.h"
#include "log.h"
+#include "common/thread.h"
int next_device_id;
@@ -125,19 +125,19 @@ struct mux_device
};
static struct collection device_list;
-pthread_mutex_t device_list_mutex;
+mutex_t device_list_mutex;
static struct mux_device* get_mux_device_for_id(int device_id)
{
struct mux_device *dev = NULL;
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *cdev, &device_list) {
if(cdev->id == device_id) {
dev = cdev;
break;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
return dev;
}
@@ -145,7 +145,7 @@ static struct mux_device* get_mux_device_for_id(int device_id)
static struct mux_connection* get_mux_connection(int device_id, struct mux_client *client)
{
struct mux_connection *conn = NULL;
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
if(dev->id == device_id) {
FOREACH(struct mux_connection *lconn, &dev->connections) {
@@ -157,7 +157,7 @@ static struct mux_connection* get_mux_connection(int device_id, struct mux_clien
break;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
return conn;
}
@@ -166,7 +166,7 @@ static int get_next_device_id(void)
{
while(1) {
int ok = 1;
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
if(dev->id == next_device_id) {
next_device_id++;
@@ -174,7 +174,7 @@ static int get_next_device_id(void)
break;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
if(ok)
return next_device_id++;
}
@@ -552,9 +552,9 @@ static void device_version_input(struct mux_device *dev, struct version_header *
vh->minor = ntohl(vh->minor);
if(vh->major != 2 && vh->major != 1) {
usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d", dev->id, vh->major, vh->minor);
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
collection_remove(&device_list, dev);
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
free(dev);
return;
}
@@ -709,14 +709,14 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned
void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length)
{
struct mux_device *dev = NULL;
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *tdev, &device_list) {
if(tdev->usbdev == usbdev) {
dev = tdev;
break;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
if(!dev) {
usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev));
return;
@@ -832,15 +832,15 @@ int device_add(struct usb_device *usbdev)
free(dev);
return res;
}
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
collection_add(&device_list, dev);
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
return 0;
}
void device_remove(struct usb_device *usbdev)
{
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
if(dev->usbdev == usbdev) {
usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev));
@@ -856,48 +856,48 @@ void device_remove(struct usb_device *usbdev)
preflight_device_remove_cb(dev->preflight_cb_data);
}
collection_remove(&device_list, dev);
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
free(dev->pktbuf);
free(dev);
return;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev));
}
void device_set_visible(int device_id)
{
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
if(dev->id == device_id) {
dev->visible = 1;
break;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
}
void device_set_preflight_cb_data(int device_id, void* data)
{
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
if(dev->id == device_id) {
dev->preflight_cb_data = data;
break;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
}
int device_get_count(int include_hidden)
{
int count = 0;
struct collection dev_list = {NULL, 0};
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
collection_copy(&dev_list, &device_list);
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
FOREACH(struct mux_device *dev, &dev_list) {
if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible))
@@ -912,9 +912,9 @@ int device_get_list(int include_hidden, struct device_info **devices)
{
int count = 0;
struct collection dev_list = {NULL, 0};
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
collection_copy(&dev_list, &device_list);
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
*devices = malloc(sizeof(struct device_info) * dev_list.capacity);
struct device_info *p = *devices;
@@ -939,7 +939,7 @@ int device_get_list(int include_hidden, struct device_info **devices)
int device_get_timeout(void)
{
uint64_t oldest = (uint64_t)-1LL;
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
if(dev->state == MUXDEV_ACTIVE) {
FOREACH(struct mux_connection *conn, &dev->connections) {
@@ -948,7 +948,7 @@ int device_get_timeout(void)
} ENDFOREACH
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
uint64_t ct = mstime64();
if((int64_t)oldest == -1LL)
return 100000; //meh
@@ -960,7 +960,7 @@ int device_get_timeout(void)
void device_check_timeouts(void)
{
uint64_t ct = mstime64();
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
if(dev->state == MUXDEV_ACTIVE) {
FOREACH(struct mux_connection *conn, &dev->connections) {
@@ -973,14 +973,14 @@ void device_check_timeouts(void)
} ENDFOREACH
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
}
void device_init(void)
{
usbmuxd_log(LL_DEBUG, "device_init");
collection_init(&device_list);
- pthread_mutex_init(&device_list_mutex, NULL);
+ mutex_init(&device_list_mutex);
next_device_id = 1;
}
@@ -1001,7 +1001,7 @@ void device_kill_connections(void)
void device_shutdown(void)
{
usbmuxd_log(LL_DEBUG, "device_shutdown");
- pthread_mutex_lock(&device_list_mutex);
+ mutex_lock(&device_list_mutex);
FOREACH(struct mux_device *dev, &device_list) {
FOREACH(struct mux_connection *conn, &dev->connections) {
connection_teardown(conn);
@@ -1010,7 +1010,7 @@ void device_shutdown(void)
collection_remove(&device_list, dev);
free(dev);
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
- pthread_mutex_destroy(&device_list_mutex);
+ mutex_unlock(&device_list_mutex);
+ mutex_destroy(&device_list_mutex);
collection_free(&device_list);
}