]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/Thread.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
20 #include <string_view>
21 #include <system_error>
25 #include <sys/types.h>
27 #include "include/compat.h"
29 extern pid_t
ceph_gettid();
36 std::string thread_name
;
38 void *entry_wrapper();
41 Thread(const Thread
&) = delete;
42 Thread
& operator=(const Thread
&) = delete;
48 virtual void *entry() = 0;
51 static void *_entry_func(void *arg
);
54 const pthread_t
&get_thread_id() const;
55 pid_t
get_pid() const { return pid
; }
56 bool is_started() const;
59 int try_create(size_t stacksize
);
60 void create(const char *name
, size_t stacksize
= 0);
61 int join(void **prval
= 0);
63 int set_affinity(int cpuid
);
66 // Functions for with std::thread
68 void set_thread_name(std::thread
& t
, const std::string
& s
);
69 std::string
get_thread_name(const std::thread
& t
);
70 void kill(std::thread
& t
, int signal
);
72 template<typename Fun
, typename
... Args
>
73 std::thread
make_named_thread(std::string_view n
,
77 return std::thread([n
= std::string(n
)](auto&& fun
, auto&& ...args
) {
78 ceph_pthread_setname(pthread_self(), n
.data());
79 std::invoke(std::forward
<Fun
>(fun
),
80 std::forward
<Args
>(args
)...);
81 }, std::forward
<Fun
>(fun
), std::forward
<Args
>(args
)...);