File: thread-utils.h

package info (click to toggle)
vdo 8.3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,536 kB
  • sloc: ansic: 21,023; sh: 349; makefile: 314; perl: 242
file content (82 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright 2023 Red Hat
 */

#ifndef THREAD_UTILS_H
#define THREAD_UTILS_H

#include <linux/atomic.h>
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <stdbool.h>

#include "errors.h"
#include "time-utils.h"

/* Thread and synchronization utilities */

struct mutex {
	pthread_mutex_t mutex;
};

struct semaphore {
	sem_t semaphore;
};

struct thread {
	pthread_t thread;
};

struct threads_barrier {
	pthread_barrier_t barrier;
};

#ifndef NDEBUG
#define UDS_MUTEX_INITIALIZER { .mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP }
#else
#define UDS_MUTEX_INITIALIZER { .mutex = PTHREAD_MUTEX_INITIALIZER }
#endif

extern const bool UDS_DO_ASSERTIONS;

unsigned int num_online_cpus(void);
pid_t __must_check uds_get_thread_id(void);

void vdo_perform_once(atomic_t *once_state, void (*function) (void));

int __must_check vdo_create_thread(void (*thread_function)(void *), void *thread_data,
				   const char *name, struct thread **new_thread);
void vdo_join_threads(struct thread *thread);

void uds_get_thread_name(char *name);

static inline void cond_resched(void)
{
	/*
	 * On Linux sched_yield always succeeds so the result can be
	 * safely ignored.
	 */
	(void) sched_yield();
}

int uds_initialize_mutex(struct mutex *mutex, bool assert_on_error);
int __must_check uds_init_mutex(struct mutex *mutex);
int uds_destroy_mutex(struct mutex *mutex);
void uds_lock_mutex(struct mutex *mutex);
void uds_unlock_mutex(struct mutex *mutex);

void initialize_threads_barrier(struct threads_barrier *barrier,
				unsigned int thread_count);
void destroy_threads_barrier(struct threads_barrier *barrier);
void enter_threads_barrier(struct threads_barrier *barrier);

int __must_check uds_initialize_semaphore(struct semaphore *semaphore,
					  unsigned int value);
int uds_destroy_semaphore(struct semaphore *semaphore);
void uds_acquire_semaphore(struct semaphore *semaphore);
bool __must_check uds_attempt_semaphore(struct semaphore *semaphore, ktime_t timeout);
void uds_release_semaphore(struct semaphore *semaphore);

#endif /* UDS_THREADS_H */