mirror of
https://github.com/kanjitalk755/macemu.git
synced 2025-02-03 22:37:56 +00:00
use linked list for timers to avoid running out of descriptors
This commit is contained in:
parent
812db6ed4f
commit
efad3ba70a
@ -53,11 +53,10 @@ enum { // TMTask struct
|
|||||||
struct TMDesc {
|
struct TMDesc {
|
||||||
uint32 task; // Mac address of associated TMTask
|
uint32 task; // Mac address of associated TMTask
|
||||||
tm_time_t wakeup; // Time this task is scheduled for execution
|
tm_time_t wakeup; // Time this task is scheduled for execution
|
||||||
bool in_use; // Flag: descriptor in use
|
TMDesc *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
const int NUM_DESCS = 64; // Maximum number of descriptors
|
static TMDesc *tmDescList;
|
||||||
static TMDesc desc[NUM_DESCS];
|
|
||||||
|
|
||||||
#if PRECISE_TIMING
|
#if PRECISE_TIMING
|
||||||
#ifdef PRECISE_TIMING_BEOS
|
#ifdef PRECISE_TIMING_BEOS
|
||||||
@ -89,43 +88,35 @@ static void *timer_func(void *arg);
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
inline static void free_desc(TMDesc *desc)
|
||||||
* Allocate descriptor for given TMTask in list
|
|
||||||
*/
|
|
||||||
|
|
||||||
static int alloc_desc(uint32 tm)
|
|
||||||
{
|
{
|
||||||
// Search for first free descriptor
|
if (desc == tmDescList) {
|
||||||
for (int i=0; i<NUM_DESCS; i++)
|
tmDescList = desc->next;
|
||||||
if (!desc[i].in_use) {
|
} else {
|
||||||
desc[i].task = tm;
|
for (TMDesc *d = tmDescList; d; d = d->next) {
|
||||||
desc[i].in_use = true;
|
if (d->next == desc) {
|
||||||
return i;
|
d->next = desc->next;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return -1;
|
}
|
||||||
|
}
|
||||||
|
delete desc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Free descriptor in list
|
|
||||||
*/
|
|
||||||
|
|
||||||
inline static void free_desc(int i)
|
|
||||||
{
|
|
||||||
desc[i].in_use = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find descriptor associated with given TMTask
|
* Find descriptor associated with given TMTask
|
||||||
*/
|
*/
|
||||||
|
|
||||||
inline static int find_desc(uint32 tm)
|
inline static TMDesc *find_desc(uint32 tm)
|
||||||
{
|
{
|
||||||
for (int i=0; i<NUM_DESCS; i++)
|
TMDesc *desc = tmDescList;
|
||||||
if (desc[i].in_use && desc[i].task == tm)
|
while (desc) {
|
||||||
return i;
|
if (desc->task == tm) {
|
||||||
return -1;
|
return desc;
|
||||||
|
}
|
||||||
|
desc = desc->next;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -271,9 +262,7 @@ static void timer_thread_resume(void)
|
|||||||
|
|
||||||
void TimerInit(void)
|
void TimerInit(void)
|
||||||
{
|
{
|
||||||
// Mark all descriptors as inactive
|
TimerReset();
|
||||||
for (int i=0; i<NUM_DESCS; i++)
|
|
||||||
free_desc(i);
|
|
||||||
|
|
||||||
#if PRECISE_TIMING
|
#if PRECISE_TIMING
|
||||||
// Start timer thread
|
// Start timer thread
|
||||||
@ -331,9 +320,13 @@ void TimerExit(void)
|
|||||||
|
|
||||||
void TimerReset(void)
|
void TimerReset(void)
|
||||||
{
|
{
|
||||||
// Mark all descriptors as inactive
|
TMDesc *desc = tmDescList;
|
||||||
for (int i=0; i<NUM_DESCS; i++)
|
while (desc) {
|
||||||
free_desc(i);
|
TMDesc *next = desc->next;
|
||||||
|
delete desc;
|
||||||
|
desc = desc->next;
|
||||||
|
}
|
||||||
|
tmDescList = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -345,12 +338,13 @@ int16 InsTime(uint32 tm, uint16 trap)
|
|||||||
{
|
{
|
||||||
D(bug("InsTime %08lx, trap %04x\n", tm, trap));
|
D(bug("InsTime %08lx, trap %04x\n", tm, trap));
|
||||||
WriteMacInt16((uint32)tm + qType, ReadMacInt16((uint32)tm + qType) & 0x1fff | (trap << 4) & 0x6000);
|
WriteMacInt16((uint32)tm + qType, ReadMacInt16((uint32)tm + qType) & 0x1fff | (trap << 4) & 0x6000);
|
||||||
if (find_desc(tm) >= 0)
|
if (find_desc(tm))
|
||||||
printf("WARNING: InsTime(): Task re-inserted\n");
|
printf("WARNING: InsTime(%08lx): Task re-inserted\n", tm);
|
||||||
else {
|
else {
|
||||||
int i = alloc_desc(tm);
|
TMDesc *desc = new TMDesc;
|
||||||
if (i < 0)
|
desc->task = tm;
|
||||||
printf("FATAL: InsTime(): No free Time Manager descriptor\n");
|
desc->next = tmDescList;
|
||||||
|
tmDescList = desc;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -365,8 +359,8 @@ int16 RmvTime(uint32 tm)
|
|||||||
D(bug("RmvTime %08lx\n", tm));
|
D(bug("RmvTime %08lx\n", tm));
|
||||||
|
|
||||||
// Find descriptor
|
// Find descriptor
|
||||||
int i = find_desc(tm);
|
TMDesc *desc = find_desc(tm);
|
||||||
if (i < 0) {
|
if (!desc) {
|
||||||
printf("WARNING: RmvTime(%08lx): Descriptor not found\n", tm);
|
printf("WARNING: RmvTime(%08lx): Descriptor not found\n", tm);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -392,17 +386,16 @@ int16 RmvTime(uint32 tm)
|
|||||||
#if PRECISE_TIMING
|
#if PRECISE_TIMING
|
||||||
// Look for next task to be called and set wakeup_time
|
// Look for next task to be called and set wakeup_time
|
||||||
wakeup_time = wakeup_time_max;
|
wakeup_time = wakeup_time_max;
|
||||||
for (int j=0; j<NUM_DESCS; j++) {
|
for (TMDesc *d = tmDescList; d; d = d->next)
|
||||||
if (desc[j].in_use && (ReadMacInt16(desc[j].task + qType) & 0x8000))
|
if ((ReadMacInt16(d->task + qType) & 0x8000))
|
||||||
if (timer_cmp_time(desc[j].wakeup, wakeup_time) < 0)
|
if (timer_cmp_time(d->wakeup, wakeup_time) < 0)
|
||||||
wakeup_time = desc[j].wakeup;
|
wakeup_time = d->wakeup;
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Compute remaining time
|
// Compute remaining time
|
||||||
tm_time_t remaining, current;
|
tm_time_t remaining, current;
|
||||||
timer_current_time(current);
|
timer_current_time(current);
|
||||||
timer_sub_time(remaining, desc[i].wakeup, current);
|
timer_sub_time(remaining, desc->wakeup, current);
|
||||||
WriteMacInt32(tm + tmCount, timer_host2mac_time(remaining));
|
WriteMacInt32(tm + tmCount, timer_host2mac_time(remaining));
|
||||||
} else
|
} else
|
||||||
WriteMacInt32(tm + tmCount, 0);
|
WriteMacInt32(tm + tmCount, 0);
|
||||||
@ -427,7 +420,7 @@ int16 RmvTime(uint32 tm)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Free descriptor
|
// Free descriptor
|
||||||
free_desc(i);
|
free_desc(desc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -441,9 +434,9 @@ int16 PrimeTime(uint32 tm, int32 time)
|
|||||||
D(bug("PrimeTime %08lx, time %ld\n", tm, time));
|
D(bug("PrimeTime %08lx, time %ld\n", tm, time));
|
||||||
|
|
||||||
// Find descriptor
|
// Find descriptor
|
||||||
int i = find_desc(tm);
|
TMDesc *desc = find_desc(tm);
|
||||||
if (i < 0) {
|
if (!desc) {
|
||||||
printf("FATAL: PrimeTime(): Descriptor not found\n");
|
printf("FATAL: PrimeTime(%08lx): Descriptor not found\n", tm);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -467,15 +460,15 @@ int16 PrimeTime(uint32 tm, int32 time)
|
|||||||
|
|
||||||
// Yes, calculate wakeup time relative to last scheduled time
|
// Yes, calculate wakeup time relative to last scheduled time
|
||||||
tm_time_t wakeup;
|
tm_time_t wakeup;
|
||||||
timer_add_time(wakeup, desc[i].wakeup, delay);
|
timer_add_time(wakeup, desc->wakeup, delay);
|
||||||
desc[i].wakeup = wakeup;
|
desc->wakeup = wakeup;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// No, calculate wakeup time relative to current time
|
// No, calculate wakeup time relative to current time
|
||||||
tm_time_t now;
|
tm_time_t now;
|
||||||
timer_current_time(now);
|
timer_current_time(now);
|
||||||
timer_add_time(desc[i].wakeup, now, delay);
|
timer_add_time(desc->wakeup, now, delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set tmWakeUp to indicate that task was scheduled
|
// Set tmWakeUp to indicate that task was scheduled
|
||||||
@ -486,7 +479,7 @@ int16 PrimeTime(uint32 tm, int32 time)
|
|||||||
// Not extended task, calculate wakeup time relative to current time
|
// Not extended task, calculate wakeup time relative to current time
|
||||||
tm_time_t now;
|
tm_time_t now;
|
||||||
timer_current_time(now);
|
timer_current_time(now);
|
||||||
timer_add_time(desc[i].wakeup, now, delay);
|
timer_add_time(desc->wakeup, now, delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make task active and enqueue it in the Time Manager queue
|
// Make task active and enqueue it in the Time Manager queue
|
||||||
@ -507,11 +500,10 @@ int16 PrimeTime(uint32 tm, int32 time)
|
|||||||
#if PRECISE_TIMING
|
#if PRECISE_TIMING
|
||||||
// Look for next task to be called and set wakeup_time
|
// Look for next task to be called and set wakeup_time
|
||||||
wakeup_time = wakeup_time_max;
|
wakeup_time = wakeup_time_max;
|
||||||
for (int j=0; j<NUM_DESCS; j++) {
|
for (TMDesc *d = tmDescList; d; d = d->next)
|
||||||
if (desc[j].in_use && (ReadMacInt16(desc[j].task + qType) & 0x8000))
|
if ((ReadMacInt16(d->task + qType) & 0x8000))
|
||||||
if (timer_cmp_time(desc[j].wakeup, wakeup_time) < 0)
|
if (timer_cmp_time(d->wakeup, wakeup_time) < 0)
|
||||||
wakeup_time = desc[j].wakeup;
|
wakeup_time = d->wakeup;
|
||||||
}
|
|
||||||
#ifdef PRECISE_TIMING_BEOS
|
#ifdef PRECISE_TIMING_BEOS
|
||||||
release_sem(wakeup_time_sem);
|
release_sem(wakeup_time_sem);
|
||||||
thread_info info;
|
thread_info info;
|
||||||
@ -619,10 +611,11 @@ void TimerInterrupt(void)
|
|||||||
// Look for active TMTasks that have expired
|
// Look for active TMTasks that have expired
|
||||||
tm_time_t now;
|
tm_time_t now;
|
||||||
timer_current_time(now);
|
timer_current_time(now);
|
||||||
for (int i=0; i<NUM_DESCS; i++)
|
TMDesc *desc = tmDescList;
|
||||||
if (desc[i].in_use) {
|
while (desc) {
|
||||||
uint32 tm = desc[i].task;
|
TMDesc *next = desc->next;
|
||||||
if ((ReadMacInt16(tm + qType) & 0x8000) && timer_cmp_time(desc[i].wakeup, now) <= 0) {
|
uint32 tm = desc->task;
|
||||||
|
if ((ReadMacInt16(tm + qType) & 0x8000) && timer_cmp_time(desc->wakeup, now) <= 0) {
|
||||||
|
|
||||||
// Found one, mark as inactive and remove it from the Time Manager queue
|
// Found one, mark as inactive and remove it from the Time Manager queue
|
||||||
WriteMacInt16(tm + qType, ReadMacInt16(tm + qType) & 0x7fff);
|
WriteMacInt16(tm + qType, ReadMacInt16(tm + qType) & 0x7fff);
|
||||||
@ -639,6 +632,7 @@ void TimerInterrupt(void)
|
|||||||
D(bug(" returned from TimeTask\n"));
|
D(bug(" returned from TimeTask\n"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
desc = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PRECISE_TIMING
|
#if PRECISE_TIMING
|
||||||
@ -656,11 +650,10 @@ void TimerInterrupt(void)
|
|||||||
pthread_mutex_lock(&wakeup_time_lock);
|
pthread_mutex_lock(&wakeup_time_lock);
|
||||||
#endif
|
#endif
|
||||||
wakeup_time = wakeup_time_max;
|
wakeup_time = wakeup_time_max;
|
||||||
for (int j=0; j<NUM_DESCS; j++) {
|
for (TMDesc *d = tmDescList; d; d = d->next)
|
||||||
if (desc[j].in_use && (ReadMacInt16(desc[j].task + qType) & 0x8000))
|
if ((ReadMacInt16(d->task + qType) & 0x8000))
|
||||||
if (timer_cmp_time(desc[j].wakeup, wakeup_time) < 0)
|
if (timer_cmp_time(d->wakeup, wakeup_time) < 0)
|
||||||
wakeup_time = desc[j].wakeup;
|
wakeup_time = d->wakeup;
|
||||||
}
|
|
||||||
#if PRECISE_TIMING_BEOS
|
#if PRECISE_TIMING_BEOS
|
||||||
release_sem(wakeup_time_sem);
|
release_sem(wakeup_time_sem);
|
||||||
thread_info info;
|
thread_info info;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user