Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 1 | #ifndef AOS_LINUX_CODE_IPC_LIB_QUEUE_H_ |
| 2 | #define AOS_LINUX_CODE_IPC_LIB_QUEUE_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame^] | 4 | #include <assert.h> |
| 5 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 6 | #include "aos/linux_code/ipc_lib/shared_mem.h" |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 7 | #include "aos/common/mutex.h" |
| 8 | #include "aos/common/condition.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
| 10 | // TODO(brians) add valgrind client requests to the queue and shared_mem_malloc |
| 11 | // code to make checking for leaks work better |
| 12 | // <http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.mempools> |
| 13 | // describes how |
| 14 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | // Any pointers returned from these functions can be safely passed to other |
| 16 | // processes because they are all shared memory pointers. |
| 17 | // IMPORTANT: Any message pointer must be passed back in some way |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 18 | // (FreeMessage and WriteMessage are common ones) or the |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 19 | // application will leak shared memory. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 20 | // NOTE: Taking a message from ReadMessage and then passing it to WriteMessage |
| 21 | // might work, but it is not guaranteed to. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 22 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 23 | namespace aos { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 24 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 25 | // Queues are the primary way to use shared memory. Basic use consists of |
| 26 | // calling Queue::Fetch and then reading and/or writing messages. |
| 27 | // Queues (as the name suggests) are a FIFO stack of messages. Each combination |
| 28 | // of name and type signature will result in a different queue, which means |
| 29 | // that if you only recompile some code that uses differently sized messages, |
| 30 | // it will simply use a different queue than the old code. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 31 | class RawQueue { |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 32 | public: |
| 33 | // Retrieves (and creates if necessary) a queue. Each combination of name and |
| 34 | // signature refers to a completely independent queue. |
| 35 | // length is how large each message will be |
| 36 | // hash can differentiate multiple otherwise identical queues |
| 37 | // queue_length is how many messages the queue will be able to hold |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 38 | // Will never return NULL. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 39 | static RawQueue *Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 40 | int queue_length); |
| 41 | // Same as above, except sets up the returned queue so that it will put |
| 42 | // messages on *recycle when they are freed (after they have been released by |
| 43 | // all other readers/writers and are not in the queue). |
| 44 | // recycle_queue_length determines how many freed messages will be kept. |
| 45 | // Other code can retrieve the 2 queues separately (the recycle queue will |
| 46 | // have the same length and hash as the main one). However, any frees made |
| 47 | // using a queue with only (name,length,hash,queue_length) before the |
| 48 | // recycle queue has been associated with it will not go on to the recycle |
| 49 | // queue. |
| 50 | // NOTE: calling this function with the same (name,length,hash,queue_length) |
| 51 | // but multiple recycle_queue_lengths will result in each freed message being |
| 52 | // put onto an undefined one of the recycle queues. |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 53 | // Will never return NULL. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 54 | static RawQueue *Fetch(const char *name, size_t length, int hash, |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 55 | int queue_length, |
| 56 | int recycle_hash, int recycle_queue_length, |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 57 | RawQueue **recycle); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 59 | // Constants for passing to options arguments. |
| 60 | // The non-conflicting ones can be combined with bitwise-or. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 61 | |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 62 | // Doesn't update the currently read index (the read messages in the queue or |
| 63 | // the index). This means the returned message (and any others skipped with |
| 64 | // kFromEnd) will be left in the queue. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 65 | // For reading only. |
| 66 | static const int kPeek = 0x0001; |
| 67 | // Reads the last message in the queue instead of just the next one. |
| 68 | // NOTE: This removes all of the messages until the last one from the queue |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 69 | // (which means that nobody else will read them). |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 70 | // For reading only. |
| 71 | static const int kFromEnd = 0x0002; |
| 72 | // Causes reads to return NULL and writes to fail instead of waiting. |
| 73 | // For reading and writing. |
| 74 | static const int kNonBlock = 0x0004; |
| 75 | // Causes things to block. |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 76 | // IMPORTANT: Has a value of 0 so that it is the default. This has to stay |
| 77 | // this way. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 78 | // For reading and writing. |
| 79 | static const int kBlock = 0x0000; |
| 80 | // Causes writes to overwrite the oldest message in the queue instead of |
| 81 | // blocking. |
| 82 | // For writing only. |
| 83 | static const int kOverride = 0x0008; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 84 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 85 | // Writes a message into the queue. |
| 86 | // This function takes ownership of msg. |
| 87 | // NOTE: msg must point to a valid message from this queue |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 88 | // Returns true on success. A return value of false means msg has already been |
| 89 | // freed. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 90 | bool WriteMessage(void *msg, int options); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 91 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 92 | // Reads a message out of the queue. |
| 93 | // The return value will have at least the length of this queue's worth of |
| 94 | // valid data where it's pointing to. |
| 95 | // The return value is const because other people might be viewing the same |
| 96 | // messsage. Do not cast the const away! |
| 97 | // IMPORTANT: The return value (if not NULL) must eventually be passed to |
| 98 | // FreeMessage. |
| 99 | const void *ReadMessage(int options); |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 100 | // The same as ReadMessage, except it will never return the |
| 101 | // same message twice (when used with the same index argument). However, |
| 102 | // may not return some messages that pass through the queue. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 103 | // *index should start as 0. index does not have to be in shared memory, but |
Brian Silverman | cd2d84c | 2014-03-13 23:30:58 -0700 | [diff] [blame] | 104 | // it can be. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 105 | const void *ReadMessageIndex(int options, int *index); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 106 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 107 | // Retrieves ("allocates") a message that can then be written to the queue. |
| 108 | // NOTE: the return value will be completely uninitialized |
| 109 | // The return value will have at least the length of this queue's worth of |
| 110 | // valid memory where it's pointing to. |
| 111 | // Returns NULL for error. |
| 112 | // IMPORTANT: The return value (if not NULL) must eventually be passed to |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 113 | // FreeMessage or WriteMessage. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 114 | void *GetMessage(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 115 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 116 | // It is ok to call this method with a NULL msg. |
| 117 | void FreeMessage(const void *msg) { |
| 118 | if (msg != NULL) DecrementMessageReferenceCount(msg); |
| 119 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 120 | |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 121 | // UNSAFE! Returns the number of free messages we have. Only safe to use when |
| 122 | // only 1 task is using this object (ie in tests). |
| 123 | int FreeMessages() const; |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 124 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 125 | private: |
| 126 | struct MessageHeader; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 127 | |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame^] | 128 | // Adds 1 to the given index and handles wrapping correctly. |
| 129 | int index_add1(int index); |
| 130 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 131 | bool is_readable() { return data_end_ != data_start_; } |
Brian Silverman | 4d0789d | 2014-03-23 17:03:07 -0700 | [diff] [blame^] | 132 | bool is_writable() { return index_add1(data_end_) != data_start_; } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 133 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 134 | // These next 4 allow finding the right one. |
| 135 | const char *name_; |
| 136 | size_t length_; |
| 137 | int hash_; |
| 138 | int queue_length_; |
| 139 | // The next one in the linked list of queues. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 140 | RawQueue *next_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 141 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 142 | RawQueue *recycle_; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 143 | |
| 144 | Mutex data_lock_; // protects operations on data_ etc |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 145 | // Always gets broadcasted to because different readers might have different |
| 146 | // ideas of what "readable" means (ie ones using separated indices). |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 147 | Condition readable_; |
| 148 | Condition writable_; |
| 149 | int data_length_; // max length into data + 1 |
| 150 | int data_start_; // is an index into data |
| 151 | int data_end_; // is an index into data |
| 152 | int messages_; // that have passed through |
| 153 | void **data_; // array of messages (with headers) |
| 154 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 155 | size_t msg_length_; // sizeof(each message) including the header |
Brian Silverman | c2e0422 | 2014-03-22 12:43:44 -0700 | [diff] [blame] | 156 | // A pointer to the first in the linked list of free messages. |
| 157 | MessageHeader *free_messages_; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 158 | |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 159 | // Keeps track of if the queue was writable before a read so we can Signal() a |
| 160 | // reader if we transition it. |
| 161 | bool writable_start_; |
| 162 | |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 163 | // Actually frees the given message. |
| 164 | void DoFreeMessage(const void *msg); |
| 165 | // Calls DoFreeMessage if appropriate. |
| 166 | void DecrementMessageReferenceCount(const void *msg); |
Brian Silverman | 430e7fa | 2014-03-21 16:58:33 -0700 | [diff] [blame] | 167 | // Only does the actual incrementing of the reference count. |
| 168 | void IncrementMessageReferenceCount(const void *msg) const; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 169 | |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 170 | // Must be called with data_lock_ locked. |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 171 | // *read_data will be initialized. |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 172 | // Returns with a readable message in data_ or false. |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 173 | bool ReadCommonStart(int options, int *index); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 174 | // Deals with setting/unsetting readable_ and writable_. |
Brian Silverman | eb51cbb | 2014-03-14 22:57:08 -0700 | [diff] [blame] | 175 | // Must be called after data_lock_ has been unlocked. |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 176 | // read_data should be the same thing that was passed in to ReadCommonStart. |
Brian Silverman | 42d5237 | 2014-03-23 15:29:13 -0700 | [diff] [blame] | 177 | void ReadCommonEnd(); |
Brian Silverman | 227ad48 | 2014-03-23 11:21:32 -0700 | [diff] [blame] | 178 | // Returns the index of the last message. |
| 179 | // Useful for reading with kPeek. |
| 180 | int LastMessageIndex() const; |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 181 | |
| 182 | // Gets called by Fetch when necessary (with placement new). |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 183 | RawQueue(const char *name, size_t length, int hash, int queue_length); |
Brian Silverman | a6d1b56 | 2013-09-01 14:39:39 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | } // namespace aos |
| 187 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 188 | #endif // AOS_LINUX_CODE_IPC_LIB_QUEUE_H_ |