vicon checkout fix, checkout tweak, remove_all/delete_oldest rework, ffmpeg seek...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / cache.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "assets.h"
24 #include "bcsignals.h"
25 #include "cache.h"
26 #include "condition.h"
27 #include "datatype.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "file.h"
31 #include "filesystem.h"
32 #include "mutex.h"
33 #include "thread.h"
34 #include "preferences.h"
35
36 #include <string.h>
37
38 // edl came from a command which won't exist anymore
39 CICache::CICache(Preferences *preferences)
40  : List<CICacheItem>()
41 {
42         this->preferences = preferences;
43         edl = 0;
44         check_out_lock = new Condition(0, "CICache::check_out_lock", 0);
45         total_lock = new Mutex("CICache::total_lock");
46         check_outs = 0;
47 }
48
49 CICache::~CICache()
50 {
51         while(last)
52         {
53                 CICacheItem *item = last;
54 //printf("CICache::~CICache: %s\n", item->asset->path);
55                 remove_pointer(item);
56                 item->Garbage::remove_user();
57         }
58         delete check_out_lock;
59         delete total_lock;
60 }
61
62
63
64
65
66
67 File* CICache::check_out(Asset *asset, EDL *edl, int block)
68 {
69         CICacheItem *current = 0;
70         long tid = (long)Thread::get_self();
71         if( !tid ) tid = 1;
72
73         while(1)
74         {
75                 File *file = 0;
76                 total_lock->lock("CICache::check_out");
77 // Scan directory for item
78                 current = first;
79                 while(current && strcmp(current->asset->path, asset->path) != 0)
80                         current = NEXT;
81                 if(!current) { // Create new item
82                         current = new CICacheItem(this, edl, asset);
83                         append(current);  current->checked_out = tid;
84                         total_lock->unlock();
85                         file = current->file;
86                         int result = file->open_file(preferences, asset, 1, 0);
87                         if( result ) { delete file; file = 0; }
88                         total_lock->lock("CICache::check_out 2");
89                         if( !file ) {
90                                 remove_pointer(current);
91                                 current->file = 0;
92                                 current->Garbage::remove_user();
93                                 current = 0;
94                         }
95                         else
96                                 current->Garbage::add_user();
97                 }
98                 else {
99                         file = current->file;
100                         if( !current->checked_out ) {
101 // Return existing/new item
102                                 current->Garbage::add_user();
103                                 current->age = EDL::next_id();
104                                 current->checked_out = tid;
105                         }
106                         else if( current->checked_out == tid )
107                                 ++check_outs;
108                         else
109                                 current = 0;
110                 }
111                 total_lock->unlock();
112                 if( current || !file || !block ) break;
113 // Try again after blocking
114                 check_out_lock->lock("CICache::check_out");
115         }
116
117 //printf("check out %p %lx %s\n", current, tid, asset->path);
118         return current ? current->file : 0;
119 }
120
121 int CICache::check_in(Asset *asset)
122 {
123         total_lock->lock("CICache::check_in");
124         CICacheItem *current = 0;
125         if( !check_outs ) {
126                 current = first;
127                 while(current && strcmp(current->asset->path, asset->path) != 0)
128                         current = NEXT;
129                 if(current && current->checked_out) {
130                         current->checked_out = 0;
131                         current->Garbage::remove_user();
132                 }
133         }
134         else
135                 --check_outs;
136         total_lock->unlock();
137
138 // Release for blocking check_out operations
139         check_out_lock->unlock();
140 //printf("check  in %p %lx %s\n", current, (long)Thread::get_self(), asset->path);
141         age();
142         return 0;
143 }
144
145 void CICache::remove_all()
146 {
147         List<CICacheItem> removed;
148         for(;;) {
149                 total_lock->lock("CICache::remove_all");
150                 CICacheItem *current = first;
151                 while( current ) {
152                         CICacheItem *next_item = current->next;
153                         if( !current->checked_out ) {
154                                 remove_pointer(current);
155                                 removed.append(current);
156                         }
157                         current = next_item;
158                 }
159                 total_lock->unlock();
160                 while( removed.first ) {
161                         CICacheItem *current = removed.first;
162                         removed.remove_pointer(current);
163                         current->Garbage::remove_user();
164                 }
165                 if( !first ) break;
166                 check_out_lock->lock();
167         }
168 }
169
170 int CICache::delete_entry(char *path)
171 {
172         CICacheItem *current = 0;
173         for( ;; ) {
174                 total_lock->lock("CICache::delete_entry");
175                 current = first;
176                 while( current && strcmp(current->asset->path, path) !=0 )
177                         current = NEXT;
178                 if( !current ) break;
179                 if( current->checked_out ) {
180                         remove_pointer(current);
181                         break;
182                 }
183                 total_lock->unlock();
184                 check_out_lock->lock();
185         }
186         total_lock->unlock();
187         if( current )
188                 current->Garbage::remove_user();
189         return 0;
190 }
191
192 int CICache::delete_entry(Asset *asset)
193 {
194         return delete_entry(asset->path);
195 }
196
197 int CICache::age()
198 {
199 // delete old assets if memory usage is exceeded
200         int64_t prev_memory_usage = 0;
201         int result = 0;
202         while( !result ) {
203                 int64_t memory_usage = get_memory_usage(1);
204                 if( prev_memory_usage == memory_usage ) break;
205                 if( preferences->cache_size >= memory_usage ) break;
206 //printf("CICache::age 3 %p %jd %jd\n", this, memory_usage, preferences->cache_size);
207                 prev_memory_usage = memory_usage;
208                 result = delete_oldest();
209         }
210         return result;
211 }
212
213 int64_t CICache::get_memory_usage(int use_lock)
214 {
215         CICacheItem *current;
216         int64_t result = 0;
217         if(use_lock) total_lock->lock("CICache::get_memory_usage");
218         for(current = first; current; current = NEXT)
219         {
220                 File *file = current->file;
221                 if(file) result += file->get_memory_usage();
222         }
223         if(use_lock) total_lock->unlock();
224         return result;
225 }
226
227 int CICache::get_oldest()
228 {
229         CICacheItem *current;
230         int oldest = 0x7fffffff;
231         total_lock->lock("CICache::get_oldest");
232         for(current = last; current; current = PREVIOUS)
233         {
234                 if(current->age < oldest)
235                 {
236                         oldest = current->age;
237                 }
238         }
239         total_lock->unlock();
240
241         return oldest;
242 }
243
244 int CICache::delete_oldest()
245 {
246         int result = 0;
247         total_lock->lock("CICache::delete_oldest");
248         CICacheItem *oldest = 0;
249 // at least 2
250         if( first != last ) {
251                 oldest = first;
252                 CICacheItem *current = oldest->next;
253                 while( current ) {
254                         if( current->age < oldest->age )
255                                 oldest = current;
256                         current = current->next;
257                 }
258         }
259 // settle for just deleting one frame
260         else if( first )
261                 result = first->file->delete_oldest();
262         if( oldest ) {
263 // Got the oldest file.  Try requesting cache purge from it.
264                 if( oldest->file )
265                         oldest->file->purge_cache();
266 // Delete the file if cache already empty and not checked out.
267                 if( !oldest->checked_out )
268                         remove_pointer(oldest);
269                 else
270                         oldest = 0;
271         }
272         total_lock->unlock();
273         if( oldest ) {
274                 oldest->Garbage::remove_user();
275                 result = 1;
276         }
277         return result;
278 }
279
280 void CICache::dump(FILE *fp)
281 {
282         CICacheItem *current;
283         total_lock->lock("CICache::dump");
284         fprintf(fp, "CICache::dump total size %jd\n", get_memory_usage(0));
285         for(current = first; current; current = NEXT)
286         {
287                 fprintf(fp, "cache item %p asset %p %s age=%d checked_out=%p\n",
288                         current, current->asset, current->asset->path,
289                         current->age, (void*)current->checked_out);
290         }
291         total_lock->unlock();
292 }
293
294
295 CICacheItem::CICacheItem()
296 : Garbage("CICacheItem"), ListItem<CICacheItem>()
297 {
298 }
299
300
301 CICacheItem::CICacheItem(CICache *cache, EDL *edl, Asset *asset)
302  : Garbage("CICacheItem"), ListItem<CICacheItem>()
303 {
304         age = EDL::next_id();
305
306         this->asset = new Asset;
307
308         item_lock = new Condition(1, "CICacheItem::item_lock", 0);
309
310
311 // Must copy Asset since this belongs to an EDL which won't exist forever.
312         this->asset->copy_from(asset, 1);
313         this->cache = cache;
314         checked_out = 0;
315
316         file = new File;
317         int cpus = cache->preferences->processors;
318         file->set_processors(cpus);
319         file->set_preload(edl->session->playback_preload);
320         file->set_subtitle(edl->session->decode_subtitles ?
321                 edl->session->subtitle_number : -1);
322         file->set_program(edl->session->program_no);
323         file->set_interpolate_raw(edl->session->interpolate_raw);
324         file->set_white_balance_raw(edl->session->white_balance_raw);
325
326 SET_TRACE
327 }
328
329 CICacheItem::~CICacheItem()
330 {
331         if(file) delete file;
332         if(asset) asset->Garbage::remove_user();
333         if(item_lock) delete item_lock;
334 }