aboutsummaryrefslogtreecommitdiff
path: root/event.go
diff options
context:
space:
mode:
Diffstat (limited to 'event.go')
-rw-r--r--event.go118
1 files changed, 59 insertions, 59 deletions
diff --git a/event.go b/event.go
index 3923dc8..6323907 100644
--- a/event.go
+++ b/event.go
@@ -8,24 +8,25 @@ import "runtime"
// The C pointer type for an EventContext handle.
type CEventContext = *C.eventcontext_t
type eventContext struct {
- inner CEventContext
- attached map[uintptr]interface{}
+ inner CEventContext
+ attached map[uintptr]interface{}
}
+
// The handle for an event loop.
type EventContext = *eventContext
func NewEventContext() EventContext {
- res := &eventContext{
- inner: C.eventcontext_new(),
- attached: make(map[uintptr]interface{}),
- }
- runtime.SetFinalizer(res, func(self EventContext) { self.free() })
- return res
+ res := &eventContext{
+ inner: C.eventcontext_new(),
+ attached: make(map[uintptr]interface{}),
+ }
+ runtime.SetFinalizer(res, func(self EventContext) { self.free() })
+ return res
}
func (self EventContext) attach(ptr rawptr_t, x interface{}) { self.attached[uintptr(ptr)] = x }
-func (self EventContext) detach(ptr rawptr_t) { delete(self.attached, uintptr(ptr)) }
-func (self EventContext) free() { C.eventcontext_free(self.inner) }
+func (self EventContext) detach(ptr rawptr_t) { delete(self.attached, uintptr(ptr)) }
+func (self EventContext) free() { C.eventcontext_free(self.inner) }
// Start the event loop. This is a blocking call that will hand over the
// control flow to the infinite loop which triggers callbacks upon new events.
@@ -40,7 +41,8 @@ func (self EventContext) Stop() { C.eventcontext_stop(self.inner) }
// The C pointer type for a ThreadCall handle.
type CThreadCall = *C.threadcall_t
-type threadCall struct { inner CThreadCall }
+type threadCall struct{ inner CThreadCall }
+
// The handle for scheduling a function call executed by a particular
// EventContext event loop.
type ThreadCall = *threadCall
@@ -53,23 +55,23 @@ type ThreadCallCallback = C.threadcall_callback_t
// invokcation of AsyncCall() will schedule a function call executed in the
// given EventContext event loop.
func NewThreadCall(ec EventContext) ThreadCall {
- res := &threadCall{ inner: C.threadcall_new(ec.inner) }
- runtime.SetFinalizer(res, func(self ThreadCall) { self.free() })
- return res
+ res := &threadCall{inner: C.threadcall_new(ec.inner)}
+ runtime.SetFinalizer(res, func(self ThreadCall) { self.free() })
+ return res
}
func (self ThreadCall) free() { C.threadcall_free(self.inner) }
// Schedule a function to be executed in the target event loop.
func (self ThreadCall) AsyncCall(callback ThreadCallCallback, userdata rawptr_t) {
- C.threadcall_async_call(self.inner, callback, userdata)
+ C.threadcall_async_call(self.inner, callback, userdata)
}
// The C pointer type for TimerEvent handle.
type CTimerEvent = *C.timerev_t
type timerEvent struct {
- inner CTimerEvent
- ec EventContext
+ inner CTimerEvent
+ ec EventContext
}
// The handle for a timed event.
@@ -82,45 +84,44 @@ type TimerEventCallback = C.timerev_callback_t
// Create a TimerEvent handle attached to the given EventContext, with a
// registered callback.
func NewTimerEvent(_ec EventContext, cb TimerEventCallback, userdata rawptr_t) TimerEvent {
- res := &timerEvent{
- inner: C.timerev_new(_ec.inner, cb, userdata),
- ec: _ec,
- }
- _ec.attach(rawptr_t(res.inner), res)
- runtime.SetFinalizer(res, func(self TimerEvent) { self.free() })
- return res
+ res := &timerEvent{
+ inner: C.timerev_new(_ec.inner, cb, userdata),
+ ec: _ec,
+ }
+ _ec.attach(rawptr_t(res.inner), res)
+ runtime.SetFinalizer(res, func(self TimerEvent) { self.free() })
+ return res
}
func (self TimerEvent) free() { C.timerev_free(self.inner) }
// Change the callback.
func (self TimerEvent) SetCallback(callback TimerEventCallback, userdata rawptr_t) {
- C.timerev_set_callback(self.inner, callback, userdata)
+ C.timerev_set_callback(self.inner, callback, userdata)
}
// Schedule the timer to go off after t_sec seconds.
func (self TimerEvent) Add(t_sec float64) { C.timerev_add(self.inner, C.double(t_sec)) }
-
// Unschedule the timer if it was scheduled. The timer could still be rescheduled
// by calling Add() afterwards.
func (self TimerEvent) Del() {
- self.ec.detach(rawptr_t(self.inner))
- C.timerev_del(self.inner)
+ self.ec.detach(rawptr_t(self.inner))
+ C.timerev_del(self.inner)
}
// Empty the timer. It will be unscheduled and deallocated and its methods
// should never be called again.
func (self TimerEvent) Clear() {
- self.ec.detach(rawptr_t(self.inner))
- C.timerev_clear(self.inner)
+ self.ec.detach(rawptr_t(self.inner))
+ C.timerev_clear(self.inner)
}
// The C pointer type for a SigEvent.
type CSigEvent = *C.sigev_t
type sigEvent struct {
- inner CSigEvent
- ec EventContext
+ inner CSigEvent
+ ec EventContext
}
// The handle for a UNIX signal event.
@@ -129,20 +130,20 @@ type SigEvent = *sigEvent
type SigEventCallback = C.sigev_callback_t
var (
- SIGTERM = C.SIGTERM
- SIGINT = C.SIGINT
+ SIGTERM = C.SIGTERM
+ SIGINT = C.SIGINT
)
// Create a SigEvent handle attached to the given EventContext, with a
// registered callback.
func NewSigEvent(_ec EventContext, cb SigEventCallback, userdata rawptr_t) SigEvent {
- res := &sigEvent{
- inner: C.sigev_new(_ec.inner, cb, userdata),
- ec: _ec,
- }
- _ec.attach(rawptr_t(res.inner), res)
- runtime.SetFinalizer(res, func(self SigEvent) { self.free() })
- return res
+ res := &sigEvent{
+ inner: C.sigev_new(_ec.inner, cb, userdata),
+ ec: _ec,
+ }
+ _ec.attach(rawptr_t(res.inner), res)
+ runtime.SetFinalizer(res, func(self SigEvent) { self.free() })
+ return res
}
func (self SigEvent) free() { C.sigev_free(self.inner) }
@@ -150,25 +151,24 @@ func (self SigEvent) free() { C.sigev_free(self.inner) }
// Register the handle to listen for UNIX signal sig.
func (self SigEvent) Add(sig int) { C.sigev_add(self.inner, C.int(sig)) }
-
// Unregister the handle. The handle may be re-registered in the future.
func (self SigEvent) Del() {
- self.ec.detach(rawptr_t(self.inner))
- C.sigev_del(self.inner)
+ self.ec.detach(rawptr_t(self.inner))
+ C.sigev_del(self.inner)
}
// Unregister the handle. Any methods of the handle should no longer be used
// and the handle will be deallocated.
func (self SigEvent) Clear() {
- self.ec.detach(rawptr_t(self.inner))
- C.sigev_clear(self.inner)
+ self.ec.detach(rawptr_t(self.inner))
+ C.sigev_clear(self.inner)
}
// The C pointer type for a MPSCQueue object.
type CMPSCQueue = *C.mpscqueue_t
type mpscQueue struct {
- inner CMPSCQueue
- ec EventContext
+ inner CMPSCQueue
+ ec EventContext
}
// The object for a Multi-Producer, Single-Consumer queue.
@@ -182,41 +182,41 @@ type MPSCQueueCallback = C.mpscqueue_callback_t
// Create a MPSCQueue object.
func NewMPSCQueue() MPSCQueue {
- res := &mpscQueue{ inner: C.mpscqueue_new(), ec: nil }
- runtime.SetFinalizer(res, func(self MPSCQueue) { self.free() })
- return res
+ res := &mpscQueue{inner: C.mpscqueue_new(), ec: nil}
+ runtime.SetFinalizer(res, func(self MPSCQueue) { self.free() })
+ return res
}
func (self MPSCQueue) free() { C.mpscqueue_free(self.inner) }
// Register the callback invoked when there are new elements in the MPSC queue.
func (self MPSCQueue) RegHandler(_ec EventContext, callback MPSCQueueCallback, userdata rawptr_t) {
- C.mpscqueue_reg_handler(self.inner, _ec.inner, callback, userdata)
- self.ec = _ec
- _ec.attach(rawptr_t(self.inner), self)
+ C.mpscqueue_reg_handler(self.inner, _ec.inner, callback, userdata)
+ self.ec = _ec
+ _ec.attach(rawptr_t(self.inner), self)
}
// Unregister the callback.
func (self MPSCQueue) UnregHandler() {
- self.ec.detach(rawptr_t(self.inner))
- C.mpscqueue_unreg_handler(self.inner)
+ self.ec.detach(rawptr_t(self.inner))
+ C.mpscqueue_unreg_handler(self.inner)
}
// Enqueue an element (thread-safe). It returns true if successful. If
// unbounded is true the queue is expanded when full (and this function will
// return true).
func (self MPSCQueue) Enqueue(elem rawptr_t, unbounded bool) bool {
- return bool(C.mpscqueue_enqueue(self.inner, elem, C.bool(unbounded)))
+ return bool(C.mpscqueue_enqueue(self.inner, elem, C.bool(unbounded)))
}
// Try to dequeue an element from the queue (should only be called from one
// thread). It returns true if successful.
func (self MPSCQueue) TryDequeue(elem *rawptr_t) bool {
- return bool(C.mpscqueue_try_dequeue(self.inner, elem))
+ return bool(C.mpscqueue_try_dequeue(self.inner, elem))
}
// Set the initial capacity of the queue. This should only be called before the
// first dequeue/enqueue operation.
func (self MPSCQueue) SetCapacity(capacity int) {
- C.mpscqueue_set_capacity(self.inner, C.size_t(capacity))
+ C.mpscqueue_set_capacity(self.inner, C.size_t(capacity))
}