Esempio n. 1
0
        public static void LockOSThreadMain()
        {
            // This requires GOMAXPROCS=1 from the beginning to reliably
            // start a goroutine on the main thread.
            if (runtime.GOMAXPROCS(-1L) != 1L)
            {
                println("requires GOMAXPROCS=1");
                os.Exit(1L);
            }

            var ready = make_channel <bool>(1L);

            go_(() => () =>
            {
                // Because GOMAXPROCS=1, this *should* be on the main
                // thread. Stay there.
                runtime.LockOSThread();
                var self = C.pthread_self();
                if (C.pthread_equal(mainThread, self) == 0L)
                {
                    println("failed to start goroutine on main thread");
                    os.Exit(1L);
                }
                // Exit with the thread locked, which should exit the
                // main thread.
                ready.Send(true);
            } ());
            ready.Receive();
            time.Sleep(1L * time.Millisecond);
            // Check that this goroutine is still running on a different
            // thread.
            self = C.pthread_self();
            if (C.pthread_equal(mainThread, self) != 0L)
            {
                println("goroutine migrated to locked thread");
                os.Exit(1L);
            }

            println("OK");
        }