src/events.c - Event handling stuff


An event_thread handles async events for all interpreters. When events are due, they are placed in per interpreter task_queues, where they are handled then by the check_event* opcodes.

IO events and signals are catched in the io_thread, which again dispatches these to one or all interpreters.

Signal Handling ^

static void sig_handler(int signum)

Handle signal signum.

TODO - Only SIGHUP is handled at the moment for testing

static void Parrot_sigaction(int sig, void (*handler)(int))

Signal handlers are common to all threads, signal block masks are specific, so we install one handler then block that signal and unblock it in the thread, that will receive that signal.

Initialization ^

static void init_events_first(Parrot_Interp interpreter)

Init event system for first interpreter.

static void init_events_all(Parrot_Interp interpreter)

Init events for all interpreters.

void Parrot_init_events(Parrot_Interp interpreter)

Initialize the event system.

Event Handler Functions ^


Create queue entry and insert event into task queue.

void Parrot_new_timer_event(Parrot_Interp interpreter, PMC *timer, FLOATVAL diff, FLOATVAL interval, int repeat, PMC *sub, parrot_event_type_enum typ)

Create a new timer event due at diff from now, repeated at interval and running the passed sub.

void Parrot_new_cb_event(Parrot_Interp, PMC*cbi, void*ext)

Prepare and schedul a callback event

void Parrot_del_timer_event(Parrot_Interp interpreter, PMC *timer)

Deactivate the timer identified by timer.

void Parrot_new_terminate_event(Parrot_Interp interpreter)

Create a terminate event, interpreter will leave the run-loop when this event arrives.

void Parrot_kill_event_loop(void)

Schedule event-loop terminate event. This shuts down the event thread.

void Parrot_schedule_interp_qentry(Parrot_Interp interpreter, QUEUE_ENTRY *entry)

Put a queue entry into the interpreters task queue and enable event checking for the interpreter.

void Parrot_schedule_broadcast_qentry(QUEUE_ENTRY *entry)

Broadcast an event.

IO Thread Handling ^

static void *io_thread(void *data)

The IO thread uses select/poll to handle IO events and signals.

It waits on input from the message pipe to insert file descriptors in the wait sets.

static void stop_io_thread(void)

Tell the IO thread to stop.

Event Handler Thread Functions ^

static QUEUE_ENTRY *dup_entry(QUEUE_ENTRY *entry)

Duplicate queue entry.

static QUEUE_ENTRY *dup_entry_interval(QUEUE_ENTRY *entry, FLOATVAL now)

Duplicate timed entry and add interval to abs_time.

static int process_events(QUEUE *event_q)

Do something, when an event arrived caller has locked the mutex returns 0 if event thread terminates.

static void *event_thread(void *data)

The event thread is started by the first interpreter. It handles all events for all interpreters.

Sleep Handling ^

static void *wait_for_wakeup(Parrot_Interp interpreter, void *next)

Sleep on the event queue condition. If an event arrives, the event is processed. Terminate the loop if sleeping is finished.

void *Parrot_sleep_on_event(Parrot_Interp interpreter, FLOATVAL t, void *next)

Go to sleep. This is called from the sleep opcode.

Event Handling for Run-Loops ^

void *Parrot_do_check_events(Parrot_Interp interpreter, void *next)

Explicitely sync called by the check_event opcode from run loops.

static void event_to_exception(Parrot_Interp interpreter, parrot_event *event)

Convert event to exception and throw it.

static void *do_event(Parrot_Interp interpreter, parrot_event *event, void *next)

Run user code or such.

void *Parrot_do_handle_events(Parrot_Interp interpreter, int restore, void *next)

Called by the check_event__ opcode from run loops or from above. When called from the check_events__ opcode, we have to restore the op_func_table.


include/parrot/events.h and docs/dev/events.pod.