-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run SANE session on a separate thread to avoid concurrent access
- Loading branch information
SimulPiscator
committed
Feb 6, 2024
1 parent
517e935
commit 025da58
Showing
4 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
AirSane Imaging Daemon | ||
Copyright (C) 2018-2023 Simul Piscator | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include "workerthread.h" | ||
|
||
#include <mutex> | ||
#include <condition_variable> | ||
#include <thread> | ||
#include <iostream> | ||
#include <cassert> | ||
|
||
struct WorkerThread::Private | ||
{ | ||
std::mutex mMutex; | ||
std::condition_variable mThreadCondition, mExecuteCondition; | ||
Callable* mpCallable = nullptr; | ||
bool mCallDone = false; | ||
bool mStarted = false; | ||
bool mTerminate = false; | ||
|
||
std::thread mThread; | ||
void threadFunc(); | ||
}; | ||
|
||
WorkerThread::WorkerThread() | ||
: p(new Private) | ||
{ | ||
std::unique_lock<std::mutex> lock(p->mMutex); | ||
p->mStarted = false; | ||
p->mThread = std::thread([this](){ p->threadFunc(); }); | ||
p->mThreadCondition.wait(lock, [this](){ return p->mStarted; }); | ||
} | ||
|
||
WorkerThread::~WorkerThread() | ||
{ | ||
std::unique_lock<std::mutex> lock(p->mMutex); | ||
p->mTerminate = true; | ||
lock.unlock(); | ||
p->mExecuteCondition.notify_one(); | ||
if (p->mThread.joinable()) | ||
p->mThread.join(); | ||
delete p; | ||
} | ||
|
||
void WorkerThread::executeSynchronously(Callable& c) | ||
{ | ||
std::unique_lock<std::mutex> lock(p->mMutex); | ||
assert(p->mpCallable == nullptr); | ||
p->mpCallable = &c; | ||
p->mCallDone = false; | ||
p->mExecuteCondition.notify_one(); | ||
p->mThreadCondition.wait(lock, [this](){ return p->mCallDone; }); | ||
} | ||
|
||
void WorkerThread::Private::threadFunc() | ||
{ | ||
std::unique_lock<std::mutex> lock(mMutex); | ||
mStarted = true; | ||
lock.unlock(); | ||
mThreadCondition.notify_one(); | ||
while (!mTerminate) | ||
{ | ||
std::unique_lock<std::mutex> lock(mMutex); | ||
mExecuteCondition.wait(lock, [this](){ return mTerminate || mpCallable; }); | ||
if (mpCallable) { | ||
assert(!mCallDone); | ||
mpCallable->onCall(); | ||
mpCallable = nullptr; | ||
mCallDone = true; | ||
lock.unlock(); | ||
mThreadCondition.notify_one(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
AirSane Imaging Daemon | ||
Copyright (C) 2018-2023 Simul Piscator | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef WORKER_THREAD_H | ||
#define WORKER_THREAD_H | ||
|
||
class WorkerThread | ||
{ | ||
public: | ||
WorkerThread(); | ||
~WorkerThread(); | ||
|
||
WorkerThread(const WorkerThread&) = delete; | ||
WorkerThread& operator=(const WorkerThread&) = delete; | ||
|
||
struct Callable | ||
{ | ||
virtual ~Callable() {} | ||
virtual void onCall() = 0; | ||
}; | ||
void executeSynchronously(Callable&); | ||
|
||
private: | ||
struct Private; | ||
Private* p; | ||
}; | ||
|
||
|
||
#endif // WORKER_THREAD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters