Skip to content

Commit

Permalink
PTYTask - properly protect the writeBuffer
Browse files Browse the repository at this point in the history
git-svn-id: https://iterm.svn.sourceforge.net/svnroot/iterm/trunk@1795 a86d2aa4-6e2d-4ff4-9c10-3c044852e7c9
  • Loading branch information
delx committed Jun 13, 2009
1 parent b092b35 commit eee01c8
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions PTYTask.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// -*- mode:objc -*-
// $Id: PTYTask.m,v 1.52 2008-10-24 05:25:00 yfabian Exp $
//
/*
** PTYTask.m
**
Expand Down Expand Up @@ -199,6 +197,8 @@ - (void)run
iter = [tasks objectEnumerator];
while(task = [iter nextObject]) {
int fd = [task fd];
if(fd < 0)
continue;
if(fd > highfd)
highfd = fd;
if([task wantsRead])
Expand Down Expand Up @@ -232,15 +232,14 @@ - (void)run
iter = [tasks objectEnumerator];
while(task = [iter nextObject]) {
int fd = [task fd];
if(FD_ISSET(fd, &rfds)) {
if(fd < 0)
continue;
if(FD_ISSET(fd, &rfds))
[task processRead];
}
if(FD_ISSET(fd, &wfds)) {
if(FD_ISSET(fd, &wfds))
[task processWrite];
}
if(FD_ISSET(fd, &efds)) {
if(FD_ISSET(fd, &efds))
[task brokenPipe];
}
}

[innerPool drain];
Expand Down Expand Up @@ -451,6 +450,9 @@ - (void)processWrite
__FILE__, __LINE__, [writeBuffer length]);
#endif

// Retain to prevent the object from being released during this method
// Lock to protect the writeBuffer from the main thread
[self retain];
[writeLock lock];

// Only write up to MAXRW bytes, then release control
Expand All @@ -470,7 +472,9 @@ - (void)processWrite
memmove(ptr, ptr+written, length);
[writeBuffer setLength:length];

// Clean up locks
[writeLock unlock];
[self autorelease];
}

- (BOOL)hasOutput
Expand Down Expand Up @@ -510,12 +514,11 @@ - (void)writeTask:(NSData*)data
NSLog(@"%s(%d):-[PTYTask writeTask:%@]", __FILE__, __LINE__, data);
#endif

// Write as much as we can now through the non-blocking pipe
// Lock to protect the writeBuffer from the IO thread
[writeLock lock];

/* Write as much as we can now through the non-blocking pipe */
[writeBuffer appendData:data];
[[TaskNotifier sharedInstance] unblock];

[writeLock unlock];
}

Expand Down

0 comments on commit eee01c8

Please sign in to comment.