Skip to content

Commit a695499

Browse files
committed
Hacks for Unity iOS compatibility (No Interlocked.CompareExchange<T> allowed)
1 parent f8c0863 commit a695499

File tree

12 files changed

+201
-81
lines changed

12 files changed

+201
-81
lines changed

Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
3939

4040
[assembly: AssemblyVersion("1.0.*")]
41+
[assembly: InternalsVisibleTo("Couchbase.Lite.Unity")]
4142

4243
// The following attributes are used to specify the signing key for the assembly,
4344
// if desired. See the Mono documentation for more information about signing.

System.Collections.Concurrent/ConcurrentOrderedList.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ class Node
3535
{
3636
public T Data;
3737
public int Key;
38-
public Node Next;
38+
public object next;
39+
public Node Next
40+
{
41+
get { return (Node)next; }
42+
}
43+
3944
public bool Marked;
4045

4146
public Node ()
@@ -46,7 +51,7 @@ public Node ()
4651
public Node (Node wrapped)
4752
{
4853
Marked = true;
49-
Next = wrapped;
54+
next = wrapped;
5055
}
5156
}
5257

@@ -71,7 +76,7 @@ public ConcurrentOrderedList (IEqualityComparer<T> comparer)
7176

7277
head = new Node ();
7378
tail = new Node ();
74-
head.Next = tail;
79+
head.next = tail;
7580
}
7681

7782
public bool TryAdd (T data)
@@ -139,7 +144,7 @@ public bool TryGetFromHash (int key, out T data)
139144

140145
public void Clear ()
141146
{
142-
head.Next = tail;
147+
head.next = tail;
143148
}
144149

145150
public void CopyTo (T[] array, int startIndex)
@@ -199,7 +204,7 @@ Node ListSearch (int key, ref Node left)
199204
return rightNode;
200205
}
201206

202-
if (Interlocked.CompareExchange (ref left.Next, rightNode, leftNodeNext) == leftNodeNext) {
207+
if (Interlocked.CompareExchange (ref left.next, rightNode, leftNodeNext) == leftNodeNext) {
203208
if (rightNode != tail && rightNode.Next.Marked)
204209
continue;
205210
else
@@ -222,11 +227,11 @@ bool ListDelete (int key, out T data)
222227

223228
rightNodeNext = rightNode.Next;
224229
if (!rightNodeNext.Marked)
225-
if (Interlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
230+
if (Interlocked.CompareExchange (ref rightNode.next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
226231
break;
227232
} while (true);
228233

229-
if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
234+
if (Interlocked.CompareExchange (ref leftNode.next, rightNodeNext, rightNode) != rightNodeNext)
230235
ListSearch (rightNode.Key, ref leftNode);
231236

232237
return true;
@@ -246,11 +251,11 @@ bool ListPop (out T data)
246251

247252
rightNodeNext = rightNode.Next;
248253
if (!rightNodeNext.Marked)
249-
if (Interlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
254+
if (Interlocked.CompareExchange (ref rightNode.next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext)
250255
break;
251256
} while (true);
252257

253-
if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
258+
if (Interlocked.CompareExchange (ref leftNode.next, rightNodeNext, rightNode) != rightNodeNext)
254259
ListSearch (rightNode.Key, ref leftNode);
255260

256261
return true;
@@ -266,8 +271,8 @@ bool ListInsert (Node newNode)
266271
if (rightNode != tail && rightNode.Key == key)
267272
return false;
268273

269-
newNode.Next = rightNode;
270-
if (Interlocked.CompareExchange (ref leftNode.Next, newNode, rightNode) == rightNode)
274+
newNode.next = rightNode;
275+
if (Interlocked.CompareExchange (ref leftNode.next, newNode, rightNode) == rightNode)
271276
return true;
272277
} while (true);
273278
}

System.Collections.Concurrent/ConcurrentQueue.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,23 @@ public class ConcurrentQueue<T> : IProducerConsumerCollection<T>, IEnumerable<T>
3838
class Node
3939
{
4040
public T Value;
41-
public Node Next;
41+
public Node Next
42+
{
43+
get { return (Node)next; }
44+
}
45+
public object next;
4246
}
4347

44-
Node head = new Node ();
45-
Node tail;
48+
object head = new Node ();
49+
Node Head {
50+
get { return (Node)head; }
51+
}
52+
53+
object tail;
54+
Node Tail {
55+
get { return (Node)tail; }
56+
}
57+
4658
int count;
4759

4860
public ConcurrentQueue ()
@@ -66,14 +78,14 @@ public void Enqueue (T item)
6678

6779
bool update = false;
6880
while (!update) {
69-
oldTail = tail;
81+
oldTail = Tail;
7082
oldNext = oldTail.Next;
7183

7284
// Did tail was already updated ?
7385
if (tail == oldTail) {
7486
if (oldNext == null) {
7587
// The place is for us
76-
update = Interlocked.CompareExchange (ref tail.Next, node, null) == null;
88+
update = Interlocked.CompareExchange (ref Tail.next, node, null) == null;
7789
} else {
7890
// another Thread already used the place so give him a hand by putting tail where it should be
7991
Interlocked.CompareExchange (ref tail, oldNext, oldTail);
@@ -98,8 +110,8 @@ public bool TryDequeue (out T result)
98110
bool advanced = false;
99111

100112
while (!advanced) {
101-
Node oldHead = head;
102-
Node oldTail = tail;
113+
Node oldHead = Head;
114+
Node oldTail = Tail;
103115
oldNext = oldHead.Next;
104116

105117
if (oldHead == head) {
@@ -134,7 +146,7 @@ public bool TryPeek (out T result)
134146

135147
while (update)
136148
{
137-
Node oldHead = head;
149+
Node oldHead = Head;
138150
Node oldNext = oldHead.Next;
139151

140152
if (oldNext == null) {
@@ -168,7 +180,7 @@ public IEnumerator<T> GetEnumerator ()
168180

169181
IEnumerator<T> InternalGetEnumerator ()
170182
{
171-
Node my_head = head;
183+
Node my_head = Head;
172184
while ((my_head = my_head.Next) != null) {
173185
yield return my_head.Value;
174186
}

System.Collections.Concurrent/ConcurrentStack.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ class Node
4545
public Node Next;
4646
}
4747

48-
Node head;
48+
object head;
49+
Node Head
50+
{
51+
get { return (Node)head; }
52+
}
4953

5054
int count;
5155

@@ -73,7 +77,7 @@ public void Push (T item)
7377
Node temp = new Node ();
7478
temp.Value = item;
7579
do {
76-
temp.Next = head;
80+
temp.Next = Head;
7781
} while (Interlocked.CompareExchange (ref head, temp, temp.Next) != temp.Next);
7882

7983
Interlocked.Increment (ref count);
@@ -105,7 +109,7 @@ public void PushRange (T[] items, int startIndex, int count)
105109
}
106110

107111
do {
108-
first.Next = head;
112+
first.Next = Head;
109113
} while (Interlocked.CompareExchange (ref head, insert, first.Next) != first.Next);
110114

111115
Interlocked.Add (ref this.count, count);
@@ -115,7 +119,7 @@ public bool TryPop (out T result)
115119
{
116120
Node temp;
117121
do {
118-
temp = head;
122+
temp = Head;
119123
// The stak is empty
120124
if (temp == null) {
121125
result = default (T);
@@ -145,7 +149,7 @@ public int TryPopRange (T[] items, int startIndex, int count)
145149
Node end;
146150

147151
do {
148-
temp = head;
152+
temp = Head;
149153
if (temp == null)
150154
return 0;
151155
end = temp;
@@ -169,7 +173,7 @@ public int TryPopRange (T[] items, int startIndex, int count)
169173

170174
public bool TryPeek (out T result)
171175
{
172-
Node myHead = head;
176+
Node myHead = Head;
173177
if (myHead == null) {
174178
result = default (T);
175179
return false;
@@ -197,7 +201,7 @@ public IEnumerator<T> GetEnumerator ()
197201

198202
IEnumerator<T> InternalGetEnumerator ()
199203
{
200-
Node my_head = head;
204+
Node my_head = Head;
201205
if (my_head == null) {
202206
yield break;
203207
} else {

System.Collections.Concurrent/SplitOrderedList.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class Node
3636
public ulong Key;
3737
public TKey SubKey;
3838
public T Data;
39-
public Node Next;
39+
public object next;
40+
public Node Next {
41+
get { return (Node)next; }
42+
}
4043

4144
public Node Init (ulong key, TKey subKey, T data)
4245
{
@@ -45,7 +48,7 @@ public Node Init (ulong key, TKey subKey, T data)
4548
this.Data = data;
4649

4750
this.Marked = false;
48-
this.Next = null;
51+
this.next = null;
4952

5053
return this;
5154
}
@@ -56,7 +59,7 @@ public Node Init (ulong key)
5659
this.Key = key;
5760
this.Data = default (T);
5861

59-
this.Next = null;
62+
this.next = null;
6063
this.Marked = false;
6164
this.SubKey = default (TKey);
6265

@@ -67,7 +70,7 @@ public Node Init (ulong key)
6770
public Node Init (Node wrapped)
6871
{
6972
this.Marked = true;
70-
this.Next = wrapped;
73+
this.next = wrapped;
7174

7275
this.Key = 0;
7376
this.Data = default (T);
@@ -83,7 +86,7 @@ public Node Init (Node wrapped)
8386
Node head;
8487
Node tail;
8588

86-
Node[] buckets = new Node [BucketSize];
89+
object[] buckets = new object [BucketSize];
8790
int count;
8891
int size = 2;
8992

@@ -96,7 +99,7 @@ public SplitOrderedList (IEqualityComparer<TKey> comparer)
9699
this.comparer = comparer;
97100
head = new Node ().Init (0);
98101
tail = new Node ().Init (ulong.MaxValue);
99-
head.Next = tail;
102+
head.next = tail;
100103
SetBucket (0, head);
101104
}
102105

@@ -280,7 +283,7 @@ Node GetBucket (uint index)
280283
{
281284
if (index >= buckets.Length)
282285
return null;
283-
return buckets[index];
286+
return (Node)buckets[index];
284287
}
285288

286289
Node SetBucket (uint index, Node node)
@@ -289,8 +292,8 @@ Node SetBucket (uint index, Node node)
289292
slim.EnterReadLock ();
290293
CheckSegment (index, true);
291294

292-
Interlocked.CompareExchange (ref buckets[index], node, null);
293-
return buckets[index];
295+
Interlocked.CompareExchange (ref buckets[index], node, null);
296+
return (Node)buckets[index];
294297
} finally {
295298
slim.ExitReadLock ();
296299
}
@@ -343,7 +346,7 @@ Node ListSearch (ulong key, TKey subKey, ref Node left, Node h)
343346
return rightNode;
344347
}
345348

346-
if (Interlocked.CompareExchange (ref left.Next, rightNode, leftNodeNext) == leftNodeNext) {
349+
if (Interlocked.CompareExchange (ref left.next, rightNode, leftNodeNext) == leftNodeNext) {
347350
if (rightNode != tail && rightNode.Next.Marked)
348351
continue;
349352
else
@@ -371,12 +374,12 @@ bool ListDelete (Node startPoint, ulong key, TKey subKey, out T data)
371374
markedNode = new Node ();
372375
markedNode.Init (rightNodeNext);
373376

374-
if (Interlocked.CompareExchange (ref rightNode.Next, markedNode, rightNodeNext) == rightNodeNext)
377+
if (Interlocked.CompareExchange (ref rightNode.next, markedNode, rightNodeNext) == rightNodeNext)
375378
break;
376379
}
377380
} while (true);
378381

379-
if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNode)
382+
if (Interlocked.CompareExchange (ref leftNode.next, rightNodeNext, rightNode) != rightNode)
380383
ListSearch (rightNode.Key, subKey, ref leftNode, startPoint);
381384

382385
return true;
@@ -392,10 +395,10 @@ bool ListInsert (Node newNode, Node startPoint, out Node current, Func<T> dataCr
392395
if (rightNode != tail && rightNode.Key == key && comparer.Equals (newNode.SubKey, rightNode.SubKey))
393396
return false;
394397

395-
newNode.Next = rightNode;
398+
newNode.next = rightNode;
396399
if (dataCreator != null)
397400
newNode.Data = dataCreator ();
398-
if (Interlocked.CompareExchange (ref leftNode.Next, newNode, rightNode) == rightNode)
401+
if (Interlocked.CompareExchange (ref leftNode.next, newNode, rightNode) == rightNode)
399402
return true;
400403
} while (true);
401404
}

0 commit comments

Comments
 (0)