Files
bind9/lib/isc/include/isc/mutex.h
Ondřej Surý 6ffda5920e Add the reader-writer synchronization with modified C-RW-WP
This changes the internal isc_rwlock implementation to:

  Irina Calciu, Dave Dice, Yossi Lev, Victor Luchangco, Virendra
  J. Marathe, and Nir Shavit.  2013.  NUMA-aware reader-writer locks.
  SIGPLAN Not. 48, 8 (August 2013), 157–166.
  DOI:https://doi.org/10.1145/2517327.24425

(The full article available from:
  http://mcg.cs.tau.ac.il/papers/ppopp2013-rwlocks.pdf)

The implementation is based on the The Writer-Preference Lock (C-RW-WP)
variant (see the 3.4 section of the paper for the rationale).

The implemented algorithm has been modified for simplicity and for usage
patterns in rbtdb.c.

The changes compared to the original algorithm:

  * We haven't implemented the cohort locks because that would require a
    knowledge of NUMA nodes, instead a simple atomic_bool is used as
    synchronization point for writer lock.

  * The per-thread reader counters are not being used - this would
    require the internal thread id (isc_tid_v) to be always initialized,
    even in the utilities; the change has a slight performance penalty,
    so we might revisit this change in the future.  However, this change
    also saves a lot of memory, because cache-line aligned counters were
    used, so on 32-core machine, the rwlock would be 4096+ bytes big.

  * The readers use a writer_barrier that will raise after a while when
    readers lock can't be acquired to prevent readers starvation.

  * Separate ingress and egress readers counters queues to reduce both
    inter and intra-thread contention.
2023-02-15 09:30:04 +01:00

96 lines
2.9 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.
*/
#pragma once
/*! \file */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <isc/lang.h>
#include <isc/result.h> /* for ISC_R_ codes */
#include <isc/util.h>
#define ISC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
ISC_LANG_BEGINDECLS
/*
* We use macros instead of static inline functions so that the exact code
* location can be reported when PTHREADS_RUNTIME_CHECK() fails or when mutrace
* reports lock contention.
*/
#ifdef ISC_TRACK_PTHREADS_OBJECTS
typedef pthread_mutex_t *isc_mutex_t;
#define isc_mutex_init(mp) \
{ \
*mp = malloc(sizeof(**mp)); \
isc__mutex_init(*mp); \
}
#define isc_mutex_lock(mp) isc__mutex_lock(*mp)
#define isc_mutex_unlock(mp) isc__mutex_unlock(*mp)
#define isc_mutex_trylock(mp) isc__mutex_trylock(*mp)
#define isc_mutex_destroy(mp) \
{ \
isc__mutex_destroy(*mp); \
free(*mp); \
}
#else /* ISC_TRACK_PTHREADS_OBJECTS */
typedef pthread_mutex_t isc_mutex_t;
#define isc_mutex_init(mp) isc__mutex_init(mp)
#define isc_mutex_lock(mp) isc__mutex_lock(mp)
#define isc_mutex_unlock(mp) isc__mutex_unlock(mp)
#define isc_mutex_trylock(mp) isc__mutex_trylock(mp)
#define isc_mutex_destroy(mp) isc__mutex_destroy(mp)
#endif /* ISC_TRACK_PTHREADS_OBJECTS */
extern pthread_mutexattr_t isc__mutex_init_attr;
#define isc__mutex_init(mp) \
{ \
int _ret = pthread_mutex_init(mp, &isc__mutex_init_attr); \
PTHREADS_RUNTIME_CHECK(pthread_mutex_init, _ret); \
}
#define isc__mutex_lock(mp) \
{ \
int _ret = pthread_mutex_lock(mp); \
PTHREADS_RUNTIME_CHECK(pthread_mutex_lock, _ret); \
}
#define isc__mutex_unlock(mp) \
{ \
int _ret = pthread_mutex_unlock(mp); \
PTHREADS_RUNTIME_CHECK(pthread_mutex_unlock, _ret); \
}
#define isc__mutex_trylock(mp) \
((pthread_mutex_trylock(mp) == 0) ? ISC_R_SUCCESS : ISC_R_LOCKBUSY)
#define isc__mutex_destroy(mp) \
{ \
int _ret = pthread_mutex_destroy(mp); \
PTHREADS_RUNTIME_CHECK(pthread_mutex_destroy, _ret); \
}
ISC_LANG_ENDDECLS