XN_THREAD_PROC VideoStream::newFrameThread(XN_THREAD_PARAM pThreadParam) { oni::implementation::VideoStream* pStream = (oni::implementation::VideoStream*)pThreadParam; pStream->newFrameThreadMainloop(); XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
XN_THREAD_PROC PlayerDevice::ThreadProc(XN_THREAD_PARAM pThreadParam) { PlayerDevice* pThis = reinterpret_cast<PlayerDevice*>(pThreadParam); pThis->MainLoop(); XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
// Thread static XN_THREAD_PROC threadFunc(XN_THREAD_PARAM pThreadParam) { DepthSenseStream* pStream = (DepthSenseStream*)pThreadParam; pStream->m_running = true; pStream->Mainloop(); XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
XN_THREAD_PROC XnVMessageListener::MessageListenerThread(XN_THREAD_PARAM param) { XnVMessageListener* pMessageListener = (XnVMessageListener*)param; pMessageListener->MainLoop(); XN_THREAD_PROC_RETURN(0); } // XnVMessageListener::MessageListenerThread
//--------------------------------------------------------------------------- // Code //--------------------------------------------------------------------------- XN_THREAD_PROC xnProfilingThread(XN_THREAD_PARAM /*pThreadParam*/) { XnChar csReport[4096]; int nReportChars; XnUInt64 nLastTime; xnOSGetHighResTimeStamp(&nLastTime); while (!g_ProfilingData.bKillThread) { xnOSSleep(g_ProfilingData.nProfilingInterval); XnUInt64 nNow; xnOSGetHighResTimeStamp(&nNow); // print profiled sections nReportChars = 0; nReportChars += sprintf(csReport + nReportChars, "Profiling Report:\n"); nReportChars += sprintf(csReport + nReportChars, "%-*s %-5s %-6s %-9s %-7s\n", (int)g_ProfilingData.nMaxSectionName, "TaskName", "Times", "% Time", "TotalTime", "AvgTime"); nReportChars += sprintf(csReport + nReportChars, "%-*s %-5s %-6s %-9s %-7s\n", (int)g_ProfilingData.nMaxSectionName, "========", "=====", "======", "=========", "======="); XnUInt64 nTotalTime = 0; for (XnUInt32 i = 0; i < g_ProfilingData.nSectionCount; ++i) { XnProfiledSection* pSection = &g_ProfilingData.aSections[i]; XnUInt64 nAvgTime = 0; XnDouble dCPUPercentage = ((XnDouble)pSection->nTotalTime) / (nNow - nLastTime) * 100.0; if (pSection->nTimesExecuted != 0) { nAvgTime = pSection->nTotalTime / pSection->nTimesExecuted; } nReportChars += sprintf(csReport + nReportChars, "%-*s %5u %6.2f %9llu %7llu\n", (int)g_ProfilingData.nMaxSectionName, pSection->csName, pSection->nTimesExecuted, dCPUPercentage, pSection->nTotalTime, nAvgTime); if (pSection->nIndentation == 0) nTotalTime += pSection->nTotalTime; // clear accumulated data pSection->nTotalTime = 0; pSection->nTimesExecuted = 0; } // print total XnDouble dCPUPercentage = ((XnDouble)nTotalTime) / (nNow - nLastTime) * 100.0; nReportChars += sprintf(csReport + nReportChars, "%-*s %5s %6.2f %9llu %7s\n", (int)g_ProfilingData.nMaxSectionName, "*** Total ***", "-", dCPUPercentage, nTotalTime, "-"); xnLogVerbose(XN_MASK_PROFILING, "%s", csReport); nLastTime = nNow; } XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
XN_THREAD_PROC XnServerSensorInvoker::ReaderThread(XN_THREAD_PARAM pThreadParam) { XnServerSensorInvoker* pThis = (XnServerSensorInvoker*)pThreadParam; while (pThis->m_bShouldRun) { pThis->ReadStreams(); } XN_THREAD_PROC_RETURN(0); }
// Thread for mail loop // デバイスのメインループ用スレッド static XN_THREAD_PROC threadFunc(XN_THREAD_PARAM pThreadParam) { //fprintf( stderr, "context is running\n" ); DepthSenseDriver* pDriver = (DepthSenseDriver*)pThreadParam; pDriver->m_context.startNodes(); pDriver->m_context.run(); pDriver->m_context.stopNodes(); XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
XN_THREAD_PROC Recorder::threadMain(XN_THREAD_PARAM pThreadParam) { Recorder* pSelf = reinterpret_cast<Recorder*>(pThreadParam); if (NULL != pSelf) { pSelf->m_running = TRUE; while (pSelf->m_running) { pSelf->messagePump(); } } XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
XN_THREAD_PROC DepthPanasonic::SchedulerThread( void* pCookie ) { DepthPanasonic* pThis = (DepthPanasonic*)pCookie; while (pThis->m_bGenerating) { // wait 33 ms (to produce 30 FPS) xnOSSleep(1000000/SUPPORTED_FPS/1000); pThis->OnNewFrame(); } XN_THREAD_PROC_RETURN(0); }
XN_THREAD_PROC SocketInConnection::ReadThreadProc(XN_THREAD_PARAM pThreadParam) { SocketInConnection* pThis = reinterpret_cast<SocketInConnection*>(pThreadParam); if (pThis == NULL) { xnLogError(XN_MASK_LINK, "Got NULL in socket read thread param :("); XN_ASSERT(FALSE); return NULL; } pThis->ReadThreadProcImpl(); XN_THREAD_PROC_RETURN(0); }
XN_THREAD_PROC xnUSBHandleEventsThread(XN_THREAD_PARAM pThreadParam) { // init timeout struct timeval timeout; timeout.tv_sec = XN_USB_HANDLE_EVENTS_TIMEOUT / 1000; timeout.tv_usec = XN_USB_HANDLE_EVENTS_TIMEOUT % 1000; while (g_InitData.bShouldThreadRun) { // let libusb process its asynchronous events libusb_handle_events_timeout(g_InitData.pContext, &timeout); } XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
/* This is the actual scheduler function. It is being run in its own thread. */ XN_THREAD_PROC xnSchedulerThreadFunc(XN_THREAD_PARAM pThreadParam) { XnScheduler* pScheduler = (XnScheduler*)pThreadParam; XnUInt64 nNow; while (!pScheduler->bStopThread) { // check when next task should be executed XnUInt64 nWait = XN_WAIT_INFINITE; XnScheduledTask* pTask = NULL; XnTaskCallbackFuncPtr pCallback = NULL; void* pCallbackArg = NULL; // check if something is in the list if (pScheduler->pFirst != NULL) { // enter critical section xnOSEnterCriticalSection(&pScheduler->hCriticalSection); pTask = pScheduler->pFirst; if (pTask != NULL) { xnOSGetTimeStamp(&nNow); if (pTask->nNextTime < nNow) { // task should be executed pCallback = pTask->pCallback; pCallbackArg = pTask->pCallbackArg; // remove it from the list pScheduler->pFirst = pTask->pNextTask; // calculate next time pTask->nNextTime += pTask->nInterval; // add it to the list again xnSchedulerAddTaskInternal(pScheduler, pTask); } else { nWait = pTask->nNextTime - nNow; } } // leave critical section xnOSLeaveCriticalSection(&pScheduler->hCriticalSection); if (pCallback != NULL) { // execute task (outside critical section) pCallback(pCallbackArg); // no need to wait (we don't know how much time did callback take) nWait = 0; } } // wait for a change of the list, or the time of the next task xnOSWaitEvent(pScheduler->hWakeThreadEvent, (XnUInt32)nWait); } XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
XN_THREAD_PROC XnSensorClient::ListenThread(XN_THREAD_PARAM pThreadParam) { XnSensorClient* pThis = (XnSensorClient*)pThreadParam; XnStatus nRetVal = pThis->Listen(); XN_THREAD_PROC_RETURN(nRetVal); }
XN_THREAD_PROC xnUSBUDEVEventsThread(XN_THREAD_PARAM pThreadParam) { struct udev *udev; struct udev_device *dev; struct udev_monitor *mon; int fd; /* Create the udev object */ udev = udev_new(); if (!udev) { printf("Can't create udev\n"); exit(1); } /* This section sets up a monitor which will report events when devices attached to the system change. Events include "add", "remove", "change", "online", and "offline". This section sets up and starts the monitoring. Events are polled for (and delivered) later on. It is important that the monitor be set up before the call to udev_enumerate_scan_devices() so that events (and devices) are not missed. For example, if enumeration happened first, there would be no event generated for a device which was attached after enumeration but before monitoring began. Note that a filter is added so that we only get events for "usb/usb_device" devices. */ /* Set up a monitor to monitor "usb_device" devices */ mon = udev_monitor_new_from_netlink(udev, "udev"); udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", "usb_device"); udev_monitor_enable_receiving(mon); /* Get the file descriptor (fd) for the monitor. This fd will get passed to select() */ fd = udev_monitor_get_fd(mon); ////////////////////////////////////////////////////////////////////////// /* Enumerate the currently connected devices and store them, so we can notify of their disconnection */ struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; enumerate = udev_enumerate_new(udev); /* Create a list of the devices. Note that it's not possible to match by "devtype="usb_device"", as in monitor filter, but this filter combination seems to do the job... */ udev_enumerate_add_match_subsystem(enumerate, "usb"); udev_enumerate_add_match_sysattr(enumerate, "idVendor", NULL); udev_enumerate_add_match_sysattr(enumerate, "idProduct", NULL); udev_enumerate_add_match_sysattr(enumerate, "busnum", NULL); udev_enumerate_add_match_sysattr(enumerate, "devnum", NULL); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); /* udev_list_entry_foreach is a macro which expands to a loop. The loop will be executed for each member in devices, setting dev_list_entry to a list entry which contains the device's path in /sys. */ udev_list_entry_foreach(dev_list_entry, devices) { const char *path; /* Get the filename of the /sys entry for the device and create a udev_device object (dev) representing it */ path = udev_list_entry_get_name(dev_list_entry); dev = udev_device_new_from_syspath(udev, path); /* Notify as if it was just connected */ // note - it's better that connectivity events register AFTER this point, // so they don't get notified of already connected devices. xnUSBDeviceConnected(dev); udev_device_unref(dev); } /* Free the enumerator object */ udev_enumerate_unref(enumerate); ////////////////////////////////////////////////////////////////////////// /* Begin polling for udev events. Events occur when devices attached to the system are added, removed, or change state. udev_monitor_receive_device() will return a device object representing the device which changed and what type of change occured. The select() system call is used to ensure that the call to udev_monitor_receive_device() will not block. The monitor was set up earler in this file, and monitoring is already underway. This section will run continuously, calling usleep() at the end of each pass. This is to demonstrate how to use a udev_monitor in a non-blocking way. */ while (g_bShouldRunUDEVThread) { /* Set up the call to select(). In this case, select() will only operate on a single file descriptor, the one associated with our udev_monitor. Note that the timeval object is set to 0, which will cause select() to not block. */ fd_set fds; struct timeval tv; int ret; FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = 0; tv.tv_usec = 250 * 1000; ret = select(fd+1, &fds, NULL, NULL, &tv); /* Check if our file descriptor has received data. */ if (ret > 0 && FD_ISSET(fd, &fds)) { /* Make the call to receive the device. select() ensured that this will not block. */ dev = udev_monitor_receive_device(mon); if (dev) { /* printf(" Node: %s\n", udev_device_get_devnode(dev)); printf(" Subsystem: %s\n", udev_device_get_subsystem(dev)); printf(" Devtype: %s\n", udev_device_get_devtype(dev)); printf(" Action: %s\n", udev_device_get_action(dev)); printf(" VID/PID: %s %s\n", udev_device_get_sysattr_value(dev,"idVendor"), udev_device_get_sysattr_value(dev,"idProduct")); printf(" %s\n %s\n", udev_device_get_sysattr_value(dev,"manufacturer"), udev_device_get_sysattr_value(dev,"product")); fflush(stdout); */ const XnChar *action = udev_device_get_action(dev); if (!xnOSStrCmp(action, "add")) { xnUSBDeviceConnected(dev); } else if (!xnOSStrCmp(action, "remove")) { xnUSBDeviceDisconnected(dev); } //note - handle the other events? "change" event might be useful... // now release dev udev_device_unref(dev); } else { xnLogWarning(XN_MASK_USB, "No Device from udev_monitor_receive_device(). An error occured."); } } } udev_monitor_unref(mon); udev_unref(udev); XN_THREAD_PROC_RETURN(XN_STATUS_OK); }
XN_THREAD_PROC XnServerSession::ServeThreadCallback(XN_THREAD_PARAM pThreadParam) { XnServerSession* pThis = (XnServerSession*)pThreadParam; XnStatus nRetVal = pThis->ServeThread(); XN_THREAD_PROC_RETURN(nRetVal); }
// データ受信用ワーカー XN_THREAD_PROC workerThread(XN_THREAD_PARAM) { ofLog(OF_LOG_NOTICE, "Start receiver thread"); while (1) { zmq::message_t request; clients.recv (&request); string clientRequest = static_cast<const char*>(request.data()); ofLog(OF_LOG_NOTICE, clientRequest); stringstream replyStr; if (clientRequest.find("GET_DEPTH", 0) != string::npos) { // 深度情報をJSON文字列にする int width = 640; int height = 480; replyStr << "{\"type\":\"depth\", \"data\":\""; for(int y = 0; y < height; y+=2) { for(int x = 0; x < width; x+=2) { replyStr << ofToString((int)grayPixels[x+y*width]) << ','; } } replyStr << "\", \"imageFiles\":["; int files = savedFileNames.size(); for (int i=0; i<files; i++) { int fileNo = (int)savedFileNames.front(); if (i > 0) { replyStr << ','; } replyStr << fileNo; savedFileNames.pop(); } replyStr << "]}"; } else if (clientRequest.find("UP_ANGLE", 0) != string::npos) { // チルト角を増やす #ifdef TARGET_OSX tiltAngle = min(++tiltAngle, 30); hardware.setTiltAngle(tiltAngle); #endif // 現在の角度をレスポンスとして返す replyStr << "{\"type\":\"tilt\",\"angle\":" << tiltAngle << "}"; } else if (clientRequest.find("DOWN_ANGLE", 0) != string::npos) { // チルト角を減らす #ifdef TARGET_OSX tiltAngle = max(--tiltAngle, -30); hardware.setTiltAngle(tiltAngle); #endif // 現在の角度をレスポンスとして返す replyStr << "{\"type\":\"tilt\",\"angle\":" << tiltAngle << "}"; } else { // 未知のイベント ofLog(OF_LOG_ERROR, "Unknown Event"); replyStr << "Unknown Event" << endl; } // レスポンスを返す s_send(clients, replyStr.str()); } XN_THREAD_PROC_RETURN(XN_STATUS_OK); }