2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
24 #include "NGLockingStream.h"
26 @implementation NGLockingStream
28 + (id)filterWithSource:(id<NGStream>)_source lock:(id<NSObject,NSLocking>)_lock {
29 return [[[self alloc] initWithSource:_source lock:_lock] autorelease];
32 - (id)initWithSource:(id<NGStream>)_source lock:(id<NSObject,NSLocking>)_lock {
33 if ((self = [super initWithSource:_source])) {
35 readLock = [[NSRecursiveLock allocWithZone:[self zone]] init];
36 writeLock = [readLock retain];
39 readLock = [_lock retain];
40 writeLock = [readLock retain];
46 - (id)initWithSource:(id<NGStream>)_source {
47 return [self initWithSource:_source lock:nil];
51 [self->readLock release];
52 [self->writeLock release];
58 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
59 volatile unsigned result = 0;
64 result = (readBytes != NULL)
65 ? (unsigned)readBytes(source, _cmd, _buf, _len)
66 : [source readBytes:_buf count:_len];
70 [localException raise];
78 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
79 volatile unsigned result = 0;
84 result = (writeBytes != NULL)
85 ? (unsigned)writeBytes(source, _cmd, _buf, _len)
86 : [source writeBytes:_buf count:_len];
90 [localException raise];
108 [localException raise];
115 - (BOOL)safeReadBytes:(void *)_buf count:(unsigned)_len {
121 *(&res) = [super safeReadBytes:_buf count:_len];
125 [localException raise];
133 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len {
139 res = [super safeWriteBytes:_buf count:_len];
143 [localException raise];
150 @end /* NGLockingStream */