Set thread scheduling parameters inside the new thread.

This makes it possible to restrict threads from modifying scheduling
parameters of another thread in the Chrome Linux sandbox.

BUG=
R=henrike@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/28539004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7324 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/system_wrappers/source/thread_posix.cc b/webrtc/system_wrappers/source/thread_posix.cc
index 8a24ae5..f45c6e0 100644
--- a/webrtc/system_wrappers/source/thread_posix.cc
+++ b/webrtc/system_wrappers/source/thread_posix.cc
@@ -178,11 +178,6 @@
   int result = pthread_attr_setdetachstate(&attr_, PTHREAD_CREATE_DETACHED);
   // Set the stack stack size to 1M.
   result |= pthread_attr_setstacksize(&attr_, 1024 * 1024);
-#ifdef WEBRTC_THREAD_RR
-  const int policy = SCHED_RR;
-#else
-  const int policy = SCHED_FIFO;
-#endif
   event_->Reset();
   // If pthread_create was successful, a thread was created and is running.
   // Don't return false if it was successful since if there are any other
@@ -210,26 +205,6 @@
 #if HAS_THREAD_ID
   thread_id = static_cast<unsigned int>(thread_);
 #endif
-  sched_param param;
-
-  const int min_prio = sched_get_priority_min(policy);
-  const int max_prio = sched_get_priority_max(policy);
-
-  if ((min_prio == EINVAL) || (max_prio == EINVAL)) {
-    WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
-                 "unable to retreive min or max priority for threads");
-    return true;
-  }
-  if (max_prio - min_prio <= 2) {
-    // There is no room for setting priorities with any granularity.
-    return true;
-  }
-  param.sched_priority = ConvertToSystemPriority(prio_, min_prio, max_prio);
-  result = pthread_setschedparam(thread_, policy, &param);
-  if (result == EINVAL) {
-    WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
-                 "unable to set thread priority");
-  }
   return true;
 }
 
@@ -326,6 +301,27 @@
     WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
                  "Thread without name started");
   }
+
+#ifdef WEBRTC_THREAD_RR
+  const int policy = SCHED_RR;
+#else
+  const int policy = SCHED_FIFO;
+#endif
+  const int min_prio = sched_get_priority_min(policy);
+  const int max_prio = sched_get_priority_max(policy);
+  if ((min_prio == -1) || (max_prio == -1)) {
+    WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
+                 "unable to retreive min or max priority for threads");
+  }
+  if (max_prio - min_prio > 2) {
+    sched_param param;
+    param.sched_priority = ConvertToSystemPriority(prio_, min_prio, max_prio);
+    if (pthread_setschedparam(pthread_self(), policy, &param) != 0) {
+      WEBRTC_TRACE(
+          kTraceError, kTraceUtility, -1, "unable to set thread priority");
+    }
+  }
+
   bool alive = true;
   bool run = true;
   while (alive) {