<commit_before>using System;
using System.Threading;
using Theraot.Collections.ThreadSafe;
using Theraot.Core;

namespace Theraot.Threading
{
    public sealed partial class Work : ICloneable
    {
        [ThreadStatic]
        private static Work _current;

        private readonly Action _action;
        private readonly WorkContext _context;
        private readonly bool _exclusive;
        private int _done;
        private Exception _resultException;

        internal Work(Action action, bool exclusive, WorkContext context)
        {
            if (ReferenceEquals(context, null))
            {
                throw new ArgumentNullException("context");
            }
            else
            {
                _context = context;
                _action = action ?? ActionHelper.GetNoopAction();
                _exclusive = exclusive;
            }
        }

        public static Work Current
        {
            get
            {
                return _current;
            }
        }

        public bool Done
        {
            get
            {
                return Thread.VolatileRead(ref _done) == 1;
            }
        }

        internal bool Exclusive
        {
            get
            {
                return _exclusive;
            }
        }

        public Work Clone()
        {
            return _context.AddWork(_action, _exclusive);
        }

        object ICloneable.Clone()
        {
            return Clone();
        }

        public void Start()
        {
            _context.ScheduleWork(this);
        }

        public void Wait()
        {
            while (Thread.VolatileRead(ref _done) == 0)
            {
                _context.DoOneWork();
            }
        }

        internal void Execute()
        {
            var oldCurrent = Interlocked.Exchange(ref _current, this);
            try
            {
                _action.Invoke();
            }
            catch (Exception exc)
            {
                _resultException = exc;
            }
            finally
            {
                Thread.VolatileWrite(ref _done, 1);
                Interlocked.Exchange(ref _current, oldCurrent);
            }
        }
    }

    public sealed partial class Work : ICloneable
    {
#if DEBUG || FAT
        private static int _lastId;
        private int _id = Interlocked.Increment(ref _lastId) - 1;

        public int Id<commit_msg>A Work is now an IPromise<commit_after>using System;
using System.Threading;
using Theraot.Collections.ThreadSafe;
using Theraot.Core;
using Theraot.Threading.Needles;

namespace Theraot.Threading
{
    public sealed partial class Work : ICloneable, IPromise
    {
        [ThreadStatic]
        private static Work _current;

        private readonly Action _action;
        private readonly WorkContext _context;
        private readonly bool _exclusive;
        private PromiseNeedle _result;
        private IPromised _promised;

        internal Work(Action action, bool exclusive, WorkContext context)
        {
            if (ReferenceEquals(context, null))
            {
                throw new ArgumentNullException("context");
            }
            else
            {
                _context = context;
                _action = action ?? ActionHelper.GetNoopAction();
                _exclusive = exclusive;
                _result = new PromiseNeedle(out _promised, false);
            }
        }

        public static Work Current
        {
            get
            {
                return _current;
            }
        }

        public bool IsReady
        {
            get
            {
                return _result.IsReady;
            }
        }

        internal bool Exclusive
        {
            get
            {
                return _exclusive;
            }
        }

        public Work Clone()
        {
            return _context.AddWork(_action, _exclusive);
        }

        object ICloneable.Clone()
        {
            return Clone();
        }

        public void Start()
        {
            _context.ScheduleWork(this);
        }

        public void Wait()
        {
            while (!_result.IsReady)
            {
                _context.DoOneWork();
            }
        }

        internal void Execute()
        {
            var oldCurrent = Interlocked.Exchange(ref _current, this);
            try
            {
                _action.Invoke();
                _promised.OnCompleted();
            }
            catch (Exception exception)
            {
                _promised.OnError(exception);
            }
            finally
            {
                Interlocked.Exchange(ref _current, oldCurrent);
            }
        }

        public Exception Error
        {
            get
            {
                return _result.Error;
            }
        }
    }

    public sealed partial class Work : ICloneable
    {
#if DEBUG || FAT
        private static int _lastId;
        private int _id = Interlocked.Increment(ref _lastId) - 1;

        public int Id
{'fraction_non_alphanumeric': 0.06591892955529319, 'fraction_numerical': 0.0009838646202282567, 'mean_word_length': 1.1796740994854202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '336654', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1211, 'n_words': 305}