2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGLockingStream.h"
25 @implementation NGLockingStream
27 + (id)filterWithSource:(id<NGStream>)_source lock:(id<NSObject,NSLocking>)_lock {
28 return [[[self alloc] initWithSource:_source lock:_lock] autorelease];
31 - (id)initWithSource:(id<NGStream>)_source lock:(id<NSObject,NSLocking>)_lock {
32 if ((self = [super initWithSource:_source])) {
34 readLock = [[NSRecursiveLock allocWithZone:[self zone]] init];
35 writeLock = [readLock retain];
38 readLock = [_lock retain];
39 writeLock = [readLock retain];
45 - (id)initWithSource:(id<NGStream>)_source {
46 return [self initWithSource:_source lock:nil];
50 [self->readLock release];
51 [self->writeLock release];
57 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
58 volatile unsigned result = 0;
63 result = (readBytes != NULL)
64 ? (unsigned)readBytes(source, _cmd, _buf, _len)
65 : [source readBytes:_buf count:_len];
69 [localException raise];
77 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
78 volatile unsigned result = 0;
83 result = (writeBytes != NULL)
84 ? (unsigned)writeBytes(source, _cmd, _buf, _len)
85 : [source writeBytes:_buf count:_len];
89 [localException raise];
107 [localException raise];
114 - (BOOL)safeReadBytes:(void *)_buf count:(unsigned)_len {
120 *(&res) = [super safeReadBytes:_buf count:_len];
124 [localException raise];
132 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len {
138 res = [super safeWriteBytes:_buf count:_len];
142 [localException raise];
149 @end /* NGLockingStream */