From ed932acb5860bf8b9296169676499562a55d139e Mon Sep 17 00:00:00 2001 From: Miller Puckette Date: Mon, 6 Sep 2004 20:20:36 +0000 Subject: checking in version 0.38test5. Oops, I realize I forgot some more nice files, will add them and re-commit. svn path=/trunk/; revision=2010 --- pd/src/m_sched.c | 350 +++++++++++++++++++++++++++---------------------------- 1 file changed, 175 insertions(+), 175 deletions(-) (limited to 'pd/src/m_sched.c') diff --git a/pd/src/m_sched.c b/pd/src/m_sched.c index b431848d..b0854984 100644 --- a/pd/src/m_sched.c +++ b/pd/src/m_sched.c @@ -19,7 +19,7 @@ static int sys_quit; -static double sys_time; +double sys_time; static double sys_time_per_msec = TIMEUNITPERSEC / 1000.; int sys_schedblocksize = DEFDACBLKSIZE; @@ -38,7 +38,7 @@ struct _clock t_clock *clock_setlist; -#ifdef UNIX +#ifdef UNISTD #include #endif @@ -56,14 +56,14 @@ void clock_unset(t_clock *x) { if (x->c_settime >= 0) { - if (x == clock_setlist) clock_setlist = x->c_next; - else - { - t_clock *x2 = clock_setlist; - while (x2->c_next != x) x2 = x2->c_next; - x2->c_next = x->c_next; - } - x->c_settime = -1; + if (x == clock_setlist) clock_setlist = x->c_next; + else + { + t_clock *x2 = clock_setlist; + while (x2->c_next != x) x2 = x2->c_next; + x2->c_next = x->c_next; + } + x->c_settime = -1; } } @@ -75,17 +75,17 @@ void clock_set(t_clock *x, double setticks) x->c_settime = setticks; if (clock_setlist && clock_setlist->c_settime <= setticks) { - t_clock *cbefore, *cafter; - for (cbefore = clock_setlist, cafter = clock_setlist->c_next; - cbefore; cbefore = cafter, cafter = cbefore->c_next) - { - if (!cafter || cafter->c_settime > setticks) - { - cbefore->c_next = x; - x->c_next = cafter; - return; - } - } + t_clock *cbefore, *cafter; + for (cbefore = clock_setlist, cafter = clock_setlist->c_next; + cbefore; cbefore = cafter, cafter = cbefore->c_next) + { + if (!cafter || cafter->c_settime > setticks) + { + cbefore->c_next = x; + x->c_next = cafter; + return; + } + } } else x->c_next = clock_setlist, clock_setlist = x; } @@ -135,11 +135,11 @@ static int sys_histogram[NHIST][NBIN]; static double sys_histtime; static int sched_diddsp, sched_didpoll, sched_didnothing; -static void sys_clearhist( void) +void sys_clearhist( void) { unsigned int i, j; for (i = 0; i < NHIST; i++) - for (j = 0; j < NBIN; j++) sys_histogram[i][j] = 0; + for (j = 0; j < NBIN; j++) sys_histogram[i][j] = 0; sys_histtime = sys_getrealtime(); sched_diddsp = sched_didpoll = sched_didnothing = 0; } @@ -149,23 +149,23 @@ void sys_printhist( void) unsigned int i, j; for (i = 0; i < NHIST; i++) { - int doit = 0; - for (j = 0; j < NBIN; j++) if (sys_histogram[i][j]) doit = 1; - if (doit) - { - post("%2d %8d %8d %8d %8d %8d %8d %8d %8d", i, - sys_histogram[i][0], - sys_histogram[i][1], - sys_histogram[i][2], - sys_histogram[i][3], - sys_histogram[i][4], - sys_histogram[i][5], - sys_histogram[i][6], - sys_histogram[i][7]); - } + int doit = 0; + for (j = 0; j < NBIN; j++) if (sys_histogram[i][j]) doit = 1; + if (doit) + { + post("%2d %8d %8d %8d %8d %8d %8d %8d %8d", i, + sys_histogram[i][0], + sys_histogram[i][1], + sys_histogram[i][2], + sys_histogram[i][3], + sys_histogram[i][4], + sys_histogram[i][5], + sys_histogram[i][6], + sys_histogram[i][7]); + } } post("dsp %d, pollgui %d, nothing %d", - sched_diddsp, sched_didpoll, sched_didnothing); + sched_diddsp, sched_didpoll, sched_didnothing); } static int sys_histphase; @@ -177,11 +177,11 @@ int sys_addhist(int phase) int msec = (newtime - sys_histtime) * 1000.; for (j = NBIN-1; j >= 0; j--) { - if (msec >= sys_bin[j]) - { - sys_histogram[phasewas][j]++; - break; - } + if (msec >= sys_bin[j]) + { + sys_histogram[phasewas][j]++; + break; + } } sys_histtime = newtime; sys_histphase = phase; @@ -218,18 +218,18 @@ void glob_audiostatus(void) post("seconds ago\terror type"); for (i = 0; i < nresync; i++) { - int errtype; - if (nresyncphase < 0) - nresyncphase += NRESYNC; - errtype = oss_resync[nresyncphase].r_error; - if (errtype < 0 || errtype > 4) - errtype = 0; - - post("%9.2f\t%s", - (sched_diddsp - oss_resync[nresyncphase].r_ntick) - * ((double)sys_schedblocksize) / sys_dacsr, - oss_errornames[errtype]); - nresyncphase--; + int errtype; + if (nresyncphase < 0) + nresyncphase += NRESYNC; + errtype = oss_resync[nresyncphase].r_error; + if (errtype < 0 || errtype > 4) + errtype = 0; + + post("%9.2f\t%s", + (sched_diddsp - oss_resync[nresyncphase].r_ntick) + * ((double)sys_schedblocksize) / sys_dacsr, + oss_errornames[errtype]); + nresyncphase--; } } @@ -244,78 +244,78 @@ void sys_log_error(int type) oss_nresync++; if (++oss_resyncphase == NRESYNC) oss_resyncphase = 0; if (type != ERR_NOTHING && !sched_diored && - (sched_diddsp >= sched_dioredtime)) + (sched_diddsp >= sched_dioredtime)) { - sys_vgui("pdtk_pd_dio 1\n"); - sched_diored = 1; + sys_vgui("pdtk_pd_dio 1\n"); + sched_diored = 1; } sched_dioredtime = - sched_diddsp + (int)(sys_dacsr /(double)sys_schedblocksize); + sched_diddsp + (int)(sys_dacsr /(double)sys_schedblocksize); } static int sched_lastinclip, sched_lastoutclip, sched_lastindb, sched_lastoutdb; -void glob_ping(t_pd *dummy); +void glob_watchdog(t_pd *dummy); static void sched_pollformeters( void) { int inclip, outclip, indb, outdb; static int sched_nextmeterpolltime, sched_nextpingtime; - /* if there's no GUI but we're running in "realtime", here is - where we arrange to ping the watchdog every 2 seconds. */ + /* if there's no GUI but we're running in "realtime", here is + where we arrange to ping the watchdog every 2 seconds. */ #ifdef __linux__ if (sys_nogui && sys_hipriority && (sched_diddsp - sched_nextpingtime > 0)) { - glob_ping(0); - /* ping every 2 seconds */ - sched_nextpingtime = sched_diddsp + - 2 * (int)(sys_dacsr /(double)sys_schedblocksize); + glob_watchdog(0); + /* ping every 2 seconds */ + sched_nextpingtime = sched_diddsp + + 2 * (int)(sys_dacsr /(double)sys_schedblocksize); } #endif if (sched_diddsp - sched_nextmeterpolltime < 0) - return; + return; if (sched_diored && (sched_diddsp - sched_dioredtime > 0)) { - sys_vgui("pdtk_pd_dio 0\n"); - sched_diored = 0; + sys_vgui("pdtk_pd_dio 0\n"); + sched_diored = 0; } if (sched_meterson) { - float inmax, outmax; - sys_getmeters(&inmax, &outmax); - indb = 0.5 + rmstodb(inmax); - outdb = 0.5 + rmstodb(outmax); - inclip = (inmax > 0.999); - outclip = (outmax >= 1.0); + float inmax, outmax; + sys_getmeters(&inmax, &outmax); + indb = 0.5 + rmstodb(inmax); + outdb = 0.5 + rmstodb(outmax); + inclip = (inmax > 0.999); + outclip = (outmax >= 1.0); } else { - indb = outdb = 0; - inclip = outclip = 0; + indb = outdb = 0; + inclip = outclip = 0; } if (inclip != sched_lastinclip || outclip != sched_lastoutclip - || indb != sched_lastindb || outdb != sched_lastoutdb) + || indb != sched_lastindb || outdb != sched_lastoutdb) { - sys_vgui("pdtk_pd_meters %d %d %d %d\n", indb, outdb, inclip, outclip); - sched_lastinclip = inclip; - sched_lastoutclip = outclip; - sched_lastindb = indb; - sched_lastoutdb = outdb; + sys_vgui("pdtk_pd_meters %d %d %d %d\n", indb, outdb, inclip, outclip); + sched_lastinclip = inclip; + sched_lastoutclip = outclip; + sched_lastindb = indb; + sched_lastoutdb = outdb; } sched_nextmeterpolltime = - sched_diddsp + (int)(sys_dacsr /(double)sys_schedblocksize); + sched_diddsp + (int)(sys_dacsr /(double)sys_schedblocksize); } void glob_meters(void *dummy, float f) { if (f == 0) - sys_getmeters(0, 0); + sys_getmeters(0, 0); sched_meterson = (f != 0); sched_lastinclip = sched_lastoutclip = sched_lastindb = sched_lastoutdb = - -1; + -1; } #if 0 @@ -330,38 +330,38 @@ void dsp_tick(void); static int sched_usedacs = 1; static double sched_referencerealtime, sched_referencelogicaltime; -static double sys_time_per_dsp_tick; +double sys_time_per_dsp_tick; void sched_set_using_dacs(int flag) { sched_usedacs = flag; if (!flag) { - sched_referencerealtime = sys_getrealtime(); - sched_referencelogicaltime = clock_getlogicaltime(); + sched_referencerealtime = sys_getrealtime(); + sched_referencelogicaltime = clock_getlogicaltime(); } sys_time_per_dsp_tick = (TIMEUNITPERSEC) * - ((double)sys_schedblocksize) / sys_dacsr; + ((double)sys_schedblocksize) / sys_dacsr; } /* take the scheduler forward one DSP tick, also handling clock timeouts */ -static void sched_tick(double next_sys_time) +void sched_tick(double next_sys_time) { int countdown = 5000; while (clock_setlist && clock_setlist->c_settime < next_sys_time) { - t_clock *c = clock_setlist; - sys_time = c->c_settime; - clock_unset(clock_setlist); - outlet_setstacklim(); - (*c->c_fn)(c->c_owner); - if (!countdown--) - { - countdown = 5000; - sys_pollgui(); - } - if (sys_quit) - return; + t_clock *c = clock_setlist; + sys_time = c->c_settime; + clock_unset(clock_setlist); + outlet_setstacklim(); + (*c->c_fn)(c->c_owner); + if (!countdown--) + { + countdown = 5000; + sys_pollgui(); + } + if (sys_quit) + return; } sys_time = next_sys_time; dsp_tick(); @@ -386,99 +386,99 @@ int m_scheduler( void) { int idlecount = 0; sys_time_per_dsp_tick = (TIMEUNITPERSEC) * - ((double)sys_schedblocksize) / sys_dacsr; + ((double)sys_schedblocksize) / sys_dacsr; #ifdef THREAD_LOCKING /* T.Grill - lock mutex */ - sys_lock(); + sys_lock(); #endif sys_clearhist(); if (sys_sleepgrain < 1000) - sys_sleepgrain = sys_schedadvance/4; + sys_sleepgrain = sys_schedadvance/4; if (sys_sleepgrain < 100) - sys_sleepgrain = 100; + sys_sleepgrain = 100; else if (sys_sleepgrain > 5000) - sys_sleepgrain = 5000; + sys_sleepgrain = 5000; sys_initmidiqueue(); while (!sys_quit) { - int didsomething = 0; - int timeforward; + int didsomething = 0; + int timeforward; - sys_addhist(0); + sys_addhist(0); waitfortick: - if (sched_usedacs) - { - timeforward = sys_send_dacs(); - - /* if dacs remain "idle" for 1 sec, they're hung up. */ - if (timeforward != 0) - idlecount = 0; - else - { - idlecount++; - if (!(idlecount & 31)) - { - static double idletime; - /* on 32nd idle, start a clock watch; every - 32 ensuing idles, check it */ - if (idlecount == 32) - idletime = sys_getrealtime(); - else if (sys_getrealtime() - idletime > 1.) - { - post("audio I/O stuck... closing audio\n"); - sys_close_audio(); - sched_set_using_dacs(0); - goto waitfortick; - } - } - } - } - else - { - if (1000. * (sys_getrealtime() - sched_referencerealtime) - > clock_gettimesince(sched_referencelogicaltime)) - timeforward = SENDDACS_YES; - else timeforward = SENDDACS_NO; - } - sys_setmiditimediff(0, 1e-6 * sys_schedadvance); - sys_addhist(1); - if (timeforward != SENDDACS_NO) - sched_tick(sys_time + sys_time_per_dsp_tick); - if (timeforward == SENDDACS_YES) - didsomething = 1; - - sys_addhist(2); - sys_pollmidiqueue(); - if (sys_pollgui()) - { - if (!didsomething) - sched_didpoll++; - didsomething = 1; - } - sys_addhist(3); - /* test for idle; if so, do graphics updates. */ - if (!didsomething) - { - sched_pollformeters(); - sys_reportidle(); + if (sched_usedacs) + { + timeforward = sys_send_dacs(); + + /* if dacs remain "idle" for 1 sec, they're hung up. */ + if (timeforward != 0) + idlecount = 0; + else + { + idlecount++; + if (!(idlecount & 31)) + { + static double idletime; + /* on 32nd idle, start a clock watch; every + 32 ensuing idles, check it */ + if (idlecount == 32) + idletime = sys_getrealtime(); + else if (sys_getrealtime() - idletime > 1.) + { + post("audio I/O stuck... closing audio\n"); + sys_close_audio(); + sched_set_using_dacs(0); + goto waitfortick; + } + } + } + } + else + { + if (1000. * (sys_getrealtime() - sched_referencerealtime) + > clock_gettimesince(sched_referencelogicaltime)) + timeforward = SENDDACS_YES; + else timeforward = SENDDACS_NO; + } + sys_setmiditimediff(0, 1e-6 * sys_schedadvance); + sys_addhist(1); + if (timeforward != SENDDACS_NO) + sched_tick(sys_time + sys_time_per_dsp_tick); + if (timeforward == SENDDACS_YES) + didsomething = 1; + + sys_addhist(2); + sys_pollmidiqueue(); + if (sys_pollgui()) + { + if (!didsomething) + sched_didpoll++; + didsomething = 1; + } + sys_addhist(3); + /* test for idle; if so, do graphics updates. */ + if (!didsomething) + { + sched_pollformeters(); + sys_reportidle(); #ifdef THREAD_LOCKING /* T.Grill - enter idle phase -> unlock thread lock */ sys_unlock(); #endif - if (timeforward != SENDDACS_SLEPT) - sys_microsleep(sys_sleepgrain); + if (timeforward != SENDDACS_SLEPT) + sys_microsleep(sys_sleepgrain); #ifdef THREAD_LOCKING /* T.Grill - leave idle phase -> lock thread lock */ - sys_lock(); + sys_lock(); #endif - sys_addhist(5); - sched_didnothing++; + sys_addhist(5); + sched_didnothing++; - } + } } #ifdef THREAD_LOCKING @@ -522,11 +522,11 @@ int sys_trylock(void) {} /* ------------ soft quit ------------------- */ /* added by Thomas Grill - - just set the quit flag for the scheduler loop - this is useful for applications using the PD shared library to signal the scheduler to terminate + just set the quit flag for the scheduler loop + this is useful for applications using the PD shared library to signal the scheduler to terminate */ void sys_exit(void) { - sys_quit = 1; + sys_quit = 1; } -- cgit v1.2.1