Changeset 238 in flair-src for trunk/lib/FlairCore/src/SharedMem_impl.cpp
- Timestamp:
- May 15, 2018, 4:41:02 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FlairCore/src/SharedMem_impl.cpp
r213 r238 26 26 using namespace flair::core; 27 27 28 SharedMem_impl::SharedMem_impl(const SharedMem *self, string name, size_t size, SharedMem::Type &type):self(self),type(type),size(size), 29 sem(self,1,"/" + name + "_mutex"), sem_producer(self,0,"/" + name + "_producer"),sem_consumer(self,0,"/" + name + "_consumer") { 30 28 SharedMem_impl::SharedMem_impl(const SharedMem *self, string name, size_t size, SharedMem::Type &type):self(self),type(type),size(size) { 31 29 #ifdef __XENO__ 32 30 heap_binded = false; … … 52 50 53 51 #else 52 if (type==SharedMem::Type::mutex) { 53 sem = new Semaphore(self,1,"/" + name + "_mutex",Semaphore::Type::named); 54 } else { //producerConsumer 55 sem_producer=new Semaphore(self,0,"/" + name + "_producer", Semaphore::Type::named); 56 sem_consumer=new Semaphore(self,0,"/" + name + "_consumer", Semaphore::Type::named); 57 } 54 58 shm_name = "/" + name; 55 59 fd = shm_open(shm_name.c_str(), O_RDWR | O_CREAT, 0666); … … 100 104 } 101 105 */ 106 if (type==SharedMem::Type::mutex) { 107 delete sem; 108 } else { 109 delete sem_producer; 110 delete sem_consumer; 111 } 102 112 #endif 103 113 } 104 114 105 115 void SharedMem_impl::ReaderReady() { 106 sem_consumer.ReleaseSemaphore(); 116 if (type==SharedMem::Type::producerConsumer) { 117 sem_consumer->ReleaseSemaphore(); 118 } else { 119 self->Warn("Called on a non producerConsumer type of shared memory! (this is seriously wrong)"); 120 } 107 121 } 108 122 109 123 void SharedMem_impl::Write(const char *buf, size_t size) { 110 124 if (type==SharedMem::Type::mutex) { 111 sem .GetSemaphore();125 sem->GetSemaphore(); 112 126 memcpy(mem_segment, buf, size); 113 sem .ReleaseSemaphore();127 sem->ReleaseSemaphore(); 114 128 } else if (type==SharedMem::Type::producerConsumer) { 115 //wait until consumer took the data away before writ ting a new one129 //wait until consumer took the data away before writing a new one 116 130 //but should not block if nobody's there to read 117 if (sem_consumer .TryGetSemaphore()) {131 if (sem_consumer->TryGetSemaphore()) { 118 132 memcpy(mem_segment, buf, size); 119 sem_producer .ReleaseSemaphore();133 sem_producer->ReleaseSemaphore(); 120 134 } 121 135 } … … 124 138 bool SharedMem_impl::Read(char *buf, size_t size, Time nsTimeout) { 125 139 if (type==SharedMem::Type::mutex) { 126 if (sem .GetSemaphore()) {140 if (sem->GetSemaphore()) { //may block for ever 127 141 memcpy(buf, mem_segment, size); 128 sem .ReleaseSemaphore();142 sem->ReleaseSemaphore(); 129 143 return true; 130 144 } 131 145 } else if (type==SharedMem::Type::producerConsumer) { 132 if (sem_producer .GetSemaphore(nsTimeout)) {146 if (sem_producer->GetSemaphore(nsTimeout)) { //if nobody ever writes we need to exit this after a while 133 147 memcpy(buf, mem_segment, size); 134 sem_consumer .ReleaseSemaphore();148 sem_consumer->ReleaseSemaphore(); 135 149 return true; 136 } 150 }; 137 151 } 138 152 return false;
Note:
See TracChangeset
for help on using the changeset viewer.