From a6b83118123f6281d0cad123aebcf5d20be6dd68 Mon Sep 17 00:00:00 2001
From: erdgeist <>
Date: Sat, 3 Nov 2007 13:43:05 +0000
Subject: Introducing first tools to make opentracker multithreaded.

---
 Makefile       |  4 +--
 mutex.c        | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mutex.h        | 13 ++++++++++
 trackerlogic.h |  1 +
 4 files changed, 93 insertions(+), 2 deletions(-)
 create mode 100644 mutex.c
 create mode 100644 mutex.h

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
 LDFLAGS+=-L../libowfat/ -lowfat
  
 BINARY = opentracker
-HEADERS=trackerlogic.h scan_urlencoded_query.h
-SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c
+HEADERS=trackerlogic.h scan_urlencoded_query.h mutex.h
+SOURCES=opentracker.c trackerlogic.c scan_urlencoded_query.c mutex.c
  
 all: $(BINARY) $(BINARY).debug
 
diff --git a/mutex.c b/mutex.c
new file mode 100644
index 0000000..3194949
--- /dev/null
+++ b/mutex.c
@@ -0,0 +1,77 @@
+/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
+   It is considered beerware. Prost. Skol. Cheers or whatever. */
+
+#include <pthread.h>
+#include <stdio.h>
+
+#include "trackerlogic.h"
+#include "mutex.h"
+
+static int bucket_locklist[ OT_MAX_THREADS ];
+static int bucket_locklist_count = 0;
+static pthread_mutex_t bucket_mutex;
+static pthread_cond_t bucket_being_unlocked;
+
+static int bucket_check( int bucket ) {
+  /* C should come with auto-i ;) */
+  int i;
+
+  /* No more space to acquire lock to bucket -- should not happen */
+  if( bucket_locklist_count == OT_MAX_THREADS ) {
+    fprintf( stderr, "More lock requests than mutexes. Consult source code.\n" );
+    return -1;
+  }
+
+  /* See, if bucket is already locked */
+  for( i=0; i<bucket_locklist_count; ++i )
+    if( bucket_locklist[ i ] == bucket )
+      return -1;
+
+  return 0;
+}
+
+static void bucket_push( int bucket ) {
+  bucket_locklist[ bucket_locklist_count++ ] = bucket;
+}
+
+static void bucket_remove( int bucket ) {
+  int i = 0;
+
+  while( ( i < bucket_locklist_count ) && ( bucket_locklist[ i ] != bucket ) )
+    ++i;
+
+  if( i == bucket_locklist_count ) {
+    fprintf( stderr, "Request to unlock bucket that was never lock. Consult source code.\n" );
+    return;
+  }
+
+  for( ; i < bucket_locklist_count - 1; ++i )
+    bucket_locklist[ i ] = bucket_locklist[ i + 1 ];
+
+  --bucket_locklist_count;
+}
+
+void 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 );
+}
+
+void mutex_bucket_unlock( int bucket ) {
+  pthread_mutex_lock( &bucket_mutex );
+  bucket_remove( bucket );
+  pthread_cond_broadcast( &bucket_being_unlocked );
+  pthread_mutex_unlock( &bucket_mutex );
+}   
+
+void mutex_init( ) {
+  pthread_mutex_init(&bucket_mutex, NULL);
+  pthread_cond_init (&bucket_being_unlocked, NULL);
+}
+
+void mutex_deinit( ) {
+  pthread_mutex_destroy(&bucket_mutex);
+  pthread_cond_destroy(&bucket_being_unlocked);
+}
diff --git a/mutex.h b/mutex.h
new file mode 100644
index 0000000..8d91ab3
--- /dev/null
+++ b/mutex.h
@@ -0,0 +1,13 @@
+/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
+   It is considered beerware. Prost. Skol. Cheers or whatever. */
+
+#ifndef __MUTEX_H__
+#define __MUTEX_H__
+
+void mutex_init( );
+void mutex_deinit( );
+
+void mutex_bucket_lock( int bucket );
+void mutex_bucket_unlock( int bucket );
+
+#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 {
 
 /* Number of tracker admin ip addresses allowed */
 #define OT_ADMINIP_MAX 64
+#define OT_MAX_THREADS 16
 
 /* We maintain a list of 4096 pointers to sorted list of ot_torrent structs
    Sort key is, of course, its hash */
-- 
cgit v1.2.3