diff options
Diffstat (limited to 'ot_mutex.c')
| -rw-r--r-- | ot_mutex.c | 77 | 
1 files changed, 77 insertions, 0 deletions
| diff --git a/ot_mutex.c b/ot_mutex.c new file mode 100644 index 0000000..5c14e45 --- /dev/null +++ b/ot_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 | |||
| 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 | } | ||
