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");
52 CICacheItem *item = last;
53 //printf("CICache::~CICache: %s\n", item->asset->path);
55 item->Garbage::remove_user();
57 delete check_out_lock;
66 File* CICache::check_out(Asset *asset, EDL *edl, int block)
68 CICacheItem *current = 0;
69 long tid = (long)Thread::get_self();
75 total_lock->lock("CICache::check_out");
76 // Scan directory for item
78 while(current && strcmp(current->asset->path, asset->path) != 0)
80 if(!current) { // Create new item
81 current = new CICacheItem(this, edl, asset);
82 append(current); current->checked_out = tid;
85 int result = file->open_file(preferences, asset, 1, 0);
91 total_lock->lock("CICache::check_out 2");
93 remove_pointer(current);
95 current->Garbage::remove_user();
99 current->Garbage::add_user();
102 file = current->file;
103 if(!current->checked_out) {
104 // Return existing/new item
105 current->Garbage::add_user();
106 current->age = EDL::next_id();
107 current->checked_out = tid;
112 total_lock->unlock();
113 if(current || !file || !block) break;
114 // Try again after blocking
115 check_out_lock->lock("CICache::check_out");
118 //printf("check out %p %lx %s\n", current, tid, asset->path);
119 return current ? current->file : 0;
122 int CICache::check_in(Asset *asset)
124 total_lock->lock("CICache::check_in");
125 CICacheItem *current = first;
126 while(current && strcmp(current->asset->path, asset->path) != 0)
128 if(current && current->checked_out) {
129 current->checked_out = 0;
130 current->Garbage::remove_user();
132 total_lock->unlock();
134 // Release for blocking check_out operations
135 check_out_lock->unlock();
136 //printf("check in %p %lx %s\n", current, (long)Thread::get_self(), asset->path);
141 void CICache::remove_all()
143 CICacheItem *current, *temp;
144 List<CICacheItem> removed;
145 total_lock->lock("CICache::remove_all");
146 for(current=first; current; current=temp)
149 // Must not be checked out because we need the pointer to check back in.
150 // Really need to give the user the CacheItem.
151 if(!current->checked_out)
153 //printf("CICache::remove_all: %s\n", current->asset->path);
154 remove_pointer(current);
155 removed.append(current);
158 total_lock->unlock();
159 while( (current=removed.first) != 0 )
161 removed.remove_pointer(current);
162 current->Garbage::remove_user();
166 int CICache::delete_entry(char *path)
168 total_lock->lock("CICache::delete_entry");
169 CICacheItem *current = first;
170 while( current && strcmp(current->asset->path, path) !=0 )
172 if(current && !current->checked_out)
173 remove_pointer(current);
176 //printf("CICache::delete_entry: %s\n", current->asset->path);
177 total_lock->unlock();
179 current->Garbage::remove_user();
183 int CICache::delete_entry(Asset *asset)
185 return delete_entry(asset->path);
190 // delete old assets if memory usage is exceeded
191 int64_t prev_memory_usage = 0;
194 int64_t memory_usage = get_memory_usage(1);
195 if( prev_memory_usage == memory_usage ) break;
196 if( preferences->cache_size >= memory_usage ) break;
197 //printf("CICache::age 3 %p %jd %jd\n", this, memory_usage, preferences->cache_size);
198 prev_memory_usage = memory_usage;
199 result = delete_oldest();
204 int64_t CICache::get_memory_usage(int use_lock)
206 CICacheItem *current;
208 if(use_lock) total_lock->lock("CICache::get_memory_usage");
209 for(current = first; current; current = NEXT)
211 File *file = current->file;
212 if(file) result += file->get_memory_usage();
214 if(use_lock) total_lock->unlock();
218 int CICache::get_oldest()
220 CICacheItem *current;
221 int oldest = 0x7fffffff;
222 total_lock->lock("CICache::get_oldest");
223 for(current = last; current; current = PREVIOUS)
225 if(current->age < oldest)
227 oldest = current->age;
230 total_lock->unlock();
235 int CICache::delete_oldest()
238 total_lock->lock("CICache::delete_oldest");
239 CICacheItem *oldest = 0;
241 if( first != last ) {
242 CICacheItem *current = first;
244 while( (current=NEXT) != 0 ) {
245 if( current->age < oldest->age )
248 // Got the oldest file. Try requesting cache purge from it.
250 oldest->file->purge_cache();
251 // Delete the file if cache already empty and not checked out.
252 if( !oldest->checked_out )
253 remove_pointer(oldest);
257 // settle for just deleting one frame
259 result = first->file->delete_oldest();
260 total_lock->unlock();
262 oldest->Garbage::remove_user();
270 CICacheItem *current;
271 total_lock->lock("CICache::dump");
272 printf("CICache::dump total size %jd\n", get_memory_usage(0));
273 for(current = first; current; current = NEXT)
275 printf("cache item %p asset %p %s age=%d\n",
276 current, current->asset,
277 current->asset->path, current->age);
279 total_lock->unlock();
283 CICacheItem::CICacheItem()
284 : Garbage("CICacheItem"), ListItem<CICacheItem>()
289 CICacheItem::CICacheItem(CICache *cache, EDL *edl, Asset *asset)
290 : Garbage("CICacheItem"), ListItem<CICacheItem>()
292 age = EDL::next_id();
294 this->asset = new Asset;
296 item_lock = new Condition(1, "CICacheItem::item_lock", 0);
299 // Must copy Asset since this belongs to an EDL which won't exist forever.
300 this->asset->copy_from(asset, 1);
305 int cpus = cache->preferences->processors;
306 file->set_processors(cpus);
307 file->set_preload(edl->session->playback_preload);
308 file->set_subtitle(edl->session->decode_subtitles ?
309 edl->session->subtitle_number : -1);
310 file->set_program(edl->session->program_no);
311 file->set_interpolate_raw(edl->session->interpolate_raw);
312 file->set_white_balance_raw(edl->session->white_balance_raw);
317 CICacheItem::~CICacheItem()
319 if(file) delete file;
320 if(asset) asset->Garbage::remove_user();
321 if(item_lock) delete item_lock;