summaryrefslogtreecommitdiffstats
path: root/src/device.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2014-01-09 18:49:56 +0100
committerGravatar Nikias Bassen2014-01-09 18:49:56 +0100
commit3ab20d711f8a64833e6dcad13766fcf2912ff2e2 (patch)
treee6246dbe0f9bf3df5e6237c1511d90583702e2bb /src/device.c
parent61c399db59cb61118901819e0956e514dd511885 (diff)
downloadusbmuxd-3ab20d711f8a64833e6dcad13766fcf2912ff2e2.tar.gz
usbmuxd-3ab20d711f8a64833e6dcad13766fcf2912ff2e2.tar.bz2
device: reduce locking time inside device_get_list() and device_get_count()
Diffstat (limited to 'src/device.c')
-rw-r--r--src/device.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/device.c b/src/device.c
index 356c70a..d2da51a 100644
--- a/src/device.c
+++ b/src/device.c
@@ -737,22 +737,29 @@ void device_set_preflight_cb_data(int device_id, void* data)
int device_get_count(int include_hidden)
{
int count = 0;
+ struct collection dev_list = {NULL, 0};
+ pthread_mutex_lock(&device_list_mutex);
+ collection_copy(&dev_list, &device_list);
pthread_mutex_unlock(&device_list_mutex);
- FOREACH(struct mux_device *dev, &device_list) {
+
+ FOREACH(struct mux_device *dev, &dev_list) {
if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible))
count++;
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+
+ collection_free(&dev_list);
return count;
}
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);
+ collection_copy(&dev_list, &device_list);
+ pthread_mutex_unlock(&device_list_mutex);
- int total_count = collection_count(&device_list);
- *devices = malloc(sizeof(struct device_info) * total_count);
+ *devices = malloc(sizeof(struct device_info) * dev_list.capacity);
struct device_info *p = *devices;
FOREACH(struct mux_device *dev, &device_list) {
@@ -765,7 +772,9 @@ int device_get_list(int include_hidden, struct device_info **devices)
p++;
}
} ENDFOREACH
- pthread_mutex_unlock(&device_list_mutex);
+
+ collection_free(&dev_list);
+
return count;
}