This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch eo-api-finalize
in repository ego.

View the commit online.

commit dae50436ccb06404d99deaedee66193c8a29527b
Author: Cedric Bail <[email protected]>
AuthorDate: Wed Nov 25 22:39:19 2026 -0600

    efl: tasks posted to the loop share one wake-up
    
    Every PostContext and Sync wrote to the Ecore pipe, which costs a lock
    and two write syscalls, although the pipe callback drains the whole
    queue each time it runs. A goroutine posting results in a burst paid
    about 1.2 us per post; it now pays about 0.2 us, because a byte is
    written only when no wake-up is already pending.
    
    The flag is cleared before the drain, so a task queued during a drain
    is either seen by it or writes a wake-up of its own. A poster that
    finds a wake-up pending still reports a loop that has shut down, since
    the flag stays set once the loop has ended.
    
    Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 efl/efl.go         |  31 +++++++++++---
 efl/export_test.go |   3 ++
 efl/wake_test.go   | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 148 insertions(+), 6 deletions(-)

diff --git a/efl/efl.go b/efl/efl.go
index bb623fb3..63b0bb99 100644
--- a/efl/efl.go
+++ b/efl/efl.go
@@ -95,6 +95,14 @@ var (
 	// with the teardown.
 	pipeMu sync.Mutex
 
+	// wakePending is set by the writePipe that is about to wake the loop, and
+	// cleared by pipeCallback before it drains: until then every other task
+	// queued is drained by the wake-up already on its way.
+	wakePending atomic.Bool
+
+	// pipeWrites counts the wake-ups written, for tests.
+	pipeWrites atomic.Uint64
+
 	// closed is set on the EFL thread inside the shutdown task, before
 	// ecore_main_loop_begin returns, and by Wait before it frees the pipe,
 	// for a loop EFL itself ended. Once set, PostContext/Sync must not
@@ -427,19 +435,27 @@ func runGuarded(fn func()) {
 	returned = true
 }
 
-// writePipe writes a single byte to the ecore_pipe to wake the EFL main loop.
-// It holds pipeMu for the duration, which both serialises the two-syscall write
-// (preventing header interleaving from concurrent callers) and prevents the
-// write from racing with ecore_pipe_del during shutdown.
+// writePipe makes sure the EFL main loop will drain the queue, after the
+// caller has put its task in the queue or the backlog. It writes a byte to the
+// ecore_pipe only when no wake-up is pending, under pipeMu so that the write
+// neither interleaves with another nor races with ecore_pipe_del. A caller that
+// finds one pending relies on it: pipeCallback clears the flag before it
+// drains, so it will see this caller's task.
 // Returns false if the pipe is already closed.
 func writePipe() bool {
+	if !wakePending.CompareAndSwap(false, true) {
+		// The flag stays set once the loop has ended, so it alone does not
+		// say a drain is coming.
+		return !closed.Load()
+	}
 	pipeMu.Lock()
 	defer pipeMu.Unlock()
-	// Re-check closed while holding the lock. This closes the window between
-	// the caller's closed.Load() check and the actual write.
+	// Under the lock Wait frees the pipe with, so the write below never
+	// reaches a pipe that is gone.
 	if closed.Load() {
 		return false
 	}
+	pipeWrites.Add(1)
 	dummy := C.uchar(0)
 	C.ecore_pipe_write(pipe, unsafe.Pointer(&dummy), C.uint(1))
 	return true
@@ -507,6 +523,9 @@ func appArgs() []string {
 //
 //export pipeCallback
 func pipeCallback(data unsafe.Pointer, buffer unsafe.Pointer, nbyte C.uint) {
+	// Before draining, so that a task queued from now on either is seen by
+	// the drain below or writes a wake-up of its own.
+	wakePending.Store(false)
 	// What the loop posted to itself first: it was posted before anything
 	// still in the queue could have been run.
 	for len(backlog) > 0 {
diff --git a/efl/export_test.go b/efl/export_test.go
index a93a2109..cb3ac103 100644
--- a/efl/export_test.go
+++ b/efl/export_test.go
@@ -73,3 +73,6 @@ func CallbackCounts() (callbacks, objects int) {
 // CopyForC is the copy a function that hands its result over to C returns,
 // owned by the caller as a copy EFL made would be.
 func CopyForC(v *Value) *Value { return wrapValueOwned(unsafe.Pointer(valueForC(v))) }
+
+// PipeWrites counts the wake-ups written to the loop's pipe so far.
+func PipeWrites() uint64 { return pipeWrites.Load() }
diff --git a/efl/wake_test.go b/efl/wake_test.go
new file mode 100644
index 00000000..99c48a5b
--- /dev/null
+++ b/efl/wake_test.go
@@ -0,0 +1,120 @@
+package efl_test
+
+import (
+	"context"
+	"sync"
+	"sync/atomic"
+	"testing"
+	"time"
+
+	"git.enlightenment.org/cedric/ego/efl"
+)
+
+func waitGroup(t *testing.T, wg *sync.WaitGroup, what string) {
+	t.Helper()
+	done := make(chan struct{})
+	go func() { wg.Wait(); close(done) }()
+	select {
+	case <-done:
+	case <-time.After(30 * time.Second):
+		t.Fatalf("%s: tasks were lost", what)
+	}
+}
+
+// TestPostContextConcurrentRunsEveryTask pins that coalescing wake-ups never
+// leaves a queued task without one.
+func TestPostContextConcurrentRunsEveryTask(t *testing.T) {
+	const posters, perPoster = 16, 2000
+	var ran atomic.Int64
+	var wg sync.WaitGroup
+	wg.Add(posters * perPoster)
+	for range posters {
+		go func() {
+			for range perPoster {
+				if err := efl.PostContext(context.Background(), func() {
+					ran.Add(1)
+					wg.Done()
+				}); err != nil {
+					t.Error(err)
+					wg.Done()
+				}
+			}
+		}()
+	}
+	waitGroup(t, &wg, "concurrent posts")
+	if n := ran.Load(); n != posters*perPoster {
+		t.Errorf("%d tasks ran, want %d", n, posters*perPoster)
+	}
+}
+
+// TestPostContextBurstWritesFewWakeUps checks that a burst queued while the
+// loop is busy costs one pipe write, not one per task.
+func TestPostContextBurstWritesFewWakeUps(t *testing.T) {
+	// Well under the 256-slot queue: nothing drains it while the loop is
+	// held, and finalizers post to it too.
+	const burst = 64
+	busy := make(chan struct{})
+	release := make(chan struct{})
+	syncDone := make(chan struct{})
+	go func() {
+		efl.Sync(func() {
+			close(busy)
+			<-release
+		})
+		close(syncDone)
+	}()
+	<-busy
+
+	var wg sync.WaitGroup
+	before := efl.PipeWrites()
+	for range burst {
+		wg.Add(1)
+		if err := efl.PostContext(context.Background(), wg.Done); err != nil {
+			wg.Done()
+			t.Error(err)
+		}
+	}
+	writes := efl.PipeWrites() - before
+	close(release)
+	<-syncDone
+	waitGroup(t, &wg, "burst")
+
+	if writes > burst/10 {
+		t.Errorf("%d pipe writes for %d posts, want far fewer", writes, burst)
+	}
+}
+
+// TestPostContextFromLoopRuns covers the backlog path: a task posted by a
+// running task still runs, which holds only while pipeCallback clears
+// wakePending before it drains rather than after.
+func TestPostContextFromLoopRuns(t *testing.T) {
+	const depth = 50
+	var wg sync.WaitGroup
+	wg.Add(depth)
+	var post func(left int)
+	post = func(left int) {
+		wg.Done()
+		if left == 1 {
+			return
+		}
+		if err := efl.PostContext(context.Background(), func() { post(left - 1) }); err != nil {
+			t.Error(err)
+		}
+	}
+	efl.Sync(func() { post(depth) })
+	waitGroup(t, &wg, "tasks posted from the loop")
+}
+
+func BenchmarkPostContext(b *testing.B) {
+	b.ReportAllocs()
+	var wg sync.WaitGroup
+	wg.Add(b.N)
+	b.ResetTimer()
+	for range b.N {
+		if err := efl.PostContext(context.Background(), wg.Done); err != nil {
+			b.Fatal(err)
+		}
+	}
+	b.StopTimer()
+	wg.Wait()
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to