|
| 1 | +Notes: |
| 2 | +--- |
| 3 | + |
| 4 | +- Inbox N is always associated with a message of type N, hence to inbox 1 |
| 5 | +producers will write messages of type 1 and consumers will read messages of |
| 6 | +type 1, and so on. |
| 7 | + |
| 8 | +- Each producer/consumer runs in a unique thread. |
| 9 | + |
| 10 | +- All x9_inbox sizes equal 4. This is not ideal for performance and should not |
| 11 | +be used as a guideline. The reason why I use such a small buffer is because |
| 12 | +I wanted to saturate the inbox and make it much more likely to trigger a data |
| 13 | +race (which there is none). |
| 14 | + |
| 15 | +- All examples/tests can be run by executing ./run_examples.sh |
| 16 | + |
| 17 | +``` |
| 18 | +──────▷ write to |
| 19 | +─ ─ ─ ▷ read from |
| 20 | +``` |
| 21 | + |
| 22 | +Examples |
| 23 | +--- |
| 24 | +``` |
| 25 | +x9_example_1.c: |
| 26 | +
|
| 27 | + One producer |
| 28 | + One consumer |
| 29 | + One message type |
| 30 | + |
| 31 | + ┌────────┐ ┏━━━━━━━━┓ ┌────────┐ |
| 32 | + │Producer│──────▷┃ inbox ┃◁ ─ ─ ─│Consumer│ |
| 33 | + └────────┘ ┗━━━━━━━━┛ └────────┘ |
| 34 | + |
| 35 | + This example showcases the simplest (multi-threading) pattern. |
| 36 | + |
| 37 | + Data structures used: |
| 38 | + - x9_inbox |
| 39 | + |
| 40 | + Functions used: |
| 41 | + - x9_create_inbox |
| 42 | + - x9_inbox_is_valid |
| 43 | + - x9_write_to_inbox_spin |
| 44 | + - x9_read_from_inbox_spin |
| 45 | + - x9_free_inbox |
| 46 | + |
| 47 | + Test is considered passed iff: |
| 48 | + - None of the threads stall and exit cleanly after doing the work. |
| 49 | + - All messages sent by the producer(s) are received and asserted to be |
| 50 | + valid by the consumer(s). |
| 51 | + ``` |
| 52 | +------------------------------------------------------------------------------- |
| 53 | +``` |
| 54 | +x9_example_2.c |
| 55 | +
|
| 56 | + Two producers. |
| 57 | + One consumer and producer simultaneously. |
| 58 | + One consumer. |
| 59 | +
|
| 60 | + ┌────────┐ ┏━━━━━━━━┓ ┏━━━━━━━━┓ |
| 61 | + │Producer│─────▷┃ ┃ ┌────────┐ ┃ ┃ |
| 62 | + └────────┘ ┃ ┃ │Consumer│ ┃ ┃ ┌────────┐ |
| 63 | + ┃inbox 1 ┃◁ ─ ─ │ and │─────▷┃inbox 2 ┃◁ ─ ─ │Consumer│ |
| 64 | + ┌────────┐ ┃ ┃ │Producer│ ┃ ┃ └────────┘ |
| 65 | + │Producer│─────▷┃ ┃ └────────┘ ┃ ┃ |
| 66 | + └────────┘ ┗━━━━━━━━┛ ┗━━━━━━━━┛ |
| 67 | +
|
| 68 | + This example showcase using multiple threads to write to the same inbox, |
| 69 | + using multiple message types, the 'x9_node' abstraction, and |
| 70 | + respective create/free and select functions. |
| 71 | +
|
| 72 | + Data structures used: |
| 73 | + - x9_inbox |
| 74 | + - x9_node |
| 75 | +
|
| 76 | + Functions used: |
| 77 | + - x9_create_inbox |
| 78 | + - x9_inbox_is_valid |
| 79 | + - x9_create_node |
| 80 | + - x9_node_is_valid |
| 81 | + - x9_select_inbox_from_node |
| 82 | + - x9_write_to_inbox_spin |
| 83 | + - x9_read_from_inbox_spin |
| 84 | + - x9_free_node_and_attached_inboxes |
| 85 | +
|
| 86 | + Test is considered passed iff: |
| 87 | + - None of the threads stall and exit cleanly after doing the work. |
| 88 | + - All messages sent by the producer(s) are received and asserted to be |
| 89 | + valid by the consumer(s). |
| 90 | +``` |
| 91 | +------------------------------------------------------------------------------- |
| 92 | +``` |
| 93 | +x9_example_3.c |
| 94 | +
|
| 95 | + Two producers and simultaneously consumers. |
| 96 | + |
| 97 | + ┌────────┐ ┏━━━━━━━━┓ ┌────────┐ |
| 98 | + │Producer│──────▷┃inbox 1 ┃◁ ─ ─ ─│Producer│ |
| 99 | + │ │ ┗━━━━━━━━┛ │ │ |
| 100 | + │ and │ │ and │ |
| 101 | + │ │ ┏━━━━━━━━┓ │ │ |
| 102 | + │Consumer│─ ─ ─ ▷┃inbox 2 ┃◁──────│Consumer│ |
| 103 | + └────────┘ ┗━━━━━━━━┛ └────────┘ |
| 104 | +
|
| 105 | + This example showcases the use of: 'x9_write_to_inbox' |
| 106 | + and 'x9_read_from_inbox', which, unlike 'x9_write_to_inbox_spin' and |
| 107 | + 'x9_read_from_inbox_spin' do not block until are able to write/read a msg. |
| 108 | +
|
| 109 | + Data structures used: |
| 110 | + - x9_inbox |
| 111 | + - x9_node |
| 112 | +
|
| 113 | + Functions used: |
| 114 | + - x9_create_inbox |
| 115 | + - x9_inbox_is_valid |
| 116 | + - x9_create_node |
| 117 | + - x9_node_is_valid |
| 118 | + - x9_select_inbox_from_node |
| 119 | + - x9_write_to_inbox |
| 120 | + - x9_read_from_inbox |
| 121 | + - x9_free_node_and_attached_inboxes |
| 122 | +
|
| 123 | + Test is considered passed iff: |
| 124 | + - None of the threads stall and exit cleanly after doing the work. |
| 125 | + - All messages sent by the producer(s) are received and asserted to be |
| 126 | + valid by the consumer(s). |
| 127 | +``` |
| 128 | +------------------------------------------------------------------------------- |
| 129 | +``` |
| 130 | +x9_example_4.c |
| 131 | +
|
| 132 | + One producer broadcasts the same message to three inboxes. |
| 133 | + Three consumers read from each inbox. |
| 134 | + One message type. |
| 135 | + |
| 136 | + ┏━━━━━━━━┓ ┌────────┐ |
| 137 | + ┌──▷┃ inbox ┃◁─ ─ ─│Consumer│ |
| 138 | + │ ┗━━━━━━━━┛ └────────┘ |
| 139 | + ┌────────┐ │ ┏━━━━━━━━┓ ┌────────┐ |
| 140 | + │Producer│───┼──▷┃ inbox ┃◁─ ─ ─│Consumer│ |
| 141 | + └────────┘ │ ┗━━━━━━━━┛ └────────┘ |
| 142 | + │ ┏━━━━━━━━┓ ┌────────┐ |
| 143 | + └──▷┃ inbox ┃◁─ ─ ─│Consumer│ |
| 144 | + ┗━━━━━━━━┛ └────────┘ |
| 145 | +
|
| 146 | +
|
| 147 | + This example showcases the use of 'x9_broadcast_msg_to_all_node_inboxes'. |
| 148 | +
|
| 149 | + Data structures used: |
| 150 | + - x9_inbox |
| 151 | + - x9_node |
| 152 | +
|
| 153 | + Functions used: |
| 154 | + - x9_create_inbox |
| 155 | + - x9_inbox_is_valid |
| 156 | + - x9_create_node |
| 157 | + - x9_node_is_valid |
| 158 | + - x9_broadcast_msg_to_all_node_inboxes |
| 159 | + - x9_read_from_inbox_spin |
| 160 | + - x9_free_node_and_attached_inboxes |
| 161 | +
|
| 162 | + IMPORTANT: |
| 163 | + - All inboxes must receive messages of the same type (or at least of the |
| 164 | + same size) that its being broadcasted. |
| 165 | +
|
| 166 | + Test is considered passed iff: |
| 167 | + - None of the threads stall and exit cleanly after doing the work. |
| 168 | + - All messages sent by the producer(s) are received and asserted to be |
| 169 | + valid by the consumer(s). |
| 170 | +``` |
| 171 | +------------------------------------------------------------------------------- |
| 172 | +``` |
| 173 | +x9_example_5.c |
| 174 | +
|
| 175 | + Three producers. |
| 176 | + Three consumers reading concurrently from the same inbox. |
| 177 | + One message type. |
| 178 | +
|
| 179 | + ┌────────┐ |
| 180 | + ┌────────┐ ┏━━━━━━━━┓ ─ ─│Consumer│ |
| 181 | + │Producer│──────▷┃ ┃ │ └────────┘ |
| 182 | + ├────────┤ ┃ ┃ ┌────────┐ |
| 183 | + │Producer│──────▷┃ inbox ┃◁──┤─ ─│Consumer│ |
| 184 | + ├────────┤ ┃ ┃ └────────┘ |
| 185 | + │Producer│──────▷┃ ┃ │ ┌────────┐ |
| 186 | + └────────┘ ┗━━━━━━━━┛ ─ ─│Consumer│ |
| 187 | + └────────┘ |
| 188 | +
|
| 189 | + This example showcases the use of 'x9_read_from_shared_inbox'. |
| 190 | +
|
| 191 | + Data structures used: |
| 192 | + - x9_inbox |
| 193 | + |
| 194 | + Functions used: |
| 195 | + - x9_create_inbox |
| 196 | + - x9_inbox_is_valid |
| 197 | + - x9_write_to_inbox_spin |
| 198 | + - x9_read_from_shared_inbox |
| 199 | + - x9_free_inbox |
| 200 | +
|
| 201 | + Test is considered passed iff: |
| 202 | + - None of the threads stall and exit cleanly after doing the work. |
| 203 | + - All messages sent by the producer(s) are received and asserted to be |
| 204 | + valid by the consumer(s). |
| 205 | + - Each consumer processes at least one message. |
| 206 | +``` |
| 207 | +------------------------------------------------------------------------------- |
| 208 | +``` |
| 209 | +x9_example_6.c |
| 210 | +
|
| 211 | + One producer |
| 212 | + Two consumers reading from the same inbox concurrently (busy loop). |
| 213 | + One message type. |
| 214 | +
|
| 215 | + ┏━━━━━━━━┓ ┌────────┐ |
| 216 | + ┃ ┃ ─ ─│Consumer│ |
| 217 | + ┌────────┐ ┃ ┃ │ └────────┘ |
| 218 | + │Producer│──────▷┃ inbox ┃◁ ─ |
| 219 | + └────────┘ ┃ ┃ │ ┌────────┐ |
| 220 | + ┃ ┃ ─ ─│Consumer│ |
| 221 | + ┗━━━━━━━━┛ └────────┘ |
| 222 | +
|
| 223 | + This example showcases the use of 'x9_read_from_shared_inbox_spin'. |
| 224 | +
|
| 225 | + Data structures used: |
| 226 | + - x9_inbox |
| 227 | +
|
| 228 | + Functions used: |
| 229 | + - x9_create_inbox |
| 230 | + - x9_inbox_is_valid |
| 231 | + - x9_write_to_inbox_spin |
| 232 | + - x9_read_from_shared_inbox_spin |
| 233 | + - x9_free_inbox |
| 234 | +
|
| 235 | + Test is considered passed iff: |
| 236 | + - None of the threads stall and exit cleanly after doing the work. |
| 237 | + - All messages sent by the producer(s) are received and asserted to be |
| 238 | + valid by the consumer(s). |
| 239 | + - Each consumer processes at least one message. |
| 240 | +``` |
| 241 | +------------------------------------------------------------------------------- |
0 commit comments