dispatch

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package dispatch provides structs and interfaces to organize thread-safe communication between different modules of the program.

Index

Constants

View Source
const (
	MessageEmpty = -2147483648 + iota
	MessageWorkExec
	MessageExit
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackHandler

type CallbackHandler func(message *Message)

type Looper

type Looper interface {
	Loop()
	GetMessageDispatcher() MessageDispatcher
}

type LooperImpl

type LooperImpl struct {
	// contains filtered or unexported fields
}

func NewLooperImpl

func NewLooperImpl(messageQueueBuffer uint) *LooperImpl

func NewLooperImplCompat

func NewLooperImplCompat(messageQueueBuffer int) *LooperImpl

func (*LooperImpl) Execute

func (l *LooperImpl) Execute(item WorkItem)

func (*LooperImpl) GetMessageDispatcher

func (l *LooperImpl) GetMessageDispatcher() MessageDispatcher

func (*LooperImpl) GetWorkDispatcher

func (l *LooperImpl) GetWorkDispatcher() WorkDispatcher

func (*LooperImpl) Loop

func (l *LooperImpl) Loop()

func (*LooperImpl) RemoveMessageHandler

func (l *LooperImpl) RemoveMessageHandler(what int)

func (*LooperImpl) SendMessage

func (l *LooperImpl) SendMessage(message *Message)

func (*LooperImpl) SetMessageHandler

func (l *LooperImpl) SetMessageHandler(what int, handler MessageHandlerFunc)

type Message

type Message struct {
	Id      int
	What    int
	StrData string
	AnyData interface{}
	ReplyTo *Message
}

func NewMessage

func NewMessage(what int) *Message

func NewMessageWithAnyData

func NewMessageWithAnyData(what int, data interface{}) *Message

func NewMessageWithStringData

func NewMessageWithStringData(what int, data string) *Message

type MessageDispatcher

type MessageDispatcher interface {
	SendMessage(message *Message)
}

type MessageHandler

type MessageHandler interface {
	OnMessage(msg *Message)
}

type MessageHandlerFunc

type MessageHandlerFunc func(message *Message)

type MessageQueue

type MessageQueue struct {
	// contains filtered or unexported fields
}

MessageQueue is a thread-safe message buffer. In general, it can be used to communicate between different threads. It is mostly used to communicate between the engine and the frontend. There are two channels under the hood - main and secondary. The size of main channel is specified in the constructor function, whereas the size of the secondary channel equals half the size of the main channel. You can lock the main channel, so new messages won't be sent to it. Then all messages in it can be processed. While the main channel is locked, messages are sent to the secondary channel. As soon as the main channel is unlocked, all messages from the secondary channel are sent to main channel.

func NewMessageQueue

func NewMessageQueue(bufferSize uint) *MessageQueue

NewMessageQueue creates a new instance of MessageQueue with the specified buffer size. Returns pointer to the MessageQueue instance.

func (*MessageQueue) Close

func (q *MessageQueue) Close()

Close closes the underlying channels.

func (*MessageQueue) Dequeue

func (q *MessageQueue) Dequeue() *Message

Dequeue Removes the first message from the queue. If queue is empty returns an empty Message.

func (*MessageQueue) DequeueBlocking

func (q *MessageQueue) DequeueBlocking() *Message

DequeueBlocking Removes the first message from the queue. If the queue is empty waits for a message to come.

func (*MessageQueue) Enqueue

func (q *MessageQueue) Enqueue(message *Message)

Enqueue sends the specified message to the end of the queue. Depending on the current state of the MessageQueue it sent to either main or secondary channel.

func (*MessageQueue) GetSize

func (q *MessageQueue) GetSize() uint

GetSize returns the current size of the queue.

func (*MessageQueue) IsEmpty

func (q *MessageQueue) IsEmpty() bool

IsEmpty indicate if the queue is empty, i.e. size == 0.

func (*MessageQueue) LockMainChannel

func (q *MessageQueue) LockMainChannel()

LockMainChannel locks the main channel. While it is locked new messages will be sent to the secondary channel. The locked state remains until UnlockMainChannel is called.

func (*MessageQueue) UnlockMainChannel

func (q *MessageQueue) UnlockMainChannel()

UnlockMainChannel unlocks the main channel. All messages from the secondary channel are sent to the main one. After that new messages are sent to the main channel again.

func (*MessageQueue) WaitForMessage added in v0.3.4

func (q *MessageQueue) WaitForMessage(what int)

WaitForMessage blocks the calling thread until a message with the given code is read from the queue.

type WorkDispatcher

type WorkDispatcher interface {
	Execute(item WorkItem)
}

type WorkItem

type WorkItem interface {
	Execute()
}

type WorkItemFunc

type WorkItemFunc struct {
	// contains filtered or unexported fields
}

func NewWorkItemFunc

func NewWorkItemFunc(work func()) *WorkItemFunc

func (*WorkItemFunc) Execute

func (w *WorkItemFunc) Execute()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL