Thread safety in byte streams
authorAlexander Strange <astrange@ithinksw.com>
Tue, 4 Mar 2003 06:23:50 +0000 (06:23 +0000)
committerAlexander Strange <astrange@ithinksw.com>
Tue, 4 Mar 2003 06:23:50 +0000 (06:23 +0000)
ITByteStream.h
ITByteStream.m

index 265b7a6..ceab156 100755 (executable)
@@ -14,6 +14,8 @@
 
 @interface ITByteStream : NSObject {
     NSMutableData *data;
+    @private
+    NSLock *lock;
 }
 -(id) initWithStream:(ITByteStream*)stream;
 -(int) availableDataLength;
index 301d851..e868008 100755 (executable)
@@ -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.
     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