From eab60712a03b8bc8f1afcfe9678e6ee2c0a359cf Mon Sep 17 00:00:00 2001
From: Rene Liebscher <r.liebscher@gmx.de>
Date: Sat, 21 Jun 2014 21:29:18 +0000
Subject: [PATCH] patch #8419 fix ftdi_syncbb hang with libftdi 1 * ft245r.c:
 set pthread cancel type to asynchronous, reorder ftdi_usb_close/deinit

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1324 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog | 5 +++++
 ft245r.c  | 5 +++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 14f46334..3d9aaadb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2014-06-21  Rene Liebscher <R.Liebscher@gmx.de>
+
+	patch #8419 fix ftdi_syncbb hang with libftdi 1
+	* ft245r.c: set pthread cancel type to asynchronous, reorder ftdi_usb_close/deinit
+
 2014-06-17  Rene Liebscher <R.Liebscher@gmx.de>
 
 	* avrftdi_private.h: added missing msg level for avrdude_message 
diff --git a/ft245r.c b/ft245r.c
index b516fe59..323b6a2b 100644
--- a/ft245r.c
+++ b/ft245r.c
@@ -173,6 +173,7 @@ static void add_to_buf (unsigned char c) {
 }
 
 static void *reader (void *arg) {
+    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
     struct ftdi_context *handle = (struct ftdi_context *)(arg);
     unsigned char buf[0x1000];
     int br, i;
@@ -642,10 +643,10 @@ static void ft245r_close(PROGRAMMER * pgm) {
         // I think the switch to BB mode and back flushes the buffer.
         ftdi_set_bitmode(handle, 0, BITMODE_SYNCBB); // set Synchronous BitBang, all in puts
         ftdi_set_bitmode(handle, 0, BITMODE_RESET); // disable Synchronous BitBang
+        ftdi_usb_close(handle);
+        ftdi_deinit (handle);
         pthread_cancel(readerthread);
         pthread_join(readerthread, NULL);
-        ftdi_usb_close(handle);
-        ftdi_deinit (handle); // TODO this works with libftdi 0.20, but hangs with 1.0
         free(handle);
         handle = NULL;
     }