diff options
author | erdgeist <> | 2007-11-03 13:43:05 +0000 |
---|---|---|
committer | erdgeist <> | 2007-11-03 13:43:05 +0000 |
commit | a6b83118123f6281d0cad123aebcf5d20be6dd68 (patch) | |
tree | f0a740363c251605fcc87a13aaa0658bc59d557a | |
parent | 8acc0ff48f675cf154987cfc11fe9ba7813eaf50 (diff) |
Introducing first tools to make opentracker multithreaded.
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | mutex.c | 77 | ||||
-rw-r--r-- | mutex.h | 13 | ||||
-rw-r--r-- | trackerlogic.h | 1 |
4 files changed, 93 insertions, 2 deletions
@@ -6,8 +6,8 @@ CFLAGS+=-I../libowfat -Wall -pipe -Wextra #-pedantic #-ansi | |||
6 | LDFLAGS+=-L../libowfat/ -lowfat | 6 | LDFLAGS+=-L../libowfat/ -lowfat |
7 | 7 | ||
8 | BINARY = opentracker | 8 | BINARY = opentracker |
9 | HEADERS=trackerlogic.h scan_urlencoded_query.h | 9 | HEADERS=trackerlogic.h scan_urlencoded_query.h mutex.h |
10 | SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c | 10 | SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c mutex.c |
11 | 11 | ||
12 | all: $(BINARY) $(BINARY).debug | 12 | all: $(BINARY) $(BINARY).debug |
13 | 13 | ||
@@ -0,0 +1,77 @@ | |||
1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
3 | |||
4 | #include <pthread.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | #include "trackerlogic.h" | ||
8 | #include "mutex.h" | ||
9 | |||
10 | static int bucket_locklist[ OT_MAX_THREADS ]; | ||
11 | static int bucket_locklist_count = 0; | ||
12 | static pthread_mutex_t bucket_mutex; | ||
13 | static pthread_cond_t bucket_being_unlocked; | ||
14 | |||
15 | static int bucket_check( int bucket ) { | ||
16 | /* C should come with auto-i ;) */ | ||
17 | int i; | ||
18 | |||
19 | /* No more space to acquire lock to bucket -- should not happen */ | ||
20 | if( bucket_locklist_count == OT_MAX_THREADS ) { | ||
21 | fprintf( stderr, "More lock requests than mutexes. Consult source code.\n" ); | ||
22 | return -1; | ||
23 | } | ||
24 | |||
25 | /* See, if bucket is already locked */ | ||
26 | for( i=0; i<bucket_locklist_count; ++i ) | ||
27 | if( bucket_locklist[ i ] == bucket ) | ||
28 | return -1; | ||
29 | |||
30 | return 0; | ||
31 | } | ||
32 | |||
33 | static void bucket_push( int bucket ) { | ||
34 | bucket_locklist[ bucket_locklist_count++ ] = bucket; | ||
35 | } | ||
36 | |||
37 | static void bucket_remove( int bucket ) { | ||
38 | int i = 0; | ||
39 | |||
40 | while( ( i < bucket_locklist_count ) && ( bucket_locklist[ i ] != bucket ) ) | ||
41 | ++i; | ||
42 | |||
43 | if( i == bucket_locklist_count ) { | ||
44 | fprintf( stderr, "Request to unlock bucket that was never lock. Consult source code.\n" ); | ||
45 | return; | ||
46 | } | ||
47 | |||
48 | for( ; i < bucket_locklist_count - 1; ++i ) | ||
49 | bucket_locklist[ i ] = bucket_locklist[ i + 1 ]; | ||
50 | |||
51 | --bucket_locklist_count; | ||
52 | } | ||
53 | |||
54 | void mutex_bucket_lock( int bucket ) { | ||
55 | pthread_mutex_lock( &bucket_mutex ); | ||
56 | while( !bucket_check( bucket ) ) | ||
57 | pthread_cond_wait( &bucket_being_unlocked, &bucket_mutex ); | ||
58 | bucket_push( bucket ); | ||
59 | pthread_mutex_unlock( &bucket_mutex ); | ||
60 | } | ||
61 | |||
62 | void mutex_bucket_unlock( int bucket ) { | ||
63 | pthread_mutex_lock( &bucket_mutex ); | ||
64 | bucket_remove( bucket ); | ||
65 | pthread_cond_broadcast( &bucket_being_unlocked ); | ||
66 | pthread_mutex_unlock( &bucket_mutex ); | ||
67 | } | ||
68 | |||
69 | void mutex_init( ) { | ||
70 | pthread_mutex_init(&bucket_mutex, NULL); | ||
71 | pthread_cond_init (&bucket_being_unlocked, NULL); | ||
72 | } | ||
73 | |||
74 | void mutex_deinit( ) { | ||
75 | pthread_mutex_destroy(&bucket_mutex); | ||
76 | pthread_cond_destroy(&bucket_being_unlocked); | ||
77 | } | ||
@@ -0,0 +1,13 @@ | |||
1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
2 | It is considered beerware. Prost. Skol. Cheers or whatever. */ | ||
3 | |||
4 | #ifndef __MUTEX_H__ | ||
5 | #define __MUTEX_H__ | ||
6 | |||
7 | void mutex_init( ); | ||
8 | void mutex_deinit( ); | ||
9 | |||
10 | void mutex_bucket_lock( int bucket ); | ||
11 | void mutex_bucket_unlock( int bucket ); | ||
12 | |||
13 | #endif | ||
diff --git a/trackerlogic.h b/trackerlogic.h index 7cf75b2..67b14e1 100644 --- a/trackerlogic.h +++ b/trackerlogic.h | |||
@@ -44,6 +44,7 @@ typedef struct { | |||
44 | 44 | ||
45 | /* Number of tracker admin ip addresses allowed */ | 45 | /* Number of tracker admin ip addresses allowed */ |
46 | #define OT_ADMINIP_MAX 64 | 46 | #define OT_ADMINIP_MAX 64 |
47 | #define OT_MAX_THREADS 16 | ||
47 | 48 | ||
48 | /* We maintain a list of 4096 pointers to sorted list of ot_torrent structs | 49 | /* We maintain a list of 4096 pointers to sorted list of ot_torrent structs |
49 | Sort key is, of course, its hash */ | 50 | Sort key is, of course, its hash */ |