Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.