+static void
+reset_priority (pthread_attr_t *attr)
+{
+ struct sched_param param;
+ gint res;
+ gint policy;
+
+ memset (¶m, 0, sizeof (param));
+
+ res = pthread_attr_getschedpolicy (attr, &policy);
+ if (res != 0)
+ g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
+
+#ifdef _POSIX_PRIORITY_SCHEDULING
+ gint max, min;
+
+ /* Necessary to get valid priority range */
+
+ min = sched_get_priority_min (policy);
+ max = sched_get_priority_max (policy);
+
+ if (max > 0 && min >= 0 && max > min)
+ param.sched_priority = (max - min) / 2 + min;
+ else
+#endif
+ {
+ switch (policy) {
+ case SCHED_FIFO:
+ case SCHED_RR:
+ param.sched_priority = 50;
+ break;
+#ifdef SCHED_BATCH
+ case SCHED_BATCH:
+#endif
+ case SCHED_OTHER:
+ param.sched_priority = 0;
+ break;
+ default:
+ g_warning ("%s: unknown policy %d", __func__, policy);
+ return;
+ }
+ }
+
+ res = pthread_attr_setschedparam (attr, ¶m);
+ if (res != 0)
+ g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
+}
+