Example #1
0
    internal void ScheduleNextThread()
    {
        Thread t = readyQueue.Dequeue();

        if (t == null)
        {
            if (collectionRequested)
            {
                CompilerIntrinsics.Cli(); // TODO: superfluous
                NucleusCalls.DebugPrintHex(70, ++gcCount);
                NucleusCalls.DebugPrintHex(60, 0);
                long t1 = NucleusCalls.Rdtsc();

                // No ready threads.
                // Make anyone waiting for GC ready:
                while (true)
                {
                    t = collectionQueue.Dequeue();
                    if (t == null)
                    {
                        break;
                    }
                    readyQueue.Enqueue(t);
                }

                t = readyQueue.Dequeue();

                // Garbage collect, then we're ready to go.
                CompilerIntrinsics.Sti();
                System.DebugStub.Print("GarbageCollecting. ");
                CompilerIntrinsics.Cli();
                NucleusCalls.GarbageCollect();
                collectionRequested = false;

                long t2   = NucleusCalls.Rdtsc();
                uint diff = (uint)((t2 - t1) >> 10);
                NucleusCalls.DebugPrintHex(60, diff);
            }

            while (t == null)
            {
                // TODO: let the CPU sleep here
                // TODO: enable interrupts
                CompilerIntrinsics.Cli(); // TODO: superfluous
                if (!CheckWakeUp())
                {
                    // No threads to run.  The system is finished.
                    CompilerIntrinsics.Cli(); // TODO: superfluous
                    NucleusCalls.DebugPrintHex(0, 0x76543210);
                    while (true)
                    {
                    }
                }

                t = readyQueue.Dequeue();
                CompilerIntrinsics.Cli(); // TODO: superfluous
            }
        }

        // Go to t.
        RunThread(t);

        // We're back.  Somebody (not necessarily t) yielded to us.
    }