Example #1
0
        /// <summary>
        /// Safely disposes of this container and deallocates its memory when the jobs that use it have completed.
        /// </summary>
        /// <remarks>You can call this function dispose of the container immediately after scheduling the job. Pass
        /// the [JobHandle](https://docs.unity3d.com/ScriptReference/Unity.Jobs.JobHandle.html) returned by
        /// the [Job.Schedule](https://docs.unity3d.com/ScriptReference/Unity.Jobs.IJobExtensions.Schedule.html)
        /// method using the `jobHandle` parameter so the job scheduler can dispose the container after all jobs
        /// using it have run.</remarks>
        /// <param name="jobHandle">The job handle or handles for any scheduled jobs that use this container.</param>
        /// <returns>A new job handle containing the prior handles as well as the handle for the job that deletes
        /// the container.</returns>
        public JobHandle Dispose(JobHandle inputDeps)
        {
            if (m_AllocatorLabel == Allocator.Invalid)
            {
                throw new InvalidOperationException("The NativeArray can not be Disposed because it was not allocated with a valid allocator.");
            }

            if (m_Buffer == null)
            {
                throw new InvalidOperationException("The NativeArray is already disposed.");
            }

            if (m_AllocatorLabel > Allocator.None)
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                // [DeallocateOnJobCompletion] is not supported, but we want the deallocation
                // to happen in a thread. DisposeSentinel needs to be cleared on main thread.
                // AtomicSafetyHandle can be destroyed after the job was scheduled (Job scheduling
                // will check that no jobs are writing to the container).
                DisposeSentinel.Clear(ref m_DisposeSentinel);

                var jobHandle = new NativeArrayDisposeJob {
                    Data = new NativeArrayDispose {
                        m_Buffer = m_Buffer, m_AllocatorLabel = m_AllocatorLabel, m_Safety = m_Safety
                    }
                }.Schedule(inputDeps);

                AtomicSafetyHandle.Release(m_Safety);
#else
                var jobHandle = new NativeArrayDisposeJob {
                    Data = new NativeArrayDispose {
                        m_Buffer = m_Buffer, m_AllocatorLabel = m_AllocatorLabel
                    }
                }.Schedule(inputDeps);
#endif

                m_Buffer         = null;
                m_Length         = 0;
                m_AllocatorLabel = Allocator.Invalid;

                return(jobHandle);
            }

            m_Buffer = null;
            m_Length = 0;

            return(inputDeps);
        }
Example #2
0
        public JobHandle Dispose(JobHandle inputDeps)
        {
            bool flag = this.m_AllocatorLabel == Allocator.Invalid;

            if (flag)
            {
                throw new InvalidOperationException("The NativeArray can not be Disposed because it was not allocated with a valid allocator.");
            }
            bool flag2 = this.m_Buffer == null;

            if (flag2)
            {
                throw new InvalidOperationException("The NativeArray is already disposed.");
            }
            bool      flag3 = this.m_AllocatorLabel > Allocator.None;
            JobHandle result;

            if (flag3)
            {
                DisposeSentinel.Clear(ref this.m_DisposeSentinel);
                JobHandle jobHandle = new NativeArrayDisposeJob
                {
                    Data = new NativeArrayDispose
                    {
                        m_Buffer         = this.m_Buffer,
                        m_AllocatorLabel = this.m_AllocatorLabel,
                        m_Safety         = this.m_Safety
                    }
                }.Schedule(inputDeps);
                AtomicSafetyHandle.Release(this.m_Safety);
                this.m_Buffer         = null;
                this.m_Length         = 0;
                this.m_AllocatorLabel = Allocator.Invalid;
                result = jobHandle;
            }
            else
            {
                this.m_Buffer = null;
                this.m_Length = 0;
                result        = inputDeps;
            }
            return(result);
        }
        /// <summary>
        /// Safely disposes of this container and deallocates its memory when the jobs that use it have completed.
        /// </summary>
        /// <remarks>You can call this function dispose of the container immediately after scheduling the job. Pass
        /// the [JobHandle](https://docs.unity3d.com/ScriptReference/Unity.Jobs.JobHandle.html) returned by
        /// the [Job.Schedule](https://docs.unity3d.com/ScriptReference/Unity.Jobs.IJobExtensions.Schedule.html)
        /// method using the `jobHandle` parameter so the job scheduler can dispose the container after all jobs
        /// using it have run.</remarks>
        /// <param name="jobHandle">The job handle or handles for any scheduled jobs that use this container.</param>
        /// <returns>A new job handle containing the prior handles as well as the handle for the job that deletes
        /// the container.</returns>
        public JobHandle Dispose(JobHandle inputDeps)
        {
            // [DeallocateOnJobCompletion] is not supported, but we want the deallocation
            // to happen in a thread. DisposeSentinel needs to be cleared on main thread.
            // AtomicSafetyHandle can be destroyed after the job was scheduled (Job scheduling
            // will check that no jobs are writing to the container).
            DisposeSentinel.Clear(ref m_DisposeSentinel);

            var jobHandle = new NativeArrayDisposeJob {
                Data = new NativeArrayDispose {
                    m_Buffer = m_Buffer, m_AllocatorLabel = m_AllocatorLabel, m_Safety = m_Safety
                }
            }.Schedule(inputDeps);

            AtomicSafetyHandle.Release(m_Safety);
            m_Buffer = null;
            m_Length = 0;

            return(jobHandle);
        }