EMAN2
emcache.cpp
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu), Ian Rees 06/30/2013
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifdef IMAGEIO_CACHE
33// #define DEBUG_CACHE 1
34
35#include "emcache.h"
36#include "imageio.h"
37#include "util.h"
38#include <stdio.h>
39#include <pthread.h>
40using namespace EMAN;
41
42void *thread_clean( void *ptr ) {
43 while (1) {
44 sleep(1);
45 GlobalCache::instance()->clean();
46 }
47}
48void thread_start() {
49 pthread_t thread1;
50 pthread_create(&thread1,NULL,&thread_clean,NULL);
51}
52
53
54// GlobalCache
55GlobalCache *GlobalCache::global_cache = 0;
56
57GlobalCache::GlobalCache()
58{
59 // pthread_mutex_init(&mutex, NULL);
60 thread_start();
61}
62
63GlobalCache::~GlobalCache()
64{
65 clean();
66 pthread_mutex_destroy(&mutex);
67}
68
69GlobalCache *GlobalCache::instance()
70{
71 if (!global_cache) {
72 global_cache = new GlobalCache();
73 }
74 return global_cache;
75}
76
77ImageIO *GlobalCache::get_imageio(const string & filename, int rw)
78{
79 pthread_mutex_lock(&mutex);
80#ifdef DEBUG_CACHE
81 printf("get_imageio: filename: %s, rw: %d\n", filename.c_str(), rw);
82#endif
83
84 if (file_imageio.count(filename) == 0) {
85 // printf("get_imageio: no cached imageio!\n");
86 pthread_mutex_unlock(&mutex);
87 return 0;
88 }
89
90 ImageIO *io = file_imageio[filename];
91#ifdef DEBUG_CACHE
92 printf("get_imageio: found cached; refs: %d, current rw: %d, request rw: %d\n", file_ref[filename], file_rw[filename], rw);
93#endif
94 time_t t = time(0);
95 file_time[filename] = time(0);
96 // printf("get_imageio: updated time\n");
97 if (file_rw[filename] == rw) {
98 // current == request
99 file_ref[filename]++;
100 // printf("get_imageio: ref += 1 to %d\n", file_ref[filename]);
101 } else if (file_rw[filename] == 2 && rw == 1) {
102 // current == read_write, request == read
103 file_ref[filename]++;
104 // printf("get_imageio: ref += 1 to %d (passing write mode to read request)\n", file_ref[filename]);
105 } else if (file_ref[filename] == 0) {
106 // current != request, no open handles
107 // printf("get_imageio: re-opening in mode %d\n", rw);
108 this->delete_imageio(filename);
109 io = 0;
110 } else {
111 // current != request, open handles
112 // current is read, request is write -- we can't make this change with open handles. How to fail?
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);
115 }
116 pthread_mutex_unlock(&mutex);
117 return io;
118}
119
120void GlobalCache::delete_imageio(const string & filename) {
121 // Lock acquired in caller
122 if (file_imageio.count(filename) == 0) {
123 // printf("delete_imageio: Not in cache\n");
124 } else if (file_ref[filename] == 0) {
125 ImageIO *io = file_imageio[filename];
126 file_imageio.erase(filename);
127 delete io;
128 }
129}
130
131void GlobalCache::close_imageio(const string & filename) {
132 pthread_mutex_lock(&mutex);
133#ifdef DEBUG_CACHE
134 printf("close_imageio: filename: %s\n", filename.c_str());
135#endif
136 if (file_imageio.count(filename) > 0) {
137 ImageIO *io = file_imageio[filename];
138 // Decrement the reference and update the access time.
139 file_ref[filename]--;
140 file_time[filename] = time(0);
141 // printf("close_imageio: ref -= 1 to %d\n", file_ref[filename]);
142 if (file_ref[filename] == 0 && file_persist[filename] == 0) {
143 // printf("close_imageio: auto closing!\n");
144 this->delete_imageio(filename);
145 }
146 } else {
147 // printf("close_imageio: No ImageIO to close!\n");
148 }
149 pthread_mutex_unlock(&mutex);
150}
151
152void GlobalCache::add_imageio(const string & filename, int rw, int persist, ImageIO * io)
153{
154 pthread_mutex_lock(&mutex);
155#ifdef DEBUG_CACHE
156 printf("add_imageio: filename %s, rw %d, persist %d\n", filename.c_str(), rw, persist);
157#endif
158 if (file_imageio.count(filename) > 0) {
159 // Todo: How to handle error if file is already in cache?
160 // printf("File already in cache.")
161 } else if (io && persist > 0) {
162 // Todo: Make a class to hold all these values and the ImageIO.
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;
168 }
169 pthread_mutex_unlock(&mutex);
170}
171
172int GlobalCache::contains(const string & filename) {
173 pthread_mutex_lock(&mutex);
174 int ret = 0;
175 if (file_imageio.count(filename) > 0) {
176 ret = 1;
177 }
178 pthread_mutex_unlock(&mutex);
179 return ret;
180}
181
182void GlobalCache::clean() {
183 pthread_mutex_lock(&mutex);
184#ifdef DEBUG_CACHE
185 printf("clean: %ld items in cache\n", file_imageio.size());
186#endif
187
188 // Find cached items to close
189 vector<string> toclose;
190 time_t now = time(0);
191 int seconds;
192 string filename;
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]));
196#ifdef DEBUG_CACHE
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]);
198#endif
199 if (seconds >= file_persist[filename]) {
200 // printf("clean: marking to close %s...\n", filename.c_str());
201 toclose.push_back(filename);
202 }
203 }
204
205 // Close the selected items
206 for(vector<string>::iterator iter=toclose.begin(); iter!=toclose.end(); iter++) {
207 filename = *iter;
208 // printf("clean: closing %s...\n", filename.c_str());
209 this->delete_imageio(filename);
210 }
211
212 pthread_mutex_unlock(&mutex);
213}
214
215#endif // IMAGEIO_CACHE
216
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
E2Exception class.
Definition: aligner.h:40