diff options
author | erdgeist <> | 2007-11-06 11:58:32 +0000 |
---|---|---|
committer | erdgeist <> | 2007-11-06 11:58:32 +0000 |
commit | 8900cc0dd980cb08a0af957a1d0dd849bf3c2ac6 (patch) | |
tree | 70aeed1dbaceea343e6ebd000d46df025bae21fc /ot_mutex.c | |
parent | 5749f1d8fe80cbb84d66a265bcf9bafe159985ab (diff) |
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.c | 31 |
1 files changed, 29 insertions, 2 deletions
@@ -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 */ | ||
16 | static ot_vector all_torrents[OT_BUCKET_COUNT]; | ||
9 | 17 | ||
10 | static int bucket_locklist[ OT_MAX_THREADS ]; | 18 | static int bucket_locklist[ OT_MAX_THREADS ]; |
11 | static int bucket_locklist_count = 0; | 19 | static 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 | ||
54 | void mutex_bucket_lock( int bucket ) { | 62 | ot_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 | |||
71 | ot_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 | ||
62 | void mutex_bucket_unlock( int bucket ) { | 81 | void 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 | ||
88 | void 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 | |||
69 | void mutex_init( ) { | 94 | void 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 | ||
74 | void mutex_deinit( ) { | 100 | void 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 | } |