blob: 2ee31de696ae2f512253baecbf8b0149612b2a6e [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{
91 while (1) {
92 HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
93 if (!(threads_want_rdv_mask & all_threads_mask))
94 break;
95 thread_harmless_till_end();
96 }
97}
Christopher Faulet339fff82017-10-19 11:59:15 +020098
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020099__attribute__((constructor))
100static void __hathreads_init(void)
101{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200102#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
103 memset(lock_stats, 0, sizeof(lock_stats));
104#endif
Willy Tarreau6dbd3e92017-11-05 11:50:18 +0100105 hap_register_build_opts("Built with multi-threading support.", 0);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200106}
107
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200108#endif // USE_THREAD
109
110
111/* Parse the number of threads in argument <arg>, returns it and adjusts a few
112 * internal variables accordingly, or fails and returns zero with an error
113 * reason in <errmsg>. May be called multiple times while parsing.
114 */
115int parse_nbthread(const char *arg, char **err)
116{
117 long nbthread;
118 char *errptr;
119
120 nbthread = strtol(arg, &errptr, 10);
121 if (!*arg || *errptr) {
122 memprintf(err, "passed a missing or unparsable integer value in '%s'", arg);
123 return 0;
124 }
125
126#ifndef USE_THREAD
127 if (nbthread != 1) {
128 memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD");
129 return 0;
130 }
131#else
132 if (nbthread < 1 || nbthread > MAX_THREADS) {
133 memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread);
134 return 0;
135 }
136
137 /* we proceed like this to be sure never to overflow the left shift */
138 all_threads_mask = 1UL << (nbthread - 1);
139 all_threads_mask |= all_threads_mask - 1;
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200140#endif
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200141 return nbthread;
142}