bool setThreadName(pthread_t pid, StringPiece name) { #if _WIN32 static_assert( sizeof(unsigned int) == sizeof(std::thread::id), "This assumes std::thread::id is a thin wrapper around " "the thread id as an unsigned int, but that doesn't appear to be true."); // std::thread::id is a thin wrapper around an integral thread id, // so just stick the ID in. unsigned int tid = pthread_getw32threadid_np(pid); std::thread::id id; std::memcpy(&id, &tid, sizeof(id)); return setThreadName(id, name); #else static_assert( std::is_same<pthread_t, std::thread::native_handle_type>::value, "This assumes that the native handle type is pthread_t"); static_assert( sizeof(std::thread::native_handle_type) == sizeof(std::thread::id), "This assumes std::thread::id is a thin wrapper around " "std::thread::native_handle_type, but that doesn't appear to be true."); // In most implementations, std::thread::id is a thin wrapper around // std::thread::native_handle_type, which means we can do unsafe things to // extract it. std::thread::id id; std::memcpy(static_cast<void*>(&id), &pid, sizeof(id)); return setThreadName(id, name); #endif }
static unsigned long callbackThreadID() { return static_cast<unsigned long>( #ifdef __APPLE__ pthread_mach_thread_np(pthread_self()) #elif _MSC_VER pthread_getw32threadid_np(pthread_self()) #else pthread_self() #endif ); }
void ReplayTransport::recordInput(Transport* transport, const char *filename) { assert(transport); Hdf hdf; char buf[32]; snprintf(buf, sizeof(buf), "%u", Process::GetProcessId()); hdf["pid"] = std::string(buf); #ifdef _MSC_VER snprintf(buf, sizeof(buf), "%" PRIx64, (int64_t)pthread_getw32threadid_np(Process::GetThreadId())); #else snprintf(buf, sizeof(buf), "%" PRIx64, (int64_t)Process::GetThreadId()); #endif hdf["tid"] = std::string(buf); snprintf(buf, sizeof(buf), "%u", Process::GetThreadPid()); hdf["tpid"] = std::string(buf); hdf["cmd"] = static_cast<int>(transport->getMethod()); hdf["url"] = transport->getUrl(); hdf["remote_host"] = transport->getRemoteHost(); hdf["remote_port"] = transport->getRemotePort(); transport->getHeaders(m_requestHeaders); int index = 0; for (HeaderMap::const_iterator iter = m_requestHeaders.begin(); iter != m_requestHeaders.end(); ++iter) { for (unsigned int i = 0; i < iter->second.size(); i++) { Hdf header = hdf["headers"][index++]; header["name"] = iter->first; header["value"] = iter->second[i]; } } int size; const void *data = transport->getPostData(size); if (size) { String encoded = string_uuencode((const char *)data, size); hdf["post"] = encoded.get()->data(); } else { hdf["post"] = ""; } hdf.write(filename); }