Created
August 31, 2011 20:45
-
-
Save jpf91/1184671 to your computer and use it in GitHub Desktop.
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 == 4); | |
ubyte[BUFFER_SIZE] buf; | |
auto sw = StopWatch(); | |
auto input = File(args[2], "r"); | |
if(args[1] == "--async") | |
{ | |
auto copy = spawn(©Thread, thisTid, args[3]); | |
receiveOnly!bool(); | |
sw.start(); | |
while(true) | |
{ | |
auto read = input.rawRead(new ubyte[BUFFER_SIZE]); | |
if(read.length == 0) | |
{ | |
copy.send(assumeUnique(read)); | |
break; | |
} | |
copy.send(assumeUnique(read)); | |
} | |
receiveOnly!bool(); | |
sw.stop(); | |
} | |
else if(args[1] == "--sync") | |
{ | |
auto output = File(args[3], "w"); | |
sw.start(); | |
while(true) | |
{ | |
auto read = input.rawRead(buf[]); | |
if(read.length == 0) | |
break; | |
output.rawWrite(read); | |
} | |
fsync(output.fileno); | |
output.close(); | |
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); | |
}); | |
} | |
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