Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 174


Ignore:
Timestamp:
Nov 6, 2007, 11:32:34 PM (17 years ago)
Author:
scheusso
Message:

added boost:mutex to PacketBuffer in order to make it thread safe; adjuster PacketBufferTest

Location:
code/branches/network/src/network
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/network/PacketBuffer.cc

    r173 r174  
    88
    99PacketBuffer::PacketBuffer(){
    10   locked=false;
     10  closed=false;
    1111  first=NULL;
    1212  last=NULL;
     
    1414
    1515bool PacketBuffer::push(PacketEnvelope pck){
    16   if(!locked){
    17     locked=true;
    18     // also works if fifo is null (queue empty)
    19     // just to be sure last is really the last element
    20     while(last!=NULL && last->next!=NULL)
    21       last=last->next;
    22     // first element?
    23     if(first==NULL){
    24       first=new QueueItem;
    25       last=first;
    26       last->next=NULL;
    27       // change this!!!!!!!
    28       last->packet = new PacketEnvelope;
    29       last->packet->data = pck.data;
    30       last->packet->length = pck.length;
    31     } else {
    32       //insert a new element at the bottom
    33       last->next = new QueueItem;
    34       last=last->next;
    35       // initialize last->next
    36       last->next=NULL;
    37       // save the packet to the new element
    38       // change this!!!!!!!!
    39       last->packet = new PacketEnvelope;
    40       last->packet->data = pck.data;
    41       last->packet->length = pck.length;
    42     }
    43     locked=false;
    44     return true;
     16  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
     17  // also works if fifo is null (queue empty)
     18  // just to be sure last is really the last element
     19  while(last!=NULL && last->next!=NULL)
     20    last=last->next;
     21  // first element?
     22  if(first==NULL){
     23    first=new QueueItem;
     24    last=first;
     25    last->next=NULL;
     26    // change this!!!!!!!
     27    last->packet = new PacketEnvelope;
     28    last->packet->data = pck.data;
     29    last->packet->length = pck.length;
     30  } else {
     31    //insert a new element at the bottom
     32    last->next = new QueueItem;
     33    last=last->next;
     34    // initialize last->next
     35    last->next=NULL;
     36    // save the packet to the new element
     37    // change this!!!!!!!!
     38    last->packet = new PacketEnvelope;
     39    last->packet->data = pck.data;
     40    last->packet->length = pck.length;
    4541  }
    46   else
    47     return false;
     42  return true;
    4843}
    4944
    50 PacketEnvelope *PacketBuffer::pop(){
    51   if(!locked && first!=NULL){
    52     locked = true;
     45PacketEnvelope PacketBuffer::pop(){
     46  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
     47  if(first!=NULL){
    5348    QueueItem *temp = first;
    5449    // get packet
     
    5752    first = first->next;
    5853    delete temp;
    59     locked=false;
     54    return *p;
     55  } else{
     56    PacketEnvelope p = {0,0};
    6057    return p;
    61   } else
    62     return NULL;
     58  }
    6359}
    6460
    6561bool PacketBuffer::isEmpty(){
    6662  return (first==NULL);
    67 }
    68 
    69 bool PacketBuffer::isLocked(){
    70   return locked;
    7163}
    7264
     
    8072}
    8173
     74bool PacketBuffer::isClosed(){
     75  return closed;
     76}
     77
     78void PacketBuffer::setClosed(bool value){
     79  closed=value;
     80  return;
     81}
     82
    8283}// namespace network
  • code/branches/network/src/network/PacketBuffer.h

    r173 r174  
    1616#include <queue>
    1717#include <string>
     18#include <boost/bind.hpp>
     19#include <boost/thread/mutex.hpp>
     20#include <boost/thread/mutex.hpp>
     21
     22//this is needed in order to make the packetbuffer threadsafe
     23boost::mutex networkPacketBufferMutex;
    1824
    1925namespace network{
     
    3238public:
    3339  PacketBuffer();
    34   bool isLocked();
    3540  bool isEmpty();
     41  bool isClosed();
     42  void setClosed(bool value);
     43  void print();
    3644  // pops a packet from the queue
    37   PacketEnvelope *pop();
     45  PacketEnvelope pop();
    3846  // pushs a packet to the queue
    3947  bool push(PacketEnvelope pck);
    40   void print();
    4148private:
    42   bool locked;
    4349  QueueItem *first;
    4450  QueueItem *last;
     51  bool closed;
     52 
     53  //make it threadsafe
     54//   boost::mutex mutex;
    4555};
    4656
  • code/branches/network/src/network/PacketBufferTest.cc

    r173 r174  
    1919  test.print();
    2020  while(!test.isEmpty()){
    21     int i=test.pop()->data;
     21    int i=test.pop().data;
    2222    std::cout << "We popped the value " << i << std::endl;
    2323  }
Note: See TracChangeset for help on using the changeset viewer.