Skip to content

Instantly share code, notes, and snippets.

@rustle
Created September 19, 2013 02:32
Show Gist options
  • Save rustle/6618451 to your computer and use it in GitHub Desktop.
Save rustle/6618451 to your computer and use it in GitHub Desktop.
A __attribute__ decorator that causes method calls to become implicit dispatches could be interesting: __attribute__((dispatch, async, priority))
#import <Foundation/Foundation.h>
#include <objc/message.h>
typedef void (*DISPATCH_IMP_NOARGS_NORETURN)(id, SEL);
typedef id (*DISPATCH_IMP_NOARGS)(id, SEL);
@interface Foo : NSObject
@end
@implementation Foo
- (void)foo //__attribute__((dispatch, true, DISPATCH_QUEUE_PRIORITY_HIGH))
{
NSLog(@"%@", NSStringFromSelector(_cmd));
}
// Async would probably have to be a build error on return values
- (id)bar //__attribute__((dispatch, async, priority)) i.e. __attribute__((dispatch, false, DISPATCH_QUEUE_PRIORITY_HIGH))
{
return NSStringFromSelector(_cmd);
}
@end
void _dispatch_async_objc_msgSend_noArgs_noReturn(int priority, id self, SEL _cmd)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
DISPATCH_IMP_NOARGS_NORETURN f = (DISPATCH_IMP_NOARGS_NORETURN)&objc_msgSend;
f(self, _cmd);
});
}
id _dispatch_sync_objc_msgSend_noArgs(int priority, id self, SEL _cmd)
{
__block id val;
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
DISPATCH_IMP_NOARGS f = (DISPATCH_IMP_NOARGS)&objc_msgSend;
val = f(self, _cmd);
});
return val;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
Foo *foo = [Foo new];
// With this (madeup) attribute, instead of emitting objc_msgSend
// the compiler could emit something like these
// Where [foo foo] becomes
_dispatch_async_objc_msgSend_noArgs_noReturn(DISPATCH_QUEUE_PRIORITY_HIGH, foo, @selector(foo));
// and [foo bar becomes
id val = _dispatch_sync_objc_msgSend_noArgs(DISPATCH_QUEUE_PRIORITY_HIGH, foo, @selector(bar));
NSLog(@"%@", val);
dispatch_main();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment