aboutsummaryrefslogtreecommitdiff
path: root/pd/src/m_sched.c
blob: 82c4c5d7c0aa12cc9bdf80e369b737454c8225f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/*  scheduling stuff  */

#include "m_pd.h"
#include "m_imp.h"
#include "s_stuff.h"

    /* LATER consider making this variable.  It's now the LCM of all sample
    rates we expect to see: 32000, 44100, 48000, 88200, 96000. */
#define TIMEUNITPERSEC (32.*441000.)

#define THREAD_LOCKING  
#include "pthread.h"


static int sys_quit;
double sys_time;
static double sys_time_per_msec = TIMEUNITPERSEC / 1000.;

int sys_schedblocksize = DEFDACBLKSIZE;
int sys_usecsincelastsleep(void);
int sys_sleepgrain;

typedef void (*t_clockmethod)(void *client);

struct _clock
{
    double c_settime;
    void *c_owner;
    t_clockmethod c_fn;
    struct _clock *c_next;
};

t_clock *clock_setlist;

#ifdef UNISTD
#include <unistd.h>
#endif

t_clock *clock_new(void *owner, t_method fn)
{
    t_clock *x = (t_clock *)getbytes(sizeof *x);
    x->c_settime = -1;
    x->c_owner = owner;
    x->c_fn = (t_clockmethod)fn;
    x->c_next = 0;
    return (x);
}

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;
    }
}

    /* set the clock to call back at an absolute system time */
void clock_set(t_clock *x, double setticks)
{
    if (setticks < sys_time) setticks = sys_time;
    clock_unset(x);
    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;
            }
        }
    }
    else x->c_next = clock_setlist, clock_setlist = x;
}

    /* set the clock to call back after a delay in msec */
void clock_delay(t_clock *x, double delaytime)
{
    clock_set(x, sys_time + sys_time_per_msec * delaytime);
}

    /* get current logical time.  We don't specify what units this is in;
    use clock_gettimesince() to measure intervals from time of this call. 
    This was previously, incorrectly named "clock_getsystime"; the old
    name is aliased to the new one in m_pd.h. */
double clock_getlogicaltime( void)
{
    return (sys_time);
}
    /* OBSOLETE NAME */
double clock_getsystime( void) { return (sys_time); }

    /* elapsed time in milliseconds since the given system time */
double clock_gettimesince(double prevsystime)
{
    return ((sys_time - prevsystime)/sys_time_per_msec);
}

    /* what value the system clock will have after a delay */
double clock_getsystimeafter(double delaytime)
{
    return (sys_time + sys_time_per_msec * delaytime);
}

void clock_free(t_clock *x)
{
    clock_unset(x);
    freebytes(x, sizeof *x);
}

/* the following routines maintain a real-execution-time histogram of the
various phases of real-time execution. */

static int sys_bin[] = {0, 2, 5, 10, 20, 30, 50, 100, 1000};
#define NBIN (sizeof(sys_bin)/sizeof(*sys_bin))
#define NHIST 10
static int sys_histogram[NHIST][NBIN];
static double sys_histtime;
static int sched_diddsp, sched_didpoll, sched_didnothing;

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;
    sys_histtime = sys_getrealtime();
    sched_diddsp = sched_didpoll = sched_didnothing = 0;
}

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]);
        }
    }
    post("dsp %d, pollgui %d, nothing %d",
        sched_diddsp, sched_didpoll, sched_didnothing);
}

static int sys_histphase;

int sys_addhist(int phase)
{
    int i, j, phasewas = sys_histphase;
    double newtime = sys_getrealtime();
    int msec = (newtime - sys_histtime) * 1000.;
    for (j = NBIN-1; j >= 0; j--)
    {
        if (msec >= sys_bin[j]) 
        {
            sys_histogram[phasewas][j]++;
            break;
        }
    }
    sys_histtime = newtime;
    sys_histphase = phase;
    return (phasewas);
}

#define NRESYNC 20

typedef struct _resync
{
    int r_ntick;
    int r_error;
} t_resync;

static int oss_resyncphase = 0;
static int oss_nresync = 0;
static t_resync oss_resync[NRESYNC];


static char *(oss_errornames[]) = {
"unknown",
"ADC blocked",
"DAC blocked",
"A/D/A sync",
"data late"
};

void glob_audiostatus(void)
{
    int dev, nresync, nresyncphase, i;
    nresync = (oss_nresync >= NRESYNC ? NRESYNC : oss_nresync);
    nresyncphase = oss_resyncphase - 1;
    post("audio I/O error history:");
    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--;
    }
}

static int sched_diored;
static int sched_dioredtime;
static int sched_meterson;

void sys_log_error(int type)
{
    oss_resync[oss_resyncphase].r_ntick = sched_diddsp;
    oss_resync[oss_resyncphase].r_error = type;
    oss_nresync++;
    if (++oss_resyncphase == NRESYNC) oss_resyncphase = 0;
    if (type != ERR_NOTHING && !sched_diored &&
        (sched_diddsp >= sched_dioredtime))
    {
        sys_vgui("pdtk_pd_dio 1\n");
        sched_diored = 1;
    }
    sched_dioredtime =
        sched_diddsp + (int)(sys_dacsr /(double)sys_schedblocksize);
}

static int sched_lastinclip, sched_lastoutclip,
    sched_lastindb, sched_lastoutdb;

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. */
#ifdef __linux__
    if (sys_nogui && sys_hipriority && (sched_diddsp - sched_nextpingtime > 0))
    {
        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;
    if (sched_diored && (sched_diddsp - sched_dioredtime > 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);
    }
    else
    {
        indb = outdb = 0;
        inclip = outclip = 0;
    }
    if (inclip != sched_lastinclip || outclip != sched_lastoutclip
        || 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;
    }
    sched_nextmeterpolltime =
        sched_diddsp + (int)(sys_dacsr /(double)sys_schedblocksize);
}

void glob_meters(void *dummy, float f)
{
    if (f == 0)
        sys_getmeters(0, 0);
    sched_meterson = (f != 0);
    sched_lastinclip = sched_lastoutclip = sched_lastindb = sched_lastoutdb =
        -1;
}

#if 0
void glob_foo(void *dummy, t_symbol *s, int argc, t_atom *argv)
{
    if (argc) sys_clearhist();
    else sys_printhist();
}
#endif

void dsp_tick(void);

static int sched_usedacs = 1;
static double sched_referencerealtime, sched_referencelogicaltime;
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();
    }
    sys_time_per_dsp_tick = (TIMEUNITPERSEC) *
        ((double)sys_schedblocksize) / sys_dacsr;
}

    /* take the scheduler forward one DSP tick, also handling clock timeouts */
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;
    }
    sys_time = next_sys_time;
    dsp_tick();
    sched_diddsp++;
}

/*
Here is Pd's "main loop."  This routine dispatches clock timeouts and DSP
"ticks" deterministically, and polls for input from MIDI and the GUI.  If
we're left idle we also poll for graphics updates; but these are considered
lower priority than the rest.

The time source is normally the audio I/O subsystem via the "sys_send_dacs()"
call.  This call returns true if samples were transferred; false means that
the audio I/O system is still busy with previous transfers.
*/

void sys_pollmidiqueue( void);
void sys_initmidiqueue( void);

 /* sys_idlehook is a hook the user can fill in to grab idle time.  Return
nonzero if you actually used the time; otherwise we're really really idle and
will now sleep. */
int (*sys_idlehook)(void);

int m_scheduler( void)
{
    int idlecount = 0;
    sys_time_per_dsp_tick = (TIMEUNITPERSEC) *
        ((double)sys_schedblocksize) / sys_dacsr;

#ifdef THREAD_LOCKING
        sys_lock();
#endif

    sys_clearhist();
    if (sys_sleepgrain < 1000)
        sys_sleepgrain = sys_schedadvance/4;
    if (sys_sleepgrain < 100)
        sys_sleepgrain = 100;
    else if (sys_sleepgrain > 5000)
        sys_sleepgrain = 5000;
    sys_initmidiqueue();
    while (!sys_quit)
    {
        int didsomething = 0;
        int timeforward;

        sys_addhist(0);
    waitfortick:
        if (sched_usedacs)
        {
#ifdef THREAD_LOCKING
            /* T.Grill - send_dacs may sleep -> 
                unlock thread lock make that time available 
                - could messaging do any harm while sys_send_dacs is running?
            */
            sys_unlock();
#endif
            timeforward = sys_send_dacs();
#ifdef THREAD_LOCKING
            /* T.Grill - done */
            sys_unlock();
#endif
                /* 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
            sys_unlock();   /* unlock while we idle */
#endif
                /* call externally installed idle function if any. */
            if (!sys_idlehook || !sys_idlehook())
            {
                    /* if even that had nothing to do, sleep. */
                if (timeforward != SENDDACS_SLEPT)
                    sys_microsleep(sys_sleepgrain);
            }
#ifdef THREAD_LOCKING
            sys_lock();
#endif

            sys_addhist(5);
            sched_didnothing++;

        }
    }

#ifdef THREAD_LOCKING
    sys_unlock();
#endif

    return (0);
}


/* ------------ thread locking ------------------- */

#ifdef THREAD_LOCKING
static pthread_mutex_t sys_mutex = PTHREAD_MUTEX_INITIALIZER;

void sys_lock(void)
{
    pthread_mutex_lock(&sys_mutex);
}

void sys_unlock(void)
{
    pthread_mutex_unlock(&sys_mutex);
}

int sys_trylock(void)
{
    return pthread_mutex_trylock(&sys_mutex);
}

#else

void sys_lock(void) {}
void sys_unlock(void) {}
int sys_trylock(void) {}

#endif

void sys_exit(void)
{
        sys_quit = 1;
}