Commit 947e3b06 authored by topjohnwu's avatar topjohnwu

Use template to get lambda for RAII

parent 5fd574a1
...@@ -126,9 +126,9 @@ int exec_command_sync(exec_t &exec) { ...@@ -126,9 +126,9 @@ int exec_command_sync(exec_t &exec) {
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
int new_daemon_thread(void *(*start_routine) (void *), void *arg, const pthread_attr_t *attr) { int new_daemon_thread(thread_entry entry, void *arg, const pthread_attr_t *attr) {
pthread_t thread; pthread_t thread;
int ret = xpthread_create(&thread, attr, start_routine, arg); int ret = xpthread_create(&thread, attr, entry, arg);
if (ret == 0) if (ret == 0)
pthread_detach(thread); pthread_detach(thread);
return ret; return ret;
...@@ -141,8 +141,8 @@ static void *proxy_routine(void *fp) { ...@@ -141,8 +141,8 @@ static void *proxy_routine(void *fp) {
return nullptr; return nullptr;
} }
int new_daemon_thread(std::function<void()> &&fn) { int new_daemon_thread(std::function<void()> &&entry) {
return new_daemon_thread(proxy_routine, new std::function<void()>(std::move(fn))); return new_daemon_thread(proxy_routine, new std::function<void()>(std::move(entry)));
} }
static char *argv0; static char *argv0;
......
...@@ -29,16 +29,13 @@ private: ...@@ -29,16 +29,13 @@ private:
pthread_mutex_t *mutex; pthread_mutex_t *mutex;
}; };
template <class Func>
class run_finally { class run_finally {
public: public:
explicit run_finally(std::function<void()> &&fn): fn(std::move(fn)) {} explicit run_finally(const Func &fn) : fn(fn) {}
~run_finally() { fn(); }
void disable() { fn = nullptr; }
~run_finally() { if (fn) fn(); }
private: private:
std::function<void()> fn; const Func &fn;
}; };
template <typename T> template <typename T>
...@@ -64,9 +61,9 @@ int parse_int(const char *s); ...@@ -64,9 +61,9 @@ int parse_int(const char *s);
static inline int parse_int(std::string s) { return parse_int(s.data()); } static inline int parse_int(std::string s) { return parse_int(s.data()); }
static inline int parse_int(std::string_view s) { return parse_int(s.data()); } static inline int parse_int(std::string_view s) { return parse_int(s.data()); }
int new_daemon_thread(void *(*start_routine) (void *), void *arg = nullptr, using thread_entry = void *(*)(void *);
const pthread_attr_t *attr = nullptr); int new_daemon_thread(thread_entry entry, void *arg = nullptr, const pthread_attr_t *attr = nullptr);
int new_daemon_thread(std::function<void()> &&fn); int new_daemon_thread(std::function<void()> &&entry);
struct exec_t { struct exec_t {
bool err = false; bool err = false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment