From 00792f13efcd8fcf9b175e564b153805b8fb1a23 Mon Sep 17 00:00:00 2001 From: Alexander Strange Date: Tue, 4 Mar 2003 06:23:50 +0000 Subject: [PATCH] Thread safety in byte streams --- ITByteStream.h | 2 ++ ITByteStream.m | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ITByteStream.h b/ITByteStream.h index 265b7a6..ceab156 100755 --- a/ITByteStream.h +++ b/ITByteStream.h @@ -14,6 +14,8 @@ @interface ITByteStream : NSObject { NSMutableData *data; + @private + NSLock *lock; } -(id) initWithStream:(ITByteStream*)stream; -(int) availableDataLength; diff --git a/ITByteStream.m b/ITByteStream.m index 301d851..e868008 100755 --- a/ITByteStream.m +++ b/ITByteStream.m @@ -8,7 +8,7 @@ #import "ITByteStream.h" -// TODO: Add NSCopying/NSCoding support +// TODO: Add NSCopying/NSCoding support. Blocking reads (how would this work? I could hack it with socketpair(), i guess) @implementation ITByteStream -(id) init @@ -16,6 +16,7 @@ if (self == [super init]) { data = [[NSMutableData alloc] init]; + lock = [[NSLock alloc] init]; } return self; } @@ -25,6 +26,7 @@ if (self == [super init]) { data = [stream->data copy]; + lock = [[NSLock alloc] init]; } return 0; } @@ -32,6 +34,7 @@ -(void) dealloc { [data release]; + [lock release]; [super dealloc]; } @@ -44,6 +47,7 @@ { NSData *ret, *tmp; NSRange range = {0, length}; + [lock lock]; ret = [data subdataWithRange:range]; #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED [data replaceBytesInRange:range withBytes:nil length:0]; // this should delete off the end. should test. @@ -52,11 +56,14 @@ tmp = [data subdataWithRange:range]; [data setData:tmp]; // maybe i should add a lock to this? it would be bad if someone was writing when it was reading... #endif + [lock unlock]; return ret; } -(void) writeData:(NSData*)_data { + [lock lock]; [data appendData:_data]; + [lock unlock]; } @end -- 2.20.1