diff options
Diffstat (limited to 'ot_mutex.c')
| -rw-r--r-- | ot_mutex.c | 21 |
1 files changed, 14 insertions, 7 deletions
| @@ -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 <sys/mman.h> | 11 | #include <string.h> |
| 11 | #include <sys/uio.h> | 12 | #include <sys/uio.h> |
| 12 | 13 | ||
| 13 | /* Libowfat */ | 14 | /* Libowfat */ |
| 14 | #include "byte.h" | ||
| 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 | ||
| 40 | ot_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 */ | ||
| 41 | static 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 | |||
| 47 | ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(bucket_from_hash(hash)); } | ||
| 41 | 48 | ||
| 42 | void mutex_bucket_unlock(int bucket, int delta_torrentcount) { | 49 | void 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 | ||
| 47 | void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) { | 54 | void 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 | ||
| 51 | size_t mutex_get_torrent_count(void) { return g_torrent_count; } | 58 | size_t mutex_get_torrent_count(void) { return g_torrent_count; } |
| @@ -259,7 +266,7 @@ void mutex_init(void) { | |||
| 259 | pthread_cond_init(&tasklist_being_filled, NULL); | 266 | pthread_cond_init(&tasklist_being_filled, NULL); |
| 260 | for (i = 0; i < OT_BUCKET_COUNT; ++i) | 267 | for (i = 0; i < OT_BUCKET_COUNT; ++i) |
| 261 | pthread_mutex_init(bucket_mutex + i, NULL); | 268 | pthread_mutex_init(bucket_mutex + i, NULL); |
| 262 | byte_zero(all_torrents, sizeof(all_torrents)); | 269 | memset(all_torrents, 0, sizeof(all_torrents)); |
| 263 | } | 270 | } |
| 264 | 271 | ||
| 265 | void mutex_deinit(void) { | 272 | void mutex_deinit(void) { |
| @@ -268,5 +275,5 @@ void mutex_deinit(void) { | |||
| 268 | pthread_mutex_destroy(bucket_mutex + i); | 275 | pthread_mutex_destroy(bucket_mutex + i); |
| 269 | pthread_mutex_destroy(&tasklist_mutex); | 276 | pthread_mutex_destroy(&tasklist_mutex); |
| 270 | pthread_cond_destroy(&tasklist_being_filled); | 277 | pthread_cond_destroy(&tasklist_being_filled); |
| 271 | byte_zero(all_torrents, sizeof(all_torrents)); | 278 | memset(all_torrents, 0, sizeof(all_torrents)); |
| 272 | } | 279 | } |
