/[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 3766 - (show annotations) (download)
Mon Apr 6 12:41:49 2020 UTC (4 years ago) by schoenebeck
File size: 3510 byte(s)
Fixed deadlocks (e.g. when restarting engines).

* Individual thread implementations (e.g. disk thread, etc.):
  Disable thread cancellation on critical sections, e.g. when holding
  mutex locks, to prevent deadlocks if thread is stopped and/or
  restarted.

* Added TestCancel() calls to thread implementations if missing.

* No need to wrap Thread::TestCancel() calls into
  CONFIG_PTHREAD_TESTCANCEL macro conditions (since TestCancel() is
  already a stub on systems which don't have pthread_testcancel()
  available).

* If compiled for debugging: give each thread a human readable name
  to simplify debugging of multi-threading issues.

* DiskThreadBase: TestCancel() and pthread_testcancel() calls are
  per-se redundant, so only call TestCancel().

* Added missing override keywords to silent compiler warnings.

* Bumped version (2.1.1.svn54).

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

  ViewVC Help
Powered by ViewVC