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 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 )
267 // delete oldest of at least 2 cache files
269 remove_pointer(oldest);
273 // settle for just deleting one frame
274 else if( first && !first->checked_out )
275 result = first->file->delete_oldest();
276 total_lock->unlock();
278 oldest->Garbage::remove_user();
282 void CICache::dump(FILE *fp)
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)
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);
293 total_lock->unlock();
297 CICacheItem::CICacheItem()
298 : Garbage("CICacheItem"), ListItem<CICacheItem>()
303 CICacheItem::CICacheItem(CICache *cache, EDL *edl, Asset *asset)
304 : Garbage("CICacheItem"), ListItem<CICacheItem>()
306 age = EDL::next_id();
308 this->asset = new Asset;
310 item_lock = new Condition(1, "CICacheItem::item_lock", 0);
313 // Must copy Asset since this belongs to an EDL which won't exist forever.
314 this->asset->copy_from(asset, 1);
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);
331 CICacheItem::~CICacheItem()
333 if(file) delete file;
334 if(asset) asset->Garbage::remove_user();
335 if(item_lock) delete item_lock;