diff options
| -rw-r--r-- | ot_mutex.c | 9 |
1 files changed, 6 insertions, 3 deletions
| @@ -26,7 +26,9 @@ | |||
| 26 | /* Our global all torrents list */ | 26 | /* Our global all torrents list */ |
| 27 | static ot_vector all_torrents[OT_BUCKET_COUNT]; | 27 | static ot_vector all_torrents[OT_BUCKET_COUNT]; |
| 28 | static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT]; | 28 | static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT]; |
| 29 | static 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 */ | ||
| 31 | static _Atomic size_t g_torrent_count; | ||
| 30 | 32 | ||
| 31 | /* Self pipe from opentracker.c */ | 33 | /* Self pipe from opentracker.c */ |
| 32 | extern int g_self_pipe[2]; | 34 | extern 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 | ||
| 49 | void mutex_bucket_unlock(int bucket, int delta_torrentcount) { | 51 | void 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 | ||
| 54 | void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) { | 57 | void 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 | ||
| 58 | size_t mutex_get_torrent_count(void) { return g_torrent_count; } | 61 | size_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 | ||
