1 |
/* |
---|
2 |
* Copyright (C) 2005-2008 by Pieter Palmers |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB |
---|
8 |
* |
---|
9 |
* This program is free software: you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation, either version 2 of the License, or |
---|
12 |
* (at your option) version 3 of the License. |
---|
13 |
* |
---|
14 |
* This program is distributed in the hope that it will be useful, |
---|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 |
* GNU General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU General Public License |
---|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 |
* |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
#include "realtimetools.h" |
---|
25 |
|
---|
26 |
#include "debugmodule/debugmodule.h" |
---|
27 |
|
---|
28 |
#include <stdio.h> |
---|
29 |
#include <stdlib.h> |
---|
30 |
#include <string.h> |
---|
31 |
#include <sched.h> |
---|
32 |
|
---|
33 |
// needed for clock_nanosleep |
---|
34 |
#ifndef _GNU_SOURCE |
---|
35 |
#define _GNU_SOURCE |
---|
36 |
#endif |
---|
37 |
|
---|
38 |
#include <time.h> |
---|
39 |
|
---|
40 |
DECLARE_GLOBAL_DEBUG_MODULE; |
---|
41 |
|
---|
42 |
int set_realtime_priority(unsigned int prio) |
---|
43 |
{ |
---|
44 |
debugOutput(DEBUG_LEVEL_NORMAL, "Setting thread prio to %u\n", prio); |
---|
45 |
if (prio > 0) { |
---|
46 |
struct sched_param schp; |
---|
47 |
/* |
---|
48 |
* set the process to realtime privs |
---|
49 |
*/ |
---|
50 |
memset(&schp, 0, sizeof(schp)); |
---|
51 |
schp.sched_priority = prio; |
---|
52 |
|
---|
53 |
if (sched_setscheduler(0, SCHED_FIFO, &schp) != 0) { |
---|
54 |
perror("sched_setscheduler"); |
---|
55 |
return -1; |
---|
56 |
} |
---|
57 |
} else { |
---|
58 |
struct sched_param schp; |
---|
59 |
/* |
---|
60 |
* set the process to realtime privs |
---|
61 |
*/ |
---|
62 |
memset(&schp, 0, sizeof(schp)); |
---|
63 |
schp.sched_priority = 0; |
---|
64 |
|
---|
65 |
if (sched_setscheduler(0, SCHED_OTHER, &schp) != 0) { |
---|
66 |
perror("sched_setscheduler"); |
---|
67 |
return -1; |
---|
68 |
} |
---|
69 |
} |
---|
70 |
return 0; |
---|
71 |
} |
---|
72 |
|
---|
73 |
void |
---|
74 |
rt_sleep_relative_usecs(uint64_t usecs) { |
---|
75 |
//usleep(usecs); |
---|
76 |
struct timespec ts; |
---|
77 |
ts.tv_sec = usecs / (1000000LL); |
---|
78 |
ts.tv_nsec = (usecs % (1000000LL)) * 1000LL; |
---|
79 |
clock_nanosleep(CLOCK_REALTIME, 0, &ts, NULL); |
---|
80 |
} |
---|
81 |
|
---|
82 |
void |
---|
83 |
rt_sleep_absolute_usecs(uint64_t wake_at_usec) { |
---|
84 |
struct timespec ts; |
---|
85 |
ts.tv_sec = wake_at_usec / (1000000LL); |
---|
86 |
ts.tv_nsec = (wake_at_usec % (1000000LL)) * 1000LL; |
---|
87 |
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL); |
---|
88 |
} |
---|
89 |
|
---|
90 |
uint64_t rt_gettime_usecs() { |
---|
91 |
struct timespec ts; |
---|
92 |
clock_gettime(CLOCK_REALTIME, &ts); |
---|
93 |
return (uint64_t)(ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL); |
---|
94 |
} |
---|
95 |
|
---|