Skip to content

Commit

Permalink
fix conditioned sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
vmatare committed Apr 10, 2020
1 parent 7784b74 commit 437370f
Showing 2 changed files with 19 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/thinkfan.cpp
Original file line number Diff line number Diff line change
@@ -33,8 +33,6 @@
#include <iostream>
#include <memory>
#include <cmath>
#include <mutex>
#include <condition_variable>

#include <unistd.h>

@@ -55,13 +53,17 @@ float bias_level(1.5);
float depulse = 0;
TemperatureState temp_state(0);

std::condition_variable sleep_cond;
std::mutex sleep_mutex;


#ifdef USE_YAML
std::vector<string> config_files { DEFAULT_YAML_CONFIG, DEFAULT_CONFIG };
#else
std::vector<std::string> config_files { DEFAULT_CONFIG };
#endif

volatile int interrupted(0);
std::atomic<int> interrupted(0);

#ifdef USE_ATASMART
/** Do Not Disturb disk, i.e. don't get temperature from a sleeping disk */
@@ -76,6 +78,7 @@ void sig_handler(int signum) {
case SIGINT:
case SIGTERM:
interrupted = signum;
sleep_cond.notify_all();
break;
case SIGUSR1:
log(TF_INF) << temp_state << flush;
@@ -87,6 +90,7 @@ void sig_handler(int signum) {
#endif
case SIGUSR2:
interrupted = signum;
sleep_cond.notify_all();
log(TF_INF) << "Received SIGUSR2: Re-initializing fan control." << flush;
break;
}
@@ -137,14 +141,12 @@ void run(const Config &config)
(*cur_lvl)->str() << flush;
config.fan()->set_speed(*cur_lvl);

std::mutex sleep_mutex;
std::condition_variable sleep_cond;

while (likely(!interrupted)) {
auto until = std::chrono::steady_clock::now() + tmp_sleeptime;

std::unique_lock<std::mutex> sleep_locked(sleep_mutex);
sleep_cond.wait_for(sleep_locked, tmp_sleeptime, [] () {
return !interrupted;
sleep_cond.wait_until(sleep_locked, until, [] () {
return interrupted != 0;
} );
if (unlikely(interrupted))
break;
11 changes: 9 additions & 2 deletions src/thinkfan.h
Original file line number Diff line number Diff line change
@@ -25,6 +25,9 @@
#include <vector>
#include <chrono>
#include <fstream>
#include <atomic>
#include <mutex>
#include <condition_variable>

#define DEFAULT_CONFIG "/etc/thinkfan.conf"
#define DEFAULT_YAML_CONFIG "/etc/thinkfan.yaml"
@@ -55,7 +58,7 @@ typedef std::ifstream ifstream;
typedef std::ofstream ofstream;
typedef std::fstream fstream;
typedef std::chrono::duration<unsigned int> seconds;
typedef std::chrono::duration<float> secondsf;
typedef std::chrono::duration<double> secondsf;


class TemperatureState {
@@ -103,11 +106,15 @@ extern bool dnd_disk;
#endif /* USE_ATASMART */
extern seconds sleeptime, tmp_sleeptime;
extern float bias_level;
extern volatile int interrupted;
extern std::atomic<int> interrupted;
extern TemperatureState temp_state;
extern std::vector<string> config_files;
extern float depulse;

extern std::condition_variable sleep_cond;
extern std::mutex sleep_mutex;



}
#endif /* THINKFAN_H_ */

0 comments on commit 437370f

Please sign in to comment.