public void GlobalLockTest()
    {
        GlobalLock k = new GlobalLock("test_lock1");

        Event   startEvent = new Event(true);
        Event   event2     = new Event(true);
        Event   event3     = new Event(true);
        RefBool fail       = new RefBool();

        var thread1 = ThreadObj.Start(x =>
        {
            try
            {
                startEvent.Wait();

                using (k.Lock())
                {
                    event2.Set();

                    if (event3.Wait(100))
                    {
                        fail.Set(true);
                    }
                }
            }
            catch
            {
                fail.Set(true);
            }
        });

        var thread2 = ThreadObj.Start(x =>
        {
            try
            {
                startEvent.Wait();

                event2.Wait();

                using (k.Lock())
                {
                    event3.Set();
                }
            }
            catch
            {
                fail.Set(true);
            }
        });

        startEvent.Set();

        Assert.True(thread1.WaitForEnd(5000));
        Assert.True(thread2.WaitForEnd(5000));

        Assert.False(fail.Value);
    }
        public static void jsonrpc_client_server_both_test()
        {
            //jsonrpc_server_invoke_test().Wait();return;

            // start server
            HttpServerOptions http_cfg = new HttpServerOptions()
            {
                DebugKestrelToConsole = false,
            };
            JsonRpcServerConfig rpc_cfg = new JsonRpcServerConfig()
            {
            };

            using (RpcServerApiTest h = new RpcServerApiTest())
                using (var s = JsonRpcHttpServerBuilder.StartServer(http_cfg, rpc_cfg, h))
                {
                    Ref <bool> client_stop_flag = new Ref <bool>();

                    // start client
                    ThreadObj client_thread = ThreadObj.Start(param =>
                    {
                        //Kernel.SleepThread(-1);

                        //using ()
                        {
                            //c.AddHeader("X-1", "Hello");

                            rpctmp1 t = new rpctmp1();
                            t.a       = new rpc_t()
                            {
                                Int1 = 2,
                                Str1 = "Neko",
                            };

                            //JsonRpcResponse<object> ret = c.CallOne<object>("Test1", t).Result;
                            //JsonRpcResponse<object> ret = c.CallOne<object>("Test2", t).Result;

                            Benchmark b = new Benchmark("rpccall");

                            JsonRpcHttpClient <rpc_server_api_interface_test> c = new JsonRpcHttpClient <rpc_server_api_interface_test>("http://127.0.0.1:88/rpc");
                            var threads = ThreadObj.StartMany(256, par =>
                            {
                                while (client_stop_flag.Value == false)
                                {
                                    //c.Call.Divide(8, 2).Wait();
                                    TMP1 a = new TMP1()
                                    {
                                        a = 4, b = 2
                                    };
                                    c.MT_Call <object>("Divide", a, true)._GetResult();
                                    //c.ST_CallOne<object>("Divide", a, true).Wait();
                                    b.IncrementMe++;
                                }
                            }
                                                              );

                            foreach (var thread in threads)
                            {
                                thread.WaitForEnd();
                            }

                            //c.Call.Divide(8, 2).Result.Print();
                            //c.Call.Divide(8, 2).Result.Print();
                            //c.Call.Test3(1, 2, 3).Result.Print();
                            //c.Call.Test5(1, "2").Result.ObjectToJson().Print();
                            //var fnlist = c.Call.Test6().Result;
                            ////foreach (var fn in fnlist) fn.Print();
                            //c.Call.Test7(fnlist).Result.Print();

                            //Con.WriteLine(ret.ObjectToJson());
                        }
                    }, null);

                    Con.ReadLine("Enter to quit>");

                    client_stop_flag.Set(true);

                    client_thread.WaitForEnd();
                }
        }
    public void SingleInstanceTest()
    {
        Event   startEvent = new Event(true);
        RefBool fail       = new RefBool();
        Event   event2     = new Event(true);
        Event   event3     = new Event(true);
        Event   event4     = new Event(true);
        Event   event5     = new Event(true);
        Event   event6     = new Event(true);

        var thread1 = ThreadObj.Start(x =>
        {
            try
            {
                startEvent.Wait();

                using (var si2 = new SingleInstance("si_test"))
                {
                    event2.Set();

                    event3.Wait();
                }

                event4.Set();

                event5.Wait();

                var si = SingleInstance.TryGet("si_test");
                if (si != null)
                {
                    fail.Set(true);
                }

                event6.Set();
            }
            catch
            {
                fail.Set(true);
            }
        });

        var thread2 = ThreadObj.Start(x =>
        {
            try
            {
                startEvent.Wait();

                event2.Wait();

                var si = SingleInstance.TryGet("si_test");
                if (si != null)
                {
                    fail.Set(true);
                }

                event3.Set();

                event4.Wait();

                si = SingleInstance.TryGet("si_test");
                if (si == null)
                {
                    fail.Set(true);
                }

                event5.Set();

                event6.Wait();

                si._DisposeSafe();
            }
            catch
            {
                fail.Set(true);
            }
        });

        startEvent.Set();

        Assert.True(thread1.WaitForEnd(5000));
        Assert.True(thread2.WaitForEnd(5000));

        Assert.False(fail.Value);
    }