mirror of
git://erdgeist.org/opentracker
synced 2025-02-04 11:06:42 +00:00
Introducing first tools to make opentracker multithreaded.
This commit is contained in:
parent
8acc0ff48f
commit
a6b8311812
4
Makefile
4
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
|
||||
|
||||
|
77
mutex.c
Normal file
77
mutex.c
Normal file
@ -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);
|
||||
}
|
13
mutex.h
Normal file
13
mutex.h
Normal file
@ -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
|
@ -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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user