4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
24 #include "bcsignals.h"
26 #include "condition.h"
29 #include "edlsession.h"
31 #include "filesystem.h"
34 #include "preferences.h"
38 // edl came from a command which won't exist anymore
39 CICache::CICache(Preferences *preferences)
40 : Garbage("CICache"), List<CICacheItem>()
42 this->preferences = preferences;
44 check_out_lock = new Condition(0, "CICache::check_out_lock", 0);
45 total_lock = new Mutex("CICache::total_lock");
53 CICacheItem *item = last;
54 //printf("CICache::~CICache: %s\n", item->asset->path);
56 item->Garbage::remove_user();
58 delete check_out_lock;
67 File* CICache::check_out(Asset *asset, EDL *edl, int block)
69 CICacheItem *current = 0;
70 long tid = (long)Thread::get_self();
72 total_lock->lock("CICache::check_out");
77 // Scan directory for item
79 while(current && strcmp(current->asset->path, asset->path) != 0)
81 if(!current) { // Create new item
82 current = new CICacheItem(this, edl, asset);
83 append(current); current->checked_out = tid;
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");
90 remove_pointer(current);
92 current->Garbage::remove_user();
96 current->Garbage::add_user();
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;
106 else if( current->checked_out == tid )
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");
118 // cache deleted during checkout, destroy this
125 total_lock->unlock();
126 //printf("check out %p %lx %s\n", current, tid, asset->path);
127 return current ? current->file : 0;
130 int CICache::check_in(Asset *asset)
132 total_lock->lock("CICache::check_in");
133 CICacheItem *current = 0;
136 while(current && strcmp(current->asset->path, asset->path) != 0)
138 if(current && current->checked_out) {
139 current->checked_out = 0;
140 current->Garbage::remove_user();
145 total_lock->unlock();
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);
154 void CICache::remove_all()
156 List<CICacheItem> removed;
158 total_lock->lock("CICache::remove_all");
159 CICacheItem *current = first;
161 CICacheItem *next_item = current->next;
162 if( !current->checked_out ) {
163 remove_pointer(current);
164 removed.append(current);
168 total_lock->unlock();
169 while( removed.first ) {
170 CICacheItem *current = removed.first;
171 removed.remove_pointer(current);
172 current->Garbage::remove_user();
175 check_out_lock->lock();
179 int CICache::delete_entry(char *path)
181 CICacheItem *current = 0;
183 total_lock->lock("CICache::delete_entry");
185 while( current && strcmp(current->asset->path, path) !=0 )
187 if( !current ) break;
188 if( !current->checked_out ) {
189 remove_pointer(current);
192 total_lock->unlock();
193 check_out_lock->lock();
195 total_lock->unlock();
197 current->Garbage::remove_user();
201 int CICache::delete_entry(Asset *asset)
203 return delete_entry(asset->path);
208 // delete old assets if memory usage is exceeded
209 int64_t prev_memory_usage = 0;
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();
222 int64_t CICache::get_memory_usage(int use_lock)
224 CICacheItem *current;
226 if(use_lock) total_lock->lock("CICache::get_memory_usage");
227 for(current = first; current; current = NEXT)
229 File *file = current->file;
230 if(file) result += file->get_memory_usage();
232 if(use_lock) total_lock->unlock();
236 int CICache::get_oldest()
238 CICacheItem *current;
239 int oldest = 0x7fffffff;
240 total_lock->lock("CICache::get_oldest");
241 for(current = last; current; current = PREVIOUS)
243 if(current->age < oldest)
245 oldest = current->age;
248 total_lock->unlock();
253 int CICache::delete_oldest()
256 total_lock->lock("CICache::delete_oldest");
257 CICacheItem *oldest = 0;
259 if( first != last ) {
261 CICacheItem *current = oldest->next;
263 if( current->age < oldest->age )
265 current = current->next;
268 // settle for just deleting one frame
270 result = first->file->delete_oldest();
272 // Got the oldest file. Try requesting cache purge from it.
274 oldest->file->purge_cache();
275 // Delete the file if cache already empty and not checked out.
276 if( !oldest->checked_out )
277 remove_pointer(oldest);
281 total_lock->unlock();
283 oldest->Garbage::remove_user();
289 void CICache::dump(FILE *fp)
291 CICacheItem *current;
292 total_lock->lock("CICache::dump");
293 fprintf(fp, "CICache::dump total size %jd\n", get_memory_usage(0));
294 for(current = first; current; current = NEXT)
296 fprintf(fp, "cache item %p asset %p %s age=%d checked_out=%p\n",
297 current, current->asset, current->asset->path,
298 current->age, (void*)current->checked_out);
300 total_lock->unlock();
304 CICacheItem::CICacheItem()
305 : Garbage("CICacheItem"), ListItem<CICacheItem>()
310 CICacheItem::CICacheItem(CICache *cache, EDL *edl, Asset *asset)
311 : Garbage("CICacheItem"), ListItem<CICacheItem>()
313 age = EDL::next_id();
315 this->asset = new Asset;
317 item_lock = new Condition(1, "CICacheItem::item_lock", 0);
320 // Must copy Asset since this belongs to an EDL which won't exist forever.
321 this->asset->copy_from(asset, 1);
326 int cpus = cache->preferences->processors;
327 file->set_processors(cpus);
328 file->set_preload(edl->session->playback_preload);
329 file->set_subtitle(edl->session->decode_subtitles ?
330 edl->session->subtitle_number : -1);
331 file->set_program(edl->session->program_no);
332 file->set_interpolate_raw(edl->session->interpolate_raw);
333 file->set_white_balance_raw(edl->session->white_balance_raw);
338 CICacheItem::~CICacheItem()
340 if(file) delete file;
341 if(asset) asset->Garbage::remove_user();
342 if(item_lock) delete item_lock;