No need to store most channels on self

This commit is contained in:
Kovid Goyal 2022-08-26 15:26:03 +05:30
parent f103f8d5db
commit bf35817d73
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 34 additions and 46 deletions

View File

@ -32,21 +32,17 @@ func (self *timer) update_deadline(now time.Time) {
}
type Loop struct {
controlling_term *tty.Term
terminal_options TerminalStateOptions
screen_size ScreenSize
escape_code_parser wcswidth.EscapeCodeParser
keep_going bool
death_signal unix.Signal
exit_code int
timers []*timer
timer_id_counter, write_msg_id_counter IdType
tty_read_channel chan []byte
tty_write_channel chan *write_msg
write_done_channel chan IdType
err_channel chan error
tty_writing_done_channel, tty_reading_done_channel, wakeup_channel chan byte
pending_writes []*write_msg
controlling_term *tty.Term
terminal_options TerminalStateOptions
screen_size ScreenSize
escape_code_parser wcswidth.EscapeCodeParser
keep_going bool
death_signal unix.Signal
exit_code int
timers []*timer
timer_id_counter, write_msg_id_counter IdType
wakeup_channel chan byte
pending_writes []*write_msg
// Callbacks
@ -151,7 +147,7 @@ func (self *Loop) WakeupMainThread() {
func (self *Loop) QueueWriteString(data string) IdType {
self.write_msg_id_counter++
msg := write_msg{str: data, id: self.write_msg_id_counter}
self.queue_write_to_tty(&msg)
self.add_write_to_pending_queue(&msg)
return msg.id
}
@ -160,7 +156,7 @@ func (self *Loop) QueueWriteString(data string) IdType {
func (self *Loop) QueueWriteBytesDangerous(data []byte) IdType {
self.write_msg_id_counter++
msg := write_msg{bytes: data, id: self.write_msg_id_counter}
self.queue_write_to_tty(&msg)
self.add_write_to_pending_queue(&msg)
return msg.id
}

View File

@ -193,14 +193,14 @@ func (self *Loop) run() (err error) {
}
self.keep_going = true
self.tty_read_channel = make(chan []byte)
self.tty_write_channel = make(chan *write_msg, 1) // buffered so there is no race between initial queueing and startup of writer thread
self.write_done_channel = make(chan IdType)
self.tty_writing_done_channel = make(chan byte)
self.tty_reading_done_channel = make(chan byte)
tty_read_channel := make(chan []byte)
tty_write_channel := make(chan *write_msg, 1) // buffered so there is no race between initial queueing and startup of writer thread
write_done_channel := make(chan IdType)
tty_writing_done_channel := make(chan byte)
tty_reading_done_channel := make(chan byte)
self.wakeup_channel = make(chan byte, 256)
self.pending_writes = make([]*write_msg, 0, 256)
self.err_channel = make(chan error, 8)
err_channel := make(chan error, 8)
self.death_signal = SIGNULL
self.escape_code_parser.Reset()
self.exit_code = 0
@ -224,22 +224,22 @@ func (self *Loop) run() (err error) {
defer func() {
// notify tty reader that we are shutting down
r_w.Close()
close(self.tty_reading_done_channel)
close(tty_reading_done_channel)
if finalizer != "" {
self.QueueWriteString(finalizer)
}
self.QueueWriteBytesDangerous(self.terminal_options.ResetStateEscapeCodes())
// flush queued data and wait for it to be written for a timeout, then wait for writer to shutdown
flush_writer(w_w, self.tty_write_channel, self.tty_writing_done_channel, self.pending_writes, 2*time.Second)
flush_writer(w_w, tty_write_channel, tty_writing_done_channel, self.pending_writes, 2*time.Second)
self.pending_writes = nil
// wait for tty reader to exit cleanly
for more := true; more; _, more = <-self.tty_read_channel {
for more := true; more; _, more = <-tty_read_channel {
}
}()
go write_to_tty(w_r, self.controlling_term, self.tty_write_channel, self.err_channel, self.write_done_channel, self.tty_writing_done_channel)
go read_from_tty(r_r, self.controlling_term, self.tty_read_channel, self.err_channel, self.tty_reading_done_channel)
go write_to_tty(w_r, self.controlling_term, tty_write_channel, err_channel, write_done_channel, tty_writing_done_channel)
go read_from_tty(r_r, self.controlling_term, tty_read_channel, err_channel, tty_reading_done_channel)
if self.OnInitialize != nil {
finalizer, err = self.OnInitialize()
@ -249,7 +249,7 @@ func (self *Loop) run() (err error) {
}
for self.keep_going {
self.queue_write_to_tty(nil)
self.flush_pending_writes(tty_write_channel)
timeout_chan := no_timeout_channel
if len(self.timers) > 0 {
now := time.Now()
@ -269,8 +269,8 @@ func (self *Loop) run() (err error) {
for len(self.wakeup_channel) > 0 {
<-self.wakeup_channel
}
case msg_id := <-self.write_done_channel:
self.queue_write_to_tty(nil)
case msg_id := <-write_done_channel:
self.flush_pending_writes(tty_write_channel)
if self.OnWriteComplete != nil {
err = self.OnWriteComplete(msg_id)
if err != nil {
@ -282,7 +282,7 @@ func (self *Loop) run() (err error) {
if err != nil {
return err
}
case input_data, more := <-self.tty_read_channel:
case input_data, more := <-tty_read_channel:
if !more {
return io.EOF
}

View File

@ -57,28 +57,20 @@ func writestring_ignoring_temporary_errors(f *tty.Term, buf string) (int, error)
return n, err
}
func (self *Loop) queue_write_to_tty(data *write_msg) {
func (self *Loop) flush_pending_writes(tty_write_channel chan<- *write_msg) {
for len(self.pending_writes) > 0 {
select {
case self.tty_write_channel <- self.pending_writes[0]:
case tty_write_channel <- self.pending_writes[0]:
n := copy(self.pending_writes, self.pending_writes[1:])
self.pending_writes = self.pending_writes[:n]
default:
if data != nil {
self.pending_writes = append(self.pending_writes, data)
}
return
}
}
if data != nil {
select {
case self.tty_write_channel <- data:
default:
self.pending_writes = append(self.pending_writes, data)
}
}
}
func (self *Loop) add_write_to_pending_queue(data *write_msg) {
self.pending_writes = append(self.pending_writes, data)
}
func create_write_dispatcher(msg *write_msg) *write_dispatcher {
self := write_dispatcher{str: msg.str, bytes: msg.bytes, is_string: msg.bytes == nil}
if self.is_string {