Commit | Line | Data |
---|---|---|
4762e5ac | 1 | #pragma once |
cc1e352c | 2 | |
4762e5ac | 3 | class Block; |
cc1e352c | 4 | |
4762e5ac JM |
5 | class BlockQueue { |
6 | ||
7 | // friend classes | |
cc1e352c MM |
8 | friend class Planner; |
9 | friend class Conveyor; | |
cc1e352c MM |
10 | |
11 | public: | |
4762e5ac JM |
12 | BlockQueue(); |
13 | BlockQueue(unsigned int length); | |
cc1e352c | 14 | |
4762e5ac | 15 | ~BlockQueue(); |
cc1e352c MM |
16 | |
17 | /* | |
18 | * direct accessors | |
19 | */ | |
4762e5ac JM |
20 | Block& head(); |
21 | Block& tail(); | |
cc1e352c | 22 | |
4762e5ac JM |
23 | void push_front(Block&) __attribute__ ((warning("Not thread-safe if pop_back() is used in ISR context!"))); // instead, prepare(head_ref()); produce_head(); |
24 | Block& pop_back(void) __attribute__ ((warning("Not thread-safe if head_ref() is used to prepare new items, or push_front() is used in ISR context!"))); // instead, consume(tail_ref()); consume_tail(); | |
cc1e352c MM |
25 | |
26 | /* | |
27 | * pointer accessors | |
28 | */ | |
4762e5ac JM |
29 | Block* head_ref(); |
30 | Block* tail_ref(); | |
cc1e352c MM |
31 | |
32 | void produce_head(void); | |
33 | void consume_tail(void); | |
34 | ||
35 | /* | |
36 | * queue status | |
37 | */ | |
b5708347 JM |
38 | bool is_empty(void) const; |
39 | bool is_full(void) const; | |
cc1e352c MM |
40 | |
41 | /* | |
42 | * resize | |
43 | * | |
44 | * returns true on success, or false if queue is not empty or not enough memory available | |
45 | */ | |
46 | bool resize(unsigned int); | |
47 | ||
48 | /* | |
49 | * provide | |
4762e5ac | 50 | * Block* - new buffer pointer |
cc1e352c MM |
51 | * int length - number of items in buffer (NOT size in bytes!) |
52 | * | |
4762e5ac | 53 | * cause BlockQueue to use a specific memory location instead of allocating its own |
cc1e352c MM |
54 | * |
55 | * returns true on success, or false if queue is not empty | |
56 | */ | |
4762e5ac | 57 | //bool provide(Block*, unsigned int length); |
cc1e352c MM |
58 | |
59 | protected: | |
60 | /* | |
61 | * these functions are protected as they should only be used internally | |
62 | * or in extremely specific circumstances | |
63 | */ | |
4762e5ac JM |
64 | Block& item(unsigned int); |
65 | Block* item_ref(unsigned int); | |
cc1e352c | 66 | |
b5708347 JM |
67 | unsigned int next(unsigned int) const; |
68 | unsigned int prev(unsigned int) const; | |
cc1e352c MM |
69 | |
70 | /* | |
71 | * buffer variables | |
72 | */ | |
73 | unsigned int length; | |
74 | ||
75 | volatile unsigned int head_i; | |
76 | volatile unsigned int tail_i; | |
f6542ad9 | 77 | volatile unsigned int isr_tail_i; |
cc1e352c | 78 | |
1b918dbb | 79 | private: |
4762e5ac | 80 | Block* ring; |
cc1e352c | 81 | }; |