Merge pull request #3023 from dmaluka/timerchan

Rework lua timers and remove lua.Lock
This commit is contained in:
Dmytro Maluka
2024-03-14 04:58:19 +01:00
committed by GitHub
4 changed files with 17 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"log"
"time"
lua "github.com/yuin/gopher-lua"
luar "layeh.com/gopher-luar"
@@ -54,7 +55,11 @@ func luaImportMicro() *lua.LTable {
ulua.L.SetField(pkg, "Tabs", luar.New(ulua.L, func() *action.TabList {
return action.Tabs
}))
ulua.L.SetField(pkg, "Lock", luar.New(ulua.L, &ulua.Lock))
ulua.L.SetField(pkg, "After", luar.New(ulua.L, func(t time.Duration, f func()) {
time.AfterFunc(t, func() {
timerChan <- f
})
}))
return pkg
}

View File

@@ -23,7 +23,6 @@ import (
"github.com/zyedidia/micro/v2/internal/buffer"
"github.com/zyedidia/micro/v2/internal/clipboard"
"github.com/zyedidia/micro/v2/internal/config"
ulua "github.com/zyedidia/micro/v2/internal/lua"
"github.com/zyedidia/micro/v2/internal/screen"
"github.com/zyedidia/micro/v2/internal/shell"
"github.com/zyedidia/micro/v2/internal/util"
@@ -46,6 +45,8 @@ var (
sigterm chan os.Signal
sighup chan os.Signal
timerChan chan func()
)
func InitFlags() {
@@ -365,6 +366,8 @@ func main() {
signal.Notify(sigterm, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGABRT)
signal.Notify(sighup, syscall.SIGHUP)
timerChan = make(chan func())
// Here is the event loop which runs in a separate thread
go func() {
for {
@@ -415,21 +418,19 @@ func DoEvent() {
select {
case f := <-shell.Jobs:
// If a new job has finished while running in the background we should execute the callback
ulua.Lock.Lock()
f.Function(f.Output, f.Args)
ulua.Lock.Unlock()
case <-config.Autosave:
ulua.Lock.Lock()
for _, b := range buffer.OpenBuffers {
b.Save()
}
ulua.Lock.Unlock()
case <-shell.CloseTerms:
case event = <-screen.Events:
case <-screen.DrawChan():
for len(screen.DrawChan()) > 0 {
<-screen.DrawChan()
}
case f := <-timerChan:
f()
case <-sighup:
for _, b := range buffer.OpenBuffers {
if !b.Modified() {
@@ -473,12 +474,10 @@ func DoEvent() {
return
}
ulua.Lock.Lock()
_, resize := event.(*tcell.EventResize)
if action.InfoBar.HasPrompt && !resize {
action.InfoBar.HandleEvent(event)
} else {
action.Tabs.HandleEvent(event)
}
ulua.Lock.Unlock()
}

View File

@@ -17,7 +17,6 @@ import (
"regexp"
"runtime"
"strings"
"sync"
"time"
"unicode/utf8"
@@ -27,7 +26,6 @@ import (
)
var L *lua.LState
var Lock sync.Mutex
// LoadFile loads a lua file
func LoadFile(module string, file string, data []byte) error {
@@ -523,21 +521,16 @@ func importErrors() *lua.LTable {
func importTime() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "After", luar.New(L, time.After))
L.SetField(pkg, "Sleep", luar.New(L, time.Sleep))
L.SetField(pkg, "Tick", luar.New(L, time.Tick))
L.SetField(pkg, "Since", luar.New(L, time.Since))
L.SetField(pkg, "FixedZone", luar.New(L, time.FixedZone))
L.SetField(pkg, "LoadLocation", luar.New(L, time.LoadLocation))
L.SetField(pkg, "NewTicker", luar.New(L, time.NewTicker))
L.SetField(pkg, "Date", luar.New(L, time.Date))
L.SetField(pkg, "Now", luar.New(L, time.Now))
L.SetField(pkg, "Parse", luar.New(L, time.Parse))
L.SetField(pkg, "ParseDuration", luar.New(L, time.ParseDuration))
L.SetField(pkg, "ParseInLocation", luar.New(L, time.ParseInLocation))
L.SetField(pkg, "Unix", luar.New(L, time.Unix))
L.SetField(pkg, "AfterFunc", luar.New(L, time.AfterFunc))
L.SetField(pkg, "NewTimer", luar.New(L, time.NewTimer))
L.SetField(pkg, "Nanosecond", luar.New(L, time.Nanosecond))
L.SetField(pkg, "Microsecond", luar.New(L, time.Microsecond))
L.SetField(pkg, "Millisecond", luar.New(L, time.Millisecond))

View File

@@ -121,6 +121,11 @@ The packages and functions are listed below (in Go type signatures):
current pane is not a BufPane.
- `CurTab() *Tab`: returns the current tab.
- `After(t time.Duration, f func())`: run function `f` in the background
after time `t` elapses. See https://pkg.go.dev/time#Duration for the
usage of `time.Duration`.
* `micro/config`
- `MakeCommand(name string, action func(bp *BufPane, args[]string),
completer buffer.Completer)`: