summaryrefslogtreecommitdiff
path: root/ot_mutex.c
diff options
context:
space:
mode:
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 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org> 1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever. */ 2 It is considered beerware. Prost. Skol. Cheers or whatever. */
3 3
4/* System */
4#include <pthread.h> 5#include <pthread.h>
5#include <stdio.h> 6#include <stdio.h>
6 7
8/* Libowfat */
9#include "byte.h"
10
11/* Opentracker */
7#include "trackerlogic.h" 12#include "trackerlogic.h"
8#include "mutex.h" 13#include "ot_mutex.h"
14
15/* Our global all torrents list */
16static ot_vector all_torrents[OT_BUCKET_COUNT];
9 17
10static int bucket_locklist[ OT_MAX_THREADS ]; 18static int bucket_locklist[ OT_MAX_THREADS ];
11static int bucket_locklist_count = 0; 19static int bucket_locklist_count = 0;
@@ -51,12 +59,23 @@ static void bucket_remove( int bucket ) {
51 --bucket_locklist_count; 59 --bucket_locklist_count;
52} 60}
53 61
54void mutex_bucket_lock( int bucket ) { 62ot_vector *mutex_bucket_lock( int bucket ) {
55 pthread_mutex_lock( &bucket_mutex ); 63 pthread_mutex_lock( &bucket_mutex );
56 while( bucket_check( bucket ) ) 64 while( bucket_check( bucket ) )
57 pthread_cond_wait( &bucket_being_unlocked, &bucket_mutex ); 65 pthread_cond_wait( &bucket_being_unlocked, &bucket_mutex );
58 bucket_push( bucket ); 66 bucket_push( bucket );
59 pthread_mutex_unlock( &bucket_mutex ); 67 pthread_mutex_unlock( &bucket_mutex );
68 return all_torrents + bucket;
69}
70
71ot_vector *mutex_bucket_lock_by_hash( ot_hash *hash ) {
72 unsigned char *local_hash = hash[0];
73 int bucket = ( local_hash[0] << 2 ) | ( local_hash[1] >> 6 );
74
75 /* Can block */
76 mutex_bucket_lock( bucket );
77
78 return all_torrents + bucket;
60} 79}
61 80
62void mutex_bucket_unlock( int bucket ) { 81void mutex_bucket_unlock( int bucket ) {
@@ -66,12 +85,20 @@ void mutex_bucket_unlock( int bucket ) {
66 pthread_mutex_unlock( &bucket_mutex ); 85 pthread_mutex_unlock( &bucket_mutex );
67} 86}
68 87
88void mutex_bucket_unlock_by_hash( ot_hash *hash ) {
89 unsigned char *local_hash = hash[0];
90 int bucket = ( local_hash[0] << 2 ) | ( local_hash[1] >> 6 );
91 mutex_bucket_unlock( bucket );
92}
93
69void mutex_init( ) { 94void mutex_init( ) {
70 pthread_mutex_init(&bucket_mutex, NULL); 95 pthread_mutex_init(&bucket_mutex, NULL);
71 pthread_cond_init (&bucket_being_unlocked, NULL); 96 pthread_cond_init (&bucket_being_unlocked, NULL);
97 byte_zero( all_torrents, sizeof( all_torrents ) );
72} 98}
73 99
74void mutex_deinit( ) { 100void mutex_deinit( ) {
75 pthread_mutex_destroy(&bucket_mutex); 101 pthread_mutex_destroy(&bucket_mutex);
76 pthread_cond_destroy(&bucket_being_unlocked); 102 pthread_cond_destroy(&bucket_being_unlocked);
103 byte_zero( all_torrents, sizeof( all_torrents ) );
77} 104}