File: WorkerThread.cpp

package info (click to toggle)
gem 1:0.93.3-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,548 kB
  • ctags: 29,332
  • sloc: cpp: 134,188; sh: 11,215; makefile: 2,853; ansic: 84
file content (282 lines) | stat: -rw-r--r-- 7,327 bytes parent folder | download | duplicates (3)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
//    Copyright (c) 2011-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#define WORKERTHREAD_DEQUEUE


#include "WorkerThread.h"
#ifdef WORKERTHREAD_DEQUEUE
# include <deque>
# define QUEUE std::deque
# define POP pop_front
# define PUSH push_back
#else
# include <queue>
# define QUEUE std::queue
# define POP pop
# define PUSH push
#endif

#include "ThreadMutex.h"
#include "ThreadSemaphore.h"

#include <pthread.h>
#if defined __linux__ || defined __APPLE__ || defined __FreeBSD_kernel__
# include <sys/types.h>
# include <unistd.h>
# include <sys/time.h>
#endif
#ifdef _WIN32
# include <winsock2.h>
#endif

#include <iostream>

namespace gem { namespace thread {

  const WorkerThread::id_t WorkerThread::IMMEDIATE =  0;
  const WorkerThread::id_t WorkerThread::INVALID   = ~0;

  class WorkerThread::PIMPL {
  public:
    WorkerThread*owner;
    WorkerThread::id_t ID; /* for generating the next ID */

    bool keeprunning;
    bool isrunning;

    QUEUE< std::pair<WorkerThread::id_t, void*> > q_todo;
    QUEUE< std::pair<WorkerThread::id_t, void*> > q_done;
    Mutex m_todo;
    Mutex m_done;
    Semaphore s_newdata;

    WorkerThread::id_t processingID; /* the ID currently processed or INVALID: must only be written in the thread! */

    pthread_t p_thread;

    PIMPL(WorkerThread*x) : owner(x), ID(0),
                            keeprunning(true), isrunning(false),
                            m_todo(Mutex()), m_done(Mutex()),
                            s_newdata(Semaphore()),
                            processingID(WorkerThread::INVALID)
    {

    }
    ~PIMPL(void) {
      stop(true);
    }

    inline WorkerThread::id_t nextID(void) {
      ID++;
      while(ID == WorkerThread::IMMEDIATE || ID == WorkerThread::INVALID)
        ID++;
      return ID;
    }

    static inline void*process(void*you) {
      PIMPL*me=reinterpret_cast<PIMPL*>(you);
      WorkerThread*wt=me->owner;
      me->isrunning=true;
      std::pair <id_t, void*> in, out;

      while(me->keeprunning) {
        // wait till we are signalled new data

        me->m_todo.lock();
        if(me->q_todo.empty()) {
        empty:
          me->m_todo.unlock();
          //std::cerr << "THREAD: waiting for new data...freeze"<<std::endl;
          me->s_newdata.freeze();
          //std::cerr << "THREAD: waiting for new data...thawed "<<me->keeprunning<<std::endl;

          // either new data has arrived or we are told to stop
          if(!me->keeprunning)
            break;

          me->m_todo.lock();
        }
        if(me->q_todo.empty())
          goto empty;
        in=me->q_todo.front();
         me->processingID=in.first;
         me->q_todo.POP();
        me->m_todo.unlock();

        //std::cerr << "THREAD: processing data " << in.second  << " as "<<in.first<<std::endl;

        out.first = in.first;
        out.second=wt->process(in.first, in.second);

        //std::cerr << "THREAD: done data " << out.second  << " as "<<out.first<<std::endl;

        me->m_done.lock();
        bool newdata=true;//me->q_done.empty();
        //std::cerr<<"THREAD: processed "<< out.first <<" -> "<< newdata<<std::endl;
        me->q_done.PUSH(out);
        me->processingID=WorkerThread::INVALID;
        me->m_done.unlock();
        //std::cerr << "THREAD: signaling newdata "<<newdata<<" for "<< out.first << std::endl;
        if(newdata)wt->signal();
        //std::cerr << "THREAD: signalled" << std::endl;
      }
      //std::cerr << "THREAD: FINISHED" << std::endl;
      me->isrunning=false;
      return 0;
    }

    bool start(void) {
      if(isrunning)return true;
      
      keeprunning=true;
      pthread_create(&p_thread, 0, process, this);

      struct timeval sleep;
      while(!isrunning) {
        sleep.tv_sec=0;
        sleep.tv_usec=10;
        select(0,0,0,0,&sleep);
      }

      return true;

    }
    bool stop(bool wait=true) {
      if(!isrunning)return true;

      keeprunning=false;
      s_newdata.thaw();

      if(!wait)
        return (!isrunning);

      struct timeval sleep;
      while(isrunning) {
        sleep.tv_sec=0;
        sleep.tv_usec=10;
        select(0,0,0,0,&sleep);
        s_newdata.thaw();
      }
      return true;
    }

  };


  WorkerThread::WorkerThread(void) :
    m_pimpl(new PIMPL(this)) {
  }
  WorkerThread::~WorkerThread(void) {
    stop(true);

    delete m_pimpl;
    m_pimpl=0;
  }

  /* _private_ dummy implementations */
  WorkerThread&WorkerThread::operator=(const WorkerThread&org) {
    return (*this);
  }
  WorkerThread::WorkerThread(const WorkerThread&org) : m_pimpl(new PIMPL(this)) {
  }


  bool WorkerThread::start(void) {
    return m_pimpl->start();
  }
  bool WorkerThread::stop(bool wait) {
    return m_pimpl->stop(wait);
  }



  bool WorkerThread::queue(WorkerThread::id_t&ID, void*data) {
    std::pair <id_t, void*> DATA;
    DATA.second = data;

    m_pimpl->m_todo.lock();
    ID=m_pimpl->nextID();

    //std::cerr << "queuing data " << data  << " as "<<ID<<std::endl;
    if(ID==INVALID) {
      m_pimpl->m_todo.unlock();
      return false;
    }

    DATA.first = ID;
    m_pimpl->q_todo.PUSH(DATA);
    m_pimpl->m_todo.unlock();

    m_pimpl->s_newdata.thaw();
    //std::cerr << "new data thawed" << std::endl;
    return true;
  }
  bool WorkerThread::cancel(WorkerThread::id_t ID) {
    bool success=false;
#ifdef WORKERTHREAD_DEQUEUE
    if(!success) {
      /* cancel from TODO list */
      QUEUE< std::pair<WorkerThread::id_t, void*> > :: iterator it;
      //std::cerr << "cancelling "<< (int)ID <<" from TODO" << std::endl;
      m_pimpl->m_todo.lock();

      for(it=m_pimpl->q_todo.begin(); it!=m_pimpl->q_todo.end(); it++) {
        if(it->first == ID) {
          m_pimpl->q_todo.erase(it);
          success=true;
          break;
        }
      }
      m_pimpl->m_todo.unlock();

      /* TODO: if ID is currently in the process, cancel that as well ... */
      if(WorkerThread::INVALID != ID) {
        /* ... or at least block until it is done... */
        struct timeval sleep;
        while(ID==m_pimpl->processingID) {
          sleep.tv_sec=0;
          sleep.tv_usec=10;
          select(0,0,0,0,&sleep);
        }
      }
    }
    m_pimpl->m_todo.unlock();
#endif
    //    std::cerr << "cancelling "<< (int)ID <<" success " << success << std::endl;
    return success;
  }
  bool WorkerThread::dequeue(WorkerThread::id_t&ID, void*&data) {
    std::pair <id_t, void*> DATA;
    DATA.first=WorkerThread::INVALID;
    DATA.second=0;
    //std::cerr << "dequeuing "<< (int)ID << std::endl;
    m_pimpl->m_done.lock();
    if(!m_pimpl->q_done.empty()) {
      DATA=m_pimpl->q_done.front();
      m_pimpl->q_done.POP();
    }
    m_pimpl->m_done.unlock();

    ID=DATA.first;
    data=DATA.second;
    //std::cerr<<"dequeuing "<<data<<" as "<< ID<<std::endl;

    return (WorkerThread::INVALID != ID);
  }

  void WorkerThread::signal(void) {
    // nada
  }

};}; // } thread } gem