📄 fusb_darwin.cc
字号:
if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::run_thread: finished for %s.\n", l_input_p ? "read" : "write"); // release the run thread running mutex l_runThreadRunning->unlock ();}voidfusb_ephandle_darwin::read_thread (void* arg){ if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::read_thread: starting.\n"); fusb_ephandle_darwin* This = static_cast<fusb_ephandle_darwin*>(arg); // before doing anything else, lock the read running mutex. this // mutex does flow control between this thread and the run_thread mld_mutex_ptr l_readRunning = This->d_readRunning; l_readRunning->lock (); // signal the read condition from run_thread() to continue // lock the readBlock mutex first; this will force waiting until the // ->wait() command is issued in ::run_thread() mld_condition_ptr l_readBlock = This->d_readBlock; mld_mutex_ptr l_read_block_mutex = l_readBlock->mutex (); l_read_block_mutex->lock (); // now that the lock is in place, signal the parent thread that // things are running here l_readBlock->signal (); // release the run_block mutex, just in case l_read_block_mutex->unlock (); // queue up all of the available read requests s_queue_ptr l_queue = This->d_queue; l_queue->iterate_start (); s_node_ptr l_node = l_queue->iterate_next (); while (l_node) { This->read_issue (l_node->both ()); l_node = l_queue->iterate_next (); } if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::read_thread: finished.\n"); // release the read running mutex, to let the parent thread knows // that this thread is finished l_readRunning->unlock ();}voidfusb_ephandle_darwin::read_issue (s_both_ptr l_both){ if ((! l_both) || (! d_started)) { if (usb_debug > 4) fprintf (stderr, "fusb_ephandle_darwin::read_issue: Doing nothing; " "l_both is %X; started is %s\n", (unsigned int) l_both, d_started ? "TRUE" : "FALSE"); return; }// set the node and buffer from the input "both" s_node_ptr l_node = l_both->node (); s_buffer_ptr l_buf = l_node->object (); void* v_buffer = (void*) l_buf->buffer ();// read up to d_bufLenBytes UInt32 bufLen = d_bufLenBytes; l_buf->n_used (bufLen);// setup system call result io_return_t result = kIOReturnSuccess; if (d_transferType == kUSBInterrupt)/* This is an interrupt pipe. We can't specify a timeout. */ result = d_interface->ReadPipeAsync (d_interfaceRef, d_pipeRef, v_buffer, bufLen, (IOAsyncCallback1) read_completed, (void*) l_both); else result = d_interface->ReadPipeAsyncTO (d_interfaceRef, d_pipeRef, v_buffer, bufLen, 0, USB_TIMEOUT, (IOAsyncCallback1) read_completed, (void*) l_both); if (result != kIOReturnSuccess) USB_ERROR_STR_NO_RET (- darwin_to_errno (result), "fusb_ephandle_darwin::read_issue " "(ReadPipeAsync%s): %s", d_transferType == kUSBInterrupt ? "" : "TO", darwin_error_str (result)); else if (usb_debug > 4) fprintf (stderr, "fusb_ephandle_darwin::read_issue: " "Queued %X (%ld Bytes)\n", (unsigned int) l_both, bufLen);}voidfusb_ephandle_darwin::read_completed (void* refCon, io_return_t result, void* io_size){ UInt32 l_size = (UInt32) io_size; s_both_ptr l_both = static_cast<s_both_ptr>(refCon); fusb_ephandle_darwin* This = static_cast<fusb_ephandle_darwin*>(l_both->This ()); s_node_ptr l_node = l_both->node (); circular_buffer<char>* l_buffer = This->d_buffer; s_buffer_ptr l_buf = l_node->object (); UInt32 l_i_size = l_buf->n_used (); if (This->d_started && (l_i_size != l_size)) fprintf (stderr, "fusb_ephandle_darwin::read_completed: " "Expected %ld bytes; read %ld.\n", l_i_size, l_size); else if (usb_debug > 4) fprintf (stderr, "fusb_ephandle_darwin::read_completed: " "Read %X (%ld bytes)\n", (unsigned int) l_both, l_size);// add this read to the transfer buffer if (l_buffer->enqueue (l_buf->buffer (), l_size) == -1) { fputs ("iU", stderr); fflush (stderr); }// set buffer's # data to 0 l_buf->n_used (0);// issue another read for this "both" This->read_issue (l_both);}intfusb_ephandle_darwin::read (void* buffer, int nbytes){ UInt32 l_nbytes = (UInt32) nbytes; d_buffer->dequeue ((char*) buffer, &l_nbytes); if (usb_debug > 4) fprintf (stderr, "fusb_ephandle_darwin::read: request for %d bytes, %ld bytes retrieved.\n", nbytes, l_nbytes); return ((int) l_nbytes);}intfusb_ephandle_darwin::write (const void* buffer, int nbytes){ UInt32 l_nbytes = (UInt32) nbytes; if (! d_started) { if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::write: Not yet started.\n"); return (0); } while (l_nbytes != 0) {// find out how much data to copy; limited to "d_bufLenBytes" per node UInt32 t_nbytes = (l_nbytes > d_bufLenBytes) ? d_bufLenBytes : l_nbytes;// get next available node to write into;// blocks internally if none available s_node_ptr l_node = d_queue->find_next_available_node ();// copy the input into the node's buffer s_buffer_ptr l_buf = l_node->object (); l_buf->buffer ((char*) buffer, t_nbytes); void* v_buffer = (void*) l_buf->buffer ();// setup callback parameter & system call return s_both_ptr l_both = l_node->both (); io_return_t result = kIOReturnSuccess; if (d_transferType == kUSBInterrupt)/* This is an interrupt pipe ... can't specify a timeout. */ result = d_interface->WritePipeAsync (d_interfaceRef, d_pipeRef, v_buffer, t_nbytes, (IOAsyncCallback1) write_completed, (void*) l_both); else result = d_interface->WritePipeAsyncTO (d_interfaceRef, d_pipeRef, v_buffer, t_nbytes, 0, USB_TIMEOUT, (IOAsyncCallback1) write_completed, (void*) l_both); if (result != kIOReturnSuccess) USB_ERROR_STR (-1, - darwin_to_errno (result), "fusb_ephandle_darwin::write_thread " "(WritePipeAsync%s): %s", d_transferType == kUSBInterrupt ? "" : "TO", darwin_error_str (result)); else if (usb_debug > 4) { fprintf (stderr, "fusb_ephandle_darwin::write_thread: " "Queued %X (%ld Bytes)\n", (unsigned int) l_both, t_nbytes); } l_nbytes -= t_nbytes; } return (nbytes);}voidfusb_ephandle_darwin::write_completed (void* refCon, io_return_t result, void* io_size){ s_both_ptr l_both = static_cast<s_both_ptr>(refCon); fusb_ephandle_darwin* This = static_cast<fusb_ephandle_darwin*>(l_both->This ()); UInt32 l_size = (UInt32) io_size; s_node_ptr l_node = l_both->node (); s_queue_ptr l_queue = This->d_queue; s_buffer_ptr l_buf = l_node->object (); UInt32 l_i_size = l_buf->n_used (); if (This->d_started && (l_i_size != l_size)) fprintf (stderr, "fusb_ephandle_darwin::write_completed: " "Expected %ld bytes written; wrote %ld.\n", l_i_size, l_size); else if (usb_debug > 4) fprintf (stderr, "fusb_ephandle_darwin::write_completed: " "Wrote %X (%ld Bytes)\n", (unsigned int) l_both, l_size);// set buffer's # data to 0 l_buf->n_used (0);// make the node available for reuse l_queue->make_node_available (l_node);}voidfusb_ephandle_darwin::abort (){ if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::abort: starting.\n"); io_return_t result = d_interface->AbortPipe (d_interfaceRef, d_pipeRef); if (result != kIOReturnSuccess) USB_ERROR_STR_NO_RET (- darwin_to_errno (result), "fusb_ephandle_darwin::abort " "(AbortPipe): %s", darwin_error_str (result)); if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::abort: finished.\n");}boolfusb_ephandle_darwin::stop (){ if (! d_started) return (true); if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::stop: stopping %s.\n", d_input_p ? "read" : "write"); d_started = false;// abort any pending IO transfers abort ();// wait for write transfer to finish wait_for_completion ();// tell IO buffer to abort any waiting conditions d_buffer->abort ();// stop the run loop CFRunLoopStop (d_CFRunLoopRef);// wait for the runThread to stop d_runThreadRunning->lock (); d_runThreadRunning->unlock (); if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::stop: %s stopped.\n", d_input_p ? "read" : "write"); return (true);}voidfusb_ephandle_darwin::wait_for_completion (){ if (d_queue) while (d_queue->in_use ()) usleep (1000);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -