|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System;
- using System.Threading;
- using Svelto.Common;
-
- namespace Svelto.ECS.DataStructures
- {
- public struct SharedNativeInt: IDisposable
- {
- #if UNITY_COLLECTIONS || UNITY_JOBS || UNITY_BURST
- [global::Unity.Collections.LowLevel.Unsafe.NativeDisableUnsafePtrRestriction]
- #endif
- unsafe int* data;
-
- Allocator _allocator;
-
- public SharedNativeInt(Allocator allocator)
- {
- unsafe
- {
- _allocator = allocator;
- data = (int*) MemoryUtilities.Alloc(sizeof(int), allocator);
- }
- }
-
- public static SharedNativeInt Create(int t, Allocator allocator)
- {
- unsafe
- {
- var current = new SharedNativeInt();
- current._allocator = allocator;
- current.data = (int*) MemoryUtilities.Alloc(sizeof(int), allocator);
- *current.data = t;
-
- return current;
- }
- }
-
- public static implicit operator int(SharedNativeInt t)
- {
- unsafe
- {
- #if DEBUG && !PROFILE_SVELTO
- if (t.data == null)
- throw new Exception("using disposed SharedInt");
- #endif
- return *t.data;
- }
- }
-
- public void Dispose()
- {
- unsafe
- {
- if (data != null)
- {
- MemoryUtilities.Free((IntPtr) data, _allocator);
- data = null;
- }
- }
- }
-
- public int Decrement()
- {
- unsafe
- {
- #if DEBUG && !PROFILE_SVELTO
- if (data == null)
- throw new Exception("null-access");
- #endif
-
- return Interlocked.Decrement(ref *data);
- }
- }
-
- public int Increment()
- {
- unsafe
- {
- #if DEBUG && !PROFILE_SVELTO
- if (data == null)
- throw new Exception("null-access");
- #endif
-
- return Interlocked.Increment(ref *data);
- }
- }
-
- public int Add(int val)
- {
- unsafe
- {
- #if DEBUG && !PROFILE_SVELTO
- if (data == null)
- throw new Exception("null-access");
- #endif
-
- return Interlocked.Add(ref *data, val);
- }
- }
-
- public int CompareExchange(int value, int compare)
- {
- unsafe
- {
- #if DEBUG && !PROFILE_SVELTO
- if (data == null)
- throw new Exception("null-access");
- #endif
-
- return Interlocked.CompareExchange(ref *data, value, compare);
- }
- }
-
- public void Set(int val)
- {
- unsafe
- {
- #if DEBUG && !PROFILE_SVELTO
- if (data == null)
- throw new Exception("null-access");
- #endif
-
- Volatile.Write(ref *data, val);
- }
- }
- }
- }
|