Skip to content

Commit

Permalink
Added a bunch of dispatch queues to make things more multi-thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
stevestreza committed Jul 28, 2011
1 parent 3be556e commit 1e95c91
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
5 changes: 5 additions & 0 deletions XPCKit/XPCConnection.h
Expand Up @@ -18,17 +18,22 @@
//

#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
#import "XPCTypes.h"

@interface XPCConnection : NSObject{
xpc_connection_t _connection;
dispatch_queue_t _dispatchQueue;
}

- (id)initWithServiceName:(NSString *)serviceName;
- (id)initWithConnection: (xpc_connection_t)connection;

@property (nonatomic, retain) XPCEventHandler eventHandler;

@property (nonatomic, readonly) xpc_connection_t connection;
@property (nonatomic, assign) dispatch_queue_t dispatchQueue;

// connection properties
@property (nonatomic, readonly) NSString *connectionName;
@property (nonatomic, readonly) NSNumber *connectionEUID;
Expand Down
80 changes: 60 additions & 20 deletions XPCKit/XPCConnection.m
Expand Up @@ -26,7 +26,7 @@

@implementation XPCConnection

@synthesize eventHandler=_eventHandler;
@synthesize eventHandler=_eventHandler, dispatchQueue=_dispatchQueue, connection=_connection;

- (id)initWithServiceName:(NSString *)serviceName{
xpc_connection_t connection = xpc_connection_create([serviceName cStringUsingEncoding:NSUTF8StringEncoding], NULL);
Expand All @@ -44,6 +44,12 @@ -(id)initWithConnection:(xpc_connection_t)connection{
if(self = [super init]){
_connection = xpc_retain(connection);
[self receiveConnection:_connection];

dispatch_queue_t queue = dispatch_queue_create(NULL, 0);
self.dispatchQueue = queue;
dispatch_release(queue);

[self resume];
}
return self;
}
Expand All @@ -58,6 +64,19 @@ -(void)dealloc{
[super dealloc];
}

-(void)setDispatchQueue:(dispatch_queue_t)dispatchQueue{
if(dispatchQueue){
dispatch_retain(dispatchQueue);
}

if(_dispatchQueue){
dispatch_release(_dispatchQueue);
}
_dispatchQueue = dispatchQueue;

xpc_connection_set_target_queue(self.connection, self.dispatchQueue);
}

-(void)receiveConnection:(xpc_connection_t)connection{
__block XPCConnection *this = self;
xpc_connection_set_event_handler(connection, ^(xpc_object_t object){
Expand All @@ -80,57 +99,78 @@ -(void)receiveConnection:(xpc_connection_t)connection{
}
}
});

xpc_connection_resume(_connection);
}

-(void)sendMessage:(NSDictionary *)dictMessage{
if(![dictMessage isKindOfClass:[NSDictionary class]]){
dictMessage = [NSDictionary dictionaryWithObject:dictMessage forKey:@"contents"];
}
-(void)sendMessage:(NSDictionary *)aDictMessage{
dispatch_async(self.dispatchQueue, ^{
NSDictionary *dictMessage = aDictMessage;
if(![dictMessage isKindOfClass:[NSDictionary class]]){
dictMessage = [NSDictionary dictionaryWithObject:dictMessage forKey:@"contents"];
}

xpc_object_t message = NULL;
xpc_object_t message = NULL;

// NSDate *date = [NSDate date];
message = [dictMessage newXPCObject];
// NSLog(@"Message encoding took %gs on average - %@", [[NSDate date] timeIntervalSinceDate:date], dictMessage);
// NSDate *date = [NSDate date];
message = [dictMessage newXPCObject];
// NSLog(@"Message encoding took %gs on average - %@", [[NSDate date] timeIntervalSinceDate:date], dictMessage);

xpc_connection_send_message(_connection, message);
xpc_release(message);
xpc_connection_send_message(_connection, message);
xpc_release(message);
});
}

-(NSString *)connectionName{
const char* name = xpc_connection_get_name(_connection);
__block char* name = NULL;
dispatch_sync(self.dispatchQueue, ^{
name = (char*)xpc_connection_get_name(_connection);
});
if(!name) return nil;
return [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
}

-(NSNumber *)connectionEUID{
uid_t uid = xpc_connection_get_euid(_connection);
__block uid_t uid = 0;
dispatch_sync(self.dispatchQueue, ^{
uid = xpc_connection_get_euid(_connection);
});
return [NSNumber numberWithUnsignedInt:uid];
}

-(NSNumber *)connectionEGID{
gid_t egid = xpc_connection_get_egid(_connection);
__block gid_t egid = 0;
dispatch_sync(self.dispatchQueue, ^{
egid = xpc_connection_get_egid(_connection);
});
return [NSNumber numberWithUnsignedInt:egid];
}

-(NSNumber *)connectionProcessID{
pid_t pid = xpc_connection_get_pid(_connection);
__block pid_t pid = 0;
dispatch_sync(self.dispatchQueue, ^{
pid = xpc_connection_get_pid(_connection);
});
return [NSNumber numberWithUnsignedInt:pid];
}

-(NSNumber *)connectionAuditSessionID{
au_asid_t auasid = xpc_connection_get_asid(_connection);

__block au_asid_t auasid = 0;
dispatch_sync(self.dispatchQueue, ^{
auasid = xpc_connection_get_asid(_connection);
});
return [NSNumber numberWithUnsignedInt:auasid];
}

-(void)suspend{
xpc_connection_suspend(_connection);
dispatch_async(self.dispatchQueue, ^{
xpc_connection_suspend(_connection);
});
}

-(void)resume{
xpc_connection_resume(_connection);
dispatch_async(self.dispatchQueue, ^{
xpc_connection_resume(_connection);
});
}

-(void)_sendLog:(NSString *)string{
Expand Down

0 comments on commit 1e95c91

Please sign in to comment.