Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Linux #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sources/Spawn/Spawn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public final class Spawn {
var threadInfo: ThreadInfo!

func watchStreams() {
#if(OSX)
func callback(x: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
let threadInfo = unsafeBitCast(x, to: UnsafeMutablePointer<ThreadInfo>.self).pointee
let outputPipe = threadInfo.outputPipe
Expand All @@ -83,16 +84,42 @@ public final class Spawn {
dynamicBuffer.deallocate(capacity: bufferSize)
return nil
}
#else
func callback(x: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
Copy link

@esttorhe esttorhe Nov 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this repo is not mine or anything; but as seeing that the code inside the Linux path doesn't contain stuff that would fail compiling on macOS; wouldn't it be better to have something like macOSCallback and linuxCallback and then only applying the filtering inside of callback:

func callback(x: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
#if(OSX)
   return macOSCallback(x)
#else
   return linuxCallback(x)
#endif
}

Edit: I think this would make it more readable; happy to discuss if this is not true or doesn't make sense or even if it really doesn't add any value.

let threadInfo = unsafeBitCast(x!, to: UnsafeMutablePointer<ThreadInfo>.self).pointee
let outputPipe = threadInfo.outputPipe
close(outputPipe[1])
let bufferSize: size_t = 1024 * 8
let dynamicBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
while true {
let amtRead = read(outputPipe[0], dynamicBuffer, bufferSize)
if amtRead <= 0 { break }
let array = Array(UnsafeBufferPointer(start: dynamicBuffer, count: amtRead))
let tmp = array + [UInt8(0)]
tmp.withUnsafeBufferPointer { ptr in
let str = String(cString: unsafeBitCast(ptr.baseAddress, to: UnsafePointer<CChar>.self))
threadInfo.output?(str)
}
}
dynamicBuffer.deallocate(capacity: bufferSize)
return nil
}
#endif

threadInfo = ThreadInfo(outputPipe: &outputPipe, output: output)
pthread_create(&tid, nil, callback, &threadInfo)
}

deinit {
var status: Int32 = 0

#if(OSX)
if let tid = tid {
pthread_join(tid, nil)
}
#else
pthread_join(tid, nil)
#endif

waitpid(pid, &status, 0)
}
Expand Down