42void *thread_clean(
void *ptr ) {
45 GlobalCache::instance()->clean();
50 pthread_create(&thread1,NULL,&thread_clean,NULL);
55GlobalCache *GlobalCache::global_cache = 0;
57GlobalCache::GlobalCache()
63GlobalCache::~GlobalCache()
66 pthread_mutex_destroy(&mutex);
69GlobalCache *GlobalCache::instance()
72 global_cache =
new GlobalCache();
77ImageIO *GlobalCache::get_imageio(
const string & filename,
int rw)
79 pthread_mutex_lock(&mutex);
81 printf(
"get_imageio: filename: %s, rw: %d\n", filename.c_str(), rw);
84 if (file_imageio.count(filename) == 0) {
86 pthread_mutex_unlock(&mutex);
90 ImageIO *io = file_imageio[filename];
92 printf(
"get_imageio: found cached; refs: %d, current rw: %d, request rw: %d\n", file_ref[filename], file_rw[filename], rw);
95 file_time[filename] = time(0);
97 if (file_rw[filename] == rw) {
101 }
else if (file_rw[filename] == 2 && rw == 1) {
103 file_ref[filename]++;
105 }
else if (file_ref[filename] == 0) {
108 this->delete_imageio(filename);
113 file_ref[filename]++;
114 printf(
"GlobalCache::get_imageio: %s: Cannot switch mode %d to %d with open references!\n", filename.c_str(), file_rw[filename], rw);
116 pthread_mutex_unlock(&mutex);
120void GlobalCache::delete_imageio(
const string & filename) {
122 if (file_imageio.count(filename) == 0) {
124 }
else if (file_ref[filename] == 0) {
125 ImageIO *io = file_imageio[filename];
126 file_imageio.erase(filename);
131void GlobalCache::close_imageio(
const string & filename) {
132 pthread_mutex_lock(&mutex);
134 printf(
"close_imageio: filename: %s\n", filename.c_str());
136 if (file_imageio.count(filename) > 0) {
137 ImageIO *io = file_imageio[filename];
139 file_ref[filename]--;
140 file_time[filename] = time(0);
142 if (file_ref[filename] == 0 && file_persist[filename] == 0) {
144 this->delete_imageio(filename);
149 pthread_mutex_unlock(&mutex);
152void GlobalCache::add_imageio(
const string & filename,
int rw,
int persist,
ImageIO * io)
154 pthread_mutex_lock(&mutex);
156 printf(
"add_imageio: filename %s, rw %d, persist %d\n", filename.c_str(), rw, persist);
158 if (file_imageio.count(filename) > 0) {
161 }
else if (io && persist > 0) {
163 file_imageio[filename] = io;
164 file_ref[filename] = 1;
165 file_rw[filename] = rw;
166 file_time[filename] = time(0);
167 file_persist[filename] = persist;
169 pthread_mutex_unlock(&mutex);
172int GlobalCache::contains(
const string & filename) {
173 pthread_mutex_lock(&mutex);
175 if (file_imageio.count(filename) > 0) {
178 pthread_mutex_unlock(&mutex);
182void GlobalCache::clean() {
183 pthread_mutex_lock(&mutex);
185 printf(
"clean: %ld items in cache\n", file_imageio.size());
189 vector<string> toclose;
190 time_t now = time(0);
193 for (map<string, ImageIO*>::iterator iter=file_imageio.begin(); iter!=file_imageio.end(); ++iter) {
194 filename = iter->first;
195 seconds = int(difftime(now, file_time[filename]));
197 printf(
"clean: filename: %s, rw: %d, ref: %d, last access: %d, persist: %d\n", filename.c_str(), file_rw[filename], file_ref[filename], seconds, file_persist[filename]);
199 if (seconds >= file_persist[filename]) {
201 toclose.push_back(filename);
206 for(vector<string>::iterator iter=toclose.begin(); iter!=toclose.end(); iter++) {
209 this->delete_imageio(filename);
212 pthread_mutex_unlock(&mutex);
ImageIO classes are designed for reading/writing various electron micrography image formats,...