Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Internal locking behaviour of Objective-C blocks?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michael Tyson  
View profile  
 More options Aug 31 2012, 8:41 am
From: Michael Tyson <mich...@atastypixel.com>
Date: Fri, 31 Aug 2012 13:41:30 +0100
Local: Fri, Aug 31 2012 8:41 am
Subject: Internal locking behaviour of Objective-C blocks?

Hello,

I'm pondering the viability of using ObjC blocks in a realtime Core Audio thread context.

I go to extensive measures to avoid any kind of blocking code in the Core Audio thread, and that includes avoiding making Objective-C calls, as the message sending system does do some internal locking, and thus could block the thread.

I'm wondering if this also applies to the use of blocks - assuming of course, the implementation contained within the block itself doesn't do any blocking or Objective-C method calls.

Does performing a block...block?  Does it use an internal mutex, which could end up holding up the realtime thread? Is there any other reason to avoid blocks in a realtime context?

Many thanks,
Michael

--
Michael Tyson | atastypixel.com

Live, app-to-app audio streaming is coming soon.
Don't want to miss our launch? Then sign up here: http://audiob.us


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Dovey  
View profile  
 More options Aug 31 2012, 8:44 am
From: Jim Dovey <jimdo...@gmail.com>
Date: Fri, 31 Aug 2012 08:44:09 -0400
Local: Fri, Aug 31 2012 8:44 am
Subject: Re: Internal locking behaviour of Objective-C blocks?

A block compiles down to a single method call. Copying a block to the heap
will copy any referenced variables, so if it captures an ObjC object you'd
need to ensure that object's -copy implementation didn't block. But calling
a block itself just compiles down to a call instruction, so no blocking or
locking behavior there.

Sent from my iPhone

On 2012-08-31, at 8:41 AM, Michael Tyson <mich...@atastypixel.com> wrote:

Hello,

I'm pondering the viability of using ObjC blocks in a realtime Core Audio
thread context.

I go to extensive measures to avoid any kind of blocking code in the Core
Audio thread, and that includes avoiding making Objective-C calls, as the
message sending system does do some internal locking, and thus could block
the thread.

I'm wondering if this also applies to the use of blocks - assuming of
course, the implementation contained within the block itself doesn't do any
blocking or Objective-C method calls.

Does performing a block...block?  Does it use an internal mutex, which
could end up holding up the realtime thread? Is there any other reason to
avoid blocks in a realtime context?

Many thanks,
Michael

--
Michael Tyson | atastypixel.com

Live, app-to-app audio streaming is coming soon.
Don't want to miss our launch? Then sign up here: http://audiob.us


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Tyson  
View profile  
 More options Aug 31 2012, 8:48 am
From: Michael Tyson <mich...@atastypixel.com>
Date: Fri, 31 Aug 2012 13:48:01 +0100
Local: Fri, Aug 31 2012 8:48 am
Subject: Re: Internal locking behaviour of Objective-C blocks?

Well - that's excellent news!

So, in that case, as long as the actual management of the block's lifespan (copy, release, etc - which may do mutex stuff) is performed outside the realtime context, the block perform call itself should presumably be perfectly fine, in the context of Apple's "don't block" guidelines.

Thanks heaps,
Michael

--
Michael Tyson | atastypixel.com

Live, app-to-app audio streaming is coming soon.
Don't want to miss our launch? Then sign up here: http://audiob.us

On 31 Aug 2012, at 13:44, Jim Dovey <jimdo...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Clement  
View profile  
 More options Aug 31 2012, 8:57 am
From: Thomas Clement <tclement...@gmail.com>
Date: Fri, 31 Aug 2012 14:57:10 +0200
Local: Fri, Aug 31 2012 8:57 am
Subject: Re: Internal locking behaviour of Objective-C blocks?

Referenced ObjC objects are retained, not copied.

Thomas

On Aug 31, 2012, at 2:44 PM, Jim Dovey <jimdo...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Ash  
View profile  
 More options Aug 31 2012, 10:24 am
From: Michael Ash <michael....@gmail.com>
Date: Fri, 31 Aug 2012 10:24:15 -0400
Local: Fri, Aug 31 2012 10:24 am
Subject: Re: Internal locking behaviour of Objective-C blocks?
On Aug 31, 2012, at 8:44 AM, Jim Dovey <jimdo...@gmail.com> wrote:

> A block compiles down to a single method call. Copying a block to the heap will copy any referenced variables, so if it captures an ObjC object you'd need to ensure that object's -copy implementation didn't block. But calling a block itself just compiles down to a call instruction, so no blocking or locking behavior there.

A block call is a single *function* call. When you write block(...), it compiles down to code that does block->implFptr(block, ...), where block is conceptually a struct, and implFptr is a member containing a function pointer to the block's actual code. Unlike ObjC method calls, there's no way this code can block short of the memory it depends on being paged out. (ObjC message sends can potentially take locks, although it's extremely rare.)

Copying a block in the render callback would be bad regardless, as the memory allocation alone is something you'd want to avoid. But assuming you copy it beforehand, it's perfectly safe to call as long as the contents are safe.

Mike


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Tyson  
View profile  
 More options Aug 31 2012, 3:08 pm
From: Michael Tyson <mich...@atastypixel.com>
Date: Fri, 31 Aug 2012 20:08:24 +0100
Local: Fri, Aug 31 2012 3:08 pm
Subject: Re: Internal locking behaviour of Objective-C blocks?

Lovely, thanks for the reassuring clarification, Mike. I've revised my implementation and it's working splendidly with blocks now.

(It's a mechanism I've build into my audio engine that allows me to synchronise between the Core Audio thread and other threads, without needing locks.) So:

- (void)performSynchronousMessageExchangeWithBlock:(void (^)())block;

- (void)performAsynchronousMessageExchangeWithBlock:(void (^)())block
                                      responseBlock:(void (^)())responseBlock;

For sending messages to the Core Audio thread, and

void AEAudioControllerSendAsynchronousMessageToMainThread(AEAudioController                 *audioController,
                                                          AEAudioControllerMainThreadMessageHandler    handler,
                                                          void                              *userInfo,
                                                          int                                userInfoLength);

for sending messages back to the main thread; both based upon a lock-free circular buffer.

Working very nicely now with blocks - a lot easier than what I was doing before, which was using a C function pointer and userInfo, like the third method above.

On 31 Aug 2012, at 15:24, Michael Ash <michael....@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »