mostly DPX list read inclusion
[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  : Garbage("CICache"), 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         total_lock->lock("CICache::check_out");
73         add_user();
74
75         while( users > 1 ) {
76                 File *file = 0;
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                 if( current || !file || !block ) break;
112 // Try again after blocking
113                 total_lock->unlock();
114                 check_out_lock->lock("CICache::check_out");
115                 total_lock->lock("CICache::check_out");
116         }
117
118 // cache deleted during checkout, destroy this
119         if( users == 1 ) {
120                 remove_user();
121                 return 0;
122         }
123
124         remove_user();
125         total_lock->unlock();
126 //printf("check out %p %lx %s\n", current, tid, asset->path);
127         return current ? current->file : 0;
128 }
129
130 int CICache::check_in(Asset *asset)
131 {
132         total_lock->lock("CICache::check_in");
133         CICacheItem *current = 0;
134         if( !check_outs ) {
135                 current = first;
136                 while(current && strcmp(current->asset->path, asset->path) != 0)
137                         current = NEXT;
138                 if(current && current->checked_out) {
139                         current->checked_out = 0;
140                         current->Garbage::remove_user();
141                 }
142         }
143         else
144                 --check_outs;
145         total_lock->unlock();
146
147 // Release for blocking check_out operations
148         check_out_lock->unlock();
149 //printf("check  in %p %lx %s\n", current, (long)Thread::get_self(), asset->path);
150         age();
151         return 0;
152 }
153
154 void CICache::remove_all()
155 {
156         List<CICacheItem> removed;
157         for(;;) {
158                 total_lock->lock("CICache::remove_all");
159                 CICacheItem *current = first;
160                 while( current ) {
161                         CICacheItem *next_item = current->next;
162                         if( !current->checked_out ) {
163                                 remove_pointer(current);
164                                 removed.append(current);
165                         }
166                         current = next_item;
167                 }
168                 total_lock->unlock();
169                 while( removed.first ) {
170                         CICacheItem *current = removed.first;
171                         removed.remove_pointer(current);
172                         current->Garbage::remove_user();
173                 }
174                 if( !first ) break;
175                 check_out_lock->lock();
176         }
177 }
178
179 int CICache::delete_entry(char *path)
180 {
181         CICacheItem *current = 0;
182         for( ;; ) {
183                 total_lock->lock("CICache::delete_entry");
184                 current = first;
185                 while( current && strcmp(current->asset->path, path) !=0 )
186                         current = NEXT;
187                 if( !current ) break;
188                 if( !current->checked_out ) {
189                         remove_pointer(current);
190                         break;
191                 }
192                 total_lock->unlock();
193                 check_out_lock->lock();
194         }
195         total_lock->unlock();
196         if( current )
197                 current->Garbage::remove_user();
198         return 0;
199 }
200
201 int CICache::delete_entry(Asset *asset)
202 {
203         return delete_entry(asset->path);
204 }
205
206 int CICache::age()
207 {
208 // delete old assets if memory usage is exceeded
209         int64_t prev_memory_usage = 0;
210         int result = 0;
211         while( !result ) {
212                 int64_t memory_usage = get_memory_usage(1);
213                 if( prev_memory_usage == memory_usage ) break;
214                 if( preferences->cache_size >= memory_usage ) break;
215 //printf("CICache::age 3 %p %jd %jd\n", this, memory_usage, preferences->cache_size);
216                 prev_memory_usage = memory_usage;
217                 result = delete_oldest();
218         }
219         return result;
220 }
221
222 int64_t CICache::get_memory_usage(int use_lock)
223 {
224         CICacheItem *current;
225         int64_t result = 0;
226         if(use_lock) total_lock->lock("CICache::get_memory_usage");
227         for(current = first; current; current = NEXT)
228         {
229                 File *file = current->file;
230                 if(file) result += file->get_memory_usage();
231         }
232         if(use_lock) total_lock->unlock();
233         return result;
234 }
235
236 int CICache::get_oldest()
237 {
238         CICacheItem *current;
239         int oldest = 0x7fffffff;
240         total_lock->lock("CICache::get_oldest");
241         for(current = last; current; current = PREVIOUS)
242         {
243                 if(current->age < oldest)
244                 {
245                         oldest = current->age;
246                 }
247         }
248         total_lock->unlock();
249
250         return oldest;
251 }
252
253 int CICache::delete_oldest()
254 {
255         int result = 0;
256         CICacheItem *oldest = 0;
257         total_lock->lock("CICache::delete_oldest");
258         if( first != last ) { // at least 2
259                 CICacheItem *current = first;
260                 for( ; !oldest && current; current=current->next )
261                         if( !current->checked_out ) oldest = current;
262                 for( ; current; current=current->next ) {
263                         if( current->checked_out ) continue;
264                         if( current->age < oldest->age )
265                                 oldest = current;
266                 }
267 // delete oldest of at least 2 cache files
268                 if( oldest ) {
269                         remove_pointer(oldest);
270                         result = 1;
271                 }
272         }
273 // settle for just deleting one frame
274         else if( first && !first->checked_out )
275                 result = first->file->delete_oldest();
276         total_lock->unlock();
277         if( oldest )
278                 oldest->Garbage::remove_user();
279         return result;
280 }
281
282 void CICache::dump(FILE *fp)
283 {
284         CICacheItem *current;
285         total_lock->lock("CICache::dump");
286         fprintf(fp, "CICache::dump total size %jd\n", get_memory_usage(0));
287         for(current = first; current; current = NEXT)
288         {
289                 fprintf(fp, "cache item %p asset %p %s age=%d checked_out=%p\n",
290                         current, current->asset, current->asset->path,
291                         current->age, (void*)current->checked_out);
292         }
293         total_lock->unlock();
294 }
295
296
297 CICacheItem::CICacheItem()
298 : Garbage("CICacheItem"), ListItem<CICacheItem>()
299 {
300 }
301
302
303 CICacheItem::CICacheItem(CICache *cache, EDL *edl, Asset *asset)
304  : Garbage("CICacheItem"), ListItem<CICacheItem>()
305 {
306         age = EDL::next_id();
307
308         this->asset = new Asset;
309
310         item_lock = new Condition(1, "CICacheItem::item_lock", 0);
311
312
313 // Must copy Asset since this belongs to an EDL which won't exist forever.
314         this->asset->copy_from(asset, 1);
315         this->cache = cache;
316         checked_out = 0;
317
318         file = new File;
319         int cpus = cache->preferences->processors;
320         file->set_processors(cpus);
321         file->set_preload(edl->session->playback_preload);
322         file->set_subtitle(edl->session->decode_subtitles ?
323                 edl->session->subtitle_number : -1);
324         file->set_program(edl->session->program_no);
325         file->set_interpolate_raw(edl->session->interpolate_raw);
326         file->set_white_balance_raw(edl->session->white_balance_raw);
327
328 SET_TRACE
329 }
330
331 CICacheItem::~CICacheItem()
332 {
333         if(file) delete file;
334         if(asset) asset->Garbage::remove_user();
335         if(item_lock) delete item_lock;
336 }