aboutsummaryrefslogtreecommitdiff
path: root/include/pdp_queue.h
blob: f8bd9dda11f56259569b257223cc9fdae26b6155 (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
/*
 *   Pure Data Packet - processor queue interface
 *   Copyright (c) by Tom Schouten <tom@zwizwa.be>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#ifndef PDP_QUEUE_H
#define PDP_QUEUE_H

#include "pdp_pd.h"
#include <pthread.h>

/********************* general purpose pd process queue class *********************/

typedef void (*t_pdpmethod)(void *client);

/* the process queue data record */
typedef struct process_queue_struct
{
  void *x_owner;             /* the object we are dealing with */
  t_pdpmethod x_process;     /* the process method */
  t_pdpmethod x_callback;    /* the function to be called when finished */
  int *x_queue_id;           /* place to store the queue id for task */
} t_process_queue_item;


/* a pd process queue object */
typedef struct _pd_queue
{
    /* clock members */
    t_clock *pdp_clock;
    double deltime;

    /* some bookkeeping vars */
    long long ticks;
    long long packets;

    /* queue members */
    t_process_queue_item *q;    /* queue */
    int mask;
    int head;              /* last entry in queue + 1 */
    int tail;              /* first entry in queque */
    int curr;              /* the object currently processed in other thread */

    /* pthread vars */
    pthread_mutex_t mut;
    pthread_cond_t cond_dataready;
    pthread_cond_t cond_processingdone;
    pthread_t thread_id;
    
    /* toggle for thread usage */
    int use_thread;

} t_pdp_procqueue;


/* all symbols are C-style */
#ifdef __cplusplus
extern "C"
{
#endif


/* returns 1 if full, 0 if there's space available */
int pdp_procqueue_full(t_pdp_procqueue *q);


void pdp_procqueue_flush(t_pdp_procqueue *q);
void pdp_procqueue_wait(t_pdp_procqueue *q);
void pdp_procqueue_finish(t_pdp_procqueue *q, int indx);
void pdp_procqueue_add(t_pdp_procqueue *q, void *owner, void *process, void *callback, int *queue_id);
void pdp_procqueue_use_thread(t_pdp_procqueue* q, int t);
void pdp_procqueue_init(t_pdp_procqueue *q, double milliseconds, int logsize);

/********************* interface to pdp process queue singleton *********************/

/* processor queue methods, callable from main pd thread */

/* get the default queue */
t_pdp_procqueue *pdp_queue_get_queue(void);



#if 1 

/* add a method to the processing queue */
void  pdp_queue_add(void *owner, void *process, void *callback, int *queue_id);

/* halt main tread until processing is done */
void pdp_queue_wait(void);

/* halt main tread until processing is done and remove 
   callback from queue(for destructors) */
void pdp_queue_finish(int queue_id);

#endif


/* misc signals to pdp */
void pdp_control_notify_drop(int packet);



#ifdef __cplusplus
}
#endif

#endif