annotate read_cache.c @ 138:5ce26c18c455 src

dvd_read_name can be static and does not need the whole vm_t*
author mroi
date Sat, 29 Mar 2003 12:21:15 +0000
parents b6834e6359cf
children 3bfaec0c1288
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
1 /*
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net>
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
3 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
4 * This file is part of libdvdnav, a DVD navigation library.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
5 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
6 * libdvdnav is free software; you can redistribute it and/or modify
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
9 * (at your option) any later version.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
10 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
11 * libdvdnav is distributed in the hope that it will be useful,
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
14 * GNU General Public License for more details.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
15 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
19 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
20 * $Id$
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
21 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
22 */
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
23
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
25 #include "config.h"
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
26 #endif
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
27
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
28 #include "dvdnav.h"
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
29 #include "read_cache.h"
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
30 #include <pthread.h>
46
654705f4e7af Add some readcache profiling code.
jcdutton
parents: 45
diff changeset
31 #include <sys/time.h>
654705f4e7af Add some readcache profiling code.
jcdutton
parents: 45
diff changeset
32 #include <time.h>
654705f4e7af Add some readcache profiling code.
jcdutton
parents: 45
diff changeset
33
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
34
46
654705f4e7af Add some readcache profiling code.
jcdutton
parents: 45
diff changeset
35 #define DVDNAV_PROFILE
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
36
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
37
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
38 /* Read-ahead cache structure. */
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
39 #if _MULTITHREAD_
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
41 /* For the multithreaded cache, the cache is a ring buffer + writing
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
42 * thread that continuously reads data into the buffer until it is
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
43 * full or the 'upper-bound' has been reached.
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
44 */
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
45
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
46 #define CACHE_BUFFER_SIZE 2048 /* Cache this number of blocks at a time */
37
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
47
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
48 struct read_cache_s {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
49 pthread_mutex_t cache_lock;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
50 pthread_t read_thread;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
51
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
52 /* Buffer */
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
53 uint8_t *buffer;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
54
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
55 /* Size of buffer */
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
56 int32_t size;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
57 /* block offset from sector start of buffer 'head' */
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
58 uint32_t pos;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
59 /* block offset from sector start of read point */
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
60 uint32_t read_point;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
61 /* block offset from buffer start to ring-boundary */
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
62 uint32_t start;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
63
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
64 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
65 dvdnav_t *dvd_self;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
66 };
37
832ca4921e04 Fixed (what appears to be) an error in JumpVTS_PTT implementation, it didn't call play_PGC after jumping.
richwareham
parents: 34
diff changeset
67
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
68 #else
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
69
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
70 #define READ_CACHE_CHUNKS 10
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
71
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
72 /* all cache chunks must be memory aligned to allow use of raw devices */
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
73 #define ALIGNMENT 2048
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
74
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
75 #define READ_AHEAD_SIZE_MIN 4
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
76 #define READ_AHEAD_SIZE_MAX 512
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
77
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
78 typedef struct read_cache_chunk_s {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
79 uint8_t *cache_buffer;
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
80 uint8_t *cache_buffer_base; /* used in malloc and free for alignment */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
81 int32_t cache_start_sector; /* -1 means cache invalid */
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
82 int32_t cache_read_count; /* this many sectors are already read */
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
83 size_t cache_block_count; /* this many sectors will go in this chunk */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
84 size_t cache_malloc_size;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
85 int cache_valid;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
86 int usage_count; /* counts how many buffers where issued from this chunk */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
87 } read_cache_chunk_t;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
88
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
89 struct read_cache_s {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
90 read_cache_chunk_t chunk[READ_CACHE_CHUNKS];
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
91 int current;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
92 int freeing; /* is set to one when we are about to dispose the cache */
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
93 int read_ahead_size;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
94 int read_ahead_incr;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
95 int last_sector;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
96 pthread_mutex_t lock;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
97
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
98 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
99 dvdnav_t *dvd_self;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
100 };
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
101 #endif
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
102
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
103 /*
76
0e2abe7083de more consistent console output
mroi
parents: 74
diff changeset
104 #define READ_CACHE_TRACE 0
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
105 */
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
106
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
107 #ifdef __GNUC__
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
108 # if READ_CACHE_TRACE
114
b6834e6359cf big libdvdnav cleanup, quoting the ChangeLog:
mroi
parents: 108
diff changeset
109 # define dprintf(fmt, args...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , ## args)
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
110 # else
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
111 # define dprintf(fmt, args...) /* Nowt */
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
112 # endif
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
113 #else
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
114 # if READ_CACHE_TRACE
114
b6834e6359cf big libdvdnav cleanup, quoting the ChangeLog:
mroi
parents: 108
diff changeset
115 # define dprintf(fmt, ...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , __VA_ARGS__)
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
116 # else
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
117 # define dprintf(fmt, ...) /* Nowt */
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
118 # endif
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
119 #endif
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
120
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
121 #if _MULTITHREAD_
45
7bf97b8922b4 Revert back to old read_cache method.
jcdutton
parents: 41
diff changeset
122
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
123 void * read_cache_read_thread (void * this_gen) {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
124 int cont = 1;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
125 int32_t diff, start;
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
126 uint32_t pos, size, startp, endp;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
127 uint32_t s,c;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
128 uint8_t *at;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
129 read_cache_t *self = (read_cache_t*)this_gen;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
130
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
131 while(cont) {
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
132
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
133 pthread_mutex_lock(&self->cache_lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
134
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
135 if(self->size >= 0) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
136 diff = self->read_point - self->pos;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
137 if(diff >= self->size/2) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
138 dprintf("(II) Read thread -- ");
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
139
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
140 startp = (self->start) % CACHE_BUFFER_SIZE;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
141 endp = abs((self->start + diff - 1) % CACHE_BUFFER_SIZE);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
142 dprintf("startp = %i, endp = %i -- ",startp, endp);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
143
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
144 pos = self->pos + diff;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
145 size = self->size - diff;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
146 start = (self->start + diff) % CACHE_BUFFER_SIZE;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
147
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
148 /* Fill remainder of buffer */
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
149
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
150 if(startp > endp) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
151 s = pos + size; c = CACHE_BUFFER_SIZE - startp;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
152 at = self->buffer + (startp * DVD_VIDEO_LB_LEN);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
153 if(c > 0) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
154 dprintf("(1) Reading from %i to %i to %i ", s, s+c-1, startp);
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
155 pthread_mutex_unlock(&self->cache_lock);
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
156 DVDReadBlocks(self->dvd_self->file, s,c, at);
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
157 pthread_mutex_lock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
158 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
159
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
160 s = pos + size + c; c = CACHE_BUFFER_SIZE - size - c;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
161 at = self->buffer;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
162 if(c > 0) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
163 dprintf("(2) Reading from %i to %i to %i ", s, s+c-1, 0);
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
164 pthread_mutex_unlock(&self->cache_lock);
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
165 DVDReadBlocks(self->dvd_self->file, s,c, at);
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
166 pthread_mutex_lock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
167 }
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
168 } else {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
169 s = pos + size; c = CACHE_BUFFER_SIZE - size;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
170 at = self->buffer + (startp * DVD_VIDEO_LB_LEN);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
171 if(c > 0) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
172 dprintf("(3) Reading from %i to %i to %i ", s, s+c-1, startp);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
173 pthread_mutex_unlock(&self->cache_lock);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
174 DVDReadBlocks(self->dvd_self->file, s,c, at);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
175 pthread_mutex_lock(&self->cache_lock);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
176 }
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
177 }
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
178
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
179 dprintf("\n");
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
180
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
181 self->pos = pos;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
182 self->start = start; self->size = CACHE_BUFFER_SIZE;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
183 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
184 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
185
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
186 pthread_mutex_unlock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
187 cont = (self->buffer != NULL);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
188 usleep(100);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
189 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
190
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
191 return NULL;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
192 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
193
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
194 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
195 read_cache_t *me;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
196
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
197 me = (read_cache_t*)malloc(sizeof(struct read_cache_s));
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
198
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
199 if(me) {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
200 int err;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
201
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
202 me->dvd_self = dvd_self;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
203 me->buffer = (uint8_t*)malloc(CACHE_BUFFER_SIZE * DVD_VIDEO_LB_LEN);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
204 me->start = 0;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
205 me->pos = 0;
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
206 me->read_point = 0;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
207 me->size = -1;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
208
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
209 /* Initialise the mutex */
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
210 pthread_mutex_init(&me->cache_lock, NULL);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
211
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
212 if ((err = pthread_create (&me->read_thread,
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
213 NULL, read_cache_read_thread, me)) != 0) {
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
214 dprintf("read_cache: can't create new thread (%s)\n",strerror(err));
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
215 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
216 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
217
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
218 return me;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
219 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
220
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
221 void dvdnav_read_cache_free(read_cache_t* self) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
222 dvdnav_t *tmp;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
223
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
224 pthread_mutex_lock(&self->cache_lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
225
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
226 if(self->buffer) {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
227 free(self->buffer);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
228 self->buffer = NULL;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
229 self->size = -2;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
230 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
231
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
232 pthread_mutex_unlock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
233
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
234 pthread_join(self->read_thread, NULL);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
235
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
236 pthread_mutex_destroy(&self->cache_lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
237
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
238 tmp = self->dvd_self;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
239 free(self);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
240
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
241 /* We free the main structure, too, because we have no buffers out there. */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
242 free(tmp);
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
243 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
244
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
245 /* This function MUST be called whenever self->file changes. */
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
246 void dvdnav_read_cache_clear(read_cache_t *self) {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
247 if(!self)
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
248 return;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
249
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
250 pthread_mutex_lock(&self->cache_lock);
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
251 self->size = -1;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
252 self->start = 0;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
253 self->pos = 0;
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
254 self->read_point = 0;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
255 pthread_mutex_unlock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
256 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
257
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
258 /* This function is called just after reading the NAV packet. */
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
259 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
260 if(!self)
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
261 return;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
262
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
263 if(!self->dvd_self->use_read_ahead) {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
264 return;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
265 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
266
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
267 pthread_mutex_lock(&self->cache_lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
268 dprintf("Requested pre-cache (%i -> +%i) : current state pos=%i, size=%i.\n",
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
269 sector, block_count, self->pos, self->size);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
270
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
271 /* Are the contents of the buffer in any way relevant? */
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
272 if((self->size > 0) && (sector >= self->pos) && (sector <= self->pos+self->size)) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
273 dprintf("Contents relevant ... adjusting\n");
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
274 self->read_point = sector;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
275 } else {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
276 /* Flush the cache as its not much use */
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
277 dprintf("Contents irrelevent... flushing\n");
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
278 self->size = 0;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
279 self->start = 0;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
280 self->pos = sector;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
281 self->read_point = sector;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
282 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
283
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
284 pthread_mutex_unlock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
285 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
286
114
b6834e6359cf big libdvdnav cleanup, quoting the ChangeLog:
mroi
parents: 108
diff changeset
287 /* This function will do the cache read */
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
288 int dvdnav_read_cache_block( read_cache_t *self, int sector, size_t block_count, uint8_t **buf) {
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
289 int result, diff;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
290
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
291 if(!self)
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
292 return 0;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
293
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
294 pthread_mutex_lock(&self->cache_lock);
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
295 dprintf("Read from %i -> +%i (buffer pos=%i, read_point=%i, size=%i)... ", sector, block_count,
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
296 self->pos, self->read_point, self->size);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
297 if((self->size > 0) && (sector >= self->read_point) &&
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
298 (sector + block_count <= self->pos + self->size)) {
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
299 /* Hit */
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
300
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
301 /* Drop any skipped blocks */
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
302 diff = sector - self->read_point;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
303 if(diff > 0)
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
304 self->read_point += diff;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
305
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
306 diff = self->read_point - self->pos;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
307
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
308 if(((self->start + diff) % CACHE_BUFFER_SIZE) + block_count <= CACHE_BUFFER_SIZE) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
309 dprintf("************** Single read\n");
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
310 memcpy(*buf, self->buffer + (((self->start + diff) % CACHE_BUFFER_SIZE) * DVD_VIDEO_LB_LEN),
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
311 block_count * DVD_VIDEO_LB_LEN);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
312 self->read_point += block_count;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
313 pthread_mutex_unlock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
314
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
315 return (int)block_count;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
316 } else {
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
317 int32_t boundary = CACHE_BUFFER_SIZE - self->start;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
318
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
319 dprintf("************** Multiple read\n");
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
320 memcpy(*buf, self->buffer + (((self->start + diff) % CACHE_BUFFER_SIZE) * DVD_VIDEO_LB_LEN),
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
321 boundary * DVD_VIDEO_LB_LEN);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
322 memcpy(*buf + (boundary * DVD_VIDEO_LB_LEN), self->buffer,
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
323 (block_count-boundary) * DVD_VIDEO_LB_LEN);
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
324 self->read_point += block_count;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
325 pthread_mutex_unlock(&self->cache_lock);
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
326
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
327 return (int)block_count;
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
328 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
329 } else {
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
330 /* Miss */
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
331
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
332 fprintf(MSG_OUT, "libdvdnav: DVD read cache miss! (not bad but a performance hit) sector=%d\n", sector);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
333 result = DVDReadBlocks( self->dvd_self->file, sector, block_count, *buf);
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
334 self->read_point = sector+block_count;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
335 if(self->read_point > self->pos + self->size) {
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
336 /* Flush the cache as its not much use */
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
337 dprintf("Contents irrelevent... flushing\n");
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
338 self->size = 0;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
339 self->start = 0;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
340 self->pos = sector+block_count;
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
341 }
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
342 pthread_mutex_unlock(&self->cache_lock);
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
343 usleep(300);
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
344 return result;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
345 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
346
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
347 /* Should never get here */
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
348 return 0;
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
349 }
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
350
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
351 dvdnav_status_t dvdnav_free_cache_block(dvdnav_t *self, unsigned char *buf) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
352 return DVDNAV_STATUS_OK;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
353 }
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
354
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
355 #else
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
356
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
357 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
358 read_cache_t *self;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
359 int i;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
360
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
361 self = (read_cache_t *)malloc(sizeof(read_cache_t));
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
362
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
363 if(self) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
364 self->current = 0;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
365 self->freeing = 0;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
366 self->dvd_self = dvd_self;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
367 self->last_sector = 0;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
368 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
369 self->read_ahead_incr = 0;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
370 pthread_mutex_init(&self->lock, NULL);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
371 dvdnav_read_cache_clear(self);
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
372 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
373 self->chunk[i].cache_buffer = NULL;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
374 self->chunk[i].usage_count = 0;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
375 }
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
376 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
377
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
378 return self;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
379 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
380
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
381 void dvdnav_read_cache_free(read_cache_t* self) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
382 dvdnav_t *tmp;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
383 int i;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
384
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
385 pthread_mutex_lock(&self->lock);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
386 self->freeing = 1;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
387 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
388 if (self->chunk[i].cache_buffer && self->chunk[i].usage_count == 0) {
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
389 free(self->chunk[i].cache_buffer_base);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
390 self->chunk[i].cache_buffer = NULL;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
391 }
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
392 pthread_mutex_unlock(&self->lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
393
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
394 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
395 if (self->chunk[i].cache_buffer) return;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
396
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
397 /* all buffers returned, free everything */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
398 tmp = self->dvd_self;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
399 pthread_mutex_destroy(&self->lock);
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
400 free(self);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
401 free(tmp);
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
402 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
403
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
404 /* This function MUST be called whenever self->file changes. */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
405 void dvdnav_read_cache_clear(read_cache_t *self) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
406 int i;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
407
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
408 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
409 return;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
410
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
411 pthread_mutex_lock(&self->lock);
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
412 for (i = 0; i < READ_CACHE_CHUNKS; i++)
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
413 self->chunk[i].cache_valid = 0;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
414 pthread_mutex_unlock(&self->lock);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
415 }
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
416
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
417 /* This function is called just after reading the NAV packet. */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
418 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
419 int i, use;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
420
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
421 if(!self)
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
422 return;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
423
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
424 if(!self->dvd_self->use_read_ahead)
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
425 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
426
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
427 pthread_mutex_lock(&self->lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
428
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
429 /* find a free cache chunk that best fits the required size */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
430 use = -1;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
431 for (i = 0; i < READ_CACHE_CHUNKS; i++)
61
6b7520caf9a1 fix stupid bug: test if the buffer is there before using it
mroi
parents: 60
diff changeset
432 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
6b7520caf9a1 fix stupid bug: test if the buffer is there before using it
mroi
parents: 60
diff changeset
433 self->chunk[i].cache_malloc_size >= block_count &&
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
434 (use == -1 || self->chunk[use].cache_malloc_size > self->chunk[i].cache_malloc_size))
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
435 use = i;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
436
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
437 if (use == -1) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
438 /* we haven't found a cache chunk, so we try to reallocate an existing one */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
439 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
440 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
441 (use == -1 || self->chunk[use].cache_malloc_size < self->chunk[i].cache_malloc_size))
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
442 use = i;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
443 if (use >= 0) {
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
444 self->chunk[use].cache_buffer_base = realloc(self->chunk[use].cache_buffer_base,
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
445 block_count * DVD_VIDEO_LB_LEN + ALIGNMENT);
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
446 self->chunk[use].cache_buffer =
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
447 (uint8_t *)(((int)self->chunk[use].cache_buffer_base & ~(ALIGNMENT - 1)) + ALIGNMENT);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
448 dprintf("pre_cache DVD read realloc happened\n");
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
449 self->chunk[use].cache_malloc_size = block_count;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
450 } else {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
451 /* we still haven't found a cache chunk, let's allocate a new one */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
452 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
453 if (!self->chunk[i].cache_buffer) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
454 use = i;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
455 break;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
456 }
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
457 if (use >= 0) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
458 /* We start with a sensible figure for the first malloc of 500 blocks.
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
459 * Some DVDs I have seen venture to 450 blocks.
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
460 * This is so that fewer realloc's happen if at all.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
461 */
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
462 self->chunk[i].cache_buffer_base =
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
463 malloc((block_count > 500 ? block_count : 500) * DVD_VIDEO_LB_LEN + ALIGNMENT);
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
464 self->chunk[i].cache_buffer =
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
465 (uint8_t *)(((int)self->chunk[i].cache_buffer_base & ~(ALIGNMENT - 1)) + ALIGNMENT);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
466 self->chunk[i].cache_malloc_size = block_count > 500 ? block_count : 500;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
467 dprintf("pre_cache DVD read malloc %d blocks\n",
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
468 (block_count > 500 ? block_count : 500 ));
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
469 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
470 }
48
9030797161f0 Compile why don't you. :-(
jcdutton
parents: 47
diff changeset
471 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
472
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
473 if (use >= 0) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
474 self->chunk[use].cache_start_sector = sector;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
475 self->chunk[use].cache_block_count = block_count;
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
476 self->chunk[use].cache_read_count = 0;
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
477 self->chunk[use].cache_valid = 1;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
478 self->current = use;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
479 } else {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
480 dprintf("pre_caching was impossible, no cache chunk available\n");
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
481 }
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
482 pthread_mutex_unlock(&self->lock);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
483 }
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
484
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
485 int dvdnav_read_cache_block(read_cache_t *self, int sector, size_t block_count, uint8_t **buf) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
486 int i, use;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
487 int start;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
488 int size;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
489 int incr;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
490 uint8_t *read_ahead_buf;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
491 int32_t res;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
492
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
493 if(!self)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
494 return 0;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
495
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
496 use = -1;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
497
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
498 if(self->dvd_self->use_read_ahead) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
499 /* first check, if sector is in current chunk */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
500 read_cache_chunk_t cur = self->chunk[self->current];
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
501 if (cur.cache_valid && sector >= cur.cache_start_sector &&
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
502 sector <= (cur.cache_start_sector + cur.cache_read_count) &&
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
503 sector + block_count <= cur.cache_start_sector + cur.cache_block_count)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
504 use = self->current;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
505 else
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
506 for (i = 0; i < READ_CACHE_CHUNKS; i++)
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
507 if (self->chunk[i].cache_valid &&
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
508 sector >= self->chunk[i].cache_start_sector &&
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
509 sector <= (self->chunk[i].cache_start_sector + self->chunk[i].cache_read_count) &&
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
510 sector + block_count <= self->chunk[i].cache_start_sector + self->chunk[i].cache_block_count)
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
511 use = i;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
512 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
513
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
514 if (use >= 0) {
108
293bb1798e86 Remove C++ism.
f1rmb
parents: 107
diff changeset
515 read_cache_chunk_t *chunk;
293bb1798e86 Remove C++ism.
f1rmb
parents: 107
diff changeset
516
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
517 /* Increment read-ahead size if sector follows the last sector */
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
518 if (sector == (self->last_sector + 1)) {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
519 self->read_ahead_incr++;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
520 } else {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
521 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
522 self->read_ahead_incr = 0;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
523 }
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
524 self->last_sector = sector;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
525
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
526 /* The following resources need to be protected by a mutex :
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
527 * self->chunk[*].cache_buffer
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
528 * self->chunk[*].cache_malloc_size
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
529 * self->chunk[*].usage_count
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
530 */
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
531 pthread_mutex_lock(&self->lock);
108
293bb1798e86 Remove C++ism.
f1rmb
parents: 107
diff changeset
532 chunk = &self->chunk[use];
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
533 read_ahead_buf = chunk->cache_buffer + chunk->cache_read_count * DVD_VIDEO_LB_LEN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
534 *buf = chunk->cache_buffer + (sector - chunk->cache_start_sector) * DVD_VIDEO_LB_LEN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
535 chunk->usage_count++;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
536 pthread_mutex_unlock(&self->lock);
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
537
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
538 /* Read blocks if needed */
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
539 if (sector >= (chunk->cache_start_sector + chunk->cache_read_count)) {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
540
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
541 dprintf("libdvdnav: sector=%d, start_sector=%d, last_sector=%d\n", sector, chunk->cache_start_sector, chunk->cache_start_sector + chunk->cache_block_count);
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
542
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
543 /* read_ahead_size */
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
544 incr = self->read_ahead_incr >> 1;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
545 if ((self->read_ahead_size + incr) > READ_AHEAD_SIZE_MAX) {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
546 self->read_ahead_size = READ_AHEAD_SIZE_MAX;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
547 } else {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
548 self->read_ahead_size = self->read_ahead_size + incr;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
549 }
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
550 self->read_ahead_incr = 0;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
551
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
552 /* real read size */
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
553 start = chunk->cache_start_sector + chunk->cache_read_count;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
554 if (chunk->cache_read_count + self->read_ahead_size > chunk->cache_block_count) {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
555 size = chunk->cache_block_count - chunk->cache_read_count;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
556 } else {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
557 size = self->read_ahead_size;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
558 }
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
559 dprintf("libdvdnav: read_ahead_size=%d, size=%d\n", self->read_ahead_size, size);
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
560
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
561 chunk->cache_read_count += DVDReadBlocks(self->dvd_self->file,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
562 start,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
563 size,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
564 read_ahead_buf);
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
565 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
566
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
567 res = DVD_VIDEO_LB_LEN * block_count;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
568
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
569 } else {
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
570
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
571 if (self->dvd_self->use_read_ahead)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
572 dprintf("cache miss on sector %d\n", sector);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
573
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
574 res = DVDReadBlocks(self->dvd_self->file,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
575 sector,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
576 block_count,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
577 *buf) * DVD_VIDEO_LB_LEN;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
578 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
579
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
580 return res;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
581
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
582 }
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
583
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
584 dvdnav_status_t dvdnav_free_cache_block(dvdnav_t *self, unsigned char *buf) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
585 read_cache_t *cache;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
586 int i;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
587
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
588 if (!self)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
589 return DVDNAV_STATUS_ERR;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
590
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
591 cache = self->cache;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
592 if (!cache)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
593 return DVDNAV_STATUS_ERR;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
594
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
595 pthread_mutex_lock(&cache->lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
596 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
597 if (cache->chunk[i].cache_buffer && buf >= cache->chunk[i].cache_buffer &&
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
598 buf < cache->chunk[i].cache_buffer + cache->chunk[i].cache_malloc_size * DVD_VIDEO_LB_LEN) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
599 cache->chunk[i].usage_count--;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
600 }
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
601 }
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
602 pthread_mutex_unlock(&cache->lock);
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
603
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
604 if (cache->freeing)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
605 /* when we want to dispose the cache, try freeing it now */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
606 dvdnav_read_cache_free(cache);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
607
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
608 return DVDNAV_STATUS_OK;
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
609 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
610
48
9030797161f0 Compile why don't you. :-(
jcdutton
parents: 47
diff changeset
611 #endif