dispatch_queue_t should support width

Originator:doug
Number:rdar://11919777 Date Originated:
Status:Open Resolved:
Product:Mac OS X SDK Product Version:10.8
Classification:Enhancement Reproducible:N/A
 
19-Jul-2012 10:40 PM Doug Russell:
GCD does not handle io bound tasks well and often spawns enough threads to cripple an app if you try to do a lot of (for example) file io in a dispatch_async()

I've made a sample implementation of a queue that uses a semaphore, group and another queue as associated objects to handle throttling width just to show how the API could be useful:

https://github.com/rustle/QueueWithWidth

Related: rdar://problem/11919747 http://openradar.appspot.com/11919747

This is what I propose the new API could look like:

// dispatch_queue_attr_t
// Used by dispatch_queue_create() to create a
// queue with a concept of width
#define DISPATCH_QUEUE_CONCURRENT_WITH_WIDTH

#define DISPATCH_QUEUE_CONCURRENT_WITH_WIDTH_DEFAULT_WIDTH /*Processor Count*/

// Set queues width (internally this creates a semaphore
// with given width and attaches it to the queue)
__OSX_AVAILABLE_STARTING(__MAC_10_8,__IPHONE_6_0)
DISPATCH_EXPORT DISPATCH_NOTHROW
void
dispatch_set_queue_width(dispatch_queue_t queue, long width);

// Only accepts DISPATCH_QUEUE_CONCURRENT_WITH_WIDTH queues
// and waits with the same behavior as dispatch_semaphore_wait()
__OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
long
dispatch_queue_wait(dispatch_queue_t queue, dispatch_time_t timeout);

// Only accepts DISPATCH_QUEUE_CONCURRENT_WITH_WIDTH queues
// and notifies with the same behavior as dispatch_group_notify(),
// but instead of waiting for it's group to reach
// value==originalvalue for it's value to become non-negative
#ifdef __BLOCKS__
__OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
void
dispatch_queue_notify(dispatch_queue_t queue,
					  dispatch_block_t block);
#endif /* __BLOCKS__ */

// Notifies with the same behavior as dispatch_group_notify(),
// but instead of waiting for it's group to reach
// value==originalvalue for it's value to become non-negative
// Should queue with the same ordering as dispatch_semaphore_wait()
// i.e. if you called wait then notify then wait, they'd signal in
// that order
#ifdef __BLOCKS__
__OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
void
dispatch_semaphore_notify(dispatch_semaphore_t semaphore,
						  dispatch_block_t block);
#endif /* __BLOCKS__ */


19-Jul-2012 10:44 PM Doug Russell:
Fwiw, I imagine the Foundation team had to solve nearly identical issues making NSOperationQueue sit on top of GCD, so between the work I've demonstrated and the work Foundation has demonstrated, it's certainly possible.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!