Skip to content

Commit

Permalink
ZMQContext locks around sockets array mutations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-w committed Jul 3, 2010
1 parent 28b2140 commit 76a1a04
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
50 changes: 50 additions & 0 deletions README.mkd
Expand Up @@ -10,3 +10,53 @@ laid out by the official

[zmq-docs]: http://api.zeromq.org/zmq.html (zmq(7) Manual Page)
[binding-zmq]: http://www.zeromq.org/guidelines:bindings (Guidelines for ZeroMQ Bindings)

Documentation
-------------
Refer to the [ZeroMQ manual pages][zmq-docs].
The Objective-C binding
maintains a bit more state
than the C API exposes,
in that you can query
a `ZMQContext`
for its sockets
and query a `ZMQSocket`
for its context.

Thread Safety
-------------
ZeroMQ has some restrictive thread safety and coupling issues:

* Sockets can only be used from the thread that created them.
* All ZMQ sockets provided in a single call to `zmq_poll()` must have been created using the same context.

Because sockets
are coupled to contexts
for polling,
you have to track
each socket's context
and make sure not to mix them.
(The `ZMQSocket` class tracks this for you.)

Because each socket
is bound to the thread that created it,
you must be very careful when using
ZeroMQ sockets
with Grand Central Dispatch
or `NSOperationQueue`.
The only persistent thread
that these two expose
is the thread
you're least likely
to want to perform socket operations on:
the main thread.

To Do
-----
* Add functional tests in the form of sample code.
* Provide a more (Core)Foundation-like API
(CFSocket, CFFileDescriptor)
that takes advantage of the runloop.
This is complicated by
the threading constraints
present in libzmq-2.0.7.
2 changes: 2 additions & 0 deletions src/ZMQContext.h
@@ -1,9 +1,11 @@
#import <Foundation/Foundation.h>
#import "ZMQSocket.h" // ZMQSocketType
#import <libkern/OSAtomic.h>

@interface ZMQContext : NSObject {
void *context;
NSMutableArray *sockets;
OSSpinLock socketsLock;
BOOL terminated;
}
+ (void)getZMQVersionMajor:(int *)major minor:(int *)minor patch:(int *)patch;
Expand Down
5 changes: 4 additions & 1 deletion src/ZMQContext.m
Expand Up @@ -27,6 +27,7 @@ - (id)initWithIOThreads:(NSUInteger)threadCount {
return nil;
}

socketsLock = OS_SPINLOCK_INIT;
sockets = [[NSMutableArray alloc] init];
return self;
}
Expand All @@ -44,7 +45,9 @@ - (ZMQSocket *)socketWithType:(ZMQSocketType)type {
ZMQSocket *
socket = [[[ZMQSocket alloc] initWithContext:self type:type] autorelease];
if (socket) {
[(NSMutableArray *)self.sockets addObject:socket];
OSSpinLockLock(&socketsLock); {
[(NSMutableArray *)self.sockets addObject:socket];
} OSSpinLockUnlock(&socketsLock);
}
return socket;
}
Expand Down

0 comments on commit 76a1a04

Please sign in to comment.