This approach just crash the app:
DWORD WINAPI LibUSBDevice::LoopEvent(void* lpParam)
{
while (RunLoopCode)
{
try
{
Sleep(100);
struct timeval tv = { 1, 0 };
int completed =0;
//cout << "before libusb_handle_events_timeout " << std::endl;
int err = libusb_handle_events_timeout_completed(NULL, &tv,&completed );
//cout << "after libusb_handle_events_timeout " << std::endl;
if (err<0)
{
cout << "!!!!!!!!!!!!!!!!!!err calling done
libusb_handle_events_timeout!!!!!!!!!!!!" << std::endl;
}
}
catch (...)
{
cout << "!!!!!!!!!!!!unexpected exception!!!!!" << std::endl;
}
}
return 0;
}
This is in a seperator thread, which is always running, other threads will
call libusb_submit_transfer to submit read and write request simultaneously.
it crash the app, the stack trace are as following:
winusb_abort_transfers+0xf6 [c:\program
files\libusbx-1.0.12\libusb\os\windows_usb.c @ 2863]
windows_abort_transfers+0x41 [c:\program
files\libusbx-1.0.12\libusb\os\windows_usb.c @ 1929]
windows_cancel_transfer+0x51 [c:\program
files\libusbx-1.0.12\libusb\os\windows_usb.c @ 1941]
libusb_cancel_transfer+0x54 [c:\program files\libusbx-1.0.12\libusb\io.c @
1358]
handle_timeout+0x35 [c:\program files\libusbx-1.0.12\libusb\io.c @ 1770]
handle_timeouts_locked+0x110 [c:\program files\libusbx-1.0.12\libusb\io.c @
1814]
handle_timeouts+0x38 [c:\program files\libusbx-1.0.12\libusb\io.c @ 1823]
handle_events+0x258 [c:\program files\libusbx-1.0.12\libusb\io.c @ 1897]
libusb_handle_events_timeout_completed+0x9e [c:\program
files\libusbx-1.0.12\libusb\io.c @ 2029]
LibUSBDevice::LoopEvent+0x62 [c:\USB\usb_io_native\libusbdevice.cpp @ 108]
KERNEL32!BaseThreadInitThunk+0xd
ntdll!RtlUserThreadStart+0x21
Asynchronous device I/O
specifically, the following code:
if (!WinUsb_AbortPipe(winusb_handle, transfer->endpoint)) {
in windows_usb.c crash the app.
strangely enough, the catch (...) does not catch the exception.
Please advice.
Thx
On Wed, Sep 12, 2012 at 4:50 PM, John Chen <john...@gmail.com> wrote:
> Oops! I was not understand " running multiple simultaneous event loops"
> right, I thought in multi-threading app, of course you are running event
> multiple time.
>
> Anyway, I created a new thread and it sees to fix the problem, but now I
> am having a performance issue, in the new thread , I have
> while (continue)
> {
> err = libusb_handle_events_timeout_completed(NULL, &tv,&completed );
> Sleep(100)
> }
>
> Also, in the BulkRead Thread, I have
> while (!userdata->ExitTransfer)
> {
> cout << "Bulk Write waiting the call back............" << std::endl;
> Sleep(100);
> }
>
> So I will lost 200 ms or whatever time I need to wait, anyway around this?
> thx
>
>
>
> On Wed, Sep 12, 2012 at 2:57 PM, David Grant <davidgr...@gmail.com> wrote:
>
>>
>>
>> On Wed, Sep 12, 2012 at 2:35 PM, John Chen <john...@gmail.com> wrote:
>>
>>> Tim,
>>>
>>> If I call the same code with one thread, it works without any problem.
>>> so I believe it has something to do with how libusb work in multi-threading
>>> code.
>>>
>>> Do you have any sample code how to handle multi-threading for libusb
>>> asynchronous operations? do I need to anything to protect call to
>>> libusb_handle_events_timeout_completed ? (can
>>> libusb_handle_events_timeout_completed
>>> be call at same time from multiple thread?)
>>> Did I do anything wrong in the loop? anything I am suppose to do I did
>>> not do in code?
>>>
>>> I pretty much showed you the whole libusb related code here, the only
>>> thing I did not show is the client, which is very complicated and I am not
>>> sure how it was related to this libusb problem.
>>>
>>> The bottom line is I got a client spawn multiple threads, calling my
>>> code, one BulkRead, one BulkWrite, in both function, it has the
>>> same identical loop as following:
>>> while (!userdata->ExitTransfer)
>>> {
>>> //I also added Lock here for the whole block here and
>>> to serialize the call to
>>> //libusb_handle_events_timeout_completed, but it still does not
>>> work.
>>> struct timeval tv = { 1, 0 };
>>> int completed =0;
>>>
>>> err = libusb_handle_events_timeout_completed(NULL, &tv,&completed
>>> );
>>> }
>>>
>>>
>>>
>>>
>>>
>> Tim said: "It looks like you are running multiple simultaneous event
>> loops. Is that correct? You don't need to do that."
>>
>> Did you address that?
>>
>> Don't put the libusb_handle_events_timeout_completed where you currently
>> have it.... Have just one event loop thread. So when your code initializes,
>> create a thread that does this event handling and move the event handling
>> out of your code that is doing the submit_transfer.
>>
>> Dave
>>
>>
>> ------------------------------------------------------------------------------
>> Live Security Virtual Conference
>> Exclusive live event will cover all the ways today's security and
>> threat landscape has changed and how IT managers can respond. Discussions
>> will include endpoint security, mobile security and the latest in malware
>> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>> _______________________________________________
>> libusbx-devel mailing list
>> libusbx-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/libusbx-devel
>>
>>
>
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel