Example #1
0
bool server::bind(const std::string& ipcfile, size_t buffer_size) {
  logstream(LOG_INFO) << "Server attaching to " << ipcfile << " " << buffer_size << std::endl;
  size_t _run_progress = 0; 
  try {
    m_shmname = ipcfile;
    if (m_shmname.empty()) {
      std::stringstream strm;
      strm << get_my_pid() << "_" << SERVER_IPC_COUNTER.inc();
      m_shmname= strm.str();
    }
    
    // sets up the raii deleter so that we eventually
    // delete this file
    _run_progress = 1;
    m_ipcfile_deleter = register_shared_memory_name(m_shmname);

    
    // this is really modified from code in 
    // http://www.boost.org/doc/libs/1_58_0/doc/html/interprocess/synchronization_mechanisms.html
    _run_progress = 2;
    m_shared_object.reset(new shared_memory_object(create_only, 
                                                   m_shmname.c_str(), 
                                                   read_write));

    //Set size
    _run_progress = 3;
    m_shared_object->truncate(buffer_size + sizeof(shared_memory_buffer));

    //Map the whole shared memory in this process
    _run_progress = 4;
    m_mapped_region.reset(new mapped_region(*m_shared_object, 
                                            read_write));

    _run_progress = 5;
    void* buffer = m_mapped_region->get_address();
    // placement new. put the data in the buffer
    
    _run_progress = 6;
    m_buffer = new (buffer) shared_memory_buffer;
    m_buffer->m_buffer_size = buffer_size;
    m_buffer->m_server_to_client.sender_pid = get_my_pid();

  } catch (const std::string& error) {
    logstream(LOG_ERROR) << "SHMIPC initialization Error (1), stage "
                         << _run_progress << ": " << error << std::endl;
    return false;
    
  } catch (const std::exception& error) {
    logstream(LOG_ERROR) << "SHMIPC initialization Error (2), stage "
                         << _run_progress << ": " << error.what() << std::endl;
    return false;
    
  } catch (...) {
    logstream(LOG_ERROR) << "Unknown SHMIPC Initialization Error, stage "
                         << _run_progress << "." << std::endl;
    return false;
  }
  
  return true;
}
Example #2
0
 inline void readlock(request *I)  {
   I->lockclass =QUEUED_RW_LOCK_REQUEST_READ;
   I->next = NULL;
   I->s.stateu = 0;
   I->s.state.successor_class = QUEUED_RW_LOCK_REQUEST_NONE;
   I->s.state.blocked = true;
   __sync_synchronize(); 
   request* predecessor = __sync_lock_test_and_set(&tail, I);
   if (predecessor == NULL) {
     reader_count.inc();
     I->s.state.blocked = false;
   }
   else {
     
     state_union tempold, tempnew;
     tempold.state.blocked = true;
     tempold.state.successor_class = QUEUED_RW_LOCK_REQUEST_NONE;
     tempnew.state.blocked = true;
     tempnew.state.successor_class = QUEUED_RW_LOCK_REQUEST_READ;
     __sync_synchronize();
     if (predecessor->lockclass == QUEUED_RW_LOCK_REQUEST_WRITE ||
         atomic_compare_and_swap(predecessor->s.stateu,
                                 tempold.stateu,
                                 tempnew.stateu)) {
       
       predecessor->next = I;
       // wait
       __sync_synchronize(); 
       volatile state_union& is = I->s;
       while(is.state.blocked) sched_yield();
     }
     else {
       reader_count.inc();
       predecessor->next = I;
       __sync_synchronize();
       I->s.state.blocked = false;
     }
   }
   __sync_synchronize();
   if (I->s.state.successor_class == QUEUED_RW_LOCK_REQUEST_READ) {
     
     // wait
     while(I->next == NULL) sched_yield();
     reader_count.inc();
     I->next->s.state.blocked = false;
   }
 }
 bool try_terminate(size_t cpuid, std::pair<size_t, bool> &job) {
   job.second = false;
   
   numactive.dec();
   cons.begin_done_critical_section(cpuid);
   job = queue.try_dequeue();
   if (job.second == false) {
     bool ret = cons.end_done_critical_section(cpuid);
     numactive.inc();
     return ret;
   }
   else {
     cons.cancel_critical_section(cpuid);
     numactive.inc();
     return false;
   }
 }
Example #4
0
 inline void wrunlock(request *I) {
   __sync_synchronize(); 
   if (I->next != NULL || !__sync_bool_compare_and_swap(&tail, I, (request*)NULL)) {
     // wait
     while(I->next == NULL) sched_yield();
    __sync_synchronize(); 
  
     if (I->next->lockclass == QUEUED_RW_LOCK_REQUEST_READ) {
       reader_count.inc();
     }
     I->next->s.state.blocked = false;
   }
 }
Example #5
0
 void receive_string(const std::string &s) {
   ctr.inc();
 }
Example #6
0
 void receive_vector(const std::vector<size_t> &s) {
   ctr.inc();
 }
Example #7
0
 void receive_ints(size_t i0, size_t i1, size_t i2, size_t i3) {
   ctr.inc();
 }
Example #8
0
 inline void increment_running_counter() {
   threads_running.inc();
 }