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