blob: 8a4085ee66e538d577b3e10e1db9adb5fa41228e [file] [log] [blame]
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001/*
2 * functions about threads.
3 *
4 * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Christopher Faulet339fff82017-10-19 11:59:15 +020013#include <unistd.h>
Willy Tarreau0ccd3222018-07-30 10:34:35 +020014#include <stdlib.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020015#include <fcntl.h>
16
Willy Tarreau04931492017-11-03 23:39:25 +010017#include <common/cfgparse.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020018#include <common/hathreads.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020019#include <common/standard.h>
Willy Tarreau80713382018-11-26 10:19:54 +010020#include <types/global.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020021#include <proto/fd.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020022
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020023
24#ifdef USE_THREAD
25
Willy Tarreau60b639c2018-08-02 10:16:17 +020026volatile unsigned long threads_want_rdv_mask = 0;
27volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +020028volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default
Willy Tarreau0c026f42018-08-01 19:12:20 +020029THREAD_LOCAL unsigned int tid = 0;
30THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
31
Christopher Faulet339fff82017-10-19 11:59:15 +020032
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020033#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
34struct lock_stat lock_stats[LOCK_LABELS];
35#endif
36
Willy Tarreau60b639c2018-08-02 10:16:17 +020037/* Marks the thread as harmless until the last thread using the rendez-vous
38 * point quits. Given that we can wait for a long time, sched_yield() is used
39 * when available to offer the CPU resources to competing threads if needed.
40 */
41void thread_harmless_till_end()
42{
43 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
44 while (threads_want_rdv_mask & all_threads_mask) {
45#if _POSIX_PRIORITY_SCHEDULING
46 sched_yield();
47#else
48 pl_cpu_relax();
49#endif
50 }
51}
52
53/* Isolates the current thread : request the ability to work while all other
54 * threads are harmless. Only returns once all of them are harmless, with the
55 * current thread's bit in threads_harmless_mask cleared. Needs to be completed
56 * using thread_release().
57 */
58void thread_isolate()
59{
60 unsigned long old;
61
62 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
63 __ha_barrier_store();
64 HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
65
66 /* wait for all threads to become harmless */
67 old = threads_harmless_mask;
68 while (1) {
69 if (unlikely((old & all_threads_mask) != all_threads_mask))
70 old = threads_harmless_mask;
71 else if (HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
72 break;
73
74#if _POSIX_PRIORITY_SCHEDULING
75 sched_yield();
76#else
77 pl_cpu_relax();
78#endif
79 }
80 /* one thread gets released at a time here, with its harmess bit off.
81 * The loss of this bit makes the other one continue to spin while the
82 * thread is working alone.
83 */
84}
85
86/* Cancels the effect of thread_isolate() by releasing the current thread's bit
87 * in threads_want_rdv_mask and by marking this thread as harmless until the
88 * last worker finishes.
89 */
90void thread_release()
91{
Willy Tarreaua9c02522018-10-16 16:11:56 +020092 HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
93 thread_harmless_end();
Willy Tarreau60b639c2018-08-02 10:16:17 +020094}
Christopher Faulet339fff82017-10-19 11:59:15 +020095
Willy Tarreaua8ae77d2018-11-25 19:28:23 +010096/* these calls are used as callbacks at init time */
97void ha_spin_init(HA_SPINLOCK_T *l)
98{
99 HA_SPIN_INIT(l);
100}
101
102/* these calls are used as callbacks at init time */
103void ha_rwlock_init(HA_RWLOCK_T *l)
104{
105 HA_RWLOCK_INIT(l);
106}
107
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200108__attribute__((constructor))
109static void __hathreads_init(void)
110{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100111 char *ptr = NULL;
112
113 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
114 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
115 "HAProxy was built with value %d, please fix it and rebuild.\n",
116 LONGBITS, MAX_THREADS);
117 exit(1);
118 }
119 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d).", MAX_THREADS);
120 hap_register_build_opts(ptr, 1);
121
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200122#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
123 memset(lock_stats, 0, sizeof(lock_stats));
124#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200125}
126
Willy Tarreau8459f252018-12-15 16:48:14 +0100127#else
128
129REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
130
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200131#endif // USE_THREAD
132
133
134/* Parse the number of threads in argument <arg>, returns it and adjusts a few
135 * internal variables accordingly, or fails and returns zero with an error
136 * reason in <errmsg>. May be called multiple times while parsing.
137 */
138int parse_nbthread(const char *arg, char **err)
139{
140 long nbthread;
141 char *errptr;
142
143 nbthread = strtol(arg, &errptr, 10);
144 if (!*arg || *errptr) {
145 memprintf(err, "passed a missing or unparsable integer value in '%s'", arg);
146 return 0;
147 }
148
149#ifndef USE_THREAD
150 if (nbthread != 1) {
151 memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD");
152 return 0;
153 }
154#else
155 if (nbthread < 1 || nbthread > MAX_THREADS) {
156 memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread);
157 return 0;
158 }
159
Willy Tarreaufc647362019-02-02 17:05:03 +0100160 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200161#endif
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200162 return nbthread;
163}