diff options
| author | 2014-01-09 18:49:56 +0100 | |
|---|---|---|
| committer | 2014-01-09 18:49:56 +0100 | |
| commit | 3ab20d711f8a64833e6dcad13766fcf2912ff2e2 (patch) | |
| tree | e6246dbe0f9bf3df5e6237c1511d90583702e2bb | |
| parent | 61c399db59cb61118901819e0956e514dd511885 (diff) | |
| download | usbmuxd-3ab20d711f8a64833e6dcad13766fcf2912ff2e2.tar.gz usbmuxd-3ab20d711f8a64833e6dcad13766fcf2912ff2e2.tar.bz2 | |
device: reduce locking time inside device_get_list() and device_get_count()
| -rw-r--r-- | src/device.c | 19 |
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) | |||
| 737 | int device_get_count(int include_hidden) | 737 | int device_get_count(int include_hidden) |
| 738 | { | 738 | { |
| 739 | int count = 0; | 739 | int count = 0; |
| 740 | struct collection dev_list = {NULL, 0}; | ||
| 741 | pthread_mutex_lock(&device_list_mutex); | ||
| 742 | collection_copy(&dev_list, &device_list); | ||
| 740 | pthread_mutex_unlock(&device_list_mutex); | 743 | pthread_mutex_unlock(&device_list_mutex); |
| 741 | FOREACH(struct mux_device *dev, &device_list) { | 744 | |
| 745 | FOREACH(struct mux_device *dev, &dev_list) { | ||
| 742 | if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) | 746 | if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) |
| 743 | count++; | 747 | count++; |
| 744 | } ENDFOREACH | 748 | } ENDFOREACH |
| 745 | pthread_mutex_unlock(&device_list_mutex); | 749 | |
| 750 | collection_free(&dev_list); | ||
| 746 | return count; | 751 | return count; |
| 747 | } | 752 | } |
| 748 | 753 | ||
| 749 | int device_get_list(int include_hidden, struct device_info **devices) | 754 | int device_get_list(int include_hidden, struct device_info **devices) |
| 750 | { | 755 | { |
| 751 | int count = 0; | 756 | int count = 0; |
| 757 | struct collection dev_list = {NULL, 0}; | ||
| 752 | pthread_mutex_lock(&device_list_mutex); | 758 | pthread_mutex_lock(&device_list_mutex); |
| 759 | collection_copy(&dev_list, &device_list); | ||
| 760 | pthread_mutex_unlock(&device_list_mutex); | ||
| 753 | 761 | ||
| 754 | int total_count = collection_count(&device_list); | 762 | *devices = malloc(sizeof(struct device_info) * dev_list.capacity); |
| 755 | *devices = malloc(sizeof(struct device_info) * total_count); | ||
| 756 | struct device_info *p = *devices; | 763 | struct device_info *p = *devices; |
| 757 | 764 | ||
| 758 | FOREACH(struct mux_device *dev, &device_list) { | 765 | FOREACH(struct mux_device *dev, &device_list) { |
| @@ -765,7 +772,9 @@ int device_get_list(int include_hidden, struct device_info **devices) | |||
| 765 | p++; | 772 | p++; |
| 766 | } | 773 | } |
| 767 | } ENDFOREACH | 774 | } ENDFOREACH |
| 768 | pthread_mutex_unlock(&device_list_mutex); | 775 | |
| 776 | collection_free(&dev_list); | ||
| 777 | |||
| 769 | return count; | 778 | return count; |
| 770 | } | 779 | } |
| 771 | 780 | ||
