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
|
/* iemnet
*
* data handling code
* - wrappers for data "chunks"
* - queues
*
* copyright (c) 2010 IOhannes m zmölnig, IEM
*/
/* 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., */
/* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
/* */
#define DEBUGLEVEL 8
#include "iemnet.h"
#include "iemnet_data.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#define INBUFSIZE 65536L /* was 4096: size of receiving data buffer */
/* data handling */
t_iemnet_floatlist*iemnet__floatlist_init(t_iemnet_floatlist*cl) {
unsigned int i;
if(NULL==cl)return NULL;
for(i=0; i<cl->size; i++)
SETFLOAT((cl->argv+i), 0.f);
return cl;
}
void iemnet__floatlist_destroy(t_iemnet_floatlist*cl) {
if(NULL==cl)return;
if(cl->argv) free(cl->argv);
cl->argv=NULL;
cl->argc=0;
cl->size=0;
free(cl);
}
t_iemnet_floatlist*iemnet__floatlist_create(unsigned int size) {
t_iemnet_floatlist*result=(t_iemnet_floatlist*)malloc(sizeof(t_iemnet_floatlist));
if(NULL==result)return NULL;
result->argv = (t_atom*)malloc(size*sizeof(t_atom));
if(NULL==result->argv) {
iemnet__floatlist_destroy(result);
return NULL;
}
result->argc=size;
result->size=size;
result=iemnet__floatlist_init(result);
return result;
}
t_iemnet_floatlist*iemnet__floatlist_resize(t_iemnet_floatlist*cl, unsigned int size) {
t_atom*tmp;
if (NULL==cl) {
return iemnet__floatlist_create(size);
}
if(size<=cl->size) {
cl->argc=size;
return cl;
}
tmp=(t_atom*)malloc(size*sizeof(t_atom));
if(NULL==tmp) return NULL;
free(cl->argv);
cl->argv=tmp;
cl->argc=cl->size=size;
cl=iemnet__floatlist_init(cl);
return cl;
}
void iemnet__chunk_destroy(t_iemnet_chunk*c) {
if(NULL==c)return;
if(c->data)free(c->data);
c->data=NULL;
c->size=0;
free(c);
}
t_iemnet_chunk* iemnet__chunk_create_empty(int size) {
t_iemnet_chunk*result=(t_iemnet_chunk*)malloc(sizeof(t_iemnet_chunk));
if(result) {
result->size=size;
result->data=(unsigned char*)malloc(sizeof(unsigned char)*size);
if(NULL == result->data) {
result->size=0;
iemnet__chunk_destroy(result);
return NULL;
}
memset(result->data, 0, result->size);
result->addr=0L;
result->port=0;
}
return result;
}
t_iemnet_chunk* iemnet__chunk_create_data(int size, unsigned char*data) {
t_iemnet_chunk*result=iemnet__chunk_create_empty(size);
if(result) {
memcpy(result->data, data, result->size);
}
return result;
}
t_iemnet_chunk* iemnet__chunk_create_dataaddr(int size,
unsigned char*data,
struct sockaddr_in*addr) {
t_iemnet_chunk*result=iemnet__chunk_create_data(size, data);
if(result && addr) {
result->addr = ntohl(addr->sin_addr.s_addr);
result->port = ntohs(addr->sin_port);
}
return result;
}
t_iemnet_chunk* iemnet__chunk_create_list(int argc, t_atom*argv) {
int i;
t_iemnet_chunk*result=iemnet__chunk_create_empty(argc);
if(NULL==result)return NULL;
for(i=0; i<argc; i++) {
unsigned char c = atom_getint(argv);
result->data[i]=c;
argv++;
}
return result;
}
t_iemnet_chunk*iemnet__chunk_create_chunk(t_iemnet_chunk*c) {
t_iemnet_chunk*result=NULL;
if(NULL==c)return NULL;
result=iemnet__chunk_create_data(c->size, c->data);
result->addr=c->addr;
result->port=c->port;
return result;
}
t_iemnet_floatlist*iemnet__chunk2list(t_iemnet_chunk*c, t_iemnet_floatlist*dest) {
unsigned int i;
if(NULL==c)return NULL;
dest=iemnet__floatlist_resize(dest, c->size);
if(NULL==dest)return NULL;
for(i=0; i<c->size; i++) {
dest->argv[i].a_w.w_float = c->data[i];
}
return dest;
}
/* queue handling */
/*
* using code found at http://newsgroups.derkeiler.com/Archive/Comp/comp.programming.threads/2008-02/msg00502.html
*/
#ifdef t_iemnet_queue
# undef t_iemnet_queue
#endif
typedef struct _node {
struct _node* next;
t_iemnet_chunk*data;
} t_node;
struct _iemnet_queue {
t_node* head; /* = 0 */
t_node* tail; /* = 0 */
pthread_mutex_t mtx;
pthread_cond_t cond;
int done; // in cleanup state
int size;
};
/* push a chunk into the queue
* this will return the current queue size
*/
int queue_push(
t_iemnet_queue* const _this,
t_iemnet_chunk* const data
) {
t_node* tail;
t_node* n=NULL;
int size=-1;
if(NULL == _this)return size;
pthread_mutex_lock(&_this->mtx);
size=_this->size;
pthread_mutex_unlock(&_this->mtx);
if(NULL == data) return size;
n=(t_node*)malloc(sizeof(t_node));
n->next = 0;
n->data = data;
pthread_mutex_lock(&_this->mtx);
if (! (tail = _this->tail)) {
_this->head = n;
} else {
tail->next = n;
}
_this->tail = n;
_this->size+=data->size;
size=_this->size;
// added new chunk, so tell waiting threads that they can pop the data
pthread_cond_signal(&_this->cond);
pthread_mutex_unlock(&_this->mtx);
return size;
}
/* pop a chunk from the queue
* if the queue is empty, this will block until
* something has been pushed
* OR the queue is "done" (in which case NULL is returned)
*/
t_iemnet_chunk* queue_pop_block(
t_iemnet_queue* const _this
) {
t_node* head=0;
t_iemnet_chunk*data=0;
if(NULL == _this)return NULL;
pthread_mutex_lock(&_this->mtx);
/* if the queue is empty, wait */
if(NULL == _this->head) {
pthread_cond_wait(&_this->cond, &_this->mtx);
/* somebody signaled us, that we should do some work
* either the queue has been filled, or we are done...
*/
if(_this->done) {
pthread_mutex_unlock(&_this->mtx);
return NULL;
}
}
/* save the head, below we gonna work on this */
head = _this->head;
/* update _this */
if (! (_this->head = head->next)) {
_this->tail = 0;
}
if(head && head->data) {
_this->size-=head->data->size;
}
pthread_mutex_unlock(&_this->mtx);
if(head) {
data=head->data;
free(head);
head=NULL;
}
return data;
}
/* pop a chunk from the queue
* if the queue is empty, this will immediately return NULL
* (note that despite of the name this does block for synchronization)
*/
t_iemnet_chunk* queue_pop_noblock(
t_iemnet_queue* const _this
) {
t_node* head=0;
t_iemnet_chunk*data=0;
if(NULL == _this)return NULL;
pthread_mutex_lock(&_this->mtx);
if (! (head = _this->head)) {
// empty head
pthread_mutex_unlock(&_this->mtx);
return NULL;
}
if (! (_this->head = head->next)) {
_this->tail = 0;
}
if(head && head->data) {
_this->size-=head->data->size;
}
pthread_mutex_unlock(&_this->mtx);
if(head) {
data=head->data;
free(head);
head=NULL;
}
return data;
}
t_iemnet_chunk* queue_pop(t_iemnet_queue* const _this) {
return queue_pop_block(_this);
}
int queue_getsize(t_iemnet_queue* const _this) {
int size=-1;
if(_this) {
pthread_mutex_lock(&_this->mtx);
size=_this->size;
pthread_mutex_unlock(&_this->mtx);
}
return size;
}
void queue_finish(t_iemnet_queue* q) {
DEBUG("queue_finish: %x", q);
if(NULL==q) return;
pthread_mutex_lock(&q->mtx);
q->done=1;
DEBUG("queue signaling: %x", q);
pthread_cond_signal(&q->cond);
DEBUG("queue signaled: %x", q);
pthread_mutex_unlock(&q->mtx);
DEBUG("queue_finished: %x", q);
}
void queue_destroy(t_iemnet_queue* q) {
t_iemnet_chunk*c=NULL;
if(NULL==q)
return;
DEBUG("queue destroy %x", q);
queue_finish(q);
/* remove all the chunks from the queue */
while(NULL!=(c=queue_pop_noblock(q))) {
iemnet__chunk_destroy(c);
}
q->head=NULL;
q->tail=NULL;
pthread_mutex_destroy(&q->mtx);
pthread_cond_destroy(&q->cond);
free(q);
q=NULL;
DEBUG("queue destroyed %x", q);
}
t_iemnet_queue* queue_create(void) {
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
t_iemnet_queue*q=(t_iemnet_queue*)malloc(sizeof(t_iemnet_queue));
DEBUG("queue create %x", q);
if(NULL==q)return NULL;
q->head = NULL;
q->tail = NULL;
memcpy(&q->cond, &cond, sizeof(pthread_cond_t));
memcpy(&q->mtx , &mtx, sizeof(pthread_mutex_t));
q->done = 0;
q->size = 0;
DEBUG("queue created %x", q);
return q;
}
|