summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--mutex.c77
-rw-r--r--mutex.h13
-rw-r--r--trackerlogic.h1
4 files changed, 93 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 2e2cfb8..4dd8957 100644
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,8 @@ CFLAGS+=-I../libowfat -Wall -pipe -Wextra #-pedantic #-ansi
6LDFLAGS+=-L../libowfat/ -lowfat 6LDFLAGS+=-L../libowfat/ -lowfat
7 7
8BINARY = opentracker 8BINARY = opentracker
9HEADERS=trackerlogic.h scan_urlencoded_query.h 9HEADERS=trackerlogic.h scan_urlencoded_query.h mutex.h
10SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c 10SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c mutex.c
11 11
12all: $(BINARY) $(BINARY).debug 12all: $(BINARY) $(BINARY).debug
13 13
diff --git a/mutex.c b/mutex.c
new file mode 100644
index 0000000..3194949
--- /dev/null
+++ b/mutex.c
@@ -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
10static int bucket_locklist[ OT_MAX_THREADS ];
11static int bucket_locklist_count = 0;
12static pthread_mutex_t bucket_mutex;
13static pthread_cond_t bucket_being_unlocked;
14
15static 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
33static void bucket_push( int bucket ) {
34 bucket_locklist[ bucket_locklist_count++ ] = bucket;
35}
36
37static 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
54void 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
62void 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
69void mutex_init( ) {
70 pthread_mutex_init(&bucket_mutex, NULL);
71 pthread_cond_init (&bucket_being_unlocked, NULL);
72}
73
74void mutex_deinit( ) {
75 pthread_mutex_destroy(&bucket_mutex);
76 pthread_cond_destroy(&bucket_being_unlocked);
77}
diff --git a/mutex.h b/mutex.h
new file mode 100644
index 0000000..8d91ab3
--- /dev/null
+++ b/mutex.h
@@ -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
7void mutex_init( );
8void mutex_deinit( );
9
10void mutex_bucket_lock( int bucket );
11void 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 */