Bug00013() { lock (Lock) { nodeid = nodeidGenerator++; } nodename = ("Node" + nodeid); links = new Bug00013[maxLinks]; }
void notifyPending() { lock (Lock) { Bug00013 firstPending = pendingQueue; if ((firstPending != null) && (numThreadsActive < maxActive)) { pendingQueue = firstPending.nextPending; firstPending.nextPending = null; lock (firstPending) { firstPending.isPending = false; Monitor.PulseAll(firstPending); } } } }
} //: base("WatchDog") {} public void run() { DebugPrintSpin.Lock(); Console.WriteLine("WatchDog sleeping"); DebugPrintSpin.Unlock(); //try { Thread.Sleep(200000); //} catch(ThreadInterruptedException) { //DebugPrintSpin.Lock(); //Console.WriteLine("!"); //DebugPrintSpin.Unlock(); //} DebugPrintSpin.Lock(); Console.WriteLine("WatchDog awake"); Bug00013.DebugDumpThreadAnchorTable(4); Console.WriteLine("WatchDog exitting"); DebugPrintSpin.Unlock(); }
bool addLink(Bug00013 last, Bug00013 next) { Assert(last != null, "addlink() last should not be null"); Assert(next != null, "addlink() next should not be null"); if (last == next) { return(false); } bool lastLinked = false; int lastLinks; bool nextLinked = false; int nextLinks; lock (last) { checkConsistency(); if (last.numLinks < maxLinks) { lastLinked = true; for (int i = 0; i < last.numLinks; i++) { if (last.links[i] == next) { lastLinked = false; break; } } if (lastLinked) { last.links[last.numLinks++] = next; lastLinks = last.numLinks; } } checkConsistency(); } if (!lastLinked) { // last can't take another link // or these two nodes are already linked return(false); } lock (next) { checkConsistency(); if (next.numLinks < maxLinks) { nextLinked = true; for (int i = 0; i < next.numLinks; i++) { Assert(next.links[i] != last, "addlink() should not already be linked to last"); } next.links[next.numLinks++] = last; nextLinks = next.numLinks; } checkConsistency(); } if (nextLinked) { return(true); } lock (last) { checkConsistency(); Assert(last.numLinks > 0, "addlink() last.numLinks should be > 0"); last.numLinks--; for (int i = 0; i < last.numLinks; i++) { if (last.links[i] == next) { last.links[i] = last.links[last.numLinks]; break; } } last.links[last.numLinks] = null; checkConsistency(); } return(false); }
Bug00013 traverse(Bug00013 node, int numStrides) { lock (Lock) { recordUndead(); } for (int i = 0; i < numStrides; i++) { Assert(traversalsLeft >= 0); if (traversalsLeft > traversalLimit) { traversalsLeft = traversalLimit; } if (traversalsLeft == 0) { bool pended = false; int myNumThreadsAlive; int myNumThreadsActive; lock (Lock) { Assert(numThreadsActive > 0); Assert(nextPending == null); if ((numThreadsActive > maxActive) || (pendingQueue != null)) { pended = showPended; isPending = true; if (pendingQueue != null) { Bug00013 pend = pendingQueue; for (;;) { Bug00013 curr = pend; pend = curr.nextPending; if (pend == null) { curr.nextPending = this; break; } } } else { pendingQueue = this; } } numThreadsActive--; myNumThreadsAlive = numThreadsAlive; myNumThreadsActive = numThreadsActive; } if (pended) { DebugPrintSpin.Lock(); Console.WriteLine(); Console.Write("T_<"); Console.Write(threadid); Console.Write(","); Console.Write(myNumThreadsAlive); Console.Write(","); Console.Write(myNumThreadsActive); Console.Write("> "); DebugPrintSpin.Unlock(); } notifyPending(); lock (this) { while (isPending) { //try { Monitor.Wait(this); //} catch(ThreadInterruptedException) {} } Assert(nextPending == null); } lock (Lock) { Assert(numThreadsAlive > 0, "run() numThreadsAlive should be > 0"); Assert(numThreadsActive >= 0, "run() numThreadsActive should be >= 0"); myNumThreadsAlive = numThreadsAlive; numThreadsActive++; myNumThreadsActive = numThreadsActive; recordUndead(); } if (pended) { DebugPrintSpin.Lock(); Console.WriteLine(); Console.Write("T=<"); Console.Write(threadid); Console.Write(","); Console.Write(myNumThreadsAlive); Console.Write(","); Console.Write(myNumThreadsActive); Console.Write("< "); DebugPrintSpin.Unlock(); } traversalsLeft = traversalLimit; } traversalsLeft--; lock (node) { if (node.numLinks > 0) { int link = (int)(node.numLinks * random.NextDouble()); Assert(link >= 0, "traverse() selected link should be nonnegative"); Assert(link < node.numLinks, "traverse() selected link should be < numLinks"); if (node.links[link] == null) { DebugPrintSpin.Lock(); Console.WriteLine("BUG at link " + link); for (int ii = 0; ii < node.numLinks; ii++) { Console.WriteLine("link[" + ii + "] is " + node.links[ii]); } Console.WriteLine("link[" + link + "] is " + node.links[link]); DebugPrintSpin.Unlock(); } Assert(node.links[link] != null, "traverse() selected link should not be null"); node = node.links[link]; } } } return(node); }
// //public override String ToString() { // if (nodename != null) return(nodename); // return(base.ToString()); //} // public void run() { int myNumThreadsAlive; int myNumThreadsActive; lock (Lock) { Assert(numThreadsAlive >= 0, "run() numThreadsAlive should be >= 0"); Assert(numThreadsActive >= 0, "run() numThreadsActive should be >= 0"); numThreadsAlive++; myNumThreadsAlive = numThreadsAlive; numThreadsActive++; myNumThreadsActive = numThreadsActive; threadid = ++threadidGenerator; recordUndead(); } DebugPrintSpin.Lock(); Console.WriteLine(); Console.Write("T+<"); Console.Write(threadid); Console.Write(","); Console.Write(myNumThreadsAlive); Console.Write(","); Console.Write(myNumThreadsActive); Console.Write("> "); DebugPrintSpin.Unlock(); if ((myNumThreadsAlive <= maxThreads) && (threadid < totalThreads)) { bool conflict = false; lock (this) { if (random == null) { random = new Random(); } else { conflict = true; } } if (!conflict) { traversalLimit = nodeTraverse; Bug00013 last = this; Bug00013 next = this; Bug00013 node = new Bug00013(); anchor.addLink(this, node); for (int i = 0; i < newThreads; i++) { traversalLimit = nodeTraverse; int strides = 0; for (int j = 0; j < newNodes; j++) { recordUndead(); node = new Bug00013(); if (wasteSpace) { while (newNodes < newRejects * random.NextDouble()) { Object ooo = new int[1057]; node = new Bug00013(); } } strides = 100 + (int)(1000 * random.NextDouble()); last = traverse(last, strides); while (!addLink(last, node)) { strides = 100 + (int)(1000 * random.NextDouble()); last = traverse(last, strides); } strides = 100 + (int)(1000 * random.NextDouble()); next = traverse(next, strides); while (!addLink(next, node)) { strides = 100 + (int)(1000 * random.NextDouble()); next = traverse(next, strides); } } DebugPrintSpin.Lock(); Console.Write("n"); Console.Write(threadid); DebugPrintSpin.Unlock(); traversalLimit = linkTraverse; for (int j = 4 * newNodes; j < newLinks; j += 2) { last = traverse(last, strides); next = traverse(next, strides); while (!addLink(last, next)) { strides = 100 + (int)(1000 * random.NextDouble()); last = traverse(last, strides); strides = 100 + (int)(1000 * random.NextDouble()); next = traverse(next, strides); } } DebugPrintSpin.Lock(); Console.Write("l"); Console.Write(threadid); DebugPrintSpin.Unlock(); Thread newThread = new Thread(new ThreadStart(last.run)); DebugPrintSpin.Lock(); Console.Write("t"); Console.Write(threadid); DebugPrintSpin.Unlock(); newThread.Start(); } } } lock (Lock) { recordUndead(); Assert(numThreadsAlive > 0, "run() numThreadsAlive should be > 0"); Assert(numThreadsActive > 0, "run() numThreadsActive should be > 0"); numThreadsAlive--; myNumThreadsAlive = numThreadsAlive; numThreadsActive--; myNumThreadsActive = numThreadsActive; } DebugPrintSpin.Lock(); Console.WriteLine(); Console.Write("T-<"); Console.Write(threadid); Console.Write(","); Console.Write(myNumThreadsAlive); Console.Write(","); Console.Write(myNumThreadsActive); Console.Write("> "); DebugPrintSpin.Unlock(); notifyPending(); }
internal static void AppMain(Parameters !config) { totalThreads = (int)config.threads; undeadThreadids = new int[2 * maxThreads]; DebugPrintSpin.Lock(); Console.WriteLine("Bug00013.Main()"); DebugDumpThreadAnchorTable(4); DebugPrintSpin.Unlock(); Object Lock = new Object(); Bug00013.Lock = Lock; DebugPrintSpin.Lock(); Console.Write("Bug00013.Lock = "); Console.WriteLine(Lock.GetHashCode()); DebugPrintSpin.Unlock(); anchor = new Bug00013(); DebugPrintSpin.Lock(); Console.Write("Bug00013.Type = "); Console.WriteLine(anchor.GetType()); DebugPrintSpin.Unlock(); ClassBug00013 = anchor.GetType(); DebugPrintSpin.Lock(); Console.Write("Bug00013.class = "); Console.WriteLine(ClassBug00013.GetHashCode()); DebugPrintSpin.Unlock(); bug13Type = anchor.GetType(); linksType = anchor.links.GetType(); //================================================================== DebugPrintSpin.Lock(); Console.WriteLine(); Console.WriteLine("//======================================" + "======================================="); Console.WriteLine(); Console.WriteLine("before nonsyncv()"); DebugPrintSpin.Unlock(); anchor.nonsyncv(); DebugPrintSpin.Lock(); Console.WriteLine("after nonsyncv()"); DebugPrintSpin.Unlock(); //================================================================== DebugPrintSpin.Lock(); Console.WriteLine(); Console.WriteLine("//======================================" + "======================================="); Console.WriteLine(); Console.WriteLine("before syncv()"); DebugPrintSpin.Unlock(); anchor.syncv(); DebugPrintSpin.Lock(); Console.WriteLine("after syncv()"); DebugPrintSpin.Unlock(); //================================================================== DebugPrintSpin.Lock(); Console.WriteLine(); Console.WriteLine("//======================================" + "======================================="); Console.WriteLine(); Console.WriteLine("before nonsyncs()"); DebugPrintSpin.Unlock(); nonsyncs(); DebugPrintSpin.Lock(); Console.WriteLine("after nonsyncs()"); DebugPrintSpin.Unlock(); //================================================================== DebugPrintSpin.Lock(); Console.WriteLine(); Console.WriteLine("//======================================" + "======================================="); Console.WriteLine(); Console.WriteLine("before syncs()"); DebugPrintSpin.Unlock(); syncs(); DebugPrintSpin.Lock(); Console.WriteLine("after syncs()"); DebugPrintSpin.Unlock(); //================================================================== Thread newThread = new Thread(new ThreadStart(anchor.run)); DebugPrintSpin.Lock(); Console.WriteLine(); Console.WriteLine("//======================================" + "======================================="); Console.WriteLine(); Console.Write("newThread = "); Console.WriteLine(newThread.GetHashCode()); DebugPrintSpin.Unlock(); // //new WatchDog().start(); //try { // Thread.sleep(1000); //} //catch (InterruptedException e) { //} ///* newThread.Start(); deadman = System.DateTime.Now; for (;;) { //try { Thread.Sleep(25000); //} catch(ThreadInterruptedException) { //DebugPrintSpin.Lock(); //Console.WriteLine("!"); //DebugPrintSpin.Unlock(); // } lock (Lock) { DebugPrintSpin.Lock(); long deadms = (System.DateTime.Now - deadman).Milliseconds; if (deadms > 100000) { Console.WriteLine(); Console.WriteLine("Bug00013.Main()" + " deadman expired"); DebugDumpThreadAnchorTable(4); DebugBreak(); } else { Console.WriteLine(); Console.Write(" ?<0,"); Console.Write(numThreadsAlive); Console.Write(","); Console.Write(numThreadsActive); DebugPrintUndead(); Console.Write("> "); } DebugPrintSpin.Unlock(); if (numThreadsAlive == 0) { break; } } } Console.WriteLine("Passed Bug00013"); }