<commit_before>﻿using System;
using System.Collections.Generic;

namespace CSGL.Vulkan {
	public class FenceCreateInfo {
        public VkFenceCreateFlags Flags { get; set; }
    }

    public class Fence : IDisposable, INative<VkFence> {
        VkFence fence;
        bool disposed = false;

		public VkFence Native {
            get {
                return fence;
            }
        }

        public VkResult Status {
            get {
                return Device.Commands.getFenceStatus(Device.Native, fence);
            }
        }

		public Device Device { get; private set; }

		public static VkResult Reset(Device device, Fence[] fences) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Length];
                Interop.Marshal(fences, fencesNative);
                
                var result = device.Commands.resetFences(device.Native, (uint)fences.Length, (IntPtr)fencesNative);
                if (result != VkResult.Success) throw new FenceException(string.Format("Error resetting fences: {0}", result));
                return result;
            }
        }

        public static VkResult Reset(Device device, List<Fence> fences) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Count];
                Interop.Marshal<VkFence, Fence>(fences, fencesNative);

                var result = device.Commands.resetFences(device.Native, (uint)fences.Count, (IntPtr)fencesNative);
                if (result != VkResult.Success) throw new FenceException(string.Format("Error resetting fences: {0}", result));
                return result;
            }
        }

		public static VkResult Wait(Device device, Fence[] fences, bool waitAll, ulong timeout) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Length];
                Interop.Marshal(fences, fencesNative);
                
                uint waitAllNative = waitAll ? 1u : 0u;
                var result = device.Commands.waitFences(device.Native, (uint)fences.Length, (IntPtr)fencesNative, waitAllNative, timeout);
                if (!(result == VkResult.Success || result == VkResult.Timeout)) throw new FenceException(string.Format("Error waiting on fences: {0}", result));
                return result;
            }
        }

        public static VkResult Wait(Device device, List<Fence> fences, bool waitAll, ulong timeout) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Count];
                Interop.Marshal<VkFence, Fence>(fences, fencesNative);

                uint waitAllNative = waitAll ? 1u : 0u;
                var result = device.Commands.waitFences(device.Native, (uint)fences.Count, (IntPtr)fencesNative, waitAllNative, timeout);
                if (!(result == VkResult.Success || result == VkResult.Timeout)) throw new FenceException(string.Format("Error waiting on fences: {0}", result));
                return result;
            }
        }

		public Fence(Device device, FenceCreateInfo info) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (info == null) throw new ArgumentNullException(nameof(info));

            Device = device;

            CreateFence(info);
        }

		void CreateFence(FenceCreateInfo mInfo) {
            VkFenceCreateInfo info = new VkFenceCreateInfo();
            info.sType = VkStructureType.FenceCreateInfo;
            info.flags = mInfo.Flags;
            
            var result = Device.Commands.createFence(Device.Native, ref info, Device.Instance.AllocationCallbacks, out fence);
            if (result != VkResult.Success) throw new FenceException(string.Format("Error creating fence: {0}", result));
        }

		public VkResult Reset() {
            return Reset(Device, new Fence[] { this });
        }

		public VkResult Wait(ulong timeout) {
            return Wait(Device, new Fence[] { this }, false, timeout);
        }

		public VkResult Wait() {
            return Wait(ulong.MaxValue);
        }

		public void Dispose() {
            if (disposed) return;

            Device.Commands.destroyFence(Device.Native, fence, Device.Instance.AllocationCallbacks);

            disposed = true;
        }
    }

	public class FenceException : Exception {
		public FenceException(string message) : base(message) { }
    }
}<commit_msg>Implement Dispose pattern<commit_after>﻿using System;
using System.Collections.Generic;

namespace CSGL.Vulkan {
	public class FenceCreateInfo {
        public VkFenceCreateFlags Flags { get; set; }
    }

    public class Fence : IDisposable, INative<VkFence> {
        VkFence fence;
        bool disposed = false;

		public VkFence Native {
            get {
                return fence;
            }
        }

        public VkResult Status {
            get {
                return Device.Commands.getFenceStatus(Device.Native, fence);
            }
        }

		public Device Device { get; private set; }

		public static VkResult Reset(Device device, Fence[] fences) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Length];
                Interop.Marshal(fences, fencesNative);
                
                var result = device.Commands.resetFences(device.Native, (uint)fences.Length, (IntPtr)fencesNative);
                if (result != VkResult.Success) throw new FenceException(string.Format("Error resetting fences: {0}", result));
                return result;
            }
        }

        public static VkResult Reset(Device device, List<Fence> fences) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Count];
                Interop.Marshal<VkFence, Fence>(fences, fencesNative);

                var result = device.Commands.resetFences(device.Native, (uint)fences.Count, (IntPtr)fencesNative);
                if (result != VkResult.Success) throw new FenceException(string.Format("Error resetting fences: {0}", result));
                return result;
            }
        }

		public static VkResult Wait(Device device, Fence[] fences, bool waitAll, ulong timeout) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Length];
                Interop.Marshal(fences, fencesNative);
                
                uint waitAllNative = waitAll ? 1u : 0u;
                var result = device.Commands.waitFences(device.Native, (uint)fences.Length, (IntPtr)fencesNative, waitAllNative, timeout);
                if (!(result == VkResult.Success || result == VkResult.Timeout)) throw new FenceException(string.Format("Error waiting on fences: {0}", result));
                return result;
            }
        }

        public static VkResult Wait(Device device, List<Fence> fences, bool waitAll, ulong timeout) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (fences == null) throw new ArgumentNullException(nameof(fences));

            unsafe
            {
                var fencesNative = stackalloc VkFence[fences.Count];
                Interop.Marshal<VkFence, Fence>(fences, fencesNative);

                uint waitAllNative = waitAll ? 1u : 0u;
                var result = device.Commands.waitFences(device.Native, (uint)fences.Count, (IntPtr)fencesNative, waitAllNative, timeout);
                if (!(result == VkResult.Success || result == VkResult.Timeout)) throw new FenceException(string.Format("Error waiting on fences: {0}", result));
                return result;
            }
        }

		public Fence(Device device, FenceCreateInfo info) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (info == null) throw new ArgumentNullException(nameof(info));

            Device = device;

            CreateFence(info);
        }

		void CreateFence(FenceCreateInfo mInfo) {
            VkFenceCreateInfo info = new VkFenceCreateInfo();
            info.sType = VkStructureType.FenceCreateInfo;
            info.flags = mInfo.Flags;
            
            var result = Device.Commands.createFence(Device.Native, ref info, Device.Instance.AllocationCallbacks, out fence);
            if (result != VkResult.Success) throw new FenceException(string.Format("Error creating fence: {0}", result));
        }

		public VkResult Reset() {
            return Reset(Device, new Fence[] { this });
        }

		public VkResult Wait(ulong timeout) {
            return Wait(Device, new Fence[] { this }, false, timeout);
        }

		public VkResult Wait() {
            return Wait(ulong.MaxValue);
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        void Dispose(bool disposing) {
            if (disposed) return;

            Device.Commands.destroyFence(Device.Native, fence, Device.Instance.AllocationCallbacks);

            disposed = true;
        }

        ~Fence() {
            Dispose(false);
        }
    }

	public class FenceException : Exception {
		public FenceException(string message) : base(message) { }
    }
}
{'fraction_non_alphanumeric': 0.09756803705848292, 'fraction_numerical': 0.0017371163867979154, 'mean_word_length': 2.150805716023107, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7457163', 'n_tokens_mistral': 2857, 'n_tokens_neox': 2738, 'n_words': 683}