<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
event	B-General_Concept
loop	I-General_Concept
is	O
a	O
programming	O
construct	O
or	O
design	O
pattern	O
that	O
waits	O
for	O
and	O
dispatches	O
events	B-Application
or	O
messages	B-Application
in	O
a	O
program	B-Application
.	O
</s>
<s>
The	O
event	B-General_Concept
loop	I-General_Concept
works	O
by	O
making	O
a	O
request	O
to	O
some	O
internal	O
or	O
external	O
"	O
event	O
provider	O
"	O
(	O
that	O
generally	O
blocks	B-Operating_System
the	O
request	O
until	O
an	O
event	O
has	O
arrived	O
)	O
,	O
then	O
calls	O
the	O
relevant	O
event	O
handler	O
(	O
"	O
dispatches	O
the	O
event	O
"	O
)	O
.	O
</s>
<s>
The	O
event	B-General_Concept
loop	I-General_Concept
is	O
also	O
sometimes	O
referred	O
to	O
as	O
the	O
message	B-General_Concept
dispatcher	I-General_Concept
,	O
message	B-Library
loop	I-Library
,	O
message	B-General_Concept
pump	I-General_Concept
,	O
or	O
run	O
loop	O
.	O
</s>
<s>
The	O
event-loop	O
may	O
be	O
used	O
in	O
conjunction	O
with	O
a	O
reactor	B-Operating_System
,	O
if	O
the	O
event	O
provider	O
follows	O
the	O
file	O
interface	O
,	O
which	O
can	O
be	O
selected	O
or	O
'	O
polled	O
 '	O
(	O
the	O
Unix	B-Application
system	I-Application
call	O
,	O
not	O
actual	O
polling	B-General_Concept
)	O
.	O
</s>
<s>
The	O
event	B-General_Concept
loop	I-General_Concept
almost	O
always	O
operates	O
asynchronously	O
with	O
the	O
message	O
originator	O
.	O
</s>
<s>
When	O
the	O
event	B-General_Concept
loop	I-General_Concept
forms	O
the	O
central	O
control	O
flow	O
construct	O
of	O
a	O
program	B-Application
,	O
as	O
it	O
often	O
does	O
,	O
it	O
may	O
be	O
termed	O
the	O
main	B-General_Concept
loop	I-General_Concept
or	O
main	O
event	B-General_Concept
loop	I-General_Concept
.	O
</s>
<s>
This	O
title	O
is	O
appropriate	O
,	O
because	O
such	O
an	O
event	B-General_Concept
loop	I-General_Concept
is	O
at	O
the	O
highest	O
level	O
of	O
control	O
within	O
the	O
program	B-Application
.	O
</s>
<s>
Message	B-General_Concept
pumps	I-General_Concept
are	O
said	O
to	O
'	O
pump	O
 '	O
messages	B-Application
from	O
the	O
program	B-Application
's	O
message	B-Operating_System
queue	I-Operating_System
(	O
assigned	O
and	O
usually	O
owned	O
by	O
the	O
underlying	O
operating	O
system	O
)	O
into	O
the	O
program	B-Application
for	O
processing	O
.	O
</s>
<s>
In	O
the	O
strictest	O
sense	O
,	O
an	O
event	B-General_Concept
loop	I-General_Concept
is	O
one	O
of	O
the	O
methods	O
for	O
implementing	O
inter-process	B-Operating_System
communication	I-Operating_System
.	O
</s>
<s>
In	O
fact	O
,	O
message	O
processing	O
exists	O
in	O
many	O
systems	O
,	O
including	O
a	O
kernel-level	B-Operating_System
component	O
of	O
the	O
Mach	B-Operating_System
operating	I-Operating_System
system	I-Operating_System
.	O
</s>
<s>
The	O
event	B-General_Concept
loop	I-General_Concept
is	O
a	O
specific	O
implementation	O
technique	O
of	O
systems	O
that	O
use	O
message	B-Architecture
passing	I-Architecture
.	O
</s>
<s>
Traditionally	O
,	O
a	O
program	B-Application
simply	O
ran	O
once	O
,	O
then	O
terminated	O
.	O
</s>
<s>
This	O
type	O
of	O
program	B-Application
was	O
very	O
common	O
in	O
the	O
early	O
days	O
of	O
computing	O
,	O
and	O
lacked	O
any	O
form	O
of	O
user	O
interactivity	O
.	O
</s>
<s>
This	O
is	O
still	O
used	O
frequently	O
,	O
particularly	O
in	O
the	O
form	O
of	O
command-line-driven	B-Application
programs	O
.	O
</s>
<s>
Any	O
parameters	O
are	O
set	O
up	O
in	O
advance	O
and	O
passed	O
in	O
one	O
go	O
when	O
the	O
program	B-Application
starts	O
.	O
</s>
<s>
These	O
still	O
may	O
feature	O
a	O
main	B-General_Concept
loop	I-General_Concept
,	O
but	O
are	O
not	O
usually	O
thought	O
of	O
as	O
event	B-Application
driven	I-Application
in	O
the	O
usual	O
sense	O
.	O
</s>
<s>
Due	O
to	O
the	O
predominance	O
of	O
graphical	B-Application
user	I-Application
interfaces	I-Application
,	O
most	O
modern	O
applications	B-Application
feature	O
a	O
main	B-General_Concept
loop	I-General_Concept
.	O
</s>
<s>
The	O
get_next_message( )	O
routine	O
is	O
typically	O
provided	O
by	O
the	O
operating	O
system	O
,	O
and	O
blocks	B-Operating_System
until	O
a	O
message	O
is	O
available	O
.	O
</s>
<s>
Under	O
Unix	B-Application
,	O
the	O
"	O
everything	B-Application
is	I-Application
a	I-Application
file	I-Application
"	O
paradigm	O
naturally	O
leads	O
to	O
a	O
file-based	O
event	B-General_Concept
loop	I-General_Concept
.	O
</s>
<s>
Reading	O
from	O
and	O
writing	O
to	O
files	O
,	O
inter-process	B-Operating_System
communication	I-Operating_System
,	O
network	O
communication	O
,	O
and	O
device	O
control	O
are	O
all	O
achieved	O
using	O
file	O
I/O	O
,	O
with	O
the	O
target	O
identified	O
by	O
a	O
file	B-Application
descriptor	I-Application
.	O
</s>
<s>
The	O
select	B-Language
and	O
poll	B-Language
system	O
calls	O
allow	O
a	O
set	O
of	O
file	B-Application
descriptors	I-Application
to	O
be	O
monitored	O
for	O
a	O
change	O
of	O
state	O
,	O
e.g.	O
</s>
<s>
For	O
example	O
,	O
consider	O
a	O
program	B-Application
that	O
reads	O
from	O
a	O
continuously	O
updated	O
file	O
and	O
displays	O
its	O
contents	O
in	O
the	O
X	B-Operating_System
Window	I-Operating_System
System	I-Operating_System
,	O
which	O
communicates	O
with	O
clients	O
over	O
a	O
socket	B-Protocol
(	O
either	O
Unix	B-Protocol
domain	I-Protocol
or	O
Berkeley	B-Protocol
)	O
:	O
</s>
<s>
One	O
of	O
the	O
few	O
things	O
in	O
Unix	B-Application
that	O
does	O
not	O
conform	O
to	O
the	O
file	O
interface	O
are	O
asynchronous	O
events	B-Application
(	O
signals	B-Operating_System
)	O
.	O
</s>
<s>
Signals	B-Operating_System
are	O
received	O
in	O
signal	B-Operating_System
handlers	I-Operating_System
,	O
small	O
,	O
limited	O
pieces	O
of	O
code	O
that	O
run	O
while	O
the	O
rest	O
of	O
the	O
task	O
is	O
suspended	O
;	O
if	O
a	O
signal	B-Operating_System
is	O
received	O
and	O
handled	O
while	O
the	O
task	O
is	O
blocking	B-Operating_System
in	O
select( )	O
,	O
select	B-Language
will	O
return	O
early	O
with	O
EINTR	B-Error_Name
;	O
if	O
a	O
signal	B-Operating_System
is	O
received	O
while	O
the	O
task	O
is	O
CPU	O
bound	O
,	O
the	O
task	O
will	O
be	O
suspended	O
between	O
instructions	O
until	O
the	O
signal	B-Operating_System
handler	I-Operating_System
returns	O
.	O
</s>
<s>
Thus	O
an	O
obvious	O
way	O
to	O
handle	B-Language
signals	B-Operating_System
is	O
for	O
signal	B-Operating_System
handlers	I-Operating_System
to	O
set	O
a	O
global	O
flag	O
and	O
have	O
the	O
event	B-General_Concept
loop	I-General_Concept
check	O
for	O
the	O
flag	O
immediately	O
before	O
and	O
after	O
the	O
select( )	O
call	O
;	O
if	O
it	O
is	O
set	O
,	O
handle	B-Language
the	O
signal	B-Operating_System
in	O
the	O
same	O
manner	O
as	O
with	O
events	B-Application
on	O
file	B-Application
descriptors	I-Application
.	O
</s>
<s>
Unfortunately	O
,	O
this	O
gives	O
rise	O
to	O
a	O
race	B-Operating_System
condition	I-Operating_System
:	O
if	O
a	O
signal	B-Operating_System
arrives	O
immediately	O
between	O
checking	O
the	O
flag	O
and	O
calling	O
select( )	O
,	O
it	O
will	O
not	O
be	O
handled	O
until	O
select( )	O
returns	O
for	O
some	O
other	O
reason	O
(	O
for	O
example	O
,	O
being	O
interrupted	O
by	O
a	O
frustrated	O
user	O
)	O
.	O
</s>
<s>
The	O
solution	O
arrived	O
at	O
by	O
POSIX	O
is	O
the	O
pselect( )	O
call	O
,	O
which	O
is	O
similar	O
to	O
select( )	O
but	O
takes	O
an	O
additional	O
sigmask	O
parameter	O
,	O
which	O
describes	O
a	O
signal	B-Operating_System
mask	O
.	O
</s>
<s>
This	O
allows	O
an	O
application	O
to	O
mask	O
signals	B-Operating_System
in	O
the	O
main	O
task	O
,	O
then	O
remove	O
the	O
mask	O
for	O
the	O
duration	O
of	O
the	O
select( )	O
call	O
such	O
that	O
signal	B-Operating_System
handlers	I-Operating_System
are	O
only	O
called	O
while	O
the	O
application	O
is	O
I/O	B-General_Concept
bound	I-General_Concept
.	O
</s>
<s>
However	O
,	O
implementations	O
of	O
pselect( )	O
have	O
not	O
always	O
been	O
reliable	O
;	O
versions	O
of	O
Linux	B-Operating_System
prior	O
to	O
2.6.16	O
do	O
not	O
have	O
a	O
pselect( )	O
system	O
call	O
,	O
forcing	O
glibc	B-Language
to	O
emulate	O
it	O
via	O
a	O
method	O
prone	O
to	O
the	O
very	O
same	O
race	B-Operating_System
condition	I-Operating_System
pselect( )	O
is	O
intended	O
to	O
avoid	O
.	O
</s>
<s>
An	O
alternative	O
,	O
more	O
portable	O
solution	O
,	O
is	O
to	O
convert	O
asynchronous	O
events	B-Application
to	O
file-based	O
events	B-Application
using	O
the	O
self-pipe	O
trick	O
,	O
where	O
"	O
a	O
signal	B-Operating_System
handler	I-Operating_System
writes	O
a	O
byte	O
to	O
a	O
pipe	O
whose	O
other	O
end	O
is	O
monitored	O
by	O
select( )	O
in	O
the	O
main	O
program	B-Application
"	O
.	O
</s>
<s>
In	O
Linux	B-Operating_System
kernel	I-Operating_System
version	O
2.6.22	O
,	O
a	O
new	O
system	O
call	O
signalfd( )	O
was	O
added	O
,	O
which	O
allows	O
receiving	O
signals	B-Operating_System
via	O
a	O
special	O
file	B-Application
descriptor	I-Application
.	O
</s>
<s>
On	O
the	O
Microsoft	B-Application
Windows	I-Application
operating	I-Application
system	I-Application
,	O
a	O
process	O
that	O
interacts	O
with	O
the	O
user	O
must	O
accept	O
and	O
react	O
to	O
incoming	O
messages	B-Application
,	O
which	O
is	O
almost	O
inevitably	O
done	O
by	O
a	O
message	B-Library
loop	I-Library
in	O
that	O
process	O
.	O
</s>
<s>
An	O
event	O
can	O
be	O
user	O
interaction	O
,	O
network	O
traffic	O
,	O
system	O
processing	O
,	O
timer	O
activity	O
,	O
inter-process	B-Operating_System
communication	I-Operating_System
,	O
among	O
others	O
.	O
</s>
<s>
For	O
non-interactive	O
,	O
I/O	O
only	O
events	B-Application
,	O
Windows	O
has	O
I/O	B-Library
completion	I-Library
ports	I-Library
.	O
</s>
<s>
I/O	B-Library
completion	I-Library
port	I-Library
loops	O
run	O
separately	O
from	O
the	O
Message	B-Library
loop	I-Library
,	O
and	O
do	O
not	O
interact	O
with	O
the	O
Message	B-Library
loop	I-Library
out	O
of	O
the	O
box	O
.	O
</s>
<s>
The	O
"	O
heart	O
"	O
of	O
most	O
Win32	B-Library
applications	B-Application
is	O
the	O
function	O
,	O
which	O
calls	O
in	O
a	O
loop	O
.	O
</s>
<s>
GetMessage( )	O
blocks	B-Operating_System
until	O
a	O
message	O
,	O
or	O
"	O
event	O
"	O
,	O
is	O
received	O
(	O
with	O
function	O
as	O
a	O
non-blocking	O
alternative	O
)	O
.	O
</s>
<s>
After	O
some	O
optional	O
processing	O
,	O
it	O
will	O
call	O
,	O
which	O
dispatches	O
the	O
message	O
to	O
the	O
relevant	O
handler	O
,	O
also	O
known	O
as	O
WindowProc	B-Library
.	O
</s>
<s>
Normally	O
,	O
messages	B-Application
that	O
have	O
no	O
special	O
are	O
dispatched	O
to	O
DefWindowProc	O
,	O
the	O
default	O
one	O
.	O
</s>
<s>
DispatchMessage( )	O
calls	O
the	O
WindowProc	B-Library
of	O
the	O
HWND	O
handle	B-Language
of	O
the	O
message	O
(	O
registered	O
with	O
the	O
function	O
)	O
.	O
</s>
<s>
More	O
recent	O
versions	O
of	O
Microsoft	B-Application
Windows	I-Application
guarantee	O
to	O
the	O
programmer	O
that	O
messages	B-Application
will	O
be	O
delivered	O
to	O
an	O
application	O
's	O
message	B-Library
loop	I-Library
in	O
the	O
order	O
that	O
they	O
were	O
perceived	O
by	O
the	O
system	O
and	O
its	O
peripherals	O
.	O
</s>
<s>
This	O
guarantee	O
is	O
essential	O
when	O
considering	O
the	O
design	O
consequences	O
of	O
multithreaded	B-Operating_System
applications	B-Application
.	O
</s>
<s>
However	O
,	O
some	O
messages	B-Application
have	O
different	O
rules	O
,	O
such	O
as	O
messages	B-Application
that	O
are	O
always	O
received	O
last	O
,	O
or	O
messages	B-Application
with	O
a	O
different	O
documented	O
priority	O
.	O
</s>
<s>
X	B-Operating_System
applications	B-Application
using	O
Xlib	B-Language
directly	O
are	O
built	O
around	O
the	O
XNextEvent	O
family	O
of	O
functions	O
;	O
XNextEvent	O
blocks	B-Operating_System
until	O
an	O
event	O
appears	O
on	O
the	O
event	B-Operating_System
queue	I-Operating_System
,	O
whereupon	O
the	O
application	O
processes	O
it	O
appropriately	O
.	O
</s>
<s>
The	O
Xlib	B-Language
event	B-General_Concept
loop	I-General_Concept
only	O
handles	O
window	B-Application
system	I-Application
events	B-Application
;	O
applications	B-Application
that	O
need	O
to	O
be	O
able	O
to	O
wait	O
on	O
other	O
files	O
and	O
devices	O
could	O
construct	O
their	O
own	O
event	B-General_Concept
loop	I-General_Concept
from	O
primitives	O
such	O
as	O
ConnectionNumber	O
,	O
but	O
in	O
practice	O
tend	O
to	O
use	O
multithreading	B-Operating_System
.	O
</s>
<s>
Very	O
few	O
programs	O
use	O
Xlib	B-Language
directly	O
.	O
</s>
<s>
In	O
the	O
more	O
common	O
case	O
,	O
GUI	B-Application
toolkits	O
based	O
on	O
Xlib	B-Language
usually	O
support	O
adding	O
events	B-Application
.	O
</s>
<s>
For	O
example	O
,	O
toolkits	O
based	O
on	O
Xt	B-Language
Intrinsics	I-Language
have	O
XtAppAddInput( )	O
and	O
XtAppAddTimeout( )	O
.	O
</s>
<s>
Please	O
note	O
that	O
it	O
is	O
not	O
safe	O
to	O
call	O
Xlib	B-Language
functions	O
from	O
a	O
signal	B-Operating_System
handler	I-Operating_System
,	O
because	O
the	O
X	B-Operating_System
application	O
may	O
have	O
been	O
interrupted	O
in	O
an	O
arbitrary	O
state	O
,	O
e.g.	O
</s>
<s>
See	O
for	O
a	O
solution	O
for	O
X11R5	B-Operating_System
,	O
X11R6	B-Operating_System
and	O
Xt	O
.	O
</s>
<s>
The	O
GLib	B-Language
event	B-General_Concept
loop	I-General_Concept
was	O
originally	O
created	O
for	O
use	O
in	O
GTK	B-Application
but	O
is	O
now	O
used	O
in	O
non-GUI	O
applications	B-Application
as	O
well	O
,	O
such	O
as	O
D-Bus	B-Language
.	O
</s>
<s>
The	O
resource	O
polled	O
is	O
the	O
collection	O
of	O
file	B-Application
descriptors	I-Application
the	O
application	O
is	O
interested	O
in	O
;	O
the	O
polling	B-General_Concept
block	O
will	O
be	O
interrupted	O
if	O
a	O
signal	B-Operating_System
arrives	O
or	O
a	O
timeout	O
expires	O
(	O
e.g.	O
</s>
<s>
While	O
GLib	B-Language
has	O
built-in	O
support	O
for	O
file	B-Application
descriptor	I-Application
and	O
child	O
termination	O
events	B-Application
,	O
it	O
is	O
possible	O
to	O
add	O
an	O
event	O
source	O
for	O
any	O
event	O
that	O
can	O
be	O
handled	O
in	O
a	O
prepare-check-dispatch	O
model	O
.	O
</s>
<s>
Application	O
libraries	O
that	O
are	O
built	O
on	O
the	O
GLib	B-Language
event	B-General_Concept
loop	I-General_Concept
include	O
GStreamer	B-Language
and	O
the	O
asynchronous	B-Architecture
I/O	I-Architecture
methods	O
of	O
GnomeVFS	B-Application
,	O
but	O
GTK	B-Application
remains	O
the	O
most	O
visible	O
client	O
library	O
.	O
</s>
<s>
Events	B-Application
from	O
the	O
windowing	B-Application
system	I-Application
(	O
in	O
X	B-Operating_System
,	O
read	O
off	O
the	O
X	B-Operating_System
socket	B-Protocol
)	O
are	O
translated	O
by	O
GDK	B-Language
into	O
GTK	B-Application
events	B-Application
and	O
emitted	O
as	O
GLib	B-Language
signals	B-Operating_System
on	O
the	O
application	O
's	O
widget	O
objects	O
.	O
</s>
<s>
Exactly	O
one	O
CFRunLoop	O
is	O
allowed	O
per	O
thread	B-Operating_System
,	O
and	O
arbitrarily	O
many	O
sources	O
and	O
observers	O
can	O
be	O
attached	O
.	O
</s>
<s>
Sources	O
then	O
communicate	O
with	O
observers	O
through	O
the	O
run	O
loop	O
,	O
with	O
it	O
organising	O
queueing	O
and	O
dispatch	O
of	O
messages	B-Application
.	O
</s>
<s>
The	O
CFRunLoop	O
is	O
abstracted	O
in	O
Cocoa	B-Operating_System
as	O
an	O
NSRunLoop	O
,	O
which	O
allows	O
any	O
message	O
(	O
equivalent	O
to	O
a	O
function	O
call	O
in	O
non-reflective	O
runtimes	O
)	O
to	O
be	O
queued	O
for	O
dispatch	O
to	O
any	O
object	O
.	O
</s>
