summaryrefslogtreecommitdiff
path: root/ot_mutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_mutex.c')
-rw-r--r--ot_mutex.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/ot_mutex.c b/ot_mutex.c
index 300da3a..9aa476d 100644
--- a/ot_mutex.c
+++ b/ot_mutex.c
@@ -4,16 +4,15 @@
4 $id$ */ 4 $id$ */
5 5
6/* System */ 6/* System */
7#include <arpa/inet.h>
7#include <pthread.h> 8#include <pthread.h>
8#include <stdio.h> 9#include <stdio.h>
9#include <stdlib.h> 10#include <stdlib.h>
10#include <string.h> 11#include <string.h>
11#include <sys/mman.h>
12#include <sys/uio.h> 12#include <sys/uio.h>
13 13
14/* Libowfat */ 14/* Libowfat */
15#include "io.h" 15#include "io.h"
16#include "uint32.h"
17 16
18/* Opentracker */ 17/* Opentracker */
19#include "ot_iovec.h" 18#include "ot_iovec.h"
@@ -37,7 +36,15 @@ ot_vector *mutex_bucket_lock(int bucket) {
37 return all_torrents + bucket; 36 return all_torrents + bucket;
38} 37}
39 38
40ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(uint32_read_big((const char *)hash) >> OT_BUCKET_COUNT_SHIFT); } 39/* Torrents are spread over buckets by the first four bytes of their info_hash,
40 which may sit at any alignment, hence the memcpy */
41static int bucket_from_hash(ot_hash const hash) {
42 uint32_t bucket;
43 memcpy(&bucket, hash, sizeof(bucket));
44 return ntohl(bucket) >> OT_BUCKET_COUNT_SHIFT;
45}
46
47ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(bucket_from_hash(hash)); }
41 48
42void mutex_bucket_unlock(int bucket, int delta_torrentcount) { 49void mutex_bucket_unlock(int bucket, int delta_torrentcount) {
43 pthread_mutex_unlock(bucket_mutex + bucket); 50 pthread_mutex_unlock(bucket_mutex + bucket);
@@ -45,7 +52,7 @@ void mutex_bucket_unlock(int bucket, int delta_torrentcount) {
45} 52}
46 53
47void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) { 54void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) {
48 mutex_bucket_unlock(uint32_read_big((char *)hash) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount); 55 mutex_bucket_unlock(bucket_from_hash(hash), delta_torrentcount);
49} 56}
50 57
51size_t mutex_get_torrent_count(void) { return g_torrent_count; } 58size_t mutex_get_torrent_count(void) { return g_torrent_count; }