GCD needs an API for semaphore notify

Originator:doug
Number:rdar://11919747 Date Originated:
Status:Closed as Duplicate Resolved:
Product:Mac OS X SDK Product Version:10.8
Classification:Enhancement Reproducible:N/A
 
19-Jul-2012 10:22 PM Doug Russell:
Summary: GCD should provide a non blocking alternative to dispatch_semaphore_wait() in the form of something like dispatch_semaphore_notify(). It should behave and be implemented in much the same way as groups (which are really semaphores anyway), but instead of calling on dispatch_group_leave(), it would call after a dispatch_semaphore_signal() when the semaphore has returned to a non negative value or immediately if the semaphore is already non negative. Notify blocks should be queued in the same order that dispatch_semaphore_wait() is signaled. I.e. if an API consumer calls wait, then notify, then wait, they'll wake up in that order.

This would allow for the implementation of things like queues doing io bound work without needing to block on a dispatch_semaphore_wait()

#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__ */

void
dispatch_semaphore_notify(dispatch_semaphore_t semaphore,
						  dispatch_block_t block)
{
	// Same basic implementation as
	// dispatch_group_notify
	// but instead of value==origvalue
	// use value >= 0
	// dispatch_semaphore_signal() would need to
	// consider to consider these notify blocks
	// in the same way it considers semaphores
	// waiting in dispatch_semaphore_wait()
}

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!