/* * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
#ifdefined(WEBRTC_WIN) int Win32PriorityFromThreadPriority(ThreadPriority priority) { switch (priority) { case ThreadPriority::kLow: return THREAD_PRIORITY_BELOW_NORMAL; case ThreadPriority::kNormal: return THREAD_PRIORITY_NORMAL; case ThreadPriority::kHigh: return THREAD_PRIORITY_ABOVE_NORMAL; case ThreadPriority::kRealtime: return THREAD_PRIORITY_TIME_CRITICAL;
}
} #endif
bool SetPriority(ThreadPriority priority) { #ifdefined(WEBRTC_WIN) return SetThreadPriority(GetCurrentThread(),
Win32PriorityFromThreadPriority(priority)) != FALSE; #elifdefined(__native_client__) || defined(WEBRTC_FUCHSIA) || \
(defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)) // Setting thread priorities is not supported in NaCl, Fuchsia or Emscripten // without pthreads. returntrue; #elifdefined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX) // TODO(tommi): Switch to the same mechanism as Chromium uses for changing // thread priorities. returntrue; #elifdefined(WEBRTC_MOZILLA_BUILD) && defined(WEBRTC_LINUX) // Only realtime audio uses realtime scheduling in Firefox. returntrue; #else constint policy = SCHED_FIFO; constint min_prio = sched_get_priority_min(policy); constint max_prio = sched_get_priority_max(policy); if (min_prio == -1 || max_prio == -1) { returnfalse;
}
if (max_prio - min_prio <= 2) returnfalse;
// Convert webrtc priority to system priorities:
sched_param param; constint top_prio = max_prio - 1; constint low_prio = min_prio + 1; switch (priority) { case ThreadPriority::kLow:
param.sched_priority = low_prio; break; case ThreadPriority::kNormal: // The -1 ensures that the kHighPriority is always greater or equal to // kNormalPriority.
param.sched_priority = (low_prio + top_prio - 1) / 2; break; case ThreadPriority::kHigh:
param.sched_priority = std::max(top_prio - 2, low_prio); break; case ThreadPriority::kRealtime:
param.sched_priority = top_prio; break;
} return pthread_setschedparam(pthread_self(), policy, ¶m) == 0; #endif// defined(WEBRTC_WIN)
}
#ifdefined(WEBRTC_WIN)
DWORD WINAPI RunPlatformThread(void* param) { // The GetLastError() function only returns valid results when it is called // after a Win32 API function that returns a "failed" result. A crash dump // contains the result from GetLastError() and to make sure it does not // falsely report a Windows error we call SetLastError here.
::SetLastError(ERROR_SUCCESS); auto function = static_cast<std::function<void()>*>(param);
(*function)(); delete function; return 0;
} #else void* RunPlatformThread(void* param) { auto function = static_cast<std::function<void()>*>(param);
(*function)(); delete function; return 0;
} #endif// defined(WEBRTC_WIN)
SetPriority(attributes.priority);
thread_function();
}); #ifdefined(WEBRTC_WIN) // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. // Set the reserved stack stack size to 1M, which is the default on Windows // and Linux.
DWORD thread_id = 0; // Mozilla: Set to 256kb for consistency with nsIThreadManager.idl
PlatformThread::Handle handle = ::CreateThread(
nullptr, 256 * 1024, &RunPlatformThread, start_thread_function_ptr,
STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id);
RTC_CHECK(handle) << "CreateThread failed"; #else
pthread_attr_t attr;
pthread_attr_init(&attr); // Set the stack stack size to 1M. // Mozilla: Set to 256kb for consistency with nsIThreadManager.idl
pthread_attr_setstacksize(&attr, 256 * 1024);
pthread_attr_setdetachstate(
&attr, joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED);
PlatformThread::Handle handle;
RTC_CHECK_EQ(0, pthread_create(&handle, &attr, &RunPlatformThread,
start_thread_function_ptr));
pthread_attr_destroy(&attr); #endif// defined(WEBRTC_WIN) return PlatformThread(handle, joinable);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.