File: linux_sched_rt

package info (click to toggle)
psychtoolbox-3 3.0.9%2Bsvn2579.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 63,408 kB
  • sloc: ansic: 73,310; cpp: 11,139; objc: 3,129; sh: 1,669; python: 382; php: 272; makefile: 172; java: 113
file content (58 lines) | stat: -rw-r--r-- 2,281 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
From: Mario Kleiner <mario.kleiner@tuebingen.mpg.de>
Subject: enforce (when possible) SCHED_RR on Linux systems
Origin: PTB-3



--- a/portaudio/src/os/unix/pa_unix_util.c
+++ b/portaudio/src/os/unix/pa_unix_util.c
@@ -224,9 +224,18 @@ PaError PaUnixThread_Initialize( PaUnixT
 }
 #endif
 
+// MK: Enable Psychtoolbox specific Realtime scheduling setup code for RT scheduling the audio callback thread.
+#define MK_PSYCH_RTSCHED 1
+
 PaError PaUnixThread_New( PaUnixThread* self, void* (*threadFunc)( void* ), void* threadArg, PaTime waitForChild )
 {
-    PaError result = paNoError;
+#ifdef MK_PSYCH_RTSCHED
+	// MK: New code, not in original. Unconditionally try to boost callback threads priority
+	// to RT_FIFO realtime scheduling with given realtime priority. See below for rest.
+    struct sched_param spm = { 0 };
+	int policy;
+#endif 
+	PaError result = paNoError;
     pthread_attr_t attr;
     int started = 0;
 
@@ -260,6 +269,29 @@ PaError PaUnixThread_New( PaUnixThread*
     PA_UNLESS( !pthread_create( &self->thread, &attr, threadFunc, threadArg ), paInternalError );
     started = 1;
 
+#ifdef MK_PSYCH_RTSCHED
+	// MK: New code, not in original. Unconditionally try to boost callback threads priority
+	// to RT_FIFO realtime scheduling with given realtime priority.
+	
+	// Query parent threads policy and priority:
+	pthread_getschedparam(pthread_self(), &policy, &spm);
+	
+	// If not RT policy, assign a base priority of minimum SCHED_FIFO. Otherwise use RT priority of parent as baseline:
+	if ((policy != SCHED_FIFO ) && (policy != SCHED_RR)) spm.sched_priority = sched_get_priority_min( SCHED_FIFO );
+	
+	// Set audio callback threads RT priority to parents baseline + 4, so it gets quite a boost wrt. its parent.
+	// Clamp to allowable maximum though:
+    spm.sched_priority = PA_MIN( spm.sched_priority + 4 , sched_get_priority_max( SCHED_FIFO ) );
+	
+	// Try to switch callback thread to SCHED_FIFO Realtime scheduling with proper realtime priority,
+	// but don't be pissed if it doesn't work. No all too big deal:
+    if( pthread_setschedparam(self->thread, SCHED_FIFO, &spm) != 0 )
+    {
+        PA_DEBUG(( "WARNING: Failed bumping audio callback thread to realtime priority!\n" ));
+    }
+
+#endif
+
 #if 0
     if( th->rtSched )
     {