]> err.no Git - sope/blob - sope-core/NGStreams/NGLockingStream.m
git-svn-id: http://svn.opengroupware.org/SOPE/trunk@181 e4a50df8-12e2-0310-a44c-efbce...
[sope] / sope-core / NGStreams / NGLockingStream.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "common.h"
24 #include "NGLockingStream.h"
25
26 @implementation NGLockingStream
27
28 + (id)filterWithSource:(id<NGStream>)_source lock:(id<NSObject,NSLocking>)_lock {
29   return [[[self alloc] initWithSource:_source lock:_lock] autorelease];
30 }
31
32 - (id)initWithSource:(id<NGStream>)_source lock:(id<NSObject,NSLocking>)_lock {
33   if ((self = [super initWithSource:_source])) {
34     if (_lock == nil) {
35       readLock  = [[NSRecursiveLock allocWithZone:[self zone]] init];
36       writeLock = [readLock retain];
37     }
38     else {
39       readLock  = [_lock    retain];
40       writeLock = [readLock retain];
41     }
42   }
43   return self;
44 }
45
46 - (id)initWithSource:(id<NGStream>)_source {
47   return [self initWithSource:_source lock:nil];
48 }
49
50 - (void)dealloc {
51   [self->readLock  release];
52   [self->writeLock release];
53   [super dealloc];
54 }
55
56 // primitives
57
58 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
59   volatile unsigned result = 0;
60
61   [readLock lock];
62
63   NS_DURING {
64     result = (readBytes != NULL)
65       ? (unsigned)readBytes(source, _cmd, _buf, _len)
66       : [source readBytes:_buf count:_len];
67   }
68   NS_HANDLER {
69     [readLock unlock];
70     [localException raise];
71   }
72   NS_ENDHANDLER;
73   [readLock unlock];
74
75   return result;
76 }
77
78 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
79   volatile unsigned result = 0;
80
81   [writeLock lock];
82
83   NS_DURING {
84     result = (writeBytes != NULL)
85       ? (unsigned)writeBytes(source, _cmd, _buf, _len)
86       : [source writeBytes:_buf count:_len];
87   }
88   NS_HANDLER {
89     [writeLock unlock];
90     [localException raise];
91   }
92   NS_ENDHANDLER;
93   [writeLock unlock];
94
95   return result;
96 }
97
98 - (BOOL)flush {
99   BOOL res = NO;
100   
101   [writeLock lock];
102
103   NS_DURING {
104     res = [super flush];
105   }
106   NS_HANDLER {
107     [writeLock unlock];
108     [localException raise];
109   }
110   NS_ENDHANDLER;
111   [writeLock unlock];
112   return res;
113 }
114
115 - (BOOL)safeReadBytes:(void *)_buf  count:(unsigned)_len {
116   BOOL res;
117   
118   [readLock lock];
119
120   NS_DURING {
121     *(&res) = [super safeReadBytes:_buf count:_len];
122   }
123   NS_HANDLER {
124     [readLock unlock];
125     [localException raise];
126   }
127   NS_ENDHANDLER;
128   [readLock unlock];
129
130   return res;
131 }
132
133 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len {
134   BOOL res = NO;
135   
136   [writeLock lock];
137
138   NS_DURING {
139     res = [super safeWriteBytes:_buf count:_len];
140   }
141   NS_HANDLER {
142     [writeLock unlock];
143     [localException raise];
144   }
145   NS_ENDHANDLER;
146   [writeLock unlock];
147   return res;
148 }
149
150 @end /* NGLockingStream */