1
1
Author: Rickard Green <rickard(at)erlang(dot)org>
2
- Status: Draft
2
+ Status: Final
3
3
Type: Standards Track
4
4
Created: 07-Jan-2025
5
5
Post-History: https://erlangforums.com/t/eep-76-priority-messages
@@ -219,6 +219,12 @@ be handled as today with the exception that the process that called the
219
219
` link/2 ` BIF with the ` priority ` option will receive a priority exit signal
220
220
if the link is broken.
221
221
222
+ If a link already exists when the ` link/2 ` BIF with the ` priority ` option is
223
+ called, the link remains but will be marked for priority handling. If a link
224
+ which has been marked for priority handling already exists when the ` link/2 `
225
+ BIF without the ` priority ` option or the ` link/1 ` BIF is called, the link will
226
+ remain, but the priority handling will be disabled.
227
+
222
228
Note that priority exit signals will not have any other behavior than today
223
229
unless the receiver is trapping exits. This since the priority messages only
224
230
affects how messages are moved into the message queue after a signal has been
@@ -366,15 +372,8 @@ Reference Implementation
366
372
The reference implementation can be found in [ pull request 9269] [ ] of the
367
373
[ Erlang/OTP repository] [ ] .
368
374
369
- Note that the reference implementation has not yet been updated to reflect the
370
- change in the proposal to use * priority aliases* , but instead implement the
371
- old proposal where process flags are used in order to opt-in for priority
372
- messages.
373
-
374
- Care has been taken to have as small impact as possible on processes not
375
- using priority messages. Processes not enabling reception of priority
376
- messages will not use any more memory at all due to the priority messages
377
- implementation.
375
+ Care has been taken to have as small impact on performance and memory as
376
+ possible especially for processes not using priority messages.
378
377
379
378
A Few Notes on the Implementation
380
379
---------------------------------
@@ -387,20 +386,32 @@ adjustments that needs to be done to the message queue during certain
387
386
operations. That is, the current code traversing the message queue needs to be
388
387
prepared to encounter receive markers of different types.
389
388
390
- When the user enables reception of priority messages, a block containing three
389
+ When the user enables reception of priority messages, a block containing two
391
390
receive markers and an area for auxiliary data is allocated. The receive
392
- markers are of new types distinguishable from the already existing receive
393
- markers. The auxiliary data, among other things, contains a red/black search
394
- tree containing information about what type of messages the process accepts as
395
- priority messages. All memory allocated for handling of priority messages is
396
- referred to from this memory block.
397
-
398
- The first and the second receive markers are inserted at the start of the
399
- message queue. The first marker marks the start of priority messages and the
400
- second marks the end of priority messages. The first marker also serves as an
401
- entrance for finding all information about the priority message handling. When
402
- a priority message is accepted it will be inserted just before the end marker.
403
- The third marker is inserted in the message queue when we need to remember a
391
+ markers in this memory block are of new types distinguishable from the already
392
+ existing receive markers. All memory allocated for handling of priority
393
+ messages is referred to from this memory block.
394
+
395
+ This memory block for priority message reception is referred to from * process
396
+ specific data* . Process specific data is only used by processes that enable
397
+ functionality that seldom is used. Since the memory used for priority message
398
+ reception is referred to from process specific data, memory usage will only
399
+ increase for processes using process specific data. If such processes have not
400
+ enabled priority message reception, only one machine word more of data will be
401
+ used.
402
+
403
+ While no priority messages exist in the message queue, handling of messages is
404
+ done exactly the same way for a process that has enabled priority message
405
+ reception as for a process that has not. When a priority message is accepted
406
+ into the message queue, a * priority message end marker* is inserted at the
407
+ start of the message queue and then the priority message is inserted just
408
+ before the marker. If yet another priority message is accepted while the first
409
+ priority message still is in the queue, it will be inserted just before the
410
+ priority message end marker. The priority message end marker will remain in the
411
+ message queue until no priority messages exist in the queue. At that point, the
412
+ priority message end marker will be removed form the queue.
413
+
414
+ The second marker is inserted in the message queue when we need to remember a
404
415
place in the message queue. This is needed when a priority message is accepted
405
416
while we currently are traversing the message queue.
406
417
@@ -422,6 +433,42 @@ the reference, but I see that as a premature optimization. A process is not
422
433
expected to accumulate a large amount of priority messages. If so, the process
423
434
has used priority messages in a way not intended.
424
435
436
+ ### Determining if a Message Should be Accepted as a Priority Message
437
+
438
+ We more or less have two scenarios. In the first scenario, the signal was sent
439
+ by calling the ` erlang:send/3 ` BIF or the ` erlang:exit/3 ` BIF using a priority
440
+ alias and the ` priority ` option. In the second scenario, the signal was sent
441
+ due to a link being broken or a monitor being triggered where the link or
442
+ monitor was set up by calling the ` link/2 ` BIF or the ` monitor/3 ` BIF with the
443
+ ` priority ` option.
444
+
445
+ We first look at the first scenario. The receiver already has local information
446
+ about all active aliases for itself. When an alias message is received, it
447
+ looks up the local information about the alias. If the alias has been created
448
+ using the ` priority ` option, a priority bit is set in the local information
449
+ about the alias. A signal which has been sent using the ` priority ` option also
450
+ contains a bit set indicating that the priority option was used. In order to
451
+ determine whether or not to accept a signal as a priority message, the receiver
452
+ only needs to check that both of these priority bits are set.
453
+
454
+ When it comes to the second scenario. If an exit signal due to a broken link
455
+ or a monitor signal due to a triggered monitor should be handled as a priority
456
+ message, the link should have been set up by the ` link/2 ` BIF using the
457
+ ` priority ` option or the monitor should have been set up by the ` monitor/3 `
458
+ BIF using the ` priority ` option. In both cases, a priority bit will be set in
459
+ the local information about the link or monitor. When such a signal is
460
+ received, the receiver only needs to check if the priority bit is set in the
461
+ local data in order to determine if it should be accepted as a priority
462
+ message or not.
463
+
464
+ In both scenarios, the receiving process has local data associated with the
465
+ signal that it already needs to look up unrelated to priority messages. The
466
+ only extra work due to priority messages that it needs to do is to check if
467
+ the priority bit is set in this local data, and for signals sent using the
468
+ ` erlang:send/3 ` or ` erlang:exit/3 ` BIFs also check if the priority bit is set
469
+ in the signal. In other words, the operation of determining whether or not a
470
+ message should be accepted as a priority message is extremely cheap.
471
+
425
472
### Priority Messages in Transit
426
473
427
474
There exists a number of different types of signals. For each type of signal an
@@ -452,12 +499,13 @@ Change Log
452
499
453
500
* 2025-01-22: The ` process_flag/2 ` flags for identifying messages to handle as
454
501
priority messages were replaced by * priority aliases* and priority options.
502
+ * 2025-02-22: Updated information on the reference implementation.
503
+ * 2025-02-22: Clarified how the ` link() ` BIFs affects already existing links.
504
+ * 2025-02-22: Changed the state of the EEP to * final* .
455
505
456
506
[ Message Reception ] : eep-0076-1.png
457
- "Message Reception"
458
507
459
508
[ Priority Message Reception ] : eep-0076-2.png
460
- "Priority Message Reception"
461
509
462
510
[ the signal ordering guarantee ] : https://www.erlang.org/doc/system/ref_man_processes.html#delivery-of-signals
463
511
0 commit comments