Skip to content

Commit

Permalink
Merge pull request #3023 from dmaluka/timerchan
Browse files Browse the repository at this point in the history
Rework lua timers and remove lua.Lock
  • Loading branch information
dmaluka authored Mar 14, 2024
2 parents c24604d + 1d1b363 commit 0a69cc6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
7 changes: 6 additions & 1 deletion cmd/micro/initlua.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"time"

lua "github.com/yuin/gopher-lua"
luar "layeh.com/gopher-luar"
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -46,6 +45,8 @@ var (

sigterm chan os.Signal
sighup chan os.Signal

timerChan chan func()
)

func InitFlags() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
}
7 changes: 0 additions & 7 deletions internal/lua/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"regexp"
"runtime"
"strings"
"sync"
"time"
"unicode/utf8"

Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions runtime/help/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`:
Expand Down

0 comments on commit 0a69cc6

Please sign in to comment.