Skip to content

Commit 1022bda

Browse files
authored
hotfix same task runner for platform&render
* Revert "Revert "fixes #133: same task runner for platform&render" (#421)" This reverts commit d598482. * add debug * add debug * fix #419
1 parent d598482 commit 1022bda

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

application.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ func (a *Application) Run() error {
260260
fmt.Printf("go-flutter: engine.Run() returned result code %d (invalid library version)\n", result)
261261
case embedder.ResultInvalidArguments:
262262
fmt.Printf("go-flutter: engine.Run() returned result code %d (invalid arguments)\n", result)
263+
case embedder.ResultInternalInconsistency:
264+
fmt.Printf("go-flutter: engine.Run() returned result code %d (internal inconsistency)\n", result)
263265
default:
264266
fmt.Printf("go-flutter: engine.Run() returned result code %d (unknown result code)\n", result)
265267
}

embedder/embedder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
ResultSuccess Result = C.kSuccess
3131
ResultInvalidLibraryVersion Result = C.kInvalidLibraryVersion
3232
ResultInvalidArguments Result = C.kInvalidArguments
33+
ResultInternalInconsistency Result = C.kInternalInconsistency
3334
ResultEngineNotRunning Result = -1
3435
)
3536

embedder/embedder_helper.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ FlutterEngineResult runFlutter(void *user_data, FlutterEngine *engine, FlutterPr
5151

5252
FlutterCustomTaskRunners custom_task_runners = {};
5353
custom_task_runners.struct_size = sizeof(FlutterCustomTaskRunners);
54+
// Render task and platform task are handled by the same TaskRunner
5455
custom_task_runners.platform_task_runner = &platform_task_runner;
56+
custom_task_runners.render_task_runner = &platform_task_runner;
5557
Args->custom_task_runners = &custom_task_runners;
5658

5759
return FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, Args, user_data,

event-loop.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
"time"
88

99
"github.com/go-flutter-desktop/go-flutter/embedder"
10+
"github.com/go-flutter-desktop/go-flutter/internal/currentthread"
1011
"github.com/go-flutter-desktop/go-flutter/internal/priorityqueue"
1112
)
1213

1314
// EventLoop is a event loop for the main thread that allows for delayed task
14-
// execution.()
15+
// execution.
1516
type EventLoop struct {
1617
// store the task (event) by their priorities
1718
priorityqueue *priorityqueue.PriorityQueue
@@ -22,17 +23,19 @@ type EventLoop struct {
2223

2324
// timeout for non-Rendering events that needs to be processed in a polling manner
2425
platformMessageRefreshRate time.Duration
26+
27+
// identifier for the current thread
28+
mainThreadID currentthread.ThreadID
2529
}
2630

27-
// newEventLoop must ALWAYS be called if the calling goroutine is
28-
// `runtime.LockOSThread()`
2931
func newEventLoop(postEmptyEvent func(), onExpiredTask func(*embedder.FlutterTask) embedder.Result) *EventLoop {
3032
pq := priorityqueue.NewPriorityQueue()
3133
heap.Init(pq)
3234
return &EventLoop{
3335
priorityqueue: pq,
3436
postEmptyEvent: postEmptyEvent,
3537
onExpiredTask: onExpiredTask,
38+
mainThreadID: currentthread.ID(),
3639

3740
// 25 Millisecond is arbitrary value, not too high (adds too much delay to
3841
// platform messages) and not too low (heavy CPU consumption).
@@ -46,17 +49,10 @@ func newEventLoop(postEmptyEvent func(), onExpiredTask func(*embedder.FlutterTas
4649
}
4750
}
4851

49-
// RunOnCurrentThread FlutterDocs:
50-
// May be called from any thread. Should return true if tasks posted on the
51-
// calling thread will be run on that same thread.
52-
//
53-
// The functions PostTask and onExpiredTask should be called from the same
54-
// thread, this is ensured if the creation of the event loop (through
55-
// `newEventLoop`) and the PostTask callback (through
56-
// `a.engine.TaskRunnerPostTask = eventLoop.PostTask`) are done on a calling
57-
// goroutine which always execute in that thread (`runtime.LockOSThread()`).
52+
// RunOnCurrentThread return true if tasks posted on the
53+
// calling thread will be run on that same thread.
5854
func (t *EventLoop) RunOnCurrentThread() bool {
59-
return true
55+
return currentthread.Equal(currentthread.ID(), t.mainThreadID)
6056
}
6157

6258
// PostTask posts a Flutter engine tasks to the event loop for delayed execution.

internal/currentthread/thread-id.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Package currentthread gives you access to the underlying C thread id.
2+
package currentthread
3+
4+
// //
5+
// // Extracted from TinyCThread, a minimalist, portable, threading library for C
6+
// //
7+
//
8+
// /* Platform specific includes */
9+
// #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
10+
// #include <windows.h>
11+
// typedef DWORD thrd_t;
12+
// #else
13+
// #include <pthread.h>
14+
// typedef pthread_t thrd_t;
15+
// #endif
16+
//
17+
// int thrd_equal(thrd_t thr0, thrd_t thr1) {
18+
// #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
19+
// return thr0 == thr1;
20+
// #else
21+
// return pthread_equal(thr0, thr1);
22+
// #endif
23+
// }
24+
//
25+
// thrd_t thrd_current(void) {
26+
// #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
27+
// return GetCurrentThreadId();
28+
// #else
29+
// return pthread_self();
30+
// #endif
31+
// }
32+
import "C"
33+
34+
// ThreadID correspond to an opaque thread identifier
35+
type ThreadID C.thrd_t
36+
37+
// ID returns the id of the current thread
38+
func ID() ThreadID {
39+
return (ThreadID)(C.thrd_current())
40+
}
41+
42+
// Equal compares two thread identifiers.
43+
func Equal(t1, t2 ThreadID) bool {
44+
return C.thrd_equal((C.thrd_t)(t1), (C.thrd_t)(t2)) != 0
45+
}

0 commit comments

Comments
 (0)