[tracker-miners/sam/sched-idle-fix: 25/25] Use pthreads API to set SCHED_IDLE scheduling policy



commit 460d1d30f22a9427a8c25fa5008b14f8ab2d53c3
Author: Sam Thursfield <sam afuera me uk>
Date:   Thu Jan 9 11:55:34 2020 +0100

    Use pthreads API to set SCHED_IDLE scheduling policy
    
    Previously we were using the GLIBC sched_setscheduler() function.
    This is Linux-only and also doesn't work as documented -- it claims
    to set policy for the whole process, but in fact only affects the
    calling thread.
    
    We now use the pthread_setschedparam() function from pthreads, which
    also only affects the calling thread, and is also portable beyond
    Linux.
    
    See: https://www.openwall.com/lists/musl/2016/03/01/5

 src/libtracker-miners-common/tracker-sched.c | 52 +++++++---------------------
 1 file changed, 13 insertions(+), 39 deletions(-)
---
diff --git a/src/libtracker-miners-common/tracker-sched.c b/src/libtracker-miners-common/tracker-sched.c
index 5c9efb6b5..f0133b72b 100644
--- a/src/libtracker-miners-common/tracker-sched.c
+++ b/src/libtracker-miners-common/tracker-sched.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2011, Nokia <ivan frade nokia com>
+ * Copyright (C) 2020, Sam Thursfield <sam afuera me uk>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -19,59 +20,32 @@
 
 #include "config-miners.h"
 
-#ifdef __linux__
-
-#include <errno.h>
-#include <sched.h>
+#include <pthread.h>
 
 #include "tracker-sched.h"
 
+/* Sets the priority of the current thread to SCHED_IDLE. */
 gboolean
 tracker_sched_idle (void)
 {
        struct sched_param sp;
+       int result, policy;
 
-       /* Set process scheduling parameters:
-        * This is used so we don't steal scheduling priority from
-        * the most important applications - like the phone
-        * application which has a real time requirement here. This
-        * is detailed in Nokia bug #95573
-        */
-       g_message ("Setting scheduler policy to SCHED_IDLE");
+       result = pthread_getschedparam (pthread_self (), &policy, &sp);
 
-       if (sched_getparam (0, &sp) == 0) {
-               if (sched_setscheduler (0, SCHED_IDLE, &sp) != 0) {
-//LCOV_EXCL_START
-                       const gchar *str = g_strerror (errno);
+       if (result == 0) {
+               result = pthread_setschedparam (pthread_self(), SCHED_IDLE, &sp);
+       }
 
-                       g_warning ("Could not set scheduler policy, %s",
-                                  str ? str : "no error given");
+       if (result == 0) {
+               g_message ("Set scheduler policy for current thread to SCHED_IDLE");
 
-                       return FALSE;
-               }
+               return TRUE;
        } else {
-               const gchar *str = g_strerror (errno);
+               const gchar *str = g_strerror (result);
 
-               g_warning ("Could not get scheduler policy, %s",
-                          str ? str : "no error given");
+               g_message ("Error setting scheduler policy: %s", str ? str : "no error given");
 
                return FALSE;
        }
-//LCOV_EXCL_END
-
-       return TRUE;
 }
-
-#else /* __linux__ */
-
-#include <glib.h>
-
-#include "tracker-sched.h"
-
-gboolean
-tracker_sched_idle (void)
-{
-       return TRUE;
-}
-
-#endif /* __linux__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]