Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.arch > #31405 > unrolled thread
| Started by | jacko <jackokring@gmail.com> |
|---|---|
| First post | 2015-12-03 18:56 -0800 |
| Last post | 2015-12-06 13:37 -0800 |
| Articles | 10 on this page of 30 — 5 participants |
Back to article view | Back to comp.arch
Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-03 18:56 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-03 19:22 -0800
Re: Efficient lock free FIFO Ivan Godard <ivan@millcomputing.com> - 2015-12-03 19:32 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-03 19:36 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-03 19:40 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-03 21:46 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-04 00:12 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-04 13:59 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-04 14:06 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-04 15:36 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-04 15:40 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-04 15:53 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-05 13:36 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-05 13:39 -0800
Re: Efficient lock free FIFO already5chosen@yahoo.com - 2015-12-04 01:59 -0800
Re: Efficient lock free FIFO nmm@wheeler.UUCP (Nick Maclaren) - 2015-12-04 10:28 +0000
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-04 07:08 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-05 17:55 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-05 17:59 -0800
Re: Efficient lock free FIFO already5chosen@yahoo.com - 2015-12-06 04:03 -0800
Re: Efficient lock free FIFO nmm@wheeler.UUCP (Nick Maclaren) - 2015-12-06 12:43 +0000
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-04 06:56 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-04 07:04 -0800
Re: Efficient lock free FIFO already5chosen@yahoo.com - 2015-12-05 08:56 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-05 13:43 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-05 13:51 -0800
Re: Efficient lock free FIFO (Something ...?) jacko <jackokring@gmail.com> - 2015-12-06 07:08 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-06 11:58 -0800
Re: Efficient lock free FIFO "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-12-06 13:02 -0800
Re: Efficient lock free FIFO jacko <jackokring@gmail.com> - 2015-12-06 13:37 -0800
Page 2 of 2 — ← Prev page 1 [2]
| From | nmm@wheeler.UUCP (Nick Maclaren) |
|---|---|
| Date | 2015-12-06 12:43 +0000 |
| Message-ID | <n41ah9$hvt$1@dont-email.me> |
| In reply to | #31483 |
In article <9f7955f4-9b51-43fc-a23f-0ef1fa47f944@googlegroups.com>, <already5chosen@yahoo.com> wrote: >On Sunday, December 6, 2015 at 3:55:55 AM UTC+2, Chris M. Thomasson wrote: >> >> > It's useful, and has been used since time immemorial. The main use >> > of such techniques is with interrupt handling, where the key factor >> > is to be able to detect an interrupted update and fix it up without >> > causing other problems. >> >> Indeed they are extremely useful for such conditions. >> >Can you elaborate? > >To explain myself, I have no doubt that FIFOs implemented as >single-linked-list are useful. But I have serious doubt that lockless >implementation of such FIFO is useful in the context that you and Nick >presented. The scenario is this. A program needs to update and use some sort of data structure, but can be interrupted asynchronously. Very common. The run-time system then needs to trap the interrupt, clean up the data structure enough that it can be used, and transfer control to the code that will continue the execution. All that is needed is a reliable way to detect that a data structure was interrupted during an update, and a reliable way of cancelling or completing the update. The fixup code is not in the fast path, but the ordinary updates assuredly are, so that lock-free methods are almost essential. A variant of this is an asynchronous interrupt while a lock or unlock operation is in progress. Any lock mechanism that doesn't support that is inadequate for any system where asynchronous interrupts are possible. FIFOs are not special, but they are one of the most common forms of data structure needed in real code. Regards, Nick Maclaren.
[toc] | [prev] | [next] | [standalone]
| From | jacko <jackokring@gmail.com> |
|---|---|
| Date | 2015-12-04 06:56 -0800 |
| Message-ID | <4b280570-70dd-43b5-9265-f15a2e204b6c@googlegroups.com> |
| In reply to | #31415 |
> I don't see ho such thing can be potentially useful. > The part, omitted in your description, is a memory allocator. > For allocator I see two possibilities. An initialized FIFO with many elements, connected back to the same thread can be used as a free list ...
[toc] | [prev] | [next] | [standalone]
| From | jacko <jackokring@gmail.com> |
|---|---|
| Date | 2015-12-04 07:04 -0800 |
| Message-ID | <97630a3e-868b-461f-ad37-d96a7f11b183@googlegroups.com> |
| In reply to | #31421 |
And a FIFO from consumer to producer 'in reverse' to feedback used elements. So creating with 3* 1:1 FIFO a 1:1 FIFO with fixed maximum size control, and producer priority (sometimes done by consumer fixing it as so).
[toc] | [prev] | [next] | [standalone]
| From | already5chosen@yahoo.com |
|---|---|
| Date | 2015-12-05 08:56 -0800 |
| Message-ID | <710d5ba5-096d-42c0-96b3-f378d3354af6@googlegroups.com> |
| In reply to | #31421 |
On Friday, December 4, 2015 at 4:56:04 PM UTC+2, jacko wrote: > > I don't see ho such thing can be potentially useful. > > The part, omitted in your description, is a memory allocator. > > For allocator I see two possibilities. > > An initialized FIFO with many elements, connected back to the same thread can be used as a free list ... Using FIFO as a free list is a typical rookie mistake. LIFO is better - both simpler and friendlier to caching.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-12-05 13:43 -0800 |
| Message-ID | <n3vlqs$t2c$1@speranza.aioe.org> |
| In reply to | #31447 |
> wrote in message > news:710d5ba5-096d-42c0-96b3-f378d3354af6@googlegroups.com... > > On Friday, December 4, 2015 at 4:56:04 PM UTC+2, jacko wrote: [...] > > An initialized FIFO with many elements, connected back to the same > > thread can be used as a free list ... > Using FIFO as a free list is a typical rookie mistake. > LIFO is better - both simpler and friendlier to caching. Agreed. FWIW, sometimes one can use the typical lock-free LIFO algorithm and pull all the nodes off in a single atomic exchange op. That way one can get around having to use DWCAS and a version counter. Also, its wait-free for consumers. (DWCAS) is double width compare-and-exchange.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-12-05 13:51 -0800 |
| Message-ID | <n3vm9l$tus$1@speranza.aioe.org> |
| In reply to | #31472 |
> "Chris M. Thomasson" wrote in message > news:n3vlqs$t2c$1@speranza.aioe.org... [...] > FWIW, sometimes one can use the typical lock-free LIFO algorithm > and pull all the nodes off in a single atomic exchange op. [...] http://publibz.boulder.ibm.com/epubs/pdf/dz9zr002.pdf (Appendex A/Multi-Programming/Processing Examples/Conditional Swapping Instruction) Instead of using DWCAS to pull a single node at a time, use an atomic exchange/swap to get all the nodes at once. No version counter needed in this case. And one can delete nodes without causing the sky to fall... ;^)
[toc] | [prev] | [next] | [standalone]
| From | jacko <jackokring@gmail.com> |
|---|---|
| Date | 2015-12-06 07:08 -0800 |
| Subject | Re: Efficient lock free FIFO (Something ...?) |
| Message-ID | <c670e16e-50b3-42ea-826f-5aac9fb1c1c3@googlegroups.com> |
| In reply to | #31405 |
/*
* Copyright 2015 Simon Jackson.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package java.util;
/**
* A Queue optimised for spooling thread output.
*
* It is a regular FIFO, and supports just a single producer, and a single
* consumer which may be the same or different. Checks are made and errors are
* issued with incorrect use.
*
* @param <E> element type.
*/
public class ThreadSpoolQueue<E> extends AbstractQueue<E> {
private final Thread producer, consumer;
private volatile Item tail, spares;
private class Item {
E value;
volatile Item link;
}
/**
* This allows a producer controlled double buffer.
*
* @param other
*/
public void xchg(ThreadSpoolQueue<E> other) {
checkThread(producer);
other.checkThread(producer);
//to prevent consumer errors
if(consumer != other.consumer) checkThread(null);//default throw
Item tmp = tail;
tail = other.tail;
other.tail = tmp;
}
private Item asNew() {
Item added;
if (spares == null) {
added = new Item();
} else {
added = spares;
spares = added.link;
}
return added;
}
@Override
public boolean offer(E o) {
checkThread(producer);
if (o == null) {
throw new NullPointerException("Null is not queued");
}
Item added = asNew();
added.value = o;
Item t = tail;
Item h = t.link;
added.link = h;
tail = added;
t.link = added;
while (h.value == null && h.link != h) {
t = h;
h = h.link;
added.link = h;//compact
t.link = spares;
spares = t;
}
return true;//always completes
}
private E polla(boolean clear) {
checkThread(consumer);
Item t = tail;
Item h = t.link;
while (h.value == null && h.link != h) {
if(h == t) break;//empty
h = h.link;
}
E z = h.value;
if (clear) {
h.value = null;
}
return z;
}
@Override
public E poll() {
return polla(true);
}
@Override
public E peek() {
return polla(false);
}
// Should not be instantiated directly.
protected ThreadSpoolQueue(Thread from, Thread to) {
tail = new Item();
tail.link = tail;//enclose a blank element
producer = from;
consumer = to;
}
private void checkThread(Thread t) {
if (t == Thread.currentThread()) {
return;
}
throw new ConcurrentModificationException("One reader, one writer only");
}
@Override
public int size() {
int j = 0;
for (E e : this) {
j++;
}
return j;
}
@Override
public Iterator iterator() {
return new SpoolIterator();
}
@Override
public boolean isEmpty() {
return peek() == null;
}
private class SpoolIterator implements Iterator {
private final Item tailer;
private Item running;
public SpoolIterator() {
tailer = tail;
running = tailer.link;//get head
}
@Override
public void remove() {
//use null to indicate removal
checkThread(consumer);//essential!
running.value = null;
}
@Override
public boolean hasNext() {
return running != tailer;
}
@Override
public E next() {
running = running.link;//move next
while (running.value == null && hasNext()) {
running = running.link;
}
E z = running.value;
return (z == null) ? next() : z;//makes multithreaded
}
}
}
[toc] | [prev] | [next] | [standalone]
| From | jacko <jackokring@gmail.com> |
|---|---|
| Date | 2015-12-06 11:58 -0800 |
| Message-ID | <92565518-aa95-4dd5-8e82-83a9a31980ce@googlegroups.com> |
| In reply to | #31405 |
Some definite errors fixed and some efficiencies. <code follows>
/*
* Copyright 2015 Simon Jackson.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package java.util;
/**
* A Queue optimised for spooling thread output.
*
* It is a regular FIFO, and supports just a single producer, and a single
* consumer which may be the same or different. Checks are made and errors are
* issued with incorrect use.
*
* @param <E> element type.
*/
public class ThreadSpoolQueue<E> extends AbstractQueue<E> {
private final Thread producer, consumer;
private volatile Item tail, spares;
private volatile int wrote, wroteHi, safe, safeHi;
private volatile int sizeIn, sizeOut;
private int limiter;
private class Item {
E value;
volatile Item link;
}
/**
* This allows a producer controlled double buffer.
*
* @param other
*/
public void xchg(ThreadSpoolQueue<E> other) {
checkThread(producer);
other.checkThread(producer);
//to prevent consumer errors
if(consumer != other.consumer) checkThread(null);//default throw
if(wrote == Integer.MAX_VALUE) {
++wroteHi;
wrote = 0;
} else ++wrote;
Item tmp = tail;
tail = other.tail;
other.tail = tmp;
++safe;
if(safe == Integer.MAX_VALUE) {
safe = 0;//retrograde
++safeHi;
}
sizeIn = other.sizeIn = 0;
sizeOut = other.sizeOut = 0;
int swap = limiter;
limiter = other.limiter;
other.limiter = swap;
sizeIn = size();
other.sizeIn = other.size();
}
private Item asNew() {
Item added;
Item h = spares;
if (h == h.link) {
added = new Item();
} else {
added = spares;
spares = added.link;
}
return added;
}
@Override
public boolean offer(E o) {
checkThread(producer);
int expected = sizeIn - sizeOut;
if(expected > limiter) Thread.yield();
if (o == null) {
throw new NullPointerException("Null is not queued");
}
Item added = asNew();
added.value = o;
Item t = tail;
Item h = t.link;
added.link = h;
tail = added;
t.link = added;
++sizeIn;
Item st = h;
while (h.value == null && h.link != h) {
t = h;
h = h.link;
++sizeOut;
}
if(st == h) return true;
if(wrote == Integer.MAX_VALUE) {
++wroteHi;
wrote = 0;
} else ++wrote;
added.link = h;//compact
++safe;
if(safe == Integer.MAX_VALUE) {
safe = 0;//retrograde
++safeHi;
}
//iterators??!!
t.link = spares;
spares = st;
return true;//always completes
}
private E polla(boolean clear) {
if(clear) checkThread(consumer);//<- NEW
int expected = sizeIn - sizeOut;
if(expected < limiter) Thread.yield();
Item t = tail;
Item h = t.link;
boolean escape = false;
while (h.value == null && h.link != h) {
if(h == t) {
escape = true;
break;
}//empty
h = h.link;
}
E z = h.value;
if (clear) {
h.value = null;
}
//detect race
return (z == null && !escape) ? polla(clear) : z;
}
@Override
public E poll() {
return polla(true);
}
@Override
public E peek() {
return polla(false);
}
protected ThreadSpoolQueue(Thread from, Thread to) {
tail = new Item();
tail.link = tail;//enclose a blank element
spares = new Item();
spares.link = spares;//prevent NullPointerException
limiter = 100;
producer = from;
consumer = to;
}
protected ThreadSpoolQueue() {
throw new IllegalArgumentException("Must specify queue ends");
}
protected ThreadSpoolQueue(Thread from, Thread to, int lim) {
this(from, to);
limiter = lim;
}
private void checkThread(Thread t) {
if (t == Thread.currentThread()) {
return;
}
throw new ConcurrentModificationException("One reader:one writer only");
}
@Override
public int size() {//<- NEW allows all threads to size() and isEmpty()
int size = 0;
boolean done = false;
while(!done) {
try {
for(E e : this) {
++size;
if(Integer.MAX_VALUE == size) return size;//silly
}
done = true;
} catch(Exception e) {
size = 0;
}
}
return size;
}
@Override
public Iterator iterator() {
return new SpoolIterator();
}
private class SpoolIterator implements Iterator {
private final Item tailer;
private Item running;
private final int created, createdHi;
public SpoolIterator() {
tailer = tail;
running = tailer.link;//get head
created = safe;
createdHi = safeHi;
}
@Override
public void remove() {
//use null to indicate removal
//<-- NEW checkThread(consumer);//essential! No! (for poll race)
running.value = null;
}
@Override
public boolean hasNext() {
return running != tailer;
}
@Override
public E next() {
if(wroteHi > createdHi || wrote > created)
throw new ConcurrentModificationException("Queue modified");
running = running.link;//move next
while (running.value == null && hasNext()) {
running = running.link;
}
E z = running.value;
return (z == null) ? next() : z;//makes multithreaded
}
}
}
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <nospam@nospam.nospam> |
|---|---|
| Date | 2015-12-06 13:02 -0800 |
| Message-ID | <n427ou$9it$1@speranza.aioe.org> |
| In reply to | #31499 |
> "jacko" wrote in message > news:92565518-aa95-4dd5-8e82-83a9a31980ce@googlegroups.com... > Some definite errors fixed and some efficiencies. <code follows> [...] Do I have permission to post a link to your code over on: https://groups.google.com/forum/#!forum/lock-free My friend Dmitry Vyukov created this group a while back. When I get some time I will implement your algorithm in Relacy Race Detector: http://www.1024cores.net/home/relacy-race-detector This has the ability to tear the shi% out of an C++11 lock-free, whatever threading algorithm. Very nice. Check this out: https://www.youtube.com/watch?v=a9xrxRsIbSU Well, there he is! :^D
[toc] | [prev] | [next] | [standalone]
| From | jacko <jackokring@gmail.com> |
|---|---|
| Date | 2015-12-06 13:37 -0800 |
| Message-ID | <b6b226e9-eeea-467b-b844-b4d8925d811b@googlegroups.com> |
| In reply to | #31501 |
On Sunday, 6 December 2015 21:02:30 UTC, Chris M. Thomasson wrote: > > "jacko" wrote in message > > news:92565518-aa95-4dd5-8e82-83a9a31980ce@googlegroups.com... > > > Some definite errors fixed and some efficiencies. <code follows> > > [...] > > Do I have permission to post a link to your code over on: > > https://groups.google.com/forum/#!forum/lock-free Yep > My friend Dmitry Vyukov created this group a while back. > > When I get some time I will implement your algorithm in Relacy Race > Detector: > > http://www.1024cores.net/home/relacy-race-detector > > This has the ability to tear the shi% out of an C++11 lock-free, > whatever threading algorithm. Very nice. > > Check this out: > > https://www.youtube.com/watch?v=a9xrxRsIbSU > > Well, there he is! > > :^D
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.arch
csiph-web