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

namespace ConcurrencyUtils
{
    /// <summary>
    ///     First in first out Semaphore. A semaphore that retains 
	/// 	order of acquires so as to avoid starvation of threads.
	/// 
	/// 	Author: <NAME> 971328X
    /// </summary>
    public class FIFOSemaphore: Semaphore
    {
        private Queue<ConcurrencyUtils.Semaphore> waitingThreadsQueue = new Queue<ConcurrencyUtils.Semaphore>();
        private readonly Object queueLock = new Object();

        /// <summary>
        ///     Public constructor that calls the internal 'Release'
        ///     method to correctly add tokens to the semaphore.
        /// </summary>
        /// <param name="n"></param>
        public FIFOSemaphore(UInt64 n = 0)
        {
            Release(n);
        }

        /// <summary>
        ///     The acquiring thread creates a semaphore of it's own and adds it to the queue. 
        ///     The thread then tries to acquire from that semaphore. Once acquired, the thread can acquire a token.
        /// </summary>
        public override void Acquire()
        {
            Semaphore threadSemaphore;
            lock(queueLock)
            {
                threadSemaphore = new Semaphore(0);
				waitingThreadsQueue.Enqueue (threadSemaphore);
            }

            threadSemaphore.Acquire();
            base.Acquire();
        }

        /// <summary>
        ///     Release n tokens. If there are threads waiting, then dequeue and signal them in order.
        /// </summary>
        /// <param name="n">The number of tokens to release.</param>
        public override void Release(UInt64 n = 1)
        {
            base.Release(n);
            lock (queueLock)
            {
                if (waitingThreadsQueue.Count > 0)
                {
                    UInt64 queueSize = UInt64.Parse(waitingThreadsQueue.Count.ToString());
                    if (queueSize <= n)
                    {
                        for (UInt64 i = 0; i < n; i++)
                        {
                            Semaphore aThreadSemaphore = waitingThreadsQueue.Dequeue();
                            aThreadSemaphore.Release();
                        }<commit_msg>FIX: Hopefully a good change to the FIFO semaphore<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConcurrencyUtils
{
    /// <summary>
    ///     First in first out Semaphore. A semaphore that retains 
	/// 	order of acquires so as to avoid starvation of threads.
	/// 
	/// 	Author: <NAME> 971328X
    /// </summary>
    public class FIFOSemaphore: Semaphore
    {
        private Queue<ConcurrencyUtils.Semaphore> waitingThreadsQueue = new Queue<ConcurrencyUtils.Semaphore>();
        private readonly Object queueLock = new Object();

        /// <summary>
        ///     Public constructor that calls the internal 'Release'
        ///     method to correctly add tokens to the semaphore.
        /// </summary>
        /// <param name="n"></param>
        public FIFOSemaphore(UInt64 n = 0)
        {
            Release(n);
        }

        /// <summary>
        ///     The acquiring thread creates a semaphore of it's own and adds it to the queue. 
        ///     The thread then tries to acquire from that semaphore. Once acquired, the thread can acquire a token.
        /// </summary>
        public override void Acquire()
        {
            Semaphore threadSemaphore;
            lock(queueLock)
            {
                lock (lockObject)
                {
					if (tokens > 0 && (waitingThreadsQueue.Count == 0))
                    {
                        tokens--;
                        return;
                    }
                    else
                    {
                        threadSemaphore = new Semaphore(0);
                        waitingThreadsQueue.Enqueue(threadSemaphore);
                    }
                }
            }
            
            threadSemaphore.Acquire();
            base.Acquire();
        }

        /// <summary>
        ///     Release n tokens. If there are threads waiting, then dequeue and signal them in order.
        /// </summary>
        /// <param name="n">The number of tokens to release.</param>
        public override void Release(UInt64 n = 1)
        {
            base.Release(n);
            lock (queueLock)
            {
                if (waitingThreadsQueue.Count > 0)
                {
                    UInt64 queueSize = UInt64.Parse(waitingThreadsQueue.Count.ToString());
                    if (queueSize <= n)
                    {
                        for (UInt64 i = 0; i < n; i++)
                        {
                            Semaphore aThreadSemaphore = waitingThreadsQueue.Dequeue();
                            aThreadSemaphore.Release();
                        }
{'fraction_non_alphanumeric': 0.09431317581097316, 'fraction_numerical': 0.00881057268722467, 'mean_word_length': 1.3888091822094693, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 36, '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': '3936448', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1242, 'n_words': 369}