internal void SwitchTask()
        {
            if (null != _activeTask)
            {
                switch (_activeTask.State)
                {
                case FiberTaskState.Ready:
                    _readyTasks.Enqueue(_activeTask);
                    break;

                case FiberTaskState.Stopped:
                    _stoppedTasks.Add(_activeTask);
                    break;
                }
            }

            uint nextFiberId = _readyTasks.TryDequeue(out _activeTask)
                ? _activeTask.FiberId
                : _schedulerFiberId;

            FiberUnmanaged.SwitchToFiber(nextFiberId);

            // we are back, check we were not killed meanwhile
            if (null != _activeTask && FiberTaskState.Killed == _activeTask.State)
            {
                Thread.CurrentThread.Abort();
            }
        }
        private void Scheduler(object data)
        {
            // schedule root task
            FiberTask rootTask = (FiberTask)data;

            _scheduler = this;

            _schedulerFiberId = FiberUnmanaged.ConvertThreadToFiber(0);

            rootTask.Start();
            SwitchTask();

            // exit
            FiberUnmanaged.ConvertFiberToThread();
        }
 protected FiberTask()
 {
     State = FiberTaskState.Unknown;
     FiberUnmanaged.FiberProc fiberProc = _ => EntryPoint();
     FiberId = FiberUnmanaged.CreateFiber(0, fiberProc, 0);
 }