summaryrefslogtreecommitdiff
path: root/ot_mutex.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2007-11-06 11:58:12 +0000
committerDirk Engling <erdgeist@erdgeist.org>2007-11-06 11:58:12 +0000
commitfed78043a6929cb8906530df5fa80bce6329d148 (patch)
tree70aeed1dbaceea343e6ebd000d46df025bae21fc /ot_mutex.c
parent3205a1733cfc58b6002667a270f3f94956d2dc77 (diff)
downloadopentracker-fed78043a6929cb8906530df5fa80bce6329d148.tar.gz
opentracker-fed78043a6929cb8906530df5fa80bce6329d148.zip
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.
Diffstat (limited to 'ot_mutex.c')
-rw-r--r--ot_mutex.c31
1 files changed, 29 insertions, 2 deletions
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 <erdgeist@erdgeist.org>
It is considered beerware. Prost. Skol. Cheers or whatever. */
+/* System */
#include <pthread.h>
#include <stdio.h>
+/* 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 ) );
}