• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Threading Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Threading Question


  • Subject: Re: Threading Question
  • From: Morgan Packard <email@hidden>
  • Date: Wed, 29 Feb 2012 22:48:00 -0500

Slow response here, but I'm just finally getting to this. Thanks very much for sharing Peter.
-Morgan

On Mon, Feb 13, 2012 at 12:30 AM, Support (One Red Dog) <email@hidden> wrote:
You're welcome to consider mine, it's nothing special. I use this in my iOS app between the UI thread and audio thread, so only one producer, one consumer.

regards
peter
one red dog media


#pragma once

#include <cstdio>
#include <libkern/OSAtomic.h>

namespace quorra
{

    template <typename T>
    class lockfreequeue
    {
    private:
        int QUEUESIZE;
        T*  q;                  // body of queue
        int qsize;
        int first;              // position of first element
        int last;               // position of last element
        int count;              // number of queue elements


    public:
        lockfreequeue(const int size = 256) :
            QUEUESIZE(size), first(0), last(size - 1), count(0)
        {
            q = new T[size + 1];
        }

        ~lockfreequeue()
        {
            delete q;
        }

        

        void push_back(const T& t)
        {
            volatile int32_t tempLast;

            if (count >= QUEUESIZE)
            {
                printf("Warning: queue overflow enqueue %d\n", QUEUESIZE);
            }
            else
            {
                OSAtomicCompareAndSwap32Barrier(tempLast, last, &tempLast);
                tempLast = (tempLast + 1) % QUEUESIZE;
                OSAtomicCompareAndSwap32Barrier(last, tempLast, &last);

                OSMemoryBarrier();
                q[last] = t;
                OSAtomicIncrement32Barrier(&count);
            }
        }


        T pop_front()
        {
            volatile int32_t tempFirst;
            T x;

            if (count <= 0)
                printf("Warning: empty queue dequeue.\n");
            else
            {
                OSMemoryBarrier();
                x = q[first];

                OSAtomicCompareAndSwap32Barrier(tempFirst, first, &tempFirst);
                tempFirst = (tempFirst + 1) % QUEUESIZE;
                OSAtomicCompareAndSwap32Barrier(first, tempFirst, &first);

                

                OSAtomicDecrement32Barrier(&count);
            }

            return x;
        }

        bool empty()
        {
            if (count <= 0) return true;
            else return false;
        }

        

        T front()
        {
            OSMemoryBarrier();
            return q[first];
        }

        void clear()
        {
            first = 0;
            last = QUEUESIZE - 1;
            count = 0;
        }

        

        size_t size()
        {
            return count;
        }
    };
}



On 13/02/2012, at 12:55 PM, Morgan Packard wrote:

I'd be very grateful for a ring buffer I can trust for communication from one thread to another.
-Morgan

On Sun, Feb 12, 2012 at 8:08 PM, <email@hidden> wrote:
Michael Tyson wrote:
> Incidentally, I dunno if this is helpful, but I thought I'd chime in:
I've
> got a lock-free ring buffer implementation, TPCircularBuffer
> (https://github.com/michaeltyson/TPCircularBuffer) which might do the
> trick for this. It uses atomic operations and a memory barrier, and
works
> with one reader and one writer. It's based on some neat VM stuff that
> wraps the memory space around so the memory wrapping is transparent.

The code linked to does not have any memory barriers at all. There is
nothing stopping writes from the memcpy (in
TPCircularBufferProduceBytes) being reordered (by the CPU) such that
they become visible after the OSAtomicAdd32 (in
TPCircularBufferProduce). There needs to be a write barrier after the
memcpy in order to be safe.

Similarly, the memory barrier on read is missing too - this is needed
between copying fillCount in to availableBytes (in TPCircularBufferTail)
and returning from TPCircularBufferTail.

> It's not been formally proven to be correct, but I'm pretty sure it's
> right, and I've never had any issues with it.

Note that this problem won't show up on older iOS devices (since they
are single core) nor on x86 (which is quite strongly ordered). However,
you will likely see issues on the iPad 2 and iPhone 5.


Cheers,
Michael

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



--
===============
Morgan Packard
cell: (720) 891-0122
aim: mpackardatwork

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      (email@hidden)

This email sent to email@hidden


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden




--
===============
Morgan Packard
cell: (720) 891-0122
aim: mpackardatwork

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: Threading Question (From: Morgan Packard <email@hidden>)
 >Re: Threading Question (From: Kyle Sluder <email@hidden>)
 >Re: Threading Question (From: Michael Tyson <email@hidden>)
 >RE: Threading Question (From: email@hidden)
 >Re: Threading Question (From: Morgan Packard <email@hidden>)
 >Re: Threading Question (From: "Support (One Red Dog)" <email@hidden>)

  • Prev by Date: Urgent! How to select deviceID using AudioQueue
  • Previous by thread: Re: Threading Question
  • Next by thread: Re: Threading Question
  • Index(es):
    • Date
    • Thread