blob: 39ff7e8288db241da85c6e10c22e56b338007b49 [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{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200111#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
112 memset(lock_stats, 0, sizeof(lock_stats));
113#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200114}
115
Willy Tarreau8459f252018-12-15 16:48:14 +0100116REGISTER_BUILD_OPTS("Built with multi-threading support.");
117
118#else
119
120REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
121
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200122#endif // USE_THREAD
123
124
125/* Parse the number of threads in argument <arg>, returns it and adjusts a few
126 * internal variables accordingly, or fails and returns zero with an error
127 * reason in <errmsg>. May be called multiple times while parsing.
128 */
129int parse_nbthread(const char *arg, char **err)
130{
131 long nbthread;
132 char *errptr;
133
134 nbthread = strtol(arg, &errptr, 10);
135 if (!*arg || *errptr) {
136 memprintf(err, "passed a missing or unparsable integer value in '%s'", arg);
137 return 0;
138 }
139
140#ifndef USE_THREAD
141 if (nbthread != 1) {
142 memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD");
143 return 0;
144 }
145#else
146 if (nbthread < 1 || nbthread > MAX_THREADS) {
147 memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread);
148 return 0;
149 }
150
151 /* we proceed like this to be sure never to overflow the left shift */
152 all_threads_mask = 1UL << (nbthread - 1);
153 all_threads_mask |= all_threads_mask - 1;
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200154#endif
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200155 return nbthread;
156}