Bug 60826 - Bad MainThread quards in NSOpenGLContext methods
Summary: Bad MainThread quards in NSOpenGLContext methods
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- enhancement
Target Milestone: 15.7
Assignee: Sebastien Pouliot
Depends on:
Reported: 2017-11-23 10:01 UTC by Slava K
Modified: 2018-02-14 00:37 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

demo app (345.42 KB, application/zip)
2017-11-23 10:01 UTC, Slava K

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Slava K 2017-11-23 10:01:45 UTC
Created attachment 25810 [details]
demo app

## Description

It's impossible to use `NSOpenGLContext` from any threads except the Main UI thread.
NSOpenGLContext.FlushBuffers(), NSOpenGLContext.CGLContext.Lock() and some other methods throw an exception:

"AppKit Consistency error: you are calling a method that can only be invoked from the UI thread."

But this is not true.
According to the Apple documentations (and samples) we are allowed and we are forced to invoke this methods from other threads (e.g. due to the DisplayLink callback)

Assembly Browser shows the cause of that exception in some CompilerGenerated methods:
NSApplication.EnsureUIThread ();

It seems that this guard is unnecessary and moreover, harmful. 

## Steps to reproduce

1. Open attached TestGL demo project in Visual Studio.
2. In order to see an exception from background threads make some settings in Visual Studio for Mac (Run -> New Exception Catchpoint, choose "When an exception is thrown" for 'System.Exception' and set 'Include Subclasses' checkbox, press Create)
3. Start Debugging
4. Observe thrown exception as a result of invoking _mainContext.CGLContext.Lock() (_mainContext.CGLContext.FlushBuffer and so on)

## Environment:
Visual Studio Community 2017 for Mac
Version 7.2.2 (build 11)
 Mono (2017-06/1f4613aa1ac) (64-bit)
 GTK+ 2.24.23 (Raleigh theme)

 Package version: 504010006

NuGet Version:

.NET Core
Runtime: Not installed
SDK: Not installed
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.4.1/lib/mono/msbuild/15.0/bin/Sdks

Apple Developer Tools
Xcode 9.1 (13532)
Build 9B55

Version: (Visual Studio Community)

Operating System
Mac OS X 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0
Comment 1 Alex Soto [MSFT] 2017-11-23 17:39:52 UTC
Thanks for the information!

The threading checks are debug helpers, i.e. they are removed from release builds (by the managed linker), unless extra arguments are given to mtouch, as they have a performance impact on applications (e.g. when used in tight loops).

We acknowledge this enhancement request, therefore marked it as CONFIRMED.
However, we need to do a full API review on NSOpenGLContext to not dismiss cases
where the UI thread check is valid.

You can workaround this by disabling the check before the NSOpenGLContext.FlushBuffers() call (and re-enabling it afterward). E.g. something like:

   NSApplication.CheckForIllegalCrossThreadCalls = false;
   NSApplication.CheckForIllegalCrossThreadCalls = true;

Hope this helps!
Comment 2 Sebastien Pouliot 2018-02-13 22:37:55 UTC
OpenGL context are restricted to a single thread - but it does not have to be the main thread.


The current check is excessive.

PR https://github.com/xamarin/xamarin-macios/pull/3476