<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using RedoxLib.Objects;

namespace RedoxLib.Utilities
{
    /// <summary>
    /// A mutex that synchronizes access to a world object.
    /// 
    /// Note: Currently this implementation uses a global mutex, so it will only provide protection
    /// for instances running on the same machine.
    /// </summary>
    public class WorldObjectMutex : IDisposable
    {
        public readonly IWorldObject WorldObject;
        private readonly Mutex _mutex;

        private WorldObjectMutex(IWorldObject wo, Mutex mutex)
        {
            _mutex = mutex;
            WorldObject = wo;
        }

        public static WorldObjectMutex Obtain(IWorldObject wo)
        {
            bool owned;
            var mutex = new Mutex(true, string.Format("Global\\re_{0}", wo.Id), out owned);

            if (!owned)
                mutex.WaitOne();

            return new WorldObjectMutex(wo, mutex);
        }

        public void Dispose()
        {
            _mutex.ReleaseMutex();
            _mutex.Close();
        }
    }
}<commit_msg>Some additions to WorldObjectMutex<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using RedoxLib.Objects;

namespace RedoxLib.Utilities
{
    /// <summary>
    /// A mutex that synchronizes access to a world object.
    /// 
    /// Note: Currently this implementation uses a global mutex, so it will only provide protection
    /// for instances running on the same machine.
    /// </summary>
    public class WorldObjectMutex : IDisposable
    {
        public readonly IWorldObject WorldObject;
        private readonly Mutex _mutex;
        private bool _owned;

        private WorldObjectMutex(IWorldObject wo, Mutex mutex, bool owned)
        {
            _mutex = mutex;
            WorldObject = wo;
            _owned = owned;
        }

        public static WorldObjectMutex Create(IWorldObject wo)
        {
            var mutex = new Mutex(false, string.Format("Global\\re_{0}", wo.Id));
            return new WorldObjectMutex(wo, mutex, false);
        }

        public static WorldObjectMutex Obtain(IWorldObject wo)
        {
            bool owned;
            var mutex = new Mutex(true, string.Format("Global\\re_{0}", wo.Id), out owned);

            if (!owned)
                mutex.WaitOne();

            return new WorldObjectMutex(wo, mutex, true);
        }

        public void Obtain()
        {
            if (_owned)
                return;

            _mutex.WaitOne();
            _owned = true;
        }

        public bool TryObtain(int millisecondsTimeout)
        {
            if (_owned)
                return true;

            _owned = _mutex.WaitOne(millisecondsTimeout);
            return _owned;
        }

        public void Dispose()
        {
            _mutex.ReleaseMutex();
            _mutex.Close();
        }
    }
}
{'fraction_non_alphanumeric': 0.08784676354029063, 'fraction_numerical': 0.0009907529722589167, 'mean_word_length': 1.8255597014925373, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4538943', 'n_tokens_mistral': 805, 'n_tokens_neox': 799, 'n_words': 236}