File: thread.cpp

package info (click to toggle)
postal 0.73
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 532 kB
  • sloc: cpp: 4,197; sh: 289; ansic: 258; makefile: 134
file content (170 lines) | stat: -rw-r--r-- 3,662 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdlib.h>
#include "thread.h"
#include <stdio.h>
#include <errno.h>

#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>

Thread::Thread()
 : m_read(-1)
 , m_write(-1)
 , m_threadNum(-1)

 , m_parentRead(-1)
 , m_parentWrite(-1)
 , m_childRead(-1)
 , m_childWrite(-1)
 , m_numThreads(0)
 , m_retVal(NULL)
 , m_thread_info(NULL)
{
}

Thread::Thread(int threadNum, const Thread *parent)
 : m_read(parent->m_childRead)
 , m_write(parent->m_childWrite)
 , m_threadNum(threadNum)

 , m_parentRead(-1)
 , m_parentWrite(-1)
 , m_childRead(-1)
 , m_childWrite(-1)
 , m_numThreads(parent->m_numThreads)
 , m_retVal(&parent->m_retVal[threadNum])
 , m_thread_info(NULL)
{
}

Thread::~Thread()
{
  if(m_threadNum == -1)
  {
    for(int i = 0; i < m_numThreads; i++)
    {
      pthread_join(m_thread_info[i], NULL);
    }
    close(m_parentRead);
    close(m_parentWrite);
    close(m_childRead);
    close(m_childWrite);
    delete m_thread_info;
    delete m_retVal;
  }
}

// for the benefit of this function and the new Thread class it may create
// the Thread class must do nothing of note in it's constructor or it's
// go() member function.
PVOID thread_func(PVOID param)
{
  THREAD_DATA *td = (THREAD_DATA *)param;
  Thread *thread = td->f->newThread(td->threadNum);
  thread->setRetVal(thread->action(td->param));
  delete thread;
  delete td;
  return NULL;
}

void Thread::go(PVOID param, int num)
{
  m_numThreads += num;
  int control[2];
  int feedback[2];
  if (pipe(feedback) || pipe(control))
  {
    fprintf(stderr, "Can't open pipes.\n");
    exit(1);
  }
  m_parentRead = feedback[0];
  m_parentWrite = control[1];
  m_childRead = control[0];
  m_childWrite = feedback[1];
  m_read = m_parentRead;
  m_write = m_parentWrite;
  m_readPoll.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
  m_writePoll.events = POLLOUT | POLLERR | POLLHUP | POLLNVAL;
  m_readPoll.fd = m_parentRead;
  m_writePoll.fd = m_parentWrite;
  pthread_attr_t attr;
  if(pthread_attr_init(&attr)
   || pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)
   || pthread_attr_setstacksize(&attr, 32*1024))
    fprintf(stderr, "Can't set thread attributes.\n");

  m_retVal = new int[num + 1];
  m_thread_info = new pthread_t[num];
  for(int i = 1; i <= num; i++)
  {
    m_retVal[i] = -1;
    THREAD_DATA *td = new THREAD_DATA;
    td->f = this;
    td->param = param;
    td->threadNum = i;
    int p = pthread_create(&m_thread_info[i - 1], &attr, thread_func, PVOID(td));
    if(p)
    {
      fprintf(stderr, "Can't create a thread.\n");
      exit(1);
    }
  }
  if(pthread_attr_destroy(&attr))
    fprintf(stderr, "Can't destroy thread attributes.\n");
  m_readPoll.fd = m_read;
  m_writePoll.fd = m_write;
}

void Thread::setRetVal(int rc)
{
  *m_retVal = rc;
}

int Thread::Read(PVOID buf, int size, int timeout)
{
  if(timeout)
  {
    int rc = poll(&m_readPoll, 1, timeout * 1000);
    if(rc < 0)
    {
      fprintf(stderr, "Can't poll read ITC.\n");
      return -1;
    }
    if(!rc)
      return 0;
  }
  if(size != read(m_read, buf, size) )
  {
    fprintf(stderr, "Can't read data from ITC pipe.\n");
    return -1;
  }
  return size;
}

int Thread::Write(PVOID buf, int size, int timeout)
{
  if(timeout)
  {
    int rc;
    do
    {
      rc = poll(&m_writePoll, 1, timeout * 1000);
      if(rc < 0 && errno != EINTR)
      {
        fprintf(stderr, "Can't poll write ITC.\n");
        return -1;
      }
    } while(rc == -1);
    if(!rc)
      return 0;
  }
  if(size != write(m_write, buf, size))
  {
    fprintf(stderr, "Can't write data to ITC pipe.\n");
    return -1;
  }
  return size;
}