blob: eb3eb59fb2664c3c1f52dd637ea2eb114e104811 [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>
20#include <proto/fd.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020021
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020022
23#ifdef USE_THREAD
24
Willy Tarreau60b639c2018-08-02 10:16:17 +020025volatile unsigned long threads_want_rdv_mask = 0;
26volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +020027volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default
Willy Tarreau0c026f42018-08-01 19:12:20 +020028THREAD_LOCAL unsigned int tid = 0;
29THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
30
Christopher Faulet339fff82017-10-19 11:59:15 +020031
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020032#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
33struct lock_stat lock_stats[LOCK_LABELS];
34#endif
35
Willy Tarreau60b639c2018-08-02 10:16:17 +020036/* Marks the thread as harmless until the last thread using the rendez-vous
37 * point quits. Given that we can wait for a long time, sched_yield() is used
38 * when available to offer the CPU resources to competing threads if needed.
39 */
40void thread_harmless_till_end()
41{
42 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
43 while (threads_want_rdv_mask & all_threads_mask) {
44#if _POSIX_PRIORITY_SCHEDULING
45 sched_yield();
46#else
47 pl_cpu_relax();
48#endif
49 }
50}
51
52/* Isolates the current thread : request the ability to work while all other
53 * threads are harmless. Only returns once all of them are harmless, with the
54 * current thread's bit in threads_harmless_mask cleared. Needs to be completed
55 * using thread_release().
56 */
57void thread_isolate()
58{
59 unsigned long old;
60
61 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
62 __ha_barrier_store();
63 HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
64
65 /* wait for all threads to become harmless */
66 old = threads_harmless_mask;
67 while (1) {
68 if (unlikely((old & all_threads_mask) != all_threads_mask))
69 old = threads_harmless_mask;
70 else if (HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
71 break;
72
73#if _POSIX_PRIORITY_SCHEDULING
74 sched_yield();
75#else
76 pl_cpu_relax();
77#endif
78 }
79 /* one thread gets released at a time here, with its harmess bit off.
80 * The loss of this bit makes the other one continue to spin while the
81 * thread is working alone.
82 */
83}
84
85/* Cancels the effect of thread_isolate() by releasing the current thread's bit
86 * in threads_want_rdv_mask and by marking this thread as harmless until the
87 * last worker finishes.
88 */
89void thread_release()
90{
Willy Tarreaua9c02522018-10-16 16:11:56 +020091 HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
92 thread_harmless_end();
Willy Tarreau60b639c2018-08-02 10:16:17 +020093}
Christopher Faulet339fff82017-10-19 11:59:15 +020094
Willy Tarreaua8ae77d2018-11-25 19:28:23 +010095/* these calls are used as callbacks at init time */
96void ha_spin_init(HA_SPINLOCK_T *l)
97{
98 HA_SPIN_INIT(l);
99}
100
101/* these calls are used as callbacks at init time */
102void ha_rwlock_init(HA_RWLOCK_T *l)
103{
104 HA_RWLOCK_INIT(l);
105}
106
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200107__attribute__((constructor))
108static void __hathreads_init(void)
109{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200110#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
111 memset(lock_stats, 0, sizeof(lock_stats));
112#endif
Willy Tarreau6dbd3e92017-11-05 11:50:18 +0100113 hap_register_build_opts("Built with multi-threading support.", 0);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200114}
115
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200116#endif // USE_THREAD
117
118
119/* Parse the number of threads in argument <arg>, returns it and adjusts a few
120 * internal variables accordingly, or fails and returns zero with an error
121 * reason in <errmsg>. May be called multiple times while parsing.
122 */
123int parse_nbthread(const char *arg, char **err)
124{
125 long nbthread;
126 char *errptr;
127
128 nbthread = strtol(arg, &errptr, 10);
129 if (!*arg || *errptr) {
130 memprintf(err, "passed a missing or unparsable integer value in '%s'", arg);
131 return 0;
132 }
133
134#ifndef USE_THREAD
135 if (nbthread != 1) {
136 memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD");
137 return 0;
138 }
139#else
140 if (nbthread < 1 || nbthread > MAX_THREADS) {
141 memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread);
142 return 0;
143 }
144
145 /* we proceed like this to be sure never to overflow the left shift */
146 all_threads_mask = 1UL << (nbthread - 1);
147 all_threads_mask |= all_threads_mask - 1;
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200148#endif
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200149 return nbthread;
150}