Skip to content

Commit 26cf7d7

Browse files
committed
Centralized Linux scheduler settings (beaglebone.h) and uniform implementation.
1 parent bd96db4 commit 26cf7d7

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed

analog.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,14 @@ int analog_init( void)
195195
pd->average.remainder = 0;
196196
++num_analog_channels;
197197
}
198-
return mendel_thread_create( "analog", &worker, NULL, &analog_worker, NULL);
198+
if (mendel_thread_create( "analog", &worker, NULL, &analog_worker, NULL) != 0) {
199+
return -1;
200+
}
201+
struct sched_param param = {
202+
.sched_priority = ANALOG_PRIO
203+
};
204+
pthread_setschedparam( worker, ANALOG_SCHED, &param);
205+
return 0;
199206
}
200207
fprintf( stderr, "analog_init: no configuration data!\n");
201208
return -1;

beaglebone.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22
#define _BEAGLEBONE_H
33

44
#include <stdint.h>
5+
#include <sched.h>
6+
7+
/*
8+
* Posix defines priorities from 0 upto 99.
9+
* 0 (lowest) is for SCHED_OTHER, the default scheduling algorithm
10+
* 1 (low) is for SCHED_RR and SCHED_FIFO
11+
* 99 (high) is for SCHED_RR and SCHED_FIFO
12+
*
13+
* The Linux kernel uses other values: Posix 0 gives Linux 120,
14+
* Posix priority 1 gives Linux priority 98, Posix priority 99 gives Linux priority 0.
15+
*/
16+
17+
#define TOP_PRIO 60
18+
#define ELEV_PRIO 55
19+
#define NORM_PRIO 50
20+
21+
#define COMM_PRIO TOP_PRIO /* keep connection allive at all times */
22+
#define COMM_SCHED SCHED_FIFO
23+
24+
#define LIMSW_PRIO TOP_PRIO
25+
#define LIMSW_SCHED SCHED_FIFO
26+
27+
#define MENDEL_PRIO NORM_PRIO
28+
#define MENDEL_SCHED SCHED_RR
29+
30+
#define HEATER_PRIO NORM_PRIO
31+
#define HEATER_SCHED SCHED_RR
32+
33+
#define ANALOG_PRIO ELEV_PRIO
34+
#define ANALOG_SCHED SCHED_RR
35+
536

637
#define NR_ITEMS( x) (sizeof( (x)) / sizeof( *(x)))
738

comm.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,13 @@ int comm_init( void)
165165
alt_stdin = fds[ 1];
166166
fprintf( stderr, "alt_stdin = %d\n", alt_stdin);
167167

168-
result = mendel_thread_create( "comm", &worker, NULL, &comm_thread, NULL);
169-
if (result != 0) {
170-
exit( EXIT_FAILURE);
168+
if (mendel_thread_create( "comm", &worker, NULL, &comm_thread, NULL) != 0) {
169+
return -1;
171170
}
172171
struct sched_param param = {
173-
.sched_priority = 74
172+
.sched_priority = COMM_PRIO
174173
};
175-
// SCHED_RR seems to work for now, otherwise use SCHED_FIFO
176-
pthread_setschedparam( worker, SCHED_RR, &param);
174+
pthread_setschedparam( worker, COMM_SCHED, &param);
177175

178176
return 0;
179177
}

heater.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,14 @@ int heater_init( void)
203203
++num_heater_channels;
204204
}
205205
// Start worker thread
206-
return mendel_thread_create( "heater", &worker, NULL, &heater_thread, NULL);
206+
if (mendel_thread_create( "heater", &worker, NULL, &heater_thread, NULL) != 0) {
207+
return -1;
208+
}
209+
struct sched_param param = {
210+
.sched_priority = HEATER_PRIO
211+
};
212+
pthread_setschedparam( worker, HEATER_SCHED, &param);
213+
return 0;
207214
}
208215
fprintf( stderr, "temp_init: no configuration data!\n");
209216
return -1;

limit_switches.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,15 @@ static pthread_t worker;
159159

160160
int limsw_init( void)
161161
{
162-
int result;
163-
164162
fdset = calloc( nr_limits, sizeof( struct pollfd));
165163

166-
result = mendel_thread_create( "limit_switches", &worker, NULL, &limsw_watcher, NULL);
167-
if (result != 0) {
168-
exit( EXIT_FAILURE);
164+
if (mendel_thread_create( "limit_switches", &worker, NULL, &limsw_watcher, NULL) != 0) {
165+
return -1;
169166
}
170167
struct sched_param param = {
171-
.sched_priority = 74
168+
.sched_priority = LIMSW_PRIO
172169
};
173-
// pthread_setschedparam( worker, SCHED_FIFO, &param);
174-
pthread_setschedparam( worker, SCHED_RR, &param);
170+
pthread_setschedparam( worker, LIMSW_SCHED, &param);
175171

176172
return 0;
177173
}

mendel.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <stdlib.h>
77
#include <unistd.h>
88
#include <time.h>
9-
#include <sched.h>
109
#include <pthread.h>
1110
#include <signal.h>
1211

@@ -29,18 +28,18 @@ static int arm_init( void)
2928
exit( EXIT_FAILURE);
3029
}
3130

32-
int policy = SCHED_OTHER;
31+
int policy = MENDEL_SCHED;
3332
int prio_max = sched_get_priority_max( policy);
3433
int prio_min = sched_get_priority_min( policy);
3534

3635
struct sched_param sp = {
37-
.sched_priority = (prio_min + prio_max) / 2
36+
.sched_priority = MENDEL_PRIO
3837
};
3938
// Set realtime process scheduling using the Round Robin scheduler
4039
// with absolute priority halfway between min and max.
4140
if (sched_setscheduler( 0, policy, &sp) < 0) {
4241
}
43-
fprintf( stderr, "Scheduler set to %d, priority to %d\n", policy, sp.sched_priority);
42+
fprintf( stderr, "Scheduler set to %d, priority to %d (min:%d,max:%d)\n", policy, sp.sched_priority, prio_min, prio_max);
4443

4544
struct timespec clock_resolution;
4645
clock_getres( CLOCK_MONOTONIC, &clock_resolution);

0 commit comments

Comments
 (0)