www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Fibers

reply Andrew Edwards <ridimz yahoo.com> writes:
The script below is borrowed form a unit test in core.thread and 
modified slightly. If fails with "segmentation fault: 11" but I'm not 
sure why.

Basically what I'm trying to do is to transact on every file in give 
directory at the same time exact time.

In this exam, I'm removing the file/directory but that's just a test to 
see if I could get it to work.

Guidance is requested and appreciated.

Thanks,
Andrew

---------------------------------
import core.thread;
import std.process;
import std.file;
import std.stdio;

void main(string[] args)
{
	auto f = args[1];
	deleteWithFibers(f);
}

class TestFiber : Fiber
{
	string file;

	this(string file)
	{
		this.file = file;
		super(&run);
	}

	void run()
	{
		while (file.exists) {
			if (file.isFile)
				file.remove;
			else if (file.isDir)
				file.rmdir;
			Fiber.yield();
		}
	}
}

void deleteWithFibers(string dir)
{
	auto fibs = new TestFiber[100];
	
	int count = 0;
	foreach (entry; dirEntries(dir, SpanMode.depth))
	{
		fibs[count] = new TestFiber(entry);

		if (++count > fibs.length)
			fibs.length *= 2;
	}

	fibs.length = count+1;

	bool cont;
	do {
		cont = false;
		foreach(fib; fibs) {
			if (fib.state == Fiber.State.HOLD)
			{
				fib.call();
				cont |= fib.state != Fiber.State.TERM;
			}
		}
	} while (cont);
}
Sep 17 2014
next sibling parent "Flamencofantasy" <Flamencofantasy gmail.com> writes:
if (++count >= fibs.length)...


On Wednesday, 17 September 2014 at 10:00:49 UTC, Andrew Edwards
wrote:
 The script below is borrowed form a unit test in core.thread 
 and modified slightly. If fails with "segmentation fault: 11" 
 but I'm not sure why.

 Basically what I'm trying to do is to transact on every file in 
 give directory at the same time exact time.

 In this exam, I'm removing the file/directory but that's just a 
 test to see if I could get it to work.

 Guidance is requested and appreciated.

 Thanks,
 Andrew

 ---------------------------------
 import core.thread;
 import std.process;
 import std.file;
 import std.stdio;

 void main(string[] args)
 {
 	auto f = args[1];
 	deleteWithFibers(f);
 }

 class TestFiber : Fiber
 {
 	string file;

 	this(string file)
 	{
 		this.file = file;
 		super(&run);
 	}

 	void run()
 	{
 		while (file.exists) {
 			if (file.isFile)
 				file.remove;
 			else if (file.isDir)
 				file.rmdir;
 			Fiber.yield();
 		}
 	}
 }

 void deleteWithFibers(string dir)
 {
 	auto fibs = new TestFiber[100];
 	
 	int count = 0;
 	foreach (entry; dirEntries(dir, SpanMode.depth))
 	{
 		fibs[count] = new TestFiber(entry);

 		if (++count > fibs.length)
 			fibs.length *= 2;
 	}

 	fibs.length = count+1;

 	bool cont;
 	do {
 		cont = false;
 		foreach(fib; fibs) {
 			if (fib.state == Fiber.State.HOLD)
 			{
 				fib.call();
 				cont |= fib.state != Fiber.State.TERM;
 			}
 		}
 	} while (cont);
 }
Sep 17 2014
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
and
fibs.length = count;
Sep 17 2014
prev sibling next sibling parent reply "Kagamin" <spam here.lot> writes:
fibs ~= new TestFiber(entry);
should just work without accounting for number of items
Sep 17 2014
parent Andrew Edwards <ridimz yahoo.com> writes:
On 9/17/14, 9:43 PM, Kagamin wrote:
 fibs ~= new TestFiber(entry);
 should just work without accounting for number of items
Kagamin/Flamencofantasy, thank you very much.
Sep 17 2014
prev sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Wednesday, 17 September 2014 at 10:00:49 UTC, Andrew Edwards 
wrote:
 Basically what I'm trying to do is to transact on every file in 
 give directory at the same time exact time.

 In this exam, I'm removing the file/directory but that's just a 
 test to see if I could get it to work.
Perhaps this example is oversimplified, but fibers won't help in doing some operation at the exact same time. They will still run sequentially, on a single CPU, as if no threads/fibers were used. I don't think there is a way to perform an operation in a truly atomic way on a directory or group of files simultaneously, without using the operating system's transactional filesystem capabilities, and those don't seem to be commonly available except on Windows (and even there transactional NTFS is deprecated). An alternative would be to lock all files before operating on them (thus causing other processes to fail when attempting to access to them, instead of corrupting data), or to operate on a private copy of the directory, and then swap it with the live copy (although swapping a directory atomically is also not possible without using symbolic links).
Sep 17 2014