From 8900cc0dd980cb08a0af957a1d0dd849bf3c2ac6 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Tue, 6 Nov 2007 11:58:32 +0000 Subject: No one can get access to buckets now without locking them. Also split up the trackerlogic.c-monster in functional sub-units. HEADS UP: this code is untested and not considered stable. --- ot_mutex.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'ot_mutex.c') diff --git a/ot_mutex.c b/ot_mutex.c index 5c14e45..bb82f46 100644 --- a/ot_mutex.c +++ b/ot_mutex.c @@ -1,11 +1,19 @@ /* This software was written by Dirk Engling It is considered beerware. Prost. Skol. Cheers or whatever. */ +/* System */ #include #include +/* Libowfat */ +#include "byte.h" + +/* Opentracker */ #include "trackerlogic.h" -#include "mutex.h" +#include "ot_mutex.h" + +/* Our global all torrents list */ +static ot_vector all_torrents[OT_BUCKET_COUNT]; static int bucket_locklist[ OT_MAX_THREADS ]; static int bucket_locklist_count = 0; @@ -51,12 +59,23 @@ static void bucket_remove( int bucket ) { --bucket_locklist_count; } -void mutex_bucket_lock( int bucket ) { +ot_vector *mutex_bucket_lock( int bucket ) { pthread_mutex_lock( &bucket_mutex ); while( bucket_check( bucket ) ) pthread_cond_wait( &bucket_being_unlocked, &bucket_mutex ); bucket_push( bucket ); pthread_mutex_unlock( &bucket_mutex ); + return all_torrents + bucket; +} + +ot_vector *mutex_bucket_lock_by_hash( ot_hash *hash ) { + unsigned char *local_hash = hash[0]; + int bucket = ( local_hash[0] << 2 ) | ( local_hash[1] >> 6 ); + + /* Can block */ + mutex_bucket_lock( bucket ); + + return all_torrents + bucket; } void mutex_bucket_unlock( int bucket ) { @@ -66,12 +85,20 @@ void mutex_bucket_unlock( int bucket ) { pthread_mutex_unlock( &bucket_mutex ); } +void mutex_bucket_unlock_by_hash( ot_hash *hash ) { + unsigned char *local_hash = hash[0]; + int bucket = ( local_hash[0] << 2 ) | ( local_hash[1] >> 6 ); + mutex_bucket_unlock( bucket ); +} + void mutex_init( ) { pthread_mutex_init(&bucket_mutex, NULL); pthread_cond_init (&bucket_being_unlocked, NULL); + byte_zero( all_torrents, sizeof( all_torrents ) ); } void mutex_deinit( ) { pthread_mutex_destroy(&bucket_mutex); pthread_cond_destroy(&bucket_being_unlocked); + byte_zero( all_torrents, sizeof( all_torrents ) ); } -- cgit v1.2.3