1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
*
* Copyright (C) 1997-2005 Stuart Parmenter and others,
* See the file AUTHORS for a list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
/*
* LibBalsaMimeStreamShared: a subclass of GMimeStreamFs that supports
* locking.
*
* A single lock is shared by the original stream and all substreams and
* filtered streams derived from it. Methods that change the stream
* pointer (read, write, reset, seek) will fail with return value -1 if
* the stream is not locked. The lock should be held while carrying out
* sequences of operations such as seek+read, seek+write, read+tell,
* read+test-eos.
*/
#include "config.h"
#if BALSA_USE_THREADS
#include <gmime/gmime-stream.h>
#include <gmime/gmime-stream-filter.h>
#include "mime-stream-shared.h"
typedef struct _LibBalsaMimeStreamSharedLock LibBalsaMimeStreamSharedLock;
struct _LibBalsaMimeStreamShared {
GMimeStreamFs parent_object;
LibBalsaMimeStreamSharedLock *lock;
};
struct _LibBalsaMimeStreamSharedClass {
GMimeStreamClass parent_class;
};
static void lbmss_stream_class_init(LibBalsaMimeStreamSharedClass * klass);
static void lbmss_finalize(GObject *object);
static ssize_t lbmss_stream_read(GMimeStream * stream, char *buf,
size_t len);
static ssize_t lbmss_stream_write(GMimeStream * stream, const char *buf,
size_t len);
static int lbmss_stream_reset(GMimeStream * stream);
static off_t lbmss_stream_seek(GMimeStream * stream, off_t offset,
GMimeSeekWhence whence);
static GMimeStream *lbmss_stream_substream(GMimeStream * stream,
off_t start, off_t end);
static GMimeStreamFsClass *parent_class = NULL;
static GMutex *lbmss_mutex;
static GCond *lbmss_cond;
GType
libbalsa_mime_stream_shared_get_type(void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof(LibBalsaMimeStreamSharedClass),
NULL, /* base_class_init */
NULL, /* base_class_finalize */
(GClassInitFunc) lbmss_stream_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof(LibBalsaMimeStreamShared),
16, /* n_preallocs */
NULL /* instance init */
};
type =
g_type_register_static(GMIME_TYPE_STREAM_FS,
"LibBalsaMimeStreamShared", &info, 0);
}
return type;
}
static void
lbmss_stream_class_init(LibBalsaMimeStreamSharedClass * klass)
{
GMimeStreamClass *stream_class = GMIME_STREAM_CLASS(klass);
GObjectClass *object_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_ref(GMIME_TYPE_STREAM_FS);
lbmss_mutex = g_mutex_new();
lbmss_cond = g_cond_new();
object_class->finalize = lbmss_finalize;
stream_class->read = lbmss_stream_read;
stream_class->write = lbmss_stream_write;
stream_class->reset = lbmss_stream_reset;
stream_class->seek = lbmss_stream_seek;
stream_class->substream = lbmss_stream_substream;
}
/* The shared lock. */
struct _LibBalsaMimeStreamSharedLock {
GThread *thread;
guint count;
guint ref_count;
};
static LibBalsaMimeStreamSharedLock *
lbmss_lock_new(void)
{
LibBalsaMimeStreamSharedLock *lock;
lock = g_new(LibBalsaMimeStreamSharedLock, 1);
lock->thread = 0;
lock->count = 0;
lock->ref_count = 1;
return lock;
}
static LibBalsaMimeStreamSharedLock *
lbmss_lock_ref(LibBalsaMimeStreamSharedLock * lock)
{
++lock->ref_count;
return lock;
}
static void
lbmss_lock_unref(LibBalsaMimeStreamSharedLock * lock)
{
g_assert(lock->ref_count > 0);
if (--lock->ref_count == 0)
g_free(lock);
}
/* Object class method. */
static void
lbmss_finalize(GObject *object)
{
LibBalsaMimeStreamShared *stream_shared =
(LibBalsaMimeStreamShared *) object;
lbmss_lock_unref(stream_shared->lock);
G_OBJECT_CLASS(parent_class)->finalize(object);
}
/* Stream class methods. */
#define lbmss_thread_has_lock(stream) \
(LIBBALSA_MIME_STREAM_SHARED(stream)->lock->count > 0 \
&& LIBBALSA_MIME_STREAM_SHARED(stream)->lock->thread == g_thread_self())
static ssize_t
lbmss_stream_read(GMimeStream * stream, char *buf, size_t len)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(parent_class)->read(stream, buf, len);
}
static ssize_t
lbmss_stream_write(GMimeStream * stream, const char *buf, size_t len)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(parent_class)->write(stream, buf, len);
}
static int
lbmss_stream_reset(GMimeStream * stream)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(parent_class)->reset(stream);
}
static off_t
lbmss_stream_seek(GMimeStream * stream, off_t offset,
GMimeSeekWhence whence)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(parent_class)->seek(stream, offset, whence);
}
static GMimeStream *
lbmss_stream_substream(GMimeStream * stream, off_t start, off_t end)
{
LibBalsaMimeStreamShared *stream_shared;
GMimeStreamFs *fstream;
stream_shared =
g_object_new(LIBBALSA_TYPE_MIME_STREAM_SHARED, NULL, NULL);
stream_shared->lock =
lbmss_lock_ref(LIBBALSA_MIME_STREAM_SHARED(stream)->lock);
fstream = GMIME_STREAM_FS(stream_shared);
fstream->owner = FALSE;
fstream->fd = GMIME_STREAM_FS(stream)->fd;
g_mime_stream_construct(GMIME_STREAM(fstream), start, end);
return GMIME_STREAM(fstream);
}
/* Public methods. */
/**
* libbalsa_mime_stream_shared_new:
* @fd: file descriptor
*
* Create a new GMimeStreamShared object around @fd.
*
* Returns a stream using @fd.
*
**/
GMimeStream *
libbalsa_mime_stream_shared_new(int fd)
{
LibBalsaMimeStreamShared *stream_shared;
GMimeStreamFs *fstream;
off_t start;
stream_shared =
g_object_new(LIBBALSA_TYPE_MIME_STREAM_SHARED, NULL, NULL);
stream_shared->lock = lbmss_lock_new();
fstream = GMIME_STREAM_FS(stream_shared);
fstream->owner = TRUE;
fstream->eos = FALSE;
fstream->fd = fd;
start = lseek(fd, 0, SEEK_CUR);
g_mime_stream_construct(GMIME_STREAM(fstream), start, -1);
return GMIME_STREAM(fstream);
}
/**
* libbalsa_mime_stream_shared_lock:
* @stream: shared stream
*
* Lock the shared stream
**/
void
libbalsa_mime_stream_shared_lock(GMimeStream * stream)
{
LibBalsaMimeStreamShared *stream_shared;
LibBalsaMimeStreamSharedLock *lock;
GThread *thread_self;
g_return_if_fail(GMIME_IS_STREAM(stream));
if (GMIME_IS_STREAM_FILTER(stream))
stream = ((GMimeStreamFilter *) stream)->source;
if (!LIBBALSA_IS_MIME_STREAM_SHARED(stream))
return;
stream_shared = (LibBalsaMimeStreamShared *) stream;
lock = stream_shared->lock;
thread_self = g_thread_self();
g_mutex_lock(lbmss_mutex);
while (lock->count > 0 && lock->thread != thread_self)
g_cond_wait(lbmss_cond, lbmss_mutex);
++lock->count;
lock->thread = thread_self;
g_mutex_unlock(lbmss_mutex);
}
/**
* libbalsa_mime_stream_shared_unlock:
* @stream: shared stream
*
* Unlock the shared stream
**/
void
libbalsa_mime_stream_shared_unlock(GMimeStream * stream)
{
LibBalsaMimeStreamShared *stream_shared;
LibBalsaMimeStreamSharedLock *lock;
g_return_if_fail(GMIME_IS_STREAM(stream));
if (GMIME_IS_STREAM_FILTER(stream))
stream = ((GMimeStreamFilter *) stream)->source;
if (!LIBBALSA_IS_MIME_STREAM_SHARED(stream))
return;
stream_shared = (LibBalsaMimeStreamShared *) stream;
lock = stream_shared->lock;
g_return_if_fail(lock->count > 0);
g_mutex_lock(lbmss_mutex);
if (--lock->count == 0)
g_cond_signal(lbmss_cond);
g_mutex_unlock(lbmss_mutex);
}
#endif /* BALSA_USE_THREADS */
|