int ReapThreadProc(void* lpParameter) { fprintf(stdout, "Thread execution started.\n"); libusb_device::TListTransfers& listTransfers (*(libusb_device::TListTransfers*)lpParameter); while(!listTransfers.Empty()) { listTransfers.Head()->libusb.dev_handle->dev->ctx->processing.Wait(); transfer_wrapper* wrapper = listTransfers.Head(); if (NULL != wrapper) ReapTransfer(wrapper, 1000); if (hProblem.Check()) { fprintf(stderr, "Thread is waiting for user reaction...\n"); // wait for user reaction... hReaction.Wait(); // did the user decide to abort? if (hAbort.Check()) { fprintf(stderr, "Thread is aborting: releasing transfers...\n"); while(!listTransfers.Empty()) { transfer_wrapper* wrapper = listTransfers.Head(); libusb_cancel_transfer(&(wrapper->libusb)); ReapTransfer(wrapper, 0); } fprintf(stderr, "Thread execution aborted.\n"); return(LIBUSB_ERROR_INTERRUPTED); } } } fprintf(stdout, "Thread execution finished.\n"); return(0); }
int ReapThreadProc(void* lpParameter) { fprintf(stdout, "Thread execution started.\n"); libusb_device::TListTransfers& listTransfers (*(libusb_device::TListTransfers*)lpParameter); mutexReady.Enter(); assert(mapDeviceTransfersReady.find(&listTransfers) == mapDeviceTransfersReady.end()); mapDeviceTransfersReady[&listTransfers] = new libusb_device::TListTransfers; libusb_device::TListTransfers& lstReady = *mapDeviceTransfersReady[&listTransfers]; mutexReady.Leave(); while(!listTransfers.Empty() || !lstReady.Empty()) { if (!listTransfers.Empty()) { transfer_wrapper* wrapper = listTransfers.Head(); if (NULL != wrapper) //ReapTransfer(wrapper, 10000, &lstReady); // producer-consumer model ReapTransfer(wrapper, 10000); } else { // This is important! Otherwise the thread may "take control" of the CPU // if it happens to be running at TIME_CRITICAL priority... fprintf(stdout, "ReapThreadProc(): nothing to do, sleeping...\n"); QuickThread::Yield(); } if (hProblem.Check()) { fprintf(stderr, "Thread is waiting for user reaction...\n"); // wait for user reaction... hReaction.Wait(); // did the user decide to abort? if (hAbort.Check()) { fprintf(stderr, "Thread is aborting: releasing transfers...\n"); while(!listTransfers.Empty()) { transfer_wrapper* wrapper = listTransfers.Head(); libusb_cancel_transfer(&(wrapper->libusb)); ReapTransfer(wrapper, 0); } fprintf(stderr, "Thread execution aborted.\n"); return(LIBUSB_ERROR_INTERRUPTED); } } } mutexReady.Enter(); SAFE_DELETE(mapDeviceTransfersReady[&listTransfers]); mapDeviceTransfersReady.erase(&listTransfers); mutexReady.Leave(); fprintf(stdout, "Thread execution finished.\n"); return(0); }