iCub-main
ThreadPool2.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2008 The RobotCub Consortium
5  * Author: Lorenzo Natale
6  * website: www.robotcub.org
7  * Permission is granted to copy, distribute, and/or modify this program
8  * under the terms of the GNU General Public License, version 2 or any
9  * later version published by the Free Software Foundation.
10  *
11  * A copy of the license can be found at
12  * http://www.robotcub.org/icub/license/gpl.txt
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details
18 */
19 
20 #include <ace/config.h>
21 #include <ace/Thread.h>
22 #include "canControlConstants.h"
23 #include "ThreadTable2.h"
24 
25 /*
26  * This list contains the association between each thread and a consecutive id. */
28 {
29 private:
30  inline bool getNew(const ACE_thread_t &s, int &i)
31  {
32  fprintf(stderr, "Registering new thread %d out of %d\n", index, CANCONTROL_MAX_THREADS);
33  if (index>=CANCONTROL_MAX_THREADS)
34  {
35  fprintf(stderr, "ThreadPool: ERROR reached max number of threads\n");
36  i=-1;
37  return false;
38  }
39 
40  i=index;
41  index++;
42  pool[i].handle()=s;
43  return true;
44  }
45 
46  inline int checkExists(const ACE_thread_t &s)
47  {
48  for(int i=0; i<index; i++)
49  {
50  if (pool[i].handle()==s)
51  return i;
52  }
53  return -1;
54 
55  }
56 
57  ThreadTable2 *pool;
58  int index;
59 
60 public:
61  ThreadPool2(yarp::dev::ICanBufferFactory *ic);
62 
63  ~ThreadPool2();
64 
65  inline void reset()
66  {
67  index=0;
68  }
69 
70  inline bool getId(int &i)
71  {
72  ACE_thread_t self=ACE_Thread::self();
73  int id;
74  bool ret=true;
75  id=checkExists(self);
76 
77  if (id==-1)
78  {
79  ret=getNew(self,id);
80  }
81  i=id;
82  return ret;
83  }
84 
85  inline ThreadTable2 *getThreadTable(int id)
86  {
87  if ((id<0) || (id>CANCONTROL_MAX_THREADS))
88  return 0;
89 
90  return pool+id;
91  }
92 };
93 
94 
const int CANCONTROL_MAX_THREADS
ThreadPool2(yarp::dev::ICanBufferFactory *ic)
Definition: ThreadPool2.cpp:22
void reset()
Definition: ThreadPool2.h:65
ThreadTable2 * getThreadTable(int id)
Definition: ThreadPool2.h:85
bool getId(int &i)
Definition: ThreadPool2.h:70
ACE_thread_t & handle()
Definition: ThreadTable2.h:99
FirmwareUpdaterCore * self
static pcap_t * handle
fprintf(fid,'\n')