mirror of
https://github.com/open-simh/simh.git
synced 2026-01-25 19:57:36 +00:00
VIDEO: Added priority boost to thread performing SDL processing and updates
Reworked all priority adjustment code to leverage a new sim_os_set_thread_priority API which is coded to use pthreads or OS priority adjustment APIs as necessary.
This commit is contained in:
94
sim_timer.c
94
sim_timer.c
@@ -164,6 +164,59 @@ return sim_os_msec() - start_time;
|
||||
#define SIM_IDLE_MS_SLEEP sim_os_ms_sleep
|
||||
#endif
|
||||
|
||||
/* Mark the need for the sim_os_set_thread_priority routine, */
|
||||
/* allowing the feature and/or platform dependent code to provide it */
|
||||
#define NEED_THREAD_PRIORITY
|
||||
|
||||
/* If we've got pthreads support then use pthreads mechanisms */
|
||||
#if defined(USE_READER_THREAD)
|
||||
|
||||
#undef NEED_THREAD_PRIORITY
|
||||
|
||||
#if defined(_WIN32)
|
||||
/* On Windows there are several potentially disjoint threading APIs */
|
||||
/* in use (base win32 pthreads, libSDL provided threading, and direct */
|
||||
/* calls to beginthreadex), so go directly to the Win32 threading APIs */
|
||||
/* to manage thread priority */
|
||||
t_stat sim_os_set_thread_priority (int below_normal_above)
|
||||
{
|
||||
const static int val[3] = {THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL};
|
||||
|
||||
if ((below_normal_above < -1) || (below_normal_above > 1))
|
||||
return SCPE_ARG;
|
||||
SetThreadPriority (GetCurrentThread(), val[1 + below_normal_above]);
|
||||
return SCPE_OK;
|
||||
}
|
||||
#else
|
||||
/* Native pthreads priority implementation */
|
||||
t_stat sim_os_set_thread_priority (int below_normal_above)
|
||||
{
|
||||
int sched_policy, min_prio, max_prio;
|
||||
struct sched_param sched_priority;
|
||||
|
||||
if ((below_normal_above < -1) || (below_normal_above > 1))
|
||||
return SCPE_ARG;
|
||||
|
||||
pthread_getschedparam (pthread_self(), &sched_policy, &sched_priority);
|
||||
min_prio = sched_get_priority_min(sched_policy);
|
||||
max_prio = sched_get_priority_max(sched_policy);
|
||||
switch (below_normal_above) {
|
||||
case PRIORITY_BELOW_NORMAL:
|
||||
sched_priority.sched_priority = min_prio;
|
||||
break;
|
||||
case PRIORITY_NORMAL:
|
||||
sched_priority.sched_priority = (max_prio + min_prio) / 2;
|
||||
break;
|
||||
case PRIORITY_ABOVE_NORMAL:
|
||||
sched_priority.sched_priority = max_prio;
|
||||
break;
|
||||
}
|
||||
pthread_setschedparam (pthread_self(), sched_policy, &sched_priority);
|
||||
return SCPE_OK;
|
||||
}
|
||||
#endif
|
||||
#endif /* defined(USE_READER_THREAD) */
|
||||
|
||||
/* OS-dependent timer and clock routines */
|
||||
|
||||
/* VMS */
|
||||
@@ -261,8 +314,6 @@ return 0;
|
||||
|
||||
/* Win32 routines */
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
const t_bool rtc_avail = TRUE;
|
||||
|
||||
uint32 sim_os_msec (void)
|
||||
@@ -494,6 +545,45 @@ treq.tv_nsec = (milliseconds % MILLIS_PER_SEC) * NANOS_PER_MILLI;
|
||||
return sim_os_msec () - stime;
|
||||
}
|
||||
|
||||
#if defined(NEED_THREAD_PRIORITY)
|
||||
#undef NEED_THREAD_PRIORITY
|
||||
#include <sys/time.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
t_stat sim_os_set_thread_priority (int below_normal_above)
|
||||
{
|
||||
if ((below_normal_above < -1) || (below_normal_above > 1))
|
||||
return SCPE_ARG;
|
||||
|
||||
errno = 0;
|
||||
switch (below_normal_above) {
|
||||
case PRIORITY_BELOW_NORMAL:
|
||||
if ((getpriority (PRIO_PROCESS, 0) <= 0) && /* at or above normal pri? */
|
||||
(errno == 0))
|
||||
setpriority (PRIO_PROCESS, 0, 10);
|
||||
break;
|
||||
case PRIORITY_NORMAL:
|
||||
if (getpriority (PRIO_PROCESS, 0) != 0) /* at or above normal pri? */
|
||||
setpriority (PRIO_PROCESS, 0, 0);
|
||||
break;
|
||||
case PRIORITY_ABOVE_NORMAL:
|
||||
if ((getpriority (PRIO_PROCESS, 0) <= 0) && /* at or above normal pri? */
|
||||
(errno == 0))
|
||||
setpriority (PRIO_PROCESS, 0, -10);
|
||||
break;
|
||||
}
|
||||
return SCPE_OK;
|
||||
}
|
||||
#endif /* defined(NEED_THREAD_PRIORITY) */
|
||||
|
||||
#endif
|
||||
|
||||
/* If one hasn't been provided yet, then just stub it */
|
||||
#if defined(NEED_THREAD_PRIORITY)
|
||||
t_stat sim_os_set_thread_priority (int below_normal_above)
|
||||
{
|
||||
return SCPE_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* diff = min - sub */
|
||||
|
||||
Reference in New Issue
Block a user