Mixe for Privacy and Anonymity in the Internet
CAThreadList.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) The JAP-Team, JonDos GmbH
3 
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice,
10  this list of conditions and the following disclaimer in the documentation and/or
11  other materials provided with the distribution.
12  * Neither the name of the University of Technology Dresden, Germany, nor the name of
13  the JonDos GmbH, nor the names of their contributors may be used to endorse or
14  promote products derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "StdAfx.h"
29 
30 #if ! defined (ONLY_LOCAL_PROXY)
31 
32 #include "CAThread.hpp"
33 #include "CAThreadList.hpp"
34 #include "CAMsg.hpp"
35 
41 {
42  m_pHead = NULL;
43  m_Size=0;
44  m_pListLock = new CAMutex();
45 }
46 
48 {
49  m_pListLock->lock();
50  removeAll();
52 
53  delete m_pListLock;
54  m_pListLock = NULL;
55 }
56 
57 
58 /* safe functions */
59 SINT32 CAThreadList::put(const CAThread* const thread)
60 {
61  m_pListLock->lock();
63 
64  new_entry->tle_thread = (CAThread* const)thread;
65  new_entry->tle_next = m_pHead;
66  m_pHead = new_entry;
67  m_Size++;
69 
70  return E_SUCCESS;
71 }
72 
73 SINT32 CAThreadList::remove(const CAThread* const thread)
74 {
75  SINT32 ret=E_SUCCESS;
76 
77  m_pListLock->lock();
78  thread_list_entry_t* iterator=m_pHead;
79  thread_list_entry_t* prev=NULL;
80 
81  while (iterator!=NULL)
82  {
83  if(iterator->tle_thread->getID() == thread->getID())
84  {
85  if(prev != NULL)
86  {
87  //unchain
88  prev->tle_next = iterator->tle_next;
89  }
90  else
91  {
92  //Head holds the corresponding thread;
93  m_pHead = iterator->tle_next;
94  }
95  //dispose the entry
96  delete iterator;
97  iterator = NULL;
98  m_Size--;
99  break;
100  }
101  prev = iterator;
102  iterator = iterator->tle_next;
103  };
104 
105  m_pListLock->unlock();
106  return ret;
107 }
108 
109 /*
110 CAThread *CAThreadList::get(CAThread *thread)
111 {
112  CAThread *ret=NULL;
113  m_pListLock->lock();
114  thread_list_entry_t *iterator=m_pHead;
115  while(iterator!=NULL)
116  {
117  if(thread->getID()==iterator->tle_thread->getID())
118  {
119  ret=iterator->tle_thread;
120  break;
121  }
122  iterator = iterator->tle_next;
123  }
124  m_pListLock->unlock();
125  return ret;
126 }
127 */
128 
130 {
131  m_pListLock->lock();
132 
133  if(m_pHead == NULL)
134  {
135  CAMsg::printMsg(LOG_INFO, "CAThreadList::showAll: list is empty, no threads found\n");
136  m_pListLock->unlock();
137  return;
138  }
139 
140  thread_list_entry_t *iterator = m_pHead;
141  do
142  {
143  CAMsg::printMsg(LOG_INFO, "CAThreadList::showAll: Thread %s, id %u\n",
144  iterator->tle_thread->getName(),
145  iterator->tle_thread->getID());
146  iterator = iterator->tle_next;
147  } while(iterator != NULL);
148  m_pListLock->unlock();
149 }
150 
151 // This only deletes the list entries not the corresponding threads!
153 {
154  thread_list_entry_t* iterator=NULL;
155 
156  while(m_pHead != NULL)
157  {
158  iterator=m_pHead->tle_next;
159 
160  CAMsg::printMsg(LOG_INFO, "CAThreadList::removeAll: Thread %s, id %u\n",
162  m_pHead->tle_thread->getID());
163  delete m_pHead;
164  m_pHead=iterator;
165  }
166 
167  m_pHead = NULL;
168  m_Size=0;
169  }
170 
171 // This only deletes the list entries not the corresponding threads!
173 {
174  thread_list_entry_t* iterator = NULL;
175 
176  while (m_pHead != NULL)
177  {
178  iterator = m_pHead->tle_next;
179 #ifdef _DEBUG
180  CAMsg::printMsg(LOG_INFO, "CAThreadList::waitAndRemoveAll: Thread %s, id %u\n",
182  m_pHead->tle_thread->getID());
183 #endif
184  m_pHead->tle_thread->join();
185  delete m_pHead->tle_thread;
186  delete m_pHead;
187  m_pHead = iterator;
188  }
189 
190  m_pHead = NULL;
191  m_Size = 0;
192  return E_SUCCESS;
193 }
194 
195 
196 
197 #endif
struct thread_list_entry thread_list_entry_t
signed int SINT32
Definition: basetypedefs.h:132
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
UINT8 * getName() const
Definition: CAThread.hpp:191
UINT32 getID() const
Definition: CAThread.hpp:196
SINT32 join()
Waits for the main function to finish execution.
Definition: CAThread.cpp:187
SINT32 remove(const CAThread *const thread)
thread_list_entry_t * m_pHead
void showAll() const
SINT32 waitAndRemoveAll()
Waits for finishing of all threads in list and removes them.
CAMutex * m_pListLock
SINT32 put(const CAThread *const thread)
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
CAThread * tle_thread
struct thread_list_entry * tle_next