bind9/lib/isc/tid.c
Ondřej Surý 1846bbbc31
Only update the global tid_count once
Normally, the tid_count is initialized only once at the beginning of the
application.  The only exception is the pattern in the unit test where
isc_loopmgr is repeatedly created and torn down and each creation of
isc_loopmgr_t calls isc__tid_initcount() with the previous value.

ThreadSanitizer sees that as write operation on unprotected memory are
reports this as data race even though the value has not really changed.

This has been fixed by skipping the tid_count value update on repeated
calls.

(cherry picked from commit a570e37c06)
2026-06-04 12:50:57 +02:00

58 lines
1.2 KiB
C

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <sys/types.h>
#include <unistd.h>
#include <isc/lang.h>
#include <isc/thread.h>
#include <isc/tid.h>
#include <isc/util.h>
/**
* Private
*/
thread_local uint32_t isc__tid_local = ISC_TID_UNKNOWN;
/*
* Zero is a better nonsense value in this case than ISC_TID_UNKNOWN;
* avoids things like trying to allocate 32GB of per-thread counters.
*/
static uint32_t tid_count = 0;
/**
* Protected
*/
void
isc__tid_init(uint32_t tid) {
REQUIRE(isc__tid_local == ISC_TID_UNKNOWN || isc__tid_local == tid);
isc__tid_local = tid;
}
void
isc__tid_initcount(uint32_t count) {
REQUIRE(tid_count == 0 || tid_count == count);
if (tid_count == 0) {
tid_count = count;
}
}
/**
* Public
*/
uint32_t
isc_tid_count(void) {
return tid_count;
}