Changeset 238 in flair-src for trunk/lib/FlairCore/src/SharedMem_impl.cpp


Ignore:
Timestamp:
May 15, 2018, 4:41:02 PM (6 years ago)
Author:
Bayard Gildas
Message:

correction sémaphore. bloquant tout ça...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairCore/src/SharedMem_impl.cpp

    r213 r238  
    2626using namespace flair::core;
    2727
    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 
     28SharedMem_impl::SharedMem_impl(const SharedMem *self, string name, size_t size, SharedMem::Type &type):self(self),type(type),size(size) {
    3129#ifdef __XENO__
    3230  heap_binded = false;
     
    5250
    5351#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  }
    5458  shm_name = "/" + name;
    5559  fd = shm_open(shm_name.c_str(), O_RDWR | O_CREAT, 0666);
     
    100104   }
    101105*/
     106    if (type==SharedMem::Type::mutex) {
     107      delete sem;
     108    } else {
     109      delete sem_producer;
     110      delete sem_consumer;
     111    }
    102112#endif
    103113}
    104114
    105115void 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  }
    107121}
    108122
    109123void SharedMem_impl::Write(const char *buf, size_t size) {
    110124  if (type==SharedMem::Type::mutex) {
    111     sem.GetSemaphore();
     125    sem->GetSemaphore();
    112126    memcpy(mem_segment, buf, size);
    113     sem.ReleaseSemaphore();
     127    sem->ReleaseSemaphore();
    114128  } else if (type==SharedMem::Type::producerConsumer) {
    115     //wait until consumer took the data away before writting a new one
     129    //wait until consumer took the data away before writing a new one
    116130    //but should not block if nobody's there to read
    117     if (sem_consumer.TryGetSemaphore()) {
     131    if (sem_consumer->TryGetSemaphore()) {
    118132      memcpy(mem_segment, buf, size);
    119       sem_producer.ReleaseSemaphore();
     133      sem_producer->ReleaseSemaphore();
    120134    }
    121135  }
     
    124138bool SharedMem_impl::Read(char *buf, size_t size, Time nsTimeout) {
    125139  if (type==SharedMem::Type::mutex) {
    126     if (sem.GetSemaphore()) {
     140    if (sem->GetSemaphore()) { //may block for ever
    127141      memcpy(buf, mem_segment, size);
    128       sem.ReleaseSemaphore();
     142      sem->ReleaseSemaphore();
    129143      return true;
    130144    }
    131145  } 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
    133147      memcpy(buf, mem_segment, size);
    134       sem_consumer.ReleaseSemaphore();
     148      sem_consumer->ReleaseSemaphore();
    135149      return true;
    136     }
     150    };
    137151  }
    138152  return false;
Note: See TracChangeset for help on using the changeset viewer.