gboolean arv_make_thread_realtime (int priority) { struct sched_param p; memset(&p, 0, sizeof(p)); p.sched_priority = priority; if (sched_setscheduler(_gettid (), SCHED_RR|SCHED_RESET_ON_FORK, &p) < 0 && errno == EPERM) { struct rlimit rlim; GDBusConnection *bus; GError *error = NULL; memset(&rlim, 0, sizeof(rlim)); rlim.rlim_cur = rlim.rlim_max = 100000000ULL; /* 100ms */ if ((setrlimit(RLIMIT_RTTIME, &rlim) < 0)) { arv_warning_misc ("Failed to set RLIMIT_RTTIME: %s", strerror (errno)); return FALSE; } bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error != NULL) { arv_warning_misc ("Failed to connect to system bus: %s", error->message); g_error_free (error); return FALSE; } arv_rtkit_make_realtime(bus, _gettid (), p.sched_priority, &error); g_object_unref (bus); if (error != NULL) { arv_warning_misc ("Failed to connect make realtime: %s", error->message); g_error_free (error); return FALSE; } arv_debug_misc ("Thread became realtime with priority %d", priority); return TRUE; } return TRUE; }
int main(int argc, char *argv[]) { GDBusConnection *bus; GError *error = NULL; int max_realtime_priority, min_nice_level; long long rttime_usec_max; struct rlimit rlim; bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (!G_IS_DBUS_CONNECTION (bus)) { fprintf (stderr, "Failed to connect to system bus: %s\n", error->message); g_error_free (error); return EXIT_FAILURE; } max_realtime_priority = arv_rtkit_get_max_realtime_priority (bus, &error); if (error != NULL) { fprintf (stderr, "Failed to get MaxRealtimePriority: %s\n", error->message); g_error_free (error); error = NULL; } else printf ("MaxRealtimePriority = %d\n", max_realtime_priority); min_nice_level = arv_rtkit_get_min_nice_level (bus, &error); if (error != NULL) { fprintf (stderr, "Failed to get MinNiceLevel: %s\n", error->message); g_error_free (error); error = NULL; } else printf ("MinNiceLevel = %d\n", min_nice_level); rttime_usec_max = arv_rtkit_get_rttime_usec_max (bus, &error); if (error != NULL) { fprintf (stderr, "Failed to get RTTimeUSecMax: %s\n", error->message); g_error_free (error); error = NULL; } else printf ("RTTimeUSecMax = %Ld\n", rttime_usec_max); memset(&rlim, 0, sizeof(rlim)); rlim.rlim_cur = rlim.rlim_max = 100000000ULL; /* 100ms */ if ((setrlimit(RLIMIT_RTTIME, &rlim) < 0)) fprintf(stderr, "Failed to set RLIMIT_RTTIME: %s\n", strerror(errno)); print_status("before"); arv_rtkit_make_high_priority (bus, 0, -10, &error); if (error != NULL) { fprintf (stderr, "Failed to become high priority: %s\n", error->message); g_error_free (error); error = NULL; } else printf ("Successfully became high priority\n"); print_status("after high priority"); arv_rtkit_make_realtime (bus, 0, 10, &error); if (error != NULL) { fprintf (stderr, "Failed to get become realtime: %s\n", error->message); g_error_free (error); error = NULL; } else printf ("Successfully became realtime\n"); print_status("after realtime"); g_object_unref (bus); return EXIT_SUCCESS; }