source: doc/theses/colby_parsons_MMAth/text/waituntil.tex @ f77f648d

ast-experimental
Last change on this file since f77f648d was a0c746df, checked in by caparsons <caparson@…>, 13 months ago

added beginning of waituntil chapter

  • Property mode set to 100644
File size: 7.6 KB
Line 
1% ======================================================================
2% ======================================================================
3\chapter{Waituntil}\label{s:waituntil}
4% ======================================================================
5% ======================================================================
6
7Consider the following motivational problem that we shall title the bathroom problem.
8There are @N@ stalls (resources) in a bathroom and there are @M@ people (threads).
9Each stall has their own lock since only one person may occupy a stall at a time.
10The standard way that humans solve this problem is that they check if the stalls are occupied and if they all are they queue and watch the stalls until one is free and then enter and lock the stall.
11This solution is simple in real life, but can be difficult to implement in a concurrent context as it requires the threads to somehow wait on all the stalls at the same time.
12The naive solution to this is for each thread to spin indefinitely continually checking the stalls until one is free.
13This wastes cycles and also results in no fairness among threads waiting for stalls as a thread will jump in the first stall available without any regard to other waiting threads.
14The ability to wait for the first stall available without spinning can be done with concurrent tools that provide \gls{synch_multiplex}, the ability to wait synchronously for a resource or set of resources.
15
16% C_TODO: fill in citations in following section
17\section{History of Synchronous Multiplexing}
18There is a history of tools that provide \gls{synch_multiplex}.
19Some of the most well known include the set or unix system utilities signal(2)\cite{}, poll(2)\cite{}, and epoll(7)\cite{}, and the select statement provided by Go\cite{}.
20
21Before one can examine the history of \gls{synch_multiplex} implementations in detail, the preceding theory must be discussed.
22The theory surrounding this topic was largely introduced by Hoare in his 1985 CSP book \cite{Hoare85} and his later work with Roscoe on the theoretical language Occam\cite{Roscoe88}.
23Both include guards for communication channels and the ability to wait for a single channel to be ready out of a set of channels.
24The work on Occam in \cite{Roscoe88} calls their \gls{synch_multiplex} primitive ALT, which waits for one resource to be available and then executes a corresponding block of code.
25Waiting for one resource out of a set of resources can be thought of as a logical exclusive or over the set of resources.
26Guards are a conditional operator similar to an @if@, except they apply to the resource being waited on.
27If a guard is false then the resource it guards is considered to not be in the set of resources being waited on.
28Guards can be simulated using if statements, but to do so requires \[2^N\] if cases, where @N@ is the number of guards.
29This transformation from guards to if statements will be discussed further in Section~\ref{}. % C_TODO: fill ref when writing semantics section later
30
31Switching to implementations, it is important to discuss the resources being multiplexed.
32While the aforementioned theory discusses waiting on channels, the earliest known implementation of a synchronous multiplexing tool, Unix's select(2), is multiplexed over file descriptors.
33The select(2) system call takes in sets of file descriptors to wait on as arguments and an optional timeout.
34Select will block until either some subset of file descriptors are available or the timeout expires.
35All file descriptors that are ready will be returned.
36This early implementation differs from the theory as when the call from select returns it may provide more than one ready file descriptor.
37As such select has a logical or multiplexing semantics, whereas the theory described exclusive-or semantics.
38This is not a drawback.
39A user can easily achieve exclusive-or semantics with select by arbitrarily choosing only one of the returned descriptors to operate on.
40Select was followed by poll(2), and later epoll(7), which both improved upon drawbacks in their predecessors.
41The syscall poll(2) improved on select by allowing users to monitor descriptors with numbers higher than 1024 which was not supported by select.
42Epoll then improved on poll to return the set of file descriptors since poll would only say that some descriptor from the set was ready but not return which ones were ready.
43
44It is worth noting these \gls{synch_multiplex} tools mentioned so far interact directly with the operating system and are often used to communicate between processes.
45Later \gls{synch_multiplex} started to appear in user-space to support fast multiplexed concurrent communication between threads.
46An early example of \gls{synch_multiplex} is the select statement in Ada.
47The select statement in Ada allows a task to multiplex over some subset of its own methods that it would like to @accept@ calls to.
48Tasks in Ada can be thought of as threads which are an object of a specific class, and as such have methods, fields, etc.
49This statement has the same exclusive-or semantics as ALT from Occam, and supports guards as described in Occam, however it multiplexes over methods on rather than multiplexing over channels.
50A code block is associated with each @accept@, and the method that is @accept@ed first has its corresponding code block run after the task unblocks.
51In this way the select statement in Ada provides rendezvous points for threads, rather than providing some resource through message passing.
52The select statement in Ada also supports an optional timeout with the same semantics as select(2), and provides an @else@.
53The @else@ changes the synchronous multiplexing to asynchronous multiplexing.
54If an @else@ clause is in a select statement and no calls to the @accept@ed methods are immediately available the code block associated with the @else@ is run and the task does not block.
55The most popular example of user-space \gls{synch_multiplex} is Go with their select statement.
56Go's select statement operates on channels and has the same exclusive-or semantics as the ALT primitive from Occam, and has associated code blocks for each clause like ALT and Ada.
57However, unlike Ada and ALT, Go does not provide any guards for their select statement cases.
58Go provides a timeout utility and also provides a @default@ clause which has the same semantics as Ada's @else@ clause.
59
60\section{Other Approaches to Synchronous Multiplexing}
61To avoid the need for \gls{synch_multiplex}, all communication between threads/processes has to come from a single source.
62One key example is Erlang, in which each process has a single heterogenous mailbox that is the sole source of concurrent communication, removing the need for \gls{synch_multiplex} as there is only one place to wait on resources.
63In a similar vein, actor systems circumvent the \gls{synch_multiplex} problem as actors are traditionally non-blocking, so they will only block when waiting for the next message.
64While these approaches solve the \gls{synch_multiplex} problem, they introduce other issues.
65Consider the case where a thread has a single source of communication (like erlang and actor systems) wants one of a set of @N@ resources.
66It requests @N@ resources and waits for responses.
67In the meantime the thread may receive other communication, and may either has to save and postpone the related work or discard it.
68After the thread receives one of the @N@ resources, it will continue to receive the other ones it requested, even if it does not need them.
69If the requests for the other resources need to be retracted, the burden falls on the programmer to determine how to synchronize appropriately to ensure that only one resource is delivered.
70
71
72\section{\CFA's Waituntil Statement}
73
74
75
Note: See TracBrowser for help on using the repository browser.