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)
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();
76 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 total_lock->unlock();
112 if( current || !file || !block ) break;
113 // Try again after blocking
114 check_out_lock->lock("CICache::check_out");
117 //printf("check out %p %lx %s\n", current, tid, asset->path);
118 return current ? current->file : 0;
121 int CICache::check_in(Asset *asset)
123 total_lock->lock("CICache::check_in");
124 CICacheItem *current = 0;
127 while(current && strcmp(current->asset->path, asset->path) != 0)
129 if(current && current->checked_out) {
130 current->checked_out = 0;
131 current->Garbage::remove_user();
136 total_lock->unlock();
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);
145 void CICache::remove_all()
147 List<CICacheItem> removed;
149 total_lock->lock("CICache::remove_all");
150 CICacheItem *current = first;
152 CICacheItem *next_item = current->next;
153 if( !current->checked_out ) {
154 remove_pointer(current);
155 removed.append(current);
159 total_lock->unlock();
160 while( removed.first ) {
161 CICacheItem *current = removed.first;
162 removed.remove_pointer(current);
163 current->Garbage::remove_user();
166 check_out_lock->lock();
170 int CICache::delete_entry(char *path)
172 CICacheItem *current = 0;
174 total_lock->lock("CICache::delete_entry");
176 while( current && strcmp(current->asset->path, path) !=0 )
178 if( !current ) break;
179 if( !current->checked_out ) {
180 remove_pointer(current);
183 total_lock->unlock();
184 check_out_lock->lock();
186 total_lock->unlock();
188 current->Garbage::remove_user();
192 int CICache::delete_entry(Asset *asset)
194 return delete_entry(asset->path);
199 // delete old assets if memory usage is exceeded
200 int64_t prev_memory_usage = 0;
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();
213 int64_t CICache::get_memory_usage(int use_lock)
215 CICacheItem *current;
217 if(use_lock) total_lock->lock("CICache::get_memory_usage");
218 for(current = first; current; current = NEXT)
220 File *file = current->file;
221 if(file) result += file->get_memory_usage();
223 if(use_lock) total_lock->unlock();
227 int CICache::get_oldest()
229 CICacheItem *current;
230 int oldest = 0x7fffffff;
231 total_lock->lock("CICache::get_oldest");
232 for(current = last; current; current = PREVIOUS)
234 if(current->age < oldest)
236 oldest = current->age;
239 total_lock->unlock();
244 int CICache::delete_oldest()
247 total_lock->lock("CICache::delete_oldest");
248 CICacheItem *oldest = 0;
250 if( first != last ) {
252 CICacheItem *current = oldest->next;
254 if( current->age < oldest->age )
256 current = current->next;
259 // settle for just deleting one frame
261 result = first->file->delete_oldest();
263 // Got the oldest file. Try requesting cache purge from it.
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);
272 total_lock->unlock();
274 oldest->Garbage::remove_user();
280 void CICache::dump(FILE *fp)
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)
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);
291 total_lock->unlock();
295 CICacheItem::CICacheItem()
296 : Garbage("CICacheItem"), ListItem<CICacheItem>()
301 CICacheItem::CICacheItem(CICache *cache, EDL *edl, Asset *asset)
302 : Garbage("CICacheItem"), ListItem<CICacheItem>()
304 age = EDL::next_id();
306 this->asset = new Asset;
308 item_lock = new Condition(1, "CICacheItem::item_lock", 0);
311 // Must copy Asset since this belongs to an EDL which won't exist forever.
312 this->asset->copy_from(asset, 1);
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);
329 CICacheItem::~CICacheItem()
331 if(file) delete file;
332 if(asset) asset->Garbage::remove_user();
333 if(item_lock) delete item_lock;