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
|
/*
* SyncedBlockStream.cpp
*****************************************************************************
* Copyright (C) 2012, bitmovin Softwareentwicklung OG, All Rights Reserved
*
* Email: libdash-dev@vicky.bitmovin.net
*
* This source code and its use and distribution, is subject to the terms
* and conditions of the applicable license agreement.
*****************************************************************************/
#include "SyncedBlockStream.h"
using namespace dash::helpers;
SyncedBlockStream::SyncedBlockStream () :
eos (false)
{
InitializeConditionVariable (&this->full);
InitializeCriticalSection (&this->monitorMutex);
}
SyncedBlockStream::~SyncedBlockStream ()
{
DeleteConditionVariable(&this->full);
DeleteCriticalSection(&this->monitorMutex);
}
void SyncedBlockStream::PopAndDeleteFront ()
{
EnterCriticalSection(&this->monitorMutex);
BlockStream::PopAndDeleteFront();
LeaveCriticalSection(&this->monitorMutex);
}
void SyncedBlockStream::PushBack (block_t *block)
{
EnterCriticalSection(&this->monitorMutex);
BlockStream::PushBack(block);
WakeAllConditionVariable(&this->full);
LeaveCriticalSection(&this->monitorMutex);
}
void SyncedBlockStream::PushFront (block_t *block)
{
EnterCriticalSection(&this->monitorMutex);
BlockStream::PushFront(block);
WakeAllConditionVariable(&this->full);
LeaveCriticalSection(&this->monitorMutex);
}
const block_t* SyncedBlockStream::GetBytes (uint32_t len)
{
EnterCriticalSection(&this->monitorMutex);
while(this->length == 0 && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0)
{
LeaveCriticalSection(&this->monitorMutex);
return NULL;
}
const block_t* block = BlockStream::GetBytes(len);
LeaveCriticalSection(&this->monitorMutex);
return block;
}
size_t SyncedBlockStream::GetBytes (uint8_t *data, size_t len)
{
EnterCriticalSection(&this->monitorMutex);
while(this->length == 0 && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0)
{
LeaveCriticalSection(&this->monitorMutex);
return 0;
}
size_t ret = BlockStream::GetBytes(data, len);
LeaveCriticalSection(&this->monitorMutex);
return ret;
}
size_t SyncedBlockStream::PeekBytes (uint8_t *data, size_t len)
{
EnterCriticalSection(&this->monitorMutex);
while(this->length == 0 && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0)
{
LeaveCriticalSection(&this->monitorMutex);
return 0;
}
size_t ret = BlockStream::PeekBytes(data, len);
LeaveCriticalSection(&this->monitorMutex);
return ret;
}
size_t SyncedBlockStream::PeekBytes (uint8_t *data, size_t len, size_t offset)
{
EnterCriticalSection(&this->monitorMutex);
while((this->length == 0 || offset >= this->length) && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0 || offset >= this->length)
{
LeaveCriticalSection(&this->monitorMutex);
return 0;
}
size_t ret = BlockStream::PeekBytes(data, len, offset);
LeaveCriticalSection(&this->monitorMutex);
return ret;
}
uint64_t SyncedBlockStream::Length () const
{
EnterCriticalSection(&this->monitorMutex);
uint64_t len = BlockStream::Length();
LeaveCriticalSection(&this->monitorMutex);
return len;
}
const block_t* SyncedBlockStream::GetFront ()
{
EnterCriticalSection(&this->monitorMutex);
while(this->length == 0 && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0)
{
LeaveCriticalSection(&this->monitorMutex);
return NULL;
}
const block_t* block = BlockStream::GetFront();
LeaveCriticalSection(&this->monitorMutex);
return block;
}
const block_t* SyncedBlockStream::Front () const
{
EnterCriticalSection(&this->monitorMutex);
while(this->length == 0 && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0)
{
LeaveCriticalSection(&this->monitorMutex);
return NULL;
}
const block_t* block = BlockStream::Front();
LeaveCriticalSection(&this->monitorMutex);
return block;
}
uint8_t SyncedBlockStream::ByteAt (uint64_t position) const
{
EnterCriticalSection(&this->monitorMutex);
while(this->length < position && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length < position)
{
LeaveCriticalSection(&this->monitorMutex);
return 0;
}
uint8_t ret = BlockStream::ByteAt(position);
LeaveCriticalSection(&this->monitorMutex);
return ret;
}
const block_t* SyncedBlockStream::ToBlock ()
{
EnterCriticalSection(&this->monitorMutex);
while(this->length == 0 && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0)
{
LeaveCriticalSection(&this->monitorMutex);
return NULL;
}
const block_t* block = BlockStream::ToBlock();
LeaveCriticalSection(&this->monitorMutex);
return block;
}
void SyncedBlockStream::Clear ()
{
EnterCriticalSection(&this->monitorMutex);
BlockStream::Clear();
LeaveCriticalSection(&this->monitorMutex);
}
void SyncedBlockStream::EraseFront (uint64_t len)
{
EnterCriticalSection(&this->monitorMutex);
BlockStream::EraseFront(len);
LeaveCriticalSection(&this->monitorMutex);
}
BlockStream* SyncedBlockStream::GetBlocks (uint64_t len)
{
EnterCriticalSection(&this->monitorMutex);
while(this->length == 0 && !this->eos)
SleepConditionVariableCS(&this->full, &this->monitorMutex, INFINITE);
if(this->length == 0)
{
LeaveCriticalSection(&this->monitorMutex);
return NULL;
}
BlockStream *stream = BlockStream::GetBlocks(len);
LeaveCriticalSection(&this->monitorMutex);
return stream;
}
void SyncedBlockStream::SetEOS (bool value)
{
EnterCriticalSection(&this->monitorMutex);
this->eos = value;
WakeAllConditionVariable(&this->full);
LeaveCriticalSection(&this->monitorMutex);
}
|