1/*
2 * cf_thread.h
3 *
4 * Copyright (C) 2018 Aerospike, Inc.
5 *
6 * Portions may be licensed to Aerospike, Inc. under one or more contributor
7 * license agreements.
8 *
9 * This program is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU Affero General Public License as published by the Free
11 * Software Foundation, either version 3 of the License, or (at your option) any
12 * later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see http://www.gnu.org/licenses/
21 */
22
23#pragma once
24
25//==========================================================
26// Includes.
27//
28
29#include <pthread.h>
30#include <signal.h>
31#include <sys/syscall.h>
32#include <sys/types.h>
33#include <unistd.h>
34
35#include "dynbuf.h"
36
37
38//==========================================================
39// Typedefs & constants.
40//
41
42typedef pthread_t cf_tid;
43typedef void* (*cf_thread_run_fn) (void* udata);
44
45
46//==========================================================
47// Globals.
48//
49
50extern __thread pid_t g_sys_tid;
51
52
53//==========================================================
54// Public API.
55//
56
57void cf_thread_init(void);
58cf_tid cf_thread_create_detached(cf_thread_run_fn run, void* udata);
59cf_tid cf_thread_create_joinable(cf_thread_run_fn run, void* udata);
60int32_t cf_thread_traces(char* key, cf_dyn_buf* db);
61void cf_thread_traces_action(int32_t sig_num, siginfo_t* info, void* ctx);
62
63static inline void
64cf_thread_join(cf_tid tid)
65{
66 pthread_join(tid, NULL);
67}
68
69static inline void
70cf_thread_cancel(cf_tid tid)
71{
72 pthread_cancel(tid);
73}
74
75static inline void
76cf_thread_disable_cancel(void)
77{
78 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
79}
80
81static inline void
82cf_thread_test_cancel(void)
83{
84 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
85 pthread_testcancel();
86 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
87}
88
89// Prefer this to cf_tid (i.e. pthread_t) for logging, etc.
90static inline pid_t
91cf_thread_sys_tid(void)
92{
93 if (g_sys_tid == 0) {
94 g_sys_tid = (pid_t)syscall(SYS_gettid);
95 }
96
97 return g_sys_tid;
98}
99