Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | # Introduction |
| 2 | |
| 3 | Some libraries, especially graphical frameworks/libraries like Cocoa, OpenGL, libSDL all require it's |
| 4 | called from the main OS thread or called from the same OS thread due to its use of thread local |
| 5 | data structures. Go's runtime provides ` LockOSThread() ` function for this, but it's notoriously difficult |
| 6 | to use correctly. |
| 7 | |
| 8 | # Solutions |
| 9 | |
| 10 | Russ Cox presented a good solution for this problem in this [thread](https://groups.google.com/d/msg/golang-nuts/IiWZ2hUuLDA/SNKYYZBelsYJ). |
| 11 | |
Dmitri Shuralyov | 47f767a | 2015-01-04 21:07:59 -0800 | [diff] [blame] | 12 | ```Go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 13 | package sdl |
| 14 | |
| 15 | // Arrange that main.main runs on main thread. |
| 16 | func init() { |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 17 | runtime.LockOSThread() |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // Main runs the main SDL service loop. |
| 21 | // The binary's main.main must call sdl.Main() to run this loop. |
| 22 | // Main does not return. If the binary needs to do other work, it |
| 23 | // must do it in separate goroutines. |
| 24 | func Main() { |
Jeremy Jackins | 1804902 | 2015-01-27 22:24:43 +0900 | [diff] [blame^] | 25 | for f := range mainfunc { |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 26 | f() |
| 27 | } |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // queue of work to run in main thread. |
| 31 | var mainfunc = make(chan func()) |
| 32 | |
| 33 | // do runs f on the main thread. |
| 34 | func do(f func()) { |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 35 | done := make(chan bool, 1) |
| 36 | mainfunc <- func() { |
| 37 | f() |
| 38 | done <- true |
| 39 | } |
| 40 | <-done |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 41 | } |
| 42 | ``` |
| 43 | |
| 44 | And then other functions you write in package sdl can be like |
Dmitri Shuralyov | 47f767a | 2015-01-04 21:07:59 -0800 | [diff] [blame] | 45 | |
| 46 | ```Go |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 47 | func Beep() { |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 48 | do(func() { |
| 49 | // whatever must run in main thread |
| 50 | }) |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 51 | } |
| 52 | ``` |