diff options
| author | 2012-05-05 02:08:50 +0200 | |
|---|---|---|
| committer | 2012-05-05 02:08:50 +0200 | |
| commit | c1e71dca5279df6c13f5866ddd7febf8ba2891b5 (patch) | |
| tree | 3dadcdeb11cdf1a2ac49b789355b849a77604d8d /libusbmuxd | |
| parent | 4e0786e4b7c576f180cddce9b558484ed73d86e4 (diff) | |
| download | usbmuxd-c1e71dca5279df6c13f5866ddd7febf8ba2891b5.tar.gz usbmuxd-c1e71dca5279df6c13f5866ddd7febf8ba2891b5.tar.bz2 | |
libusbmuxd: use mutex to handle concurrency issues
Diffstat (limited to 'libusbmuxd')
| -rw-r--r-- | libusbmuxd/libusbmuxd.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c index bb5f1f0..409026a 100644 --- a/libusbmuxd/libusbmuxd.c +++ b/libusbmuxd/libusbmuxd.c | |||
| @@ -69,9 +69,15 @@ static struct collection devices; | |||
| 69 | static usbmuxd_event_cb_t event_cb = NULL; | 69 | static usbmuxd_event_cb_t event_cb = NULL; |
| 70 | #ifdef WIN32 | 70 | #ifdef WIN32 |
| 71 | HANDLE devmon = NULL; | 71 | HANDLE devmon = NULL; |
| 72 | CRITICAL_SECTION mutex; | ||
| 73 | static int mutex_initialized = 0; | ||
| 74 | #define LOCK if (!mutex_initialized) { InitializeCriticalSection(&mutex); mutex_initialized = 1; } EnterCriticalSection(&mutex); | ||
| 75 | #define UNLOCK LeaveCriticalSection(&mutex); | ||
| 72 | #else | 76 | #else |
| 73 | pthread_t devmon; | 77 | pthread_t devmon; |
| 74 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | 78 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 79 | #define LOCK pthread_mutex_lock(&mutex) | ||
| 80 | #define UNLOCK pthread_mutex_unlock(&mutex) | ||
| 75 | #endif | 81 | #endif |
| 76 | static int listenfd = -1; | 82 | static int listenfd = -1; |
| 77 | 83 | ||
| @@ -490,12 +496,15 @@ retry: | |||
| 490 | } | 496 | } |
| 491 | 497 | ||
| 492 | use_tag++; | 498 | use_tag++; |
| 499 | LOCK; | ||
| 493 | if (send_listen_packet(sfd, use_tag) <= 0) { | 500 | if (send_listen_packet(sfd, use_tag) <= 0) { |
| 501 | UNLOCK; | ||
| 494 | fprintf(stderr, "%s: ERROR: could not send listen packet\n", __func__); | 502 | fprintf(stderr, "%s: ERROR: could not send listen packet\n", __func__); |
| 495 | close_socket(sfd); | 503 | close_socket(sfd); |
| 496 | return -1; | 504 | return -1; |
| 497 | } | 505 | } |
| 498 | if (usbmuxd_get_result(sfd, use_tag, &res) && (res != 0)) { | 506 | if (usbmuxd_get_result(sfd, use_tag, &res) && (res != 0)) { |
| 507 | UNLOCK; | ||
| 499 | close_socket(sfd); | 508 | close_socket(sfd); |
| 500 | #ifdef HAVE_PLIST | 509 | #ifdef HAVE_PLIST |
| 501 | if ((res == RESULT_BADVERSION) && (proto_version != 1)) { | 510 | if ((res == RESULT_BADVERSION) && (proto_version != 1)) { |
| @@ -506,6 +515,7 @@ retry: | |||
| 506 | fprintf(stderr, "%s: ERROR: did not get OK but %d\n", __func__, res); | 515 | fprintf(stderr, "%s: ERROR: did not get OK but %d\n", __func__, res); |
| 507 | return -1; | 516 | return -1; |
| 508 | } | 517 | } |
| 518 | UNLOCK; | ||
| 509 | 519 | ||
| 510 | return sfd; | 520 | return sfd; |
| 511 | } | 521 | } |
| @@ -692,12 +702,14 @@ retry: | |||
| 692 | } | 702 | } |
| 693 | 703 | ||
| 694 | use_tag++; | 704 | use_tag++; |
| 705 | LOCK; | ||
| 695 | if (send_listen_packet(sfd, use_tag) > 0) { | 706 | if (send_listen_packet(sfd, use_tag) > 0) { |
| 696 | res = -1; | 707 | res = -1; |
| 697 | // get response | 708 | // get response |
| 698 | if (usbmuxd_get_result(sfd, use_tag, &res) && (res == 0)) { | 709 | if (usbmuxd_get_result(sfd, use_tag, &res) && (res == 0)) { |
| 699 | listen_success = 1; | 710 | listen_success = 1; |
| 700 | } else { | 711 | } else { |
| 712 | UNLOCK; | ||
| 701 | close_socket(sfd); | 713 | close_socket(sfd); |
| 702 | #ifdef HAVE_PLIST | 714 | #ifdef HAVE_PLIST |
| 703 | if ((res == RESULT_BADVERSION) && (proto_version != 1)) { | 715 | if ((res == RESULT_BADVERSION) && (proto_version != 1)) { |
| @@ -713,6 +725,7 @@ retry: | |||
| 713 | } | 725 | } |
| 714 | 726 | ||
| 715 | if (!listen_success) { | 727 | if (!listen_success) { |
| 728 | UNLOCK; | ||
| 716 | fprintf(stderr, "%s: Could not send listen request!\n", __func__); | 729 | fprintf(stderr, "%s: Could not send listen request!\n", __func__); |
| 717 | return -1; | 730 | return -1; |
| 718 | } | 731 | } |
| @@ -726,6 +739,7 @@ retry: | |||
| 726 | dev = payload; | 739 | dev = payload; |
| 727 | usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t)); | 740 | usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t)); |
| 728 | if (!devinfo) { | 741 | if (!devinfo) { |
| 742 | UNLOCK; | ||
| 729 | fprintf(stderr, "%s: Out of memory!\n", __func__); | 743 | fprintf(stderr, "%s: Out of memory!\n", __func__); |
| 730 | free(payload); | 744 | free(payload); |
| 731 | return -1; | 745 | return -1; |
| @@ -769,6 +783,7 @@ retry: | |||
| 769 | break; | 783 | break; |
| 770 | } | 784 | } |
| 771 | } | 785 | } |
| 786 | UNLOCK; | ||
| 772 | 787 | ||
| 773 | // explicitly close connection | 788 | // explicitly close connection |
| 774 | close_socket(sfd); | 789 | close_socket(sfd); |
