summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-27 21:33:04 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-10-27 21:33:04 +0100
commitfea25ce5e0dbf44ff2e46a3aa2c60faa39deeb94 (patch)
tree877181908ff9886be8e83188842bb39f97014e78
parent6cbd21542cf7a751e6f0f38864e7e9eae8fa3b14 (diff)
Make global torrent counter atomic and fix the setter and getter
-rw-r--r--ot_mutex.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/ot_mutex.c b/ot_mutex.c
index c44efed..5392264 100644
--- a/ot_mutex.c
+++ b/ot_mutex.c
@@ -26,7 +26,9 @@
26/* Our global all torrents list */ 26/* Our global all torrents list */
27static ot_vector all_torrents[OT_BUCKET_COUNT]; 27static ot_vector all_torrents[OT_BUCKET_COUNT];
28static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT]; 28static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT];
29static size_t g_torrent_count; 29/* Every bucket's unlock adjusts this, and a bucket lock only keeps other
30 threads out of that one bucket, so the counter needs its own protection */
31static _Atomic size_t g_torrent_count;
30 32
31/* Self pipe from opentracker.c */ 33/* Self pipe from opentracker.c */
32extern int g_self_pipe[2]; 34extern int g_self_pipe[2];
@@ -48,14 +50,15 @@ ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_l
48 50
49void mutex_bucket_unlock(int bucket, int delta_torrentcount) { 51void mutex_bucket_unlock(int bucket, int delta_torrentcount) {
50 pthread_mutex_unlock(bucket_mutex + bucket); 52 pthread_mutex_unlock(bucket_mutex + bucket);
51 g_torrent_count += delta_torrentcount; 53 if (delta_torrentcount)
54 atomic_fetch_add_explicit(&g_torrent_count, (size_t)delta_torrentcount, memory_order_relaxed);
52} 55}
53 56
54void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) { 57void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) {
55 mutex_bucket_unlock(bucket_from_hash(hash), delta_torrentcount); 58 mutex_bucket_unlock(bucket_from_hash(hash), delta_torrentcount);
56} 59}
57 60
58size_t mutex_get_torrent_count(void) { return g_torrent_count; } 61size_t mutex_get_torrent_count(void) { return atomic_load_explicit(&g_torrent_count, memory_order_relaxed); }
59 62
60/* TaskQueue Magic */ 63/* TaskQueue Magic */
61 64