Subversion Repositories HelenOS-doc

Rev

Rev 112 | Rev 117 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 112 Rev 114
1
<?xml version="1.0" encoding="UTF-8"?>
1
<?xml version="1.0" encoding="UTF-8"?>
2
<chapter id="ipc">
2
<chapter id="ipc">
3
  <?dbhtml filename="ipc.html"?>
3
  <?dbhtml filename="ipc.html"?>
4
 
4
 
5
  <title>IPC</title>
5
  <title>IPC</title>
6
 
6
 
7
  <para>Due to the high intertask communication traffic, IPC becomes critical
7
  <para>Due to the high intertask communication traffic, IPC becomes critical
8
  subsystem for microkernels, putting high demands on the speed, latency and
8
  subsystem for microkernels, putting high demands on the speed, latency and
9
  reliability of IPC model and implementation. Although theoretically the use
9
  reliability of IPC model and implementation. Although theoretically the use
10
  of asynchronous messaging system looks promising, it is not often
10
  of asynchronous messaging system looks promising, it is not often
11
  implemented because of a problematic implementation of end user
11
  implemented because of a problematic implementation of end user
12
  applications. HelenOS implements a fully asynchronous messaging system with
12
  applications. HelenOS implements a fully asynchronous messaging system with
13
  a special layer providing a user application developer a reasonably
13
  a special layer providing a user application developer a reasonably
14
  synchronous multithreaded environment sufficient to develop complex
14
  synchronous multithreaded environment sufficient to develop complex
15
  protocols.</para>
15
  protocols.</para>
16
 
16
 
17
  <section>
17
  <section>
18
    <title>Services provided by kernel</title>
18
    <title>Services provided by kernel</title>
19
 
19
 
20
    <para>Every message consists of 4 numeric arguments (32-bit and 64-bit on
20
    <para>Every message consists of 4 numeric arguments (32-bit and 64-bit on
21
    the corresponding platforms), from which the first one is considered a
21
    the corresponding platforms), from which the first one is considered a
22
    method number on message receipt and a return value on answer receipt. The
22
    method number on message receipt and a return value on answer receipt. The
23
    received message contains identification of the incoming connection, so
23
    received message contains identification of the incoming connection, so
24
    that the receiving application can distinguish the messages between
24
    that the receiving application can distinguish the messages between
25
    different senders. Internally the message contains pointer to the
25
    different senders. Internally the message contains pointer to the
26
    originating task and to the source of the communication channel. If the
26
    originating task and to the source of the communication channel. If the
27
    message is forwarded, the originating task identifies the recipient of the
27
    message is forwarded, the originating task identifies the recipient of the
28
    answer, the source channel identifies the connection in case of a hangup
28
    answer, the source channel identifies the connection in case of a hangup
29
    response.</para>
29
    response.</para>
30
 
30
 
31
    <para>Every message must be eventually answered. The system keeps track of
31
    <para>Every message must be eventually answered. The system keeps track of
32
    all messages, so that it can answer them with appropriate error code
32
    all messages, so that it can answer them with appropriate error code
33
    should one of the connection parties fail unexpectedly. To limit buffering
33
    should one of the connection parties fail unexpectedly. To limit buffering
34
    of the messages in the kernel, every process is has a limited account of
34
    of the messages in the kernel, every process is has a limited account of
35
    asynchronous messages it can send simultanously. If the limit is reached,
35
    asynchronous messages it can send simultanously. If the limit is reached,
36
    the kernel refuses to send any other message, until some active message is
36
    the kernel refuses to send any other message, until some active message is
37
    answered.</para>
37
    answered.</para>
38
 
38
 
39
    <para>To facilitate kernel-to-user communication, the IPC subsystem
39
    <para>To facilitate kernel-to-user communication, the IPC subsystem
40
    provides notification messages. The applications can subscribe to a
40
    provides notification messages. The applications can subscribe to a
41
    notification channel and receive messages directed to this channel. Such
41
    notification channel and receive messages directed to this channel. Such
42
    messages can be freely sent even from interrupt context as they are
42
    messages can be freely sent even from interrupt context as they are
43
    primarily destined to deliver IRQ events to userspace device drivers.
43
    primarily destined to deliver IRQ events to userspace device drivers.
44
    These messages need not be answered, there is no party that could receive
44
    These messages need not be answered, there is no party that could receive
45
    such response.</para>
45
    such response.</para>
46
 
46
 
47
    <section>
47
    <section>
48
      <title>Low level IPC</title>
48
      <title>Low level IPC</title>
49
 
49
 
50
      <para>The whole IPC subsystem consists of one-way communication
50
      <para>The whole IPC subsystem consists of one-way communication
51
      channels. Each task has one associated message queue (answerbox). The
51
      channels. Each task has one associated message queue (answerbox). The
52
      task can call other tasks and connect it's phones to their answerboxes.,
52
      task can call other tasks and connect it's phones to their answerboxes.,
53
      send and forward messages through these connections and answer received
53
      send and forward messages through these connections and answer received
54
      messages. Every sent message is identified by a unique number, so that
54
      messages. Every sent message is identified by a unique number, so that
55
      the response can be later matched against it. The message is sent over
55
      the response can be later matched against it. The message is sent over
56
      the phone to the target answerbox. Server application periodically
56
      the phone to the target answerbox. Server application periodically
57
      checks the answerbox and pulls messages from several queues associated
57
      checks the answerbox and pulls messages from several queues associated
58
      with it. After completing the requested action, server sends a reply
58
      with it. After completing the requested action, server sends a reply
59
      back to the answerbox of the originating task. If a need arises, it is
59
      back to the answerbox of the originating task. If a need arises, it is
60
      possible to <emphasis>forward</emphasis> a recevied message throught any
60
      possible to <emphasis>forward</emphasis> a recevied message throught any
61
      of the open phones to another task. This mechanism is used e.g. for
61
      of the open phones to another task. This mechanism is used e.g. for
62
      opening new connections.</para>
62
      opening new connections.</para>
63
 
63
 
64
      <para>The answerbox contains four different message queues:</para>
64
      <para>The answerbox contains four different message queues:</para>
65
 
65
 
66
      <itemizedlist>
66
      <itemizedlist>
67
        <listitem>
67
        <listitem>
68
          <para>Incoming call queue</para>
68
          <para>Incoming call queue</para>
69
        </listitem>
69
        </listitem>
70
 
70
 
71
        <listitem>
71
        <listitem>
72
          <para>Dispatched call queue</para>
72
          <para>Dispatched call queue</para>
73
        </listitem>
73
        </listitem>
74
 
74
 
75
        <listitem>
75
        <listitem>
76
          <para>Answer queue</para>
76
          <para>Answer queue</para>
77
        </listitem>
77
        </listitem>
78
 
78
 
79
        <listitem>
79
        <listitem>
80
          <para>Notification queue</para>
80
          <para>Notification queue</para>
81
        </listitem>
81
        </listitem>
82
      </itemizedlist>
82
      </itemizedlist>
83
 
83
 
-
 
84
      <figure float="1">
-
 
85
        <mediaobject id="ipc1">
-
 
86
          <imageobject role="pdf">
-
 
87
            <imagedata fileref="images/ipc1.pdf" format="PDF" />
-
 
88
          </imageobject>
-
 
89
 
-
 
90
          <imageobject role="html">
-
 
91
            <imagedata fileref="images/ipc1.png" format="PNG" />
-
 
92
          </imageobject>
-
 
93
 
-
 
94
          <imageobject role="fop">
-
 
95
            <imagedata fileref="images/ipc1.svg" format="SVG" />
-
 
96
          </imageobject>
-
 
97
        </mediaobject>
-
 
98
 
-
 
99
        <title>Low level IPC</title>
-
 
100
      </figure>
-
 
101
 
84
      <para>The communication between task A, that is connected to task B
102
      <para>The communication between task A, that is connected to task B
85
      looks as follows: Task A sends a message over it's phone to the target
103
      looks as follows: Task A sends a message over it's phone to the target
86
      asnwerbox. The message is saved in task B incoming call queue. When task
104
      asnwerbox. The message is saved in task B incoming call queue. When task
87
      B fetches the message for processing, it is automatically moved into the
105
      B fetches the message for processing, it is automatically moved into the
88
      dispatched call queue. After the server decides to answer the message,
106
      dispatched call queue. After the server decides to answer the message,
89
      it is removed from dispatched queue and the result is moved into the
107
      it is removed from dispatched queue and the result is moved into the
90
      answer queue of task A.</para>
108
      answer queue of task A.</para>
91
 
109
 
92
      <para>The arguments contained in the message are completely arbitrary
110
      <para>The arguments contained in the message are completely arbitrary
93
      and decided by the user. The low level part of kernel IPC fills in
111
      and decided by the user. The low level part of kernel IPC fills in
94
      appropriate error codes if there is an error during communication. It is
112
      appropriate error codes if there is an error during communication. It is
95
      assured that the applications are correctly notified about communication
113
      assured that the applications are correctly notified about communication
96
      state. If a program closes the outgoing connection, the target answerbox
114
      state. If a program closes the outgoing connection, the target answerbox
97
      receives a hangup message. The connection identification is not reused,
115
      receives a hangup message. The connection identification is not reused,
98
      until the hangup message is acknowledged and all other pending messages
116
      until the hangup message is acknowledged and all other pending messages
99
      are answered.</para>
117
      are answered.</para>
100
 
118
 
101
      <para>Closing an incoming connection is done by responding to any
119
      <para>Closing an incoming connection is done by responding to any
102
      incoming message with an EHANGUP error code. The connection is then
120
      incoming message with an EHANGUP error code. The connection is then
103
      immediately closed. The client connection identification (phone id) is
121
      immediately closed. The client connection identification (phone id) is
104
      not reused, until the client issues closes it's own side of the
122
      not reused, until the client issues closes it's own side of the
105
      connection ("hangs his phone up").</para>
123
      connection ("hangs his phone up").</para>
106
 
124
 
107
      <para>When a task dies (whether voluntarily or by being killed), cleanup
125
      <para>When a task dies (whether voluntarily or by being killed), cleanup
108
      process is started. </para>
126
      process is started.</para>
109
 
127
 
110
      <orderedlist>
128
      <orderedlist>
111
        <listitem>
129
        <listitem>
112
          <para>Hangs up all outgoing connections and sends hangup messages to
130
          <para>Hangs up all outgoing connections and sends hangup messages to
113
          all target answerboxes.</para>
131
          all target answerboxes.</para>
114
        </listitem>
132
        </listitem>
115
 
133
 
116
        <listitem>
134
        <listitem>
117
          <para>Disconnects all incoming connections.</para>
135
          <para>Disconnects all incoming connections.</para>
118
        </listitem>
136
        </listitem>
119
 
137
 
120
        <listitem>
138
        <listitem>
121
          <para>Disconnects from all notification channels.</para>
139
          <para>Disconnects from all notification channels.</para>
122
        </listitem>
140
        </listitem>
123
 
141
 
124
        <listitem>
142
        <listitem>
125
          <para>Answers all unanswered messages from answerbox queues with
143
          <para>Answers all unanswered messages from answerbox queues with
126
          appropriate error code.</para>
144
          appropriate error code.</para>
127
        </listitem>
145
        </listitem>
128
 
146
 
129
        <listitem>
147
        <listitem>
130
          <para>Waits until all outgoing messages are answered and all
148
          <para>Waits until all outgoing messages are answered and all
131
          remaining answerbox queues are empty.</para>
149
          remaining answerbox queues are empty.</para>
132
        </listitem>
150
        </listitem>
133
      </orderedlist>
151
      </orderedlist>
134
    </section>
152
    </section>
135
 
153
 
136
    <section>
154
    <section>
137
      <title>System call IPC layer</title>
155
      <title>System call IPC layer</title>
138
 
156
 
139
      <para>On top of this simple protocol the kernel provides special
157
      <para>On top of this simple protocol the kernel provides special
140
      services closely related to the inter-process communication. A range of
158
      services closely related to the inter-process communication. A range of
141
      method numbers is allocated and protocol is defined for these functions.
159
      method numbers is allocated and protocol is defined for these functions.
142
      The messages are interpreted by the kernel layer and appropriate actions
160
      The messages are interpreted by the kernel layer and appropriate actions
143
      are taken depending on the parameters of message and answer. </para>
161
      are taken depending on the parameters of message and answer.</para>
144
 
162
 
145
      <para>The kernel provides the following services:</para>
163
      <para>The kernel provides the following services:</para>
146
 
164
 
147
      <itemizedlist>
165
      <itemizedlist>
148
        <listitem>
166
        <listitem>
149
          <para>Creating new outgoing connection</para>
167
          <para>Creating new outgoing connection</para>
150
        </listitem>
168
        </listitem>
151
 
169
 
152
        <listitem>
170
        <listitem>
153
          <para>Creating a callback connection</para>
171
          <para>Creating a callback connection</para>
154
        </listitem>
172
        </listitem>
155
 
173
 
156
        <listitem>
174
        <listitem>
157
          <para>Sending an address space area</para>
175
          <para>Sending an address space area</para>
158
        </listitem>
176
        </listitem>
159
 
177
 
160
        <listitem>
178
        <listitem>
161
          <para>Asking for an address space area</para>
179
          <para>Asking for an address space area</para>
162
        </listitem>
180
        </listitem>
163
      </itemizedlist>
181
      </itemizedlist>
164
 
182
 
165
      <para>On startup every task is automatically connected to a
183
      <para>On startup every task is automatically connected to a
166
      <emphasis>name service task</emphasis>, which provides a switchboard
184
      <emphasis>name service task</emphasis>, which provides a switchboard
167
      functionality. To open a new outgoing connection, the client sends a
185
      functionality. To open a new outgoing connection, the client sends a
168
      <constant>CONNECT_ME_TO</constant> message using any of his phones. If
186
      <constant>CONNECT_ME_TO</constant> message using any of his phones. If
169
      the recepient of this message answers with an accepting answer, a new
187
      the recepient of this message answers with an accepting answer, a new
170
      connection is created. In itself, this mechanism would allow only
188
      connection is created. In itself, this mechanism would allow only
171
      duplicating existing connection. However, if the message is forwarded,
189
      duplicating existing connection. However, if the message is forwarded,
172
      the new connection is made to the final recipient. </para>
190
      the new connection is made to the final recipient.</para>
173
 
191
 
174
      <para>On startup every task is automatically connect to the name service
192
      <para>On startup every task is automatically connect to the name service
175
      task, which acts as a switchboard and forwards requests for connection
193
      task, which acts as a switchboard and forwards requests for connection
176
      to specific services. To be able to forward a message it must have a
194
      to specific services. To be able to forward a message it must have a
177
      phone connected to the service tasks. The task creates this connection
195
      phone connected to the service tasks. The task creates this connection
178
      using a <constant>CONNECT_TO_ME</constant> message which creates a
196
      using a <constant>CONNECT_TO_ME</constant> message which creates a
179
      callback connection. Every service that wants to receive connections
197
      callback connection. Every service that wants to receive connections
180
      asks name service task to create a callback connection.</para>
198
      asks name service task to create a callback connection.</para>
181
 
199
 
182
      <para>Tasks can share their address space areas using IPC messages. The
200
      <para>Tasks can share their address space areas using IPC messages. The
183
      2 message types - AS_AREA_SEND and AS_AREA_RECV are used for sending and
201
      2 message types - AS_AREA_SEND and AS_AREA_RECV are used for sending and
184
      receiving an address area respectively. The shared area can be accessed
202
      receiving an address area respectively. The shared area can be accessed
185
      as soon as the message is acknowledged. </para>
203
      as soon as the message is acknowledged.</para>
186
    </section>
204
    </section>
187
  </section>
205
  </section>
188
 
206
 
189
  <section>
207
  <section>
190
    <title>Userspace view</title>
208
    <title>Userspace view</title>
191
 
209
 
192
    <para>The conventional design of the asynchronous api seems to produce
210
    <para>The conventional design of the asynchronous api seems to produce
193
    applications with one event loop and several big switch statements.
211
    applications with one event loop and several big switch statements.
194
    However, by intensive utilization of user-space threads, it was possible
212
    However, by intensive utilization of user-space threads, it was possible
195
    to create an environment that is not necesarilly restricted to this type
213
    to create an environment that is not necesarilly restricted to this type
196
    of event-driven programming and allows for more fluent expression of
214
    of event-driven programming and allows for more fluent expression of
197
    application programs.</para>
215
    application programs.</para>
198
 
216
 
199
    <section>
217
    <section>
200
      <title>Single point of entry</title>
218
      <title>Single point of entry</title>
201
 
219
 
202
      <para>Each tasks is associated with only one answerbox. If a
220
      <para>Each tasks is associated with only one answerbox. If a
203
      multi-threaded application needs to communicate, it must be not only
221
      multi-threaded application needs to communicate, it must be not only
204
      able to send a message, but it should be able to retrieve the answer as
222
      able to send a message, but it should be able to retrieve the answer as
205
      well. If several threads pull messages from task answerbox, it is a
223
      well. If several threads pull messages from task answerbox, it is a
206
      matter of fortune, which thread receives which message. If a particular
224
      matter of fortune, which thread receives which message. If a particular
207
      thread needs to wait for a message answer, an idle
225
      thread needs to wait for a message answer, an idle
208
      <emphasis>manager</emphasis> task is found or a new one is created and
226
      <emphasis>manager</emphasis> task is found or a new one is created and
209
      control is transfered to this manager task. The manager tasks pops
227
      control is transfered to this manager task. The manager tasks pops
210
      messages from the answerbox and puts them into appropriate queues of
228
      messages from the answerbox and puts them into appropriate queues of
211
      running tasks. If a task waiting for a message is not running, the
229
      running tasks. If a task waiting for a message is not running, the
212
      control is transferred to it.</para>
230
      control is transferred to it.</para>
-
 
231
     
-
 
232
      <figure float="1">
-
 
233
        <mediaobject id="ipc2">
-
 
234
          <imageobject role="pdf">
-
 
235
            <imagedata fileref="images/ipc2.pdf" format="PDF" />
-
 
236
          </imageobject>
-
 
237
 
-
 
238
          <imageobject role="html">
-
 
239
            <imagedata fileref="images/ipc2.png" format="PNG" />
-
 
240
          </imageobject>
-
 
241
 
-
 
242
          <imageobject role="fop">
-
 
243
            <imagedata fileref="images/ipc2.svg" format="SVG" />
-
 
244
          </imageobject>
-
 
245
        </mediaobject>
-
 
246
 
-
 
247
        <title>Single point of entry</title>
-
 
248
      </figure>
-
 
249
 
213
 
250
 
214
      <para>Very similar situation arises when a task decides to send a lot of
251
      <para>Very similar situation arises when a task decides to send a lot of
215
      messages and reaches kernel limit of asynchronous messages. In such
252
      messages and reaches kernel limit of asynchronous messages. In such
216
      situation 2 remedies are available - the userspace liberary can either
253
      situation 2 remedies are available - the userspace liberary can either
217
      cache the message locally and resend the message when some answers
254
      cache the message locally and resend the message when some answers
218
      arrive, or it can block the thread and let it go on only after the
255
      arrive, or it can block the thread and let it go on only after the
219
      message is finally sent to the kernel layer. With one exception HelenOS
256
      message is finally sent to the kernel layer. With one exception HelenOS
220
      uses the second approach - when the kernel responds that maximum limit
257
      uses the second approach - when the kernel responds that maximum limit
221
      of asynchronous messages was reached, control is transferred to manager
258
      of asynchronous messages was reached, control is transferred to manager
222
      thread. The manager thread then handles incoming replies and when space
259
      thread. The manager thread then handles incoming replies and when space
223
      is available, sends the message to kernel and resumes application thread
260
      is available, sends the message to kernel and resumes application thread
224
      execution.</para>
261
      execution.</para>
225
 
262
 
226
      <para>If a kernel notification is received, the servicing procedure is
263
      <para>If a kernel notification is received, the servicing procedure is
227
      run in the context of the manager thread. Although it wouldn't be
264
      run in the context of the manager thread. Although it wouldn't be
228
      impossible to allow recursive calling, it could potentially lead to an
265
      impossible to allow recursive calling, it could potentially lead to an
229
      explosion of manager threads. Thus, the kernel notification procedures
266
      explosion of manager threads. Thus, the kernel notification procedures
230
      are not allowed to wait for a message result, they can only answer
267
      are not allowed to wait for a message result, they can only answer
231
      messages and send new ones without waiting for their results. If the
268
      messages and send new ones without waiting for their results. If the
232
      kernel limit for outgoing messages is reached, the data is automatically
269
      kernel limit for outgoing messages is reached, the data is automatically
233
      cached within the application. This behaviour is enforced automatically
270
      cached within the application. This behaviour is enforced automatically
234
      and the decision making is hidden from developers view.</para>
271
      and the decision making is hidden from developers view.</para>
235
    </section>
272
    </section>
236
 
273
 
237
    <section>
274
    <section>
238
      <title>Ordering problem</title>
275
      <title>Ordering problem</title>
239
 
276
 
240
      <para>Unfortunately, in the real world is is never so easy. E.g. if a
277
      <para>Unfortunately, in the real world is is never so easy. E.g. if a
241
      server handles incoming requests and as a part of it's response sends
278
      server handles incoming requests and as a part of it's response sends
242
      asynchronous messages, it can be easily prempted and other thread may
279
      asynchronous messages, it can be easily prempted and other thread may
243
      start intervening. This can happen even if the application utilizes only
280
      start intervening. This can happen even if the application utilizes only
244
      1 kernel thread. Classical synchronization using semaphores is not
281
      1 kernel thread. Classical synchronization using semaphores is not
245
      possible, as locking on them would block the thread completely and the
282
      possible, as locking on them would block the thread completely and the
246
      answer couldn't be ever processed. The IPC framework allows a developer
283
      answer couldn't be ever processed. The IPC framework allows a developer
247
      to specify, that the thread should not be preempted to any other thread
284
      to specify, that the thread should not be preempted to any other thread
248
      (except notification handlers) while still being able to queue messages
285
      (except notification handlers) while still being able to queue messages
249
      belonging to other threads and regain control when the answer
286
      belonging to other threads and regain control when the answer
250
      arrives.</para>
287
      arrives.</para>
251
 
288
 
252
      <para>This mechanism works transparently in multithreaded environment,
289
      <para>This mechanism works transparently in multithreaded environment,
253
      where classical locking mechanism (futexes) should be used. The IPC
290
      where classical locking mechanism (futexes) should be used. The IPC
254
      framework ensures that there will always be enough free threads to
291
      framework ensures that there will always be enough free threads to
255
      handle the threads requiring correct synchronization and allow the
292
      handle the threads requiring correct synchronization and allow the
256
      application to run more user-space threads inside the kernel threads
293
      application to run more user-space threads inside the kernel threads
257
      without the danger of locking all kernel threads in futexes.</para>
294
      without the danger of locking all kernel threads in futexes.</para>
258
    </section>
295
    </section>
259
 
296
 
260
    <section>
297
    <section>
261
      <title>The interface</title>
298
      <title>The interface</title>
262
 
299
 
263
      <para></para>
300
      <para></para>
264
    </section>
301
    </section>
265
  </section>
302
  </section>
266
</chapter>
303
</chapter>