Skip to content

Commit 233f381

Browse files
committed
uspace+rtai: fix priority
rtai priority numbers run opposite to pthread priority numbers. After this change, no threads.0 failures for 1000 tests. Before, failure probability was at least 1 in 50 tests. Closes: #2098
1 parent 0b61588 commit 233f381

3 files changed

Lines changed: 48 additions & 23 deletions

File tree

src/rtapi/rtapi_uspace.hh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ struct RtapiApp
7171

7272
RtapiApp(int policy = SCHED_OTHER) : policy(policy), period(0) {}
7373

74-
int prio_highest();
75-
int prio_lowest();
76-
int prio_next_higher(int prio);
77-
int prio_next_lower(int prio);
74+
virtual int prio_highest() const;
75+
virtual int prio_lowest() const;
76+
int prio_higher_delta() const;
77+
int prio_bound(int prio) const;
78+
int prio_next_higher(int prio) const;
79+
int prio_next_lower(int prio) const;
7880
long clock_set_period(long int period_nsec);
7981
int task_new(void (*taskcode)(void*), void *arg,
8082
int prio, int owner, unsigned long int stacksize, int uses_fp);

src/rtapi/uspace_rtai.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ struct RtaiApp : RtapiApp {
165165
void do_delay(long ns) {
166166
rt_sleep(nano2count(ns));
167167
}
168+
169+
int prio_highest() const {
170+
return RT_SCHED_HIGHEST_PRIORITY;
171+
}
172+
173+
int prio_lowest() const {
174+
return RT_SCHED_LOWEST_PRIORITY;
175+
}
168176
};
169177

170178
pthread_once_t RtaiApp::key_once;

src/rtapi/uspace_rtapi_app.cc

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -835,37 +835,52 @@ struct rtapi_task *task_array[MAX_TASKS];
835835

836836
/* Priority functions. Uspace uses POSIX task priorities. */
837837

838-
int RtapiApp::prio_highest()
838+
int RtapiApp::prio_highest() const
839839
{
840840
return sched_get_priority_max(policy);
841841
}
842842

843-
int RtapiApp::prio_lowest()
843+
int RtapiApp::prio_lowest() const
844844
{
845845
return sched_get_priority_min(policy);
846846
}
847847

848-
int RtapiApp::prio_next_higher(int prio)
849-
{
850-
/* return a valid priority for out of range arg */
851-
if (prio >= rtapi_prio_highest())
852-
return rtapi_prio_highest();
853-
if (prio < rtapi_prio_lowest())
854-
return rtapi_prio_lowest();
848+
int RtapiApp::prio_higher_delta() const {
849+
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
850+
return 1;
851+
}
852+
return -1;
853+
}
854+
855+
int RtapiApp::prio_bound(int prio) const {
856+
if(rtapi_prio_highest() > rtapi_prio_lowest()) {
857+
if (prio >= rtapi_prio_highest())
858+
return rtapi_prio_highest();
859+
if (prio < rtapi_prio_lowest())
860+
return rtapi_prio_lowest();
861+
} else {
862+
if (prio <= rtapi_prio_highest())
863+
return rtapi_prio_highest();
864+
if (prio > rtapi_prio_lowest())
865+
return rtapi_prio_lowest();
866+
}
867+
return prio;
868+
}
855869

856-
/* return next higher priority for in-range arg */
857-
return prio + 1;
870+
int RtapiApp::prio_next_higher(int prio) const
871+
{
872+
prio = prio_bound(prio);
873+
if(prio != rtapi_prio_highest())
874+
return prio + prio_higher_delta();
875+
return prio;
858876
}
859877

860-
int RtapiApp::prio_next_lower(int prio)
878+
int RtapiApp::prio_next_lower(int prio) const
861879
{
862-
/* return a valid priority for out of range arg */
863-
if (prio <= rtapi_prio_lowest())
864-
return rtapi_prio_lowest();
865-
if (prio > rtapi_prio_highest())
866-
return rtapi_prio_highest();
867-
/* return next lower priority for in-range arg */
868-
return prio - 1;
880+
prio = prio_bound(prio);
881+
if(prio != rtapi_prio_lowest())
882+
return prio - prio_higher_delta();
883+
return prio;
869884
}
870885

871886
int RtapiApp::allocate_task_id()

0 commit comments

Comments
 (0)