/[svn]/linuxsampler/trunk/src/common/WorkerThread.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/common/WorkerThread.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1649 - (show annotations) (download)
Fri Jan 25 15:06:02 2008 UTC (16 years, 3 months ago) by nagata
File size: 3100 byte(s)
* added a new config option --enable-pthread-testcancel, which uses
pthread_testcancel() instead of asynchronous canceling (needed for OSX)

1 /***************************************************************************
2 * *
3 * Copyright (C) 2007 Christian Schoenebeck, Grigor Iliev *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
18 * MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "WorkerThread.h"
22
23 #include "Exception.h"
24
25 namespace LinuxSampler {
26
27 WorkerThread::WorkerThread() : Thread(true, false, 0, -4) { }
28
29 void WorkerThread::Execute(Runnable* pJob) {
30 mutex.Lock();
31 queue.push_back(pJob);
32 mutex.Unlock();
33
34 StartThread(); // ensure thread is running
35 conditionJobsLeft.Set(true); // wake up thread
36 }
37
38 // Entry point for the worker thread.
39 int WorkerThread::Main() {
40 while (true) {
41
42 #if CONFIG_PTHREAD_TESTCANCEL
43 TestCancel();
44 #endif
45 while (!queue.empty()) {
46 Runnable* pJob;
47
48 // grab a new job from the queue
49 mutex.Lock();
50 pJob = queue.front();
51 mutex.Unlock();
52
53 try {
54 pJob->Run();
55 } catch (Exception e) {
56 e.PrintMessage();
57 } catch (...) {
58 std::cerr << "WorkerThread: an exception occured, could not finish the job";
59 std::cerr << std::endl << std::flush;
60 }
61
62 // remove processed job from the queue
63 mutex.Lock();
64 queue.pop_front();
65 mutex.Unlock();
66
67 delete pJob;
68 }
69
70 // nothing left to do, sleep until new jobs arrive
71 conditionJobsLeft.WaitIf(false);
72 // reset flag
73 conditionJobsLeft.Set(false);
74 // unlock condition object so it can be turned again by other thread
75 conditionJobsLeft.Unlock();
76 }
77 }
78
79 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC