pipe.c 15 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
/*
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2010 Uwe Schulzweida, Uwe.Schulzweida@zmaw.de
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  See COPYING file for copying and redistribution conditions.

  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; version 2 of the License.

  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.
*/

Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
19
20
21
22

#if  defined  (HAVE_CONFIG_H)
#  include "config.h"
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
23
24
25
26
#include <stdio.h>
#include <string.h>
#include "pipe.h"
#include "pstream_int.h"
Ralf Mueller's avatar
Ralf Mueller committed
27
#include <cdi.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
29
30
31
#include "cdo.h"
#include "error.h"
#include "dmemory.h"

Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
#if  defined  (HAVE_LIBPTHREAD)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
34
35

static int PipeDebug = 0;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
static
void pipe_init(pipe_t *pipe)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
{
  pthread_mutexattr_t m_attr;
  pthread_condattr_t c_attr;

  pthread_mutexattr_init(&m_attr);
  pthread_condattr_init(&c_attr);
  /*
#if defined (_POSIX_THREAD_PROCESS_SHARED)
  if ( PipeDebug )
    {
      Message(func, "setpshared mutexattr to PTHREAD_PROCESS_SHARED");
      Message(func, "setpshared condattr to PTHREAD_PROCESS_SHARED");
    }

  pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED);
  pthread_condattr_setpshared(&c_attr, PTHREAD_PROCESS_SHARED);

  if ( PipeDebug )
    {
      int pshared;
      pthread_mutexattr_getpshared(&m_attr, &pshared);
      if ( pshared == PTHREAD_PROCESS_SHARED )
	Message(func, "getpshared mutexattr is PTHREAD_PROCESS_SHARED");
      else if ( pshared == PTHREAD_PROCESS_PRIVATE )
	Message(func, "getpshared mutexattr is PTHREAD_PROCESS_PRIVATE");

      pthread_condattr_getpshared(&c_attr, &pshared);
      if ( pshared == PTHREAD_PROCESS_SHARED )
	Message(func, "getpshared condattr is PTHREAD_PROCESS_SHARED");
      else if ( pshared == PTHREAD_PROCESS_PRIVATE )
	Message(func, "getpshared condattr is PTHREAD_PROCESS_PRIVATE");
    }
#else
  if ( PipeDebug )
    Message(func, "_POSIX_THREAD_PROCESS_SHARED undefined");
#endif
  */
  pipe->EOP     = 0;

  pipe->recIDr  = -1;
  pipe->recIDw  = -1;
  pipe->tsIDr   = -1;
  pipe->tsIDw   = -1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
  pipe->nvals   = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
  pipe->nmiss   = 0;
  pipe->data    = NULL;
  pipe->hasdata = 0;
  pipe->usedata = TRUE;
  pipe->pstreamptr_in = 0;

  pipe->mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
  pthread_mutex_init(pipe->mutex, &m_attr);

  pipe->tsDef = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->tsDef, &c_attr);
  pipe->tsInq = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->tsInq, &c_attr);

  pipe->recDef = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->recDef, &c_attr);
  pipe->recInq = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->recInq, &c_attr);
  
  pipe->vlistDef = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->vlistDef, &c_attr);
  pipe->isclosed = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->isclosed, &c_attr);

  pipe->writeCond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->writeCond, &c_attr);

  pipe->readCond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
  pthread_cond_init(pipe->readCond, &c_attr);

  pthread_mutexattr_destroy(&m_attr);
  pthread_condattr_destroy(&c_attr);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
pipe_t *pipeNew()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121

Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
  pipe = (pipe_t *) malloc(sizeof(pipe_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
126
127
128
129

  pipe_init(pipe);

  return (pipe);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
void pipeDelete(pipe_t *pipe)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{
  if ( pipe )
    {
      if ( pipe->mutex )     free(pipe->mutex);
      if ( pipe->tsDef )     free(pipe->tsDef);
      if ( pipe->tsInq )     free(pipe->tsInq);
      if ( pipe->recDef )    free(pipe->recDef);
      if ( pipe->recInq )    free(pipe->recInq);
      if ( pipe->vlistDef )  free(pipe->vlistDef);
      if ( pipe->isclosed )  free(pipe->isclosed);
      if ( pipe->writeCond ) free(pipe->writeCond);
      if ( pipe->readCond )  free(pipe->readCond);
      free(pipe);
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
void pipeDefVlist(pstream_t *pstreamptr, int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
  static const char *func = "pipeDefVlist";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151
  char *pname = pstreamptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166

  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);

  pipe = pstreamptr->pipe;

  pthread_mutex_lock(pipe->mutex);
  pstreamptr->vlistID = vlistID;
  pthread_mutex_unlock(pipe->mutex);  

  pthread_cond_signal(pipe->vlistDef);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
int pipeInqVlist(pstream_t *pstreamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
  static const char *func = "pipeInqVlist";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
  char *pname = pstreamptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  int vlistID;

  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);

  pipe = pstreamptr->pipe;

  pthread_mutex_lock(pipe->mutex);
  while ( pstreamptr->vlistID == -1 )
    {
      if ( PipeDebug ) Message(func, "%s wait of vlist", pname);
      pthread_cond_wait(pipe->vlistDef, pipe->mutex);
    }
  vlistID = pstreamptr->vlistID;
  pthread_mutex_unlock(pipe->mutex);

  return (vlistID);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
192
int pipeInqTimestep(pstream_t *pstreamptr, int tsID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194
  static const char *func = "pipeInqTimestep";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
  char *pname = pstreamptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
  int nrecs;

  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);

  pipe = pstreamptr->pipe;

  pthread_mutex_lock(pipe->mutex);
  pipe->usedata = FALSE;
  pipe->recIDr = -1;
  if ( tsID != pipe->tsIDr+1 )
    {
      if ( ! (tsID == pipe->tsIDr && pipe->tsIDr == pipe->tsIDw && pipe->recIDr == -1) ) 
	Error(func, "%s unexpected tsID %d %d %d", pname, tsID, pipe->tsIDr+1, pipe->tsIDw);
    }
  
  pipe->tsIDr = tsID;
  while ( pipe->tsIDw != tsID )
    {
      if ( pipe->EOP == TRUE )
	{
	  if ( PipeDebug ) Message(func, "%s EOP", pname);
	  break;
	}
      if ( pipe->hasdata )
	{
	  if ( PipeDebug ) Message(func, "%s has data", pname);
	  pipe->hasdata = 0;
	  pipe->data = NULL;
	  pthread_cond_signal(pipe->readCond);
	}
      else
	if ( PipeDebug ) Message(func, "%s has no data", pname);

      pthread_cond_signal(pipe->recInq); /* o.k. ??? */

      if ( PipeDebug ) Message(func, "%s wait of tsDef", pname);
      pthread_cond_wait(pipe->tsDef, pipe->mutex);
    }

  if ( pipe->EOP == TRUE )
    nrecs = 0;
  else
    nrecs = pipe->nrecs;

  pthread_mutex_unlock(pipe->mutex);

  pthread_cond_signal(pipe->tsInq);

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
void pipeDefTimestep(pstream_t *pstreamptr, int tsID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
  static const char *func = "pipeDefTimestep";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
254
  char *pname = pstreamptr->name;
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);

  pipe = pstreamptr->pipe;

  pthread_mutex_lock(pipe->mutex);
  pipe->recIDw = -1;
  pipe->tsIDw++;
  if ( tsID != pipe->tsIDw )
    Error(func, "unexpected tsID %d(%d) for %s", tsID, pipe->tsIDw, pname);

  if ( tsID == 0 )
    nrecs = vlistNrecs(pstreamptr->vlistID);
  else
    {
      int vlistID, varID;
      vlistID = pstreamptr->vlistID;
      nrecs = 0;
      for ( varID = 0; varID < vlistNvars(vlistID); varID++ )
	if ( vlistInqVarTime(vlistID, varID) == TIME_VARIABLE )
	  nrecs += zaxisInqSize(vlistInqVarZaxis(vlistID, varID));
    }

  pipe->nrecs = nrecs;
  pthread_mutex_unlock(pipe->mutex);
  pthread_cond_signal(pipe->tsDef);
  /*
sleep(1);
*/
  pthread_mutex_lock(pipe->mutex);
  while ( pipe->tsIDr < tsID )
    {
      if ( pipe->EOP == TRUE )
	{
	  if ( PipeDebug ) Message(func, "EOP");
	  break;
	}
      if ( PipeDebug ) Message(func, "%s wait of tsInq", pname);
      pthread_cond_wait(pipe->tsInq, pipe->mutex);
    }
  pthread_mutex_unlock(pipe->mutex);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
int pipeInqRecord(pstream_t *pstreamptr, int *varID, int *levelID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
  static const char *func = "pipeInqRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
  char *pname = pstreamptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
  int condSignal = FALSE;

  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);
 
  pipe = pstreamptr->pipe;

  pthread_mutex_lock(pipe->mutex);

  if ( PipeDebug )
    Message(func, "%s has no data %d %d", pname, pipe->recIDr, pipe->recIDw);

  if ( pipe->hasdata || pipe->usedata )
    {
      pipe->hasdata = 0;
      pipe->data = NULL;
      pipe->usedata = FALSE;
      condSignal = TRUE;
    }
 	  
  pthread_mutex_unlock(pipe->mutex);
  if ( condSignal ) pthread_cond_signal(pipe->readCond);

  pthread_mutex_lock(pipe->mutex);

  pipe->usedata = TRUE;

  pipe->recIDr++;
  
  if ( PipeDebug )
    Message(func, "%s recID %d %d", pname, pipe->recIDr, pipe->recIDw);  

  while ( pipe->recIDw != pipe->recIDr )
    {
      if ( pipe->EOP == TRUE )
	{
	  if ( PipeDebug ) Message(func, "EOP");
	  break;
	}
      if ( PipeDebug ) Message(func, "%s wait of recDef", pname);
      pthread_cond_wait(pipe->recDef, pipe->mutex);
    }

  if ( pipe->EOP == TRUE )
    {
      *varID   = -1;
      *levelID = -1;
    }
  else
    {
      *varID   = pipe->varID;
      *levelID = pipe->levelID;
    }

  pthread_mutex_unlock(pipe->mutex);

  pthread_cond_signal(pipe->recInq);

  return (0);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
void pipeDefRecord(pstream_t *pstreamptr, int varID, int levelID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
  static const char *func = "pipeDefRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
  char *pname = pstreamptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
  int condSignal = FALSE;

  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);

  pipe = pstreamptr->pipe;

  pthread_mutex_lock(pipe->mutex);

  if ( PipeDebug )
    Message(func, "%s has data %d %d", pname, pipe->recIDr, pipe->recIDw);
	  
  if ( pipe->hasdata )
    {
      pipe->hasdata = 0;
      pipe->data = NULL;
      condSignal = TRUE;
    }

  pthread_mutex_unlock(pipe->mutex);
  if ( condSignal ) pthread_cond_signal(pipe->readCond);

  pthread_mutex_lock(pipe->mutex);
  pipe->usedata = TRUE;
  pipe->recIDw++;

  if ( PipeDebug )
    Message(func, "%s recID %d %d", pname, pipe->recIDr, pipe->recIDw);  

  pipe->varID = varID;
  pipe->levelID = levelID;
  pthread_mutex_unlock(pipe->mutex);
  pthread_cond_signal(pipe->recDef);

  pthread_mutex_lock(pipe->mutex);
  while ( pipe->recIDr < pipe->recIDw )
    {
      if ( pipe->tsIDw != pipe->tsIDr ) break;
411
      if ( pipe->EOP == TRUE ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412
413
414
415
416
417
418
      if ( PipeDebug ) Message(func, "%s wait of recInq %d", pname, pipe->recIDr);
      pthread_cond_wait(pipe->recInq, pipe->mutex);
    }
  pthread_mutex_unlock(pipe->mutex);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
void pipeCopyRecord(pstream_t *pstreamptr_out, pstream_t *pstreamptr_in)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
  static const char *func = "pipeCopyRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
  char *ipname = pstreamptr_in->name;
  char *opname = pstreamptr_out->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
424
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

  if ( PipeDebug )
    Message(func, "%s pstreamIDin %d", ipname, pstreamptr_in->self);
  if ( PipeDebug )
    Message(func, "%s pstreamIDout %d", opname, pstreamptr_out->self);

  pipe = pstreamptr_out->pipe;

  pthread_mutex_lock(pipe->mutex);
  pipe->hasdata = 2; /* pipe */
  pipe->pstreamptr_in = pstreamptr_in;
  pthread_mutex_unlock(pipe->mutex);

  pthread_cond_signal(pipe->writeCond);

  pthread_mutex_lock(pipe->mutex);
  while ( pipe->hasdata )
    {
      if ( pipe->usedata == FALSE ) break;

      if ( pipe->recIDw != pipe->recIDr ) break;

      if ( pipe->EOP == TRUE )
	{
	  if ( PipeDebug ) Message(func, "EOP");
	  break;
	}
      if ( PipeDebug ) Message(func, "%s wait of read record", opname);
      pthread_cond_wait(pipe->readCond, pipe->mutex);
    }
  pthread_mutex_unlock(pipe->mutex);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
void pipeReadRecord(pstream_t *pstreamptr, double *data, int *nmiss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
460
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461
  static const char *func = "pipeReadRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
462
  char *pname = pstreamptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479

  *nmiss = 0;
  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);

  pipe = pstreamptr->pipe;

  pthread_mutex_lock(pipe->mutex);
  while ( pipe->hasdata == 0 )
    {
      if ( PipeDebug ) Message(func, "%s wait of writeCond", pname);
      pthread_cond_wait(pipe->writeCond, pipe->mutex);
    }

  if ( pipe->hasdata == 2 )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480
      pstream_t *pstreamptr_in;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504

      pstreamptr_in = pipe->pstreamptr_in;
      /*
      if ( pstreamptr_in->ispipe )
	{
	  pstreamptr_in = pstreamptr_in->pipe->pstreamptr_in;
	  if ( pstreamptr_in->ispipe )
	    Error(func, "istream is pipe");
	}
      */
      pstreamptr = pstreamptr_in;
      while ( pstreamptr_in->ispipe )
	{
	  if ( PipeDebug ) fprintf(stderr, "%s: istream %d is pipe\n", func, pstreamptr_in->self);
	  pstreamptr    = pstreamptr_in;
	  pstreamptr_in = pstreamptr_in->pipe->pstreamptr_in;
	  if ( pstreamptr_in == 0 ) break;
	}

      if ( pstreamptr_in == 0 )
	{
	  if ( PipeDebug ) fprintf(stderr, "pstreamID = %d\n", pstreamptr->self);
	  if ( pstreamptr->pipe->hasdata == 1 )
	    {
505
	      int vlistID, datasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
508
509
510

	      if ( ! pstreamptr->pipe->data )
		Error(func, "No data pointer for %s", pname);

	      vlistID = pstreamptr->vlistID;
511
512
513
	      datasize = gridInqSize(vlistInqVarGrid(vlistID, pstreamptr->pipe->varID));
	      if ( vlistNumber(vlistID) != CDI_REAL ) datasize *= 2;
	      memcpy(data, pstreamptr->pipe->data, datasize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
516
517
518
519
520
521
522
523
524
525
526
	      *nmiss = pstreamptr->pipe->nmiss;
	    }
	  else
	    Error(func, "Internal problem! istream undefined");
	}
      else
	{
	  if ( PipeDebug ) fprintf(stderr, "%s: istream %d is file\n", func, pstreamptr_in->self);
	  streamReadRecord(pstreamptr_in->fileID, data, nmiss);
	}
    }
  else if ( pipe->hasdata == 1 )
    {
527
      int vlistID, datasize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
528
529
530
531
532

      if ( ! pipe->data )
	Error(func, "No data pointer for %s", pname);

      vlistID = pstreamptr->vlistID;
533
534
535
536
      datasize = gridInqSize(vlistInqVarGrid(vlistID, pipe->varID));
      pipe->nvals += datasize;
      if ( vlistNumber(vlistID) != CDI_REAL ) datasize *= 2;
      memcpy(data, pipe->data, datasize*sizeof(double));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
      *nmiss = pipe->nmiss;
    }
  else
    {
      Error(func, "data type %d not implemented", pipe->hasdata);
    }

  if ( PipeDebug ) Message(func, "%s read record %d", pname, pipe->recIDr);
 

  pipe->hasdata = 0;
  pipe->data = NULL;
  pthread_mutex_unlock(pipe->mutex);

  pthread_cond_signal(pipe->readCond);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
void pipeWriteRecord(pstream_t *pstreamptr, double *data, int nmiss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
556
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
557
  static const char *func = "pipeWriteRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
558
  char *pname = pstreamptr->name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
559
  pipe_t *pipe;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604

  if ( PipeDebug )
    Message(func, "%s pstreamID %d", pname, pstreamptr->self);

  pipe = pstreamptr->pipe;
  /*
  if ( ! pipe->usedata ) return;
  */
  pthread_mutex_lock(pipe->mutex);
  pipe->hasdata = 1; /* data pointer */
  pipe->data    = data;
  pipe->nmiss   = nmiss;
  pthread_mutex_unlock(pipe->mutex);
  pthread_cond_signal(pipe->writeCond);

  if ( PipeDebug ) Message(func, "%s write record %d", pname, pipe->recIDw);

  pthread_mutex_lock(pipe->mutex);
  while ( pipe->hasdata )
    {
      if ( pipe->usedata == FALSE ) break;
      /*     
      printf("ts ids %d %d\n", pipe->tsIDw, pipe->tsIDr);
      printf("rec ids %d %d\n", pipe->recIDw, pipe->recIDr);
      */
      if ( pipe->recIDw != pipe->recIDr ) break;

      if ( pipe->EOP == TRUE )
	{
	  if ( PipeDebug ) Message(func, "EOP");
	  break;
	}
      if ( PipeDebug ) Message(func, "%s wait of readCond", pname);
      pthread_cond_wait(pipe->readCond, pipe->mutex);
    }
  pthread_mutex_unlock(pipe->mutex);
}


void pipeDebug(int debug)
{
  PipeDebug = debug;
}

#endif