"Fossies" - the Fresh Open Source Software Archive

Member "bonnie++-2.00a/thread.cpp" (3 Jul 2009, 3564 Bytes) of package /linux/privat/bonnie++-2.00a.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "thread.cpp" see the Fossies "Dox" file reference documentation.

    1 #include <stdlib.h>
    2 #include "thread.h"
    3 #include <stdio.h>
    4 
    5 #include <unistd.h>
    6 #include <time.h>
    7 #include <sys/wait.h>
    8 #include <pthread.h>
    9 
   10 Thread::Thread()
   11  : m_read(-1)
   12  , m_write(-1)
   13  , m_threadNum(-1)
   14  , m_thread_info(NULL)
   15  , m_parentRead(-1)
   16  , m_parentWrite(-1)
   17  , m_childRead(-1)
   18  , m_childWrite(-1)
   19  , m_numThreads(0)
   20  , m_retVal(NULL)
   21 {
   22 }
   23 
   24 Thread::Thread(int threadNum, const Thread *parent)
   25  : m_read(parent->m_childRead)
   26  , m_write(parent->m_childWrite)
   27  , m_threadNum(threadNum)
   28  , m_thread_info(NULL)
   29  , m_parentRead(-1)
   30  , m_parentWrite(-1)
   31  , m_childRead(-1)
   32  , m_childWrite(-1)
   33  , m_numThreads(parent->m_numThreads)
   34  , m_retVal(&parent->m_retVal[threadNum])
   35 {
   36 }
   37 
   38 Thread::~Thread()
   39 {
   40   if(m_threadNum == -1)
   41   {
   42     for(int i = 0; i < m_numThreads; i++)
   43     {
   44       pthread_join(m_thread_info[i], NULL);
   45     }
   46     delete m_thread_info;
   47     close(m_parentRead);
   48     close(m_parentWrite);
   49     close(m_childRead);
   50     close(m_childWrite);
   51     delete m_retVal;
   52   }
   53 }
   54 
   55 // for the benefit of this function and the new Thread class it may create
   56 // the Thread class must do nothing of note in it's constructor or it's
   57 // go() member function.
   58 PVOID thread_func(PVOID param)
   59 {
   60   THREAD_DATA *td = (THREAD_DATA *)param;
   61   Thread *thread = td->f->newThread(td->threadNum);
   62   thread->setRetVal(thread->action(td->param));
   63   delete thread;
   64   delete td;
   65   return NULL;
   66 }
   67 
   68 void Thread::go(PVOID param, int num)
   69 {
   70   m_numThreads += num;
   71   FILE_TYPE control[2];
   72   FILE_TYPE feedback[2];
   73   if (pipe(feedback) || pipe(control))
   74   {
   75     fprintf(stderr, "Can't open pipes.\n");
   76     exit(1);
   77   }
   78   m_parentRead = feedback[0];
   79   m_parentWrite = control[1];
   80   m_childRead = control[0];
   81   m_childWrite = feedback[1];
   82   m_read = m_parentRead;
   83   m_write = m_parentWrite;
   84   m_readPoll.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
   85   m_writePoll.events = POLLOUT | POLLERR | POLLHUP | POLLNVAL;
   86   m_readPoll.fd = m_parentRead;
   87   m_writePoll.fd = m_parentWrite;
   88   pthread_attr_t attr;
   89   if(pthread_attr_init(&attr))
   90     fprintf(stderr, "Can't init thread attributes.\n");
   91   if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE))
   92     fprintf(stderr, "Can't set thread attributes.\n");
   93   m_thread_info = new pthread_t[num];
   94 
   95   m_retVal = new int[num + 1];
   96   for(int i = 1; i <= num; i++)
   97   {
   98     m_retVal[i] = -1;
   99     THREAD_DATA *td = new THREAD_DATA;
  100     td->f = this;
  101     td->param = param;
  102     td->threadNum = i;
  103     int p = pthread_create(&m_thread_info[i - 1], &attr, thread_func, PVOID(td));
  104     if(p)
  105     {
  106       fprintf(stderr, "Can't create a thread.\n");
  107       exit(1);
  108     }
  109   }
  110   if(pthread_attr_destroy(&attr))
  111     fprintf(stderr, "Can't destroy thread attributes.\n");
  112   m_readPoll.fd = m_read;
  113   m_writePoll.fd = m_write;
  114 }
  115 
  116 void Thread::setRetVal(int rc)
  117 {
  118   *m_retVal = rc;
  119 }
  120 
  121 int Thread::Read(PVOID buf, int size, int timeout)
  122 {
  123   if(timeout)
  124   {
  125     int rc = poll(&m_readPoll, 1, timeout * 1000);
  126     if(rc < 0)
  127     {
  128       fprintf(stderr, "Can't poll read ITC.\n");
  129       return -1;
  130     }
  131     if(!rc)
  132       return 0;
  133   }
  134   if(size != read(m_read, buf, size) )
  135   {
  136     fprintf(stderr, "Can't read data from ITC pipe.\n");
  137     return -1;
  138   }
  139   return size;
  140 }
  141 
  142 int Thread::Write(PVOID buf, int size, int timeout)
  143 {
  144   if(timeout)
  145   {
  146     int rc = poll(&m_writePoll, 1, timeout * 1000);
  147     if(rc < 0)
  148     {
  149       fprintf(stderr, "Can't poll write ITC.\n");
  150       return -1;
  151     }
  152     if(!rc)
  153       return 0;
  154   }
  155   if(size != write(m_write, buf, size))
  156   {
  157     fprintf(stderr, "Can't write data to ITC pipe.\n");
  158     return -1;
  159   }
  160   return size;
  161 }
  162