1
0
mirror of https://github.com/Zygo/bees.git synced 2025-05-17 13:25:45 +02:00
bees/include/crucible/process.h
Zygo Blaxell 82f1fd8054 process: replace crucible::gettid() with a weak symbol
Since we're now using weak symbols for dodgy libc functions, we might
as well do it for gettid() too.

Use the ::gettid() global namespace and let libc override it.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
2025-01-09 01:37:44 -05:00

87 lines
2.0 KiB
C++

#ifndef CRUCIBLE_PROCESS_H
#define CRUCIBLE_PROCESS_H
#include "crucible/resource.h"
#include <functional>
#include <memory>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
extern "C" {
pid_t gettid() throw();
};
namespace crucible {
using namespace std;
// Like thread, but for processes.
// TODO: thread has a few warts for this usage:
// - can't create one from its native_handle,
// - can't destroy one without joining/detaching it first
// - can't implement detach correctly without crossing threshold of insanity
// - WTF is native_handle() not const?
struct Process {
// These parts are for compatibility with std::thread
using id = ::pid_t;
using native_handle_type = ::pid_t;
~Process();
Process();
template <class Fn, class... Args>
Process(Fn fn, Args... args) :
Process()
{
do_fork(function<int()>([&]() { return fn(args...); }));
}
Process(const Process &) = delete;
Process(Process &&move_from);
bool joinable();
void detach();
native_handle_type native_handle();
id get_id();
// Modified thread members for Process
// join() calls waitpid(), returns status or exception (std::thread returns void)
using status_type = int;
status_type join();
// New members for Process
// kill() terminates a process in the usual Unix way
void kill(int sig = SIGTERM);
// take over ownership of an already-forked native process handle
Process(id pid);
private:
id m_pid;
void do_fork(function<int()>);
};
template <>
struct ResourceTraits<Process::id, Process> {
Process::id get_key(const Process &res) const { return (const_cast<Process&>(res)).native_handle(); }
shared_ptr<Process> make_resource(const Process::id &id) const { return make_shared<Process>(id); }
bool is_null_key(const Process::id &key) const { return !key; }
Process::id get_null_key() const { return 0; }
};
typedef ResourceHandle<Process::id, Process> Pid;
double getloadavg1();
double getloadavg5();
double getloadavg15();
string signal_ntoa(int sig);
}
#endif // CRUCIBLE_PROCESS_H