Created
September 2, 2011 07:55
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.datetime, std.concurrency, std.stdio, core.sys.posix.unistd; | |
import std.exception; | |
enum BUFFER_SIZE = 1 * 1024 * 1024UL; | |
void main(string[] args) | |
{ | |
assert(args.length == 3); | |
auto sw = StopWatch(); | |
auto input = File(args[1], "r"); | |
ubyte[BUFFER_SIZE/2] buf1, buf2; | |
auto bufptr = &buf1; | |
auto copy = spawn(©Thread, thisTid, args[2]); | |
receiveOnly!bool(); | |
sw.start(); | |
bool firstRun = true; | |
while(true) | |
{ | |
auto read = input.rawRead((*bufptr)[]); | |
if(read.length == 0) | |
{ | |
copy.send(assumeUnique(read)); | |
break; | |
} | |
copy.send(assumeUnique(read)); | |
if(firstRun) | |
firstRun = false; | |
else | |
receiveOnly!bool(); | |
if(bufptr == &buf1) | |
bufptr = &buf2; | |
else | |
bufptr = &buf1; | |
} | |
receiveOnly!bool(); | |
sw.stop(); | |
writefln("Copied %s bytes in %s msec (%s kB/s)", input.size, | |
sw.peek().msecs, input.size / (1024 * sw.peek().seconds)); | |
} | |
void copyThread(Tid sender, string outName) | |
{ | |
auto output = File(outName, "w"); | |
bool cont = true; | |
sender.send(true); | |
while(cont) | |
{ | |
receive( | |
(immutable(ubyte[]) data) { | |
if(data.length == 0) | |
{ | |
cont = false; | |
return; | |
} | |
output.rawWrite(data); | |
sender.send(true); | |
}); | |
} | |
fsync(output.fileno); | |
output.close(); | |
sender.send(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment