Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
555a4d73
Commit
555a4d73
authored
May 17, 2019
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
input/buffering: pass offset to Read() and eliminate Seek()
Another step towards supporting multiple readers.
parent
813567bf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
83 deletions
+37
-83
BufferedInputStream.cxx
src/input/BufferedInputStream.cxx
+4
-5
BufferingInputStream.cxx
src/input/BufferingInputStream.cxx
+28
-71
BufferingInputStream.hxx
src/input/BufferingInputStream.hxx
+5
-7
No files found.
src/input/BufferedInputStream.cxx
View file @
555a4d73
...
...
@@ -46,11 +46,10 @@ BufferedInputStream::Check()
}
void
BufferedInputStream
::
Seek
(
std
::
unique_lock
<
Mutex
>
&
lock
,
BufferedInputStream
::
Seek
(
std
::
unique_lock
<
Mutex
>
&
,
offset_type
new_offset
)
{
BufferingInputStream
::
Seek
(
lock
,
new_offset
);
InputStream
::
offset
=
new_offset
;
offset
=
new_offset
;
}
bool
...
...
@@ -62,14 +61,14 @@ BufferedInputStream::IsEOF() noexcept
bool
BufferedInputStream
::
IsAvailable
()
noexcept
{
return
BufferingInputStream
::
IsAvailable
();
return
BufferingInputStream
::
IsAvailable
(
offset
);
}
size_t
BufferedInputStream
::
Read
(
std
::
unique_lock
<
Mutex
>
&
lock
,
void
*
ptr
,
size_t
s
)
{
size_t
nbytes
=
BufferingInputStream
::
Read
(
lock
,
ptr
,
s
);
size_t
nbytes
=
BufferingInputStream
::
Read
(
lock
,
offset
,
ptr
,
s
);
InputStream
::
offset
+=
nbytes
;
return
nbytes
;
}
src/input/BufferingInputStream.cxx
View file @
555a4d73
...
...
@@ -55,42 +55,25 @@ BufferingInputStream::Check()
input
->
Check
();
}
void
BufferingInputStream
::
Seek
(
std
::
unique_lock
<
Mutex
>
&
lock
,
size_t
new_offset
)
bool
BufferingInputStream
::
IsAvailable
(
size_t
offset
)
noexcept
{
if
(
new_offset
>=
size
())
{
offset
=
new_offset
;
return
;
}
auto
r
=
buffer
.
Read
(
new_offset
);
if
(
r
.
HasData
())
{
/* nice, we already have some data at the desired
offset and this method call is a no-op */
offset
=
new_offset
;
return
;
}
seek_offset
=
new_offset
;
seek
=
true
;
wake_cond
.
notify_one
();
if
(
offset
>=
size
())
return
true
;
client_cond
.
wait
(
lock
,
[
this
]{
return
!
seek
;
});
if
(
buffer
.
Read
(
offset
).
HasData
())
return
true
;
if
(
seek_error
)
std
::
rethrow_exception
(
std
::
exchange
(
seek_error
,
{}));
/* if no data is available now, make sure it will be soon */
if
(
want_offset
==
INVALID_OFFSET
)
want_offset
=
offset
;
offset
=
new_offset
;
}
bool
BufferingInputStream
::
IsAvailable
()
noexcept
{
return
offset
==
size
()
||
buffer
.
Read
(
offset
).
HasData
();
return
false
;
}
size_t
BufferingInputStream
::
Read
(
std
::
unique_lock
<
Mutex
>
&
lock
,
void
*
ptr
,
size_t
s
)
BufferingInputStream
::
Read
(
std
::
unique_lock
<
Mutex
>
&
lock
,
size_t
offset
,
void
*
ptr
,
size_t
s
)
{
if
(
offset
>=
size
())
return
0
;
...
...
@@ -101,13 +84,15 @@ BufferingInputStream::Read(std::unique_lock<Mutex> &lock, void *ptr, size_t s)
/* yay, we have some data */
size_t
nbytes
=
std
::
min
(
s
,
r
.
defined_buffer
.
size
);
memcpy
(
ptr
,
r
.
defined_buffer
.
data
,
nbytes
);
offset
+=
nbytes
;
return
nbytes
;
}
if
(
error
)
std
::
rethrow_exception
(
error
);
if
(
want_offset
==
INVALID_OFFSET
)
want_offset
=
offset
;
client_cond
.
wait
(
lock
);
}
}
...
...
@@ -132,27 +117,13 @@ inline void
BufferingInputStream
::
RunThreadLocked
(
std
::
unique_lock
<
Mutex
>
&
lock
)
{
while
(
!
stop
)
{
if
(
seek
)
{
try
{
input
->
Seek
(
lock
,
seek_offset
);
}
catch
(...)
{
seek_error
=
std
::
current_exception
();
}
if
(
want_offset
!=
INVALID_OFFSET
)
{
assert
(
want_offset
<
size
());
seek
=
false
;
client_cond
.
notify_all
();
}
else
if
(
offset
!=
input
->
GetOffset
()
&&
!
IsAvailable
())
{
/* a past Seek() call was a no-op because data
was already available at that position, but
now we've reached a new position where
there is no more data in the buffer, and
our input is reading somewhere else (maybe
stuck at the end of the file); to find a
way out, we now seek our input to our
reading position to be able to fill our
buffer */
input
->
Seek
(
lock
,
offset
);
const
size_t
seek_offset
=
want_offset
;
want_offset
=
INVALID_OFFSET
;
if
(
!
buffer
.
Read
(
seek_offset
).
HasData
())
input
->
Seek
(
lock
,
seek_offset
);
}
else
if
(
input
->
IsEOF
())
{
/* our input has reached its end: prepare
reading the first remaining hole */
...
...
@@ -170,27 +141,13 @@ BufferingInputStream::RunThreadLocked(std::unique_lock<Mutex> &lock)
auto
w
=
buffer
.
Write
(
read_offset
);
if
(
w
.
empty
())
{
if
(
IsAvailable
())
{
/* we still have enough data
for the next Read() - seek
to the first hole */
size_t
new_offset
=
FindFirstHole
();
if
(
new_offset
==
INVALID_OFFSET
)
/* the file has been
read completely */
break
;
input
->
Seek
(
lock
,
new_offset
);
}
else
{
/* we need more data at our
current position, because
the next Read() will stall
- seek our input to our
offset to prepare filling
the buffer from there */
input
->
Seek
(
lock
,
offset
);
}
size_t
new_offset
=
FindFirstHole
();
if
(
new_offset
==
INVALID_OFFSET
)
/* the file has been read
completely */
break
;
input
->
Seek
(
lock
,
new_offset
);
continue
;
}
...
...
src/input/BufferingInputStream.hxx
View file @
555a4d73
...
...
@@ -55,11 +55,9 @@ class BufferingInputStream : InputStreamHandler {
SparseBuffer
<
uint8_t
>
buffer
;
bool
stop
=
false
,
seek
=
false
;
bool
stop
=
false
;
size_t
offset
=
0
;
size_t
seek_offset
;
size_t
want_offset
=
INVALID_OFFSET
;
std
::
exception_ptr
error
,
seek_error
;
...
...
@@ -78,9 +76,9 @@ public:
}
void
Check
();
void
Seek
(
std
::
unique_lock
<
Mutex
>
&
lock
,
size_t
new_offset
)
;
bool
IsAvailable
()
noexcept
;
size_t
Read
(
std
::
unique_lock
<
Mutex
>
&
lock
,
void
*
ptr
,
size_t
size
);
bool
IsAvailable
(
size_t
offset
)
noexcept
;
size_t
Read
(
std
::
unique_lock
<
Mutex
>
&
lock
,
size_t
offset
,
void
*
ptr
,
size_t
size
);
protected
:
virtual
void
OnBufferAvailable
()
noexcept
{}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment