X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=retrace%2Fglws_cocoa.mm;h=e410862f1996aa278d64db22c2ec3ba4eac81694;hb=HEAD;hp=1dd9fee78ea7e0f54550c64a2526814ab62d1bf3;hpb=4a5b1f5cf04a72ba8c148977d0539fc1fb1e1004;p=apitrace diff --git a/retrace/glws_cocoa.mm b/retrace/glws_cocoa.mm index 1dd9fee..e410862 100644 --- a/retrace/glws_cocoa.mm +++ b/retrace/glws_cocoa.mm @@ -33,6 +33,8 @@ * - http://developer.apple.com/library/mac/#samplecode/glut/Introduction/Intro.html * - http://developer.apple.com/library/mac/#samplecode/GLEssentials/Introduction/Intro.html * - http://www.glfw.org/ + * - http://cocoasamurai.blogspot.co.uk/2008/04/guide-to-threading-on-leopard.html + * - http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html */ @@ -48,10 +50,32 @@ #include "glws.hpp" +/** + * Dummy thread to force Cocoa to enter multithreading mode. + */ +@interface DummyThread : NSObject + + (void)enterMultiThreaded; + + (void)dummyThreadMethod:(id)unused; +@end + +@implementation DummyThread + + (void)dummyThreadMethod:(id)unused { + (void)unused; + } + + + (void)enterMultiThreaded { + [NSThread detachNewThreadSelector:@selector(dummyThreadMethod:) + toTarget:self + withObject:nil]; + } +@end + + namespace glws { -NSAutoreleasePool *autoreleasePool = nil; +static __thread NSAutoreleasePool * +autoreleasePool = nil; class CocoaVisual : public Visual @@ -159,14 +183,29 @@ public: }; +static inline void +initThread(void) { + if (autoreleasePool == nil) { + autoreleasePool = [[NSAutoreleasePool alloc] init]; + } +} + void init(void) { // Prevent glproc to load system's OpenGL, so that we can trace glretrace. _libGlHandle = dlopen("OpenGL", RTLD_LOCAL | RTLD_NOW | RTLD_FIRST); - [NSApplication sharedApplication]; + initThread(); + + [DummyThread enterMultiThreaded]; - autoreleasePool = [[NSAutoreleasePool alloc] init]; + bool isMultiThreaded = [NSThread isMultiThreaded]; + if (!isMultiThreaded) { + std::cerr << "error: failed to enable Cocoa multi-threading\n"; + exit(1); + } + + [NSApplication sharedApplication]; [NSApp finishLaunching]; } @@ -180,6 +219,9 @@ cleanup(void) { Visual * createVisual(bool doubleBuffer, Profile profile) { + + initThread(); + if (profile != PROFILE_COMPAT && profile != PROFILE_CORE) { return nil; @@ -218,12 +260,16 @@ createVisual(bool doubleBuffer, Profile profile) { Drawable * createDrawable(const Visual *visual, int width, int height, bool pbuffer) { + initThread(); + return new CocoaDrawable(visual, width, height, pbuffer); } Context * createContext(const Visual *visual, Context *shareContext, Profile profile, bool debug) { + initThread(); + NSOpenGLPixelFormat *pixelFormat = static_cast(visual)->pixelFormat; NSOpenGLContext *share_context = nil; NSOpenGLContext *context; @@ -248,6 +294,8 @@ createContext(const Visual *visual, Context *shareContext, Profile profile, bool bool makeCurrent(Drawable *drawable, Context *context) { + initThread(); + if (!drawable || !context) { [NSOpenGLContext clearCurrentContext]; } else { @@ -266,8 +314,9 @@ makeCurrent(Drawable *drawable, Context *context) bool processEvents(void) { - NSEvent* event; + initThread(); + NSEvent* event; do { event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast]