1 /*
2  *  Copyright (C) 2000-2020, Thomas Maier-Komor
3  *
4  *  This file is part of mbuffer's source code.
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "dest.h"
21 #include "globals.h"
22 #include <fcntl.h>
23 #include <time.h>
24 
25 dest_t *Dest = 0;
26 
27 int
28 	Hashers = 0,		/* number of hashing threads */
29 	In = -1,
30 	OptMode = O_EXCL,
31 	Terminal = 0,		/* do we have a controling terminal? */
32 	TermQ[2],
33 	Tmp = -1;
34 
35 volatile int
36 	ActSenders = 0,
37 	NumSenders = -1,	/* number of sender threads */
38 	SendSize = 0,
39 	Terminate = 0,		/* abort execution, because of error or signal */
40 	Watchdog = 0;		/* 0: off, 1: started, 2: raised */
41 
42 volatile unsigned
43 	Done = 0,
44 	EmptyCount = 0,		/* counter incremented when buffer runs empty */
45 	FullCount = 0,		/* counter incremented when buffer gets full */
46 	MainOutOK = 1;		/* is the main outputThread still writing or just coordinating senders */
47 
48 volatile unsigned long long
49 	Rest = 0,
50 	Numin = 0,
51 	Numout = 0,
52 	InSize = 0;
53 
54 char *volatile
55 	SendAt = 0;
56 
57 size_t
58 	IDevBSize = 0,
59 	PrefixLen = 0;
60 
61 long
62 	PgSz = 0,
63 	Finish = -1,		/* this is for graceful termination */
64 	TickTime = 0;
65 
66 char
67 	*Prefix,
68 	**Buffer;
69 
70 pthread_mutex_t
71 	TermMut = PTHREAD_MUTEX_INITIALIZER,	/* prevents statusThread from interfering with request*Volume */
72 	LowMut = PTHREAD_MUTEX_INITIALIZER,
73 	HighMut = PTHREAD_MUTEX_INITIALIZER,
74 	SendMut = PTHREAD_MUTEX_INITIALIZER;
75 
76 sem_t
77 	Dev2Buf,
78 	Buf2Dev;
79 
80 pthread_cond_t
81 	PercLow = PTHREAD_COND_INITIALIZER,	/* low watermark */
82 	PercHigh = PTHREAD_COND_INITIALIZER,	/* high watermark */
83 	SendCond = PTHREAD_COND_INITIALIZER;
84 
85 pthread_t
86 	ReaderThr,
87 	WatchdogThr;
88 
89 struct timespec
90 	Starttime;
91