Skip to content

Commit 539fde8

Browse files
author
Pierre Champion | Drakirus
authored
Revert "Revert "fixes #133: same task runner for platform&render" (#421)"
This reverts commit d598482.
1 parent d598482 commit 539fde8

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

application.go

+2
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

+1
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

+2
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

+9-13
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 int64
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.ID() == t.mainThreadID
6056
}
6157

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

internal/currentthread/thread-id.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Package currentthread gives you access to the underlying 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 HANDLE thrd_t;
12+
// #else
13+
// #include <pthread.h>
14+
// typedef pthread_t thrd_t;
15+
// #endif
16+
//
17+
// thrd_t thrd_current(void) {
18+
// #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
19+
// return GetCurrentThread();
20+
// #else
21+
// return pthread_self();
22+
// #endif
23+
// }
24+
// size_t getCurrentThreadID() { return (size_t)thrd_current(); }
25+
import "C"
26+
27+
// ID returns the id of the current thread
28+
func ID() int64 {
29+
return (int64)(C.getCurrentThreadID())
30+
}

0 commit comments

Comments
 (0)