Skip to content

Instantly share code, notes, and snippets.

@jpf91
Created September 2, 2011 07:55
Show Gist options
  • Save jpf91/1188128 to your computer and use it in GitHub Desktop.
Save jpf91/1188128 to your computer and use it in GitHub Desktop.
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(&copyThread, 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