Mixe for Privacy and Anonymity in the Internet
CAThreadList.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) The JAP-Team, JonDos GmbH
00003 
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice,
00010        this list of conditions and the following disclaimer in the documentation and/or
00011        other materials provided with the distribution.
00012     * Neither the name of the University of Technology Dresden, Germany, nor the name of
00013        the JonDos GmbH, nor the names of their contributors may be used to endorse or
00014        promote products derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00017 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00018 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00019 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00020 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00021 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00022 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00023 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 #include "StdAfx.h"
00029 
00030 #if defined (_DEBUG) && ! defined (ONLY_LOCAL_PROXY)
00031 
00032 #include "CAThread.hpp"
00033 #include "CAThreadList.hpp"
00034 #include "CAMsg.hpp"
00035 
00040 CAThreadList::CAThreadList()
00041 {
00042   m_pHead = NULL;
00043   m_Size=0;
00044   m_pListLock = new CAMutex();
00045 }
00046 
00047 CAThreadList::~CAThreadList()
00048 {
00049   m_pListLock->lock();
00050   removeAll();
00051   m_pListLock->unlock();
00052   
00053   delete m_pListLock;
00054   m_pListLock = NULL;
00055 }
00056 
00057 
00058 /* safe functions */
00059 SINT32 CAThreadList::put(const CAThread* const thread)
00060 {
00061   m_pListLock->lock();
00062   thread_list_entry_t *new_entry = new thread_list_entry_t;
00063   
00064   new_entry->tle_thread = (CAThread* const)thread;
00065   new_entry->tle_next = m_pHead;
00066   m_pHead = new_entry;  
00067   m_Size++;   
00068   m_pListLock->unlock();
00069   
00070   return E_SUCCESS;
00071 }
00072 
00073 SINT32 CAThreadList::remove(const CAThread* const thread)
00074 {
00075   SINT32 ret=E_SUCCESS;
00076   
00077   m_pListLock->lock();
00078   thread_list_entry_t* iterator=m_pHead;
00079   thread_list_entry_t* prev=NULL;
00080     
00081   while (iterator!=NULL) 
00082   {
00083     if(iterator->tle_thread->getID() == thread->getID())
00084       {
00085         if(prev != NULL)
00086           {
00087             //unchain
00088             prev->tle_next = iterator->tle_next;
00089           }
00090         else
00091           {
00092             //Head holds the corresponding thread;
00093             m_pHead = iterator->tle_next;
00094           }
00095         //dispose the entry     
00096         delete iterator;
00097         iterator = NULL;
00098         m_Size--;
00099         break;
00100       }
00101     prev = iterator;
00102     iterator = iterator->tle_next;
00103   };
00104 
00105   m_pListLock->unlock();
00106   return ret;
00107 }
00108 
00109 /*
00110 CAThread *CAThreadList::get(CAThread *thread)
00111 {
00112   CAThread *ret=NULL;
00113   m_pListLock->lock();
00114   thread_list_entry_t *iterator=m_pHead;
00115   while(iterator!=NULL) 
00116     {
00117       if(thread->getID()==iterator->tle_thread->getID())
00118         {
00119           ret=iterator->tle_thread;
00120           break;
00121         }
00122       iterator = iterator->tle_next;
00123     } 
00124   m_pListLock->unlock();
00125   return ret;
00126 }
00127 */
00128 
00129 void CAThreadList::showAll() const
00130 {
00131   m_pListLock->lock();
00132   
00133   if(m_pHead == NULL)
00134   {
00135     CAMsg::printMsg(LOG_INFO, "CAThreadList::showAll: list is empty, no threads found\n");
00136     m_pListLock->unlock();
00137     return;
00138   }
00139   
00140   thread_list_entry_t *iterator = m_pHead;
00141   do 
00142   {
00143     CAMsg::printMsg(LOG_INFO, "CAThreadList::showAll: Thread %s, id %u\n", 
00144                       iterator->tle_thread->getName(),
00145                       iterator->tle_thread->getID());
00146     iterator = iterator->tle_next;
00147   } while(iterator != NULL);
00148   m_pListLock->unlock();
00149 }
00150 
00151 // This only deletes the list entries not the corresponding threads!
00152 void CAThreadList::removeAll()
00153 {
00154   thread_list_entry_t* iterator=NULL;
00155   
00156   while(m_pHead != NULL)
00157     {
00158       iterator=m_pHead->tle_next;
00159 
00160       CAMsg::printMsg(LOG_INFO, "CAThreadList::removeAll: Thread %s, id %u\n", 
00161                       m_pHead->tle_thread->getName(),
00162                       m_pHead->tle_thread->getID());
00163       delete m_pHead;
00164       m_pHead=iterator;
00165     }
00166     
00167     m_pHead = NULL;
00168     m_Size=0;
00169   }
00170 
00171 #endif