-
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.
- Loading branch information
SimulPiscator
committed
Oct 22, 2023
1 parent
020c94a
commit f07dcf9
Showing
8 changed files
with
167 additions
and
19 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,93 @@ | ||
/* | ||
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 "purgethread.h" | ||
|
||
#include <thread> | ||
#include <unistd.h> | ||
#include <sys/select.h> | ||
|
||
struct PurgeThread::Private | ||
{ | ||
const ScannerList* mpScanners; | ||
std::thread* mpThread; | ||
int mWriteFd, mReadFd; | ||
int mSleepDuration, mMaxTime; | ||
|
||
void start(); | ||
void terminate(); | ||
void threadFunc(); | ||
bool interruptibleSleep(int seconds); | ||
}; | ||
|
||
void PurgeThread::Private::start() | ||
{ | ||
int fds[2] = {0}; | ||
::pipe(fds); | ||
mReadFd = fds[0]; | ||
mWriteFd = fds[1]; | ||
mpThread = new std::thread([this]{threadFunc();}); | ||
} | ||
|
||
void PurgeThread::Private::terminate() | ||
{ | ||
if (mpThread->joinable()) { | ||
char c = 'x'; | ||
::write(mWriteFd, &c, 1); | ||
mpThread->join(); | ||
} | ||
::close(mReadFd); | ||
::close(mWriteFd); | ||
} | ||
|
||
void PurgeThread::Private::threadFunc() | ||
{ | ||
while (interruptibleSleep(mSleepDuration)) { | ||
std::clog << "purging jobs with timeout of " << mMaxTime << "seconds"; | ||
for (const auto& entry : *mpScanners) | ||
entry.pScanner->purgeJobs(mMaxTime); | ||
} | ||
} | ||
|
||
bool PurgeThread::Private::interruptibleSleep(int seconds) | ||
{ | ||
fd_set readSet; | ||
FD_ZERO(&readSet); | ||
FD_SET(mReadFd, &readSet); | ||
struct timeval timeout = { seconds, 0 }; | ||
int count = 0; | ||
do { | ||
count = ::select(mReadFd + 1, &readSet, nullptr, nullptr, &timeout); | ||
} while (count < 0 && errno == EINTR); | ||
return count == 0; | ||
} | ||
|
||
PurgeThread::PurgeThread(const ScannerList& scanners, int sleepDuration, int maxTime) | ||
: p(new Private) | ||
{ | ||
p->mpScanners = &scanners; | ||
p->mSleepDuration = sleepDuration; | ||
p->mMaxTime = maxTime; | ||
p->start(); | ||
} | ||
|
||
PurgeThread::~PurgeThread() | ||
{ | ||
p->terminate(); | ||
delete p; | ||
} |
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,38 @@ | ||
/* | ||
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 PURGETHREAD_H | ||
#define PURGETHREAD_H | ||
|
||
#include "server.h" | ||
|
||
class PurgeThread | ||
{ | ||
PurgeThread(const Scanner&) = delete; | ||
PurgeThread& operator=(const PurgeThread&) = delete; | ||
|
||
public: | ||
PurgeThread(const ScannerList&, int sleepDuration, int maxTime); | ||
~PurgeThread(); | ||
|
||
private: | ||
struct Private; | ||
Private* p; | ||
}; | ||
|
||
#endif // PURGETHREAD_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
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
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