X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=retrace%2Fglws_cocoa.mm;h=351f0f32df7b4a688134970f1d2b19a59b3d1166;hb=75af867d17044b6e3992e579c35915e35d2a7e78;hp=8ec58b13903c8b9762eda4278e8a627e193260fc;hpb=cfd878f0acabbb34a428b151d63b73769464fabc;p=apitrace diff --git a/retrace/glws_cocoa.mm b/retrace/glws_cocoa.mm index 8ec58b1..351f0f3 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 */ @@ -45,14 +47,36 @@ #include +#include "os_thread.hpp" #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 { -static __thread NSAutoreleasePool * -autoreleasePool = nil; +static OS_THREAD_SPECIFIC_PTR(NSAutoreleasePool) +autoreleasePool; class CocoaVisual : public Visual @@ -74,6 +98,7 @@ class CocoaDrawable : public Drawable { public: NSWindow *window; + NSOpenGLView *view; NSOpenGLContext *currentContext; CocoaDrawable(const Visual *vis, int w, int h, bool pbuffer) : @@ -93,9 +118,9 @@ public: defer:NO]; assert(window != nil); - NSOpenGLView *view = [[NSOpenGLView alloc] - initWithFrame:winRect - pixelFormat:pixelFormat]; + view = [[NSOpenGLView alloc] + initWithFrame:winRect + pixelFormat:pixelFormat]; assert(view != nil); [window setContentView:view]; @@ -118,7 +143,7 @@ public: if (currentContext != nil) { [currentContext update]; [window makeKeyAndOrderFront:nil]; - [currentContext setView:[window contentView]]; + [currentContext setView:view]; [currentContext makeCurrentContext]; } @@ -173,6 +198,14 @@ init(void) { initThread(); + [DummyThread enterMultiThreaded]; + + bool isMultiThreaded = [NSThread isMultiThreaded]; + if (!isMultiThreaded) { + std::cerr << "error: failed to enable Cocoa multi-threading\n"; + exit(1); + } + [NSApplication sharedApplication]; [NSApp finishLaunching];