summaryrefslogtreecommitdiff
path: root/ot_clean.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_clean.c')
-rw-r--r--ot_clean.c59
1 files changed, 33 insertions, 26 deletions
diff --git a/ot_clean.c b/ot_clean.c
index 509dd2e..625e94a 100644
--- a/ot_clean.c
+++ b/ot_clean.c
@@ -4,6 +4,7 @@
4/* System */ 4/* System */
5#include <stdlib.h> 5#include <stdlib.h>
6#include <string.h> 6#include <string.h>
7#include <pthread.h>
7 8
8/* Libowfat */ 9/* Libowfat */
9#include "byte.h" 10#include "byte.h"
@@ -12,9 +13,6 @@
12#include "trackerlogic.h" 13#include "trackerlogic.h"
13#include "ot_mutex.h" 14#include "ot_mutex.h"
14 15
15/* To remember, when we last cleaned up */
16static ot_time all_torrents_clean[OT_BUCKET_COUNT];
17
18/* Clean a single torrent 16/* Clean a single torrent
19 return 1 if torrent timed out 17 return 1 if torrent timed out
20*/ 18*/
@@ -83,37 +81,46 @@ int clean_single_torrent( ot_torrent *torrent ) {
83 return 0; 81 return 0;
84} 82}
85 83
86/* Clean up all peers in current bucket, remove timedout pools and 84static void clean_make() {
87 torrents */ 85 int bucket;
88void clean_all_torrents( void ) {
89 ot_vector *torrents_list;
90 size_t i;
91 static int bucket;
92 ot_time time_now = NOW;
93
94 /* Search for an uncleaned bucked */
95 while( ( all_torrents_clean[bucket] == time_now ) && ( ++bucket < OT_BUCKET_COUNT ) );
96 if( bucket >= OT_BUCKET_COUNT ) {
97 bucket = 0; return;
98 }
99 86
100 all_torrents_clean[bucket] = time_now; 87 for( bucket = OT_BUCKET_COUNT - 1; bucket >= 0; --bucket ) {
88 ot_vector *torrents_list = mutex_bucket_lock( bucket );
89 size_t toffs;
101 90
102 torrents_list = mutex_bucket_lock( bucket ); 91 for( toffs=0; toffs<torrents_list->size; ++toffs ) {
103 for( i=0; i<torrents_list->size; ++i ) { 92 ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + toffs;
104 ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + i; 93 if( clean_single_torrent( torrent ) ) {
105 if( clean_single_torrent( torrent ) ) { 94 vector_remove_torrent( torrents_list, torrent );
106 vector_remove_torrent( torrents_list, torrent ); 95 --toffs; continue;
107 --i; continue; 96 }
108 } 97 }
98 mutex_bucket_unlock( bucket );
109 } 99 }
110 mutex_bucket_unlock( bucket );
111} 100}
112 101
102/* Clean up all peers in current bucket, remove timedout pools and
103 torrents */
104static void * clean_worker( void * args ) {
105 args = args;
106 while( 1 ) {
107 ot_tasktype tasktype = TASK_CLEAN;
108 ot_taskid taskid = mutex_workqueue_poptask( &tasktype );
109 clean_make( );
110 mutex_workqueue_pushsuccess( taskid );
111 }
112 return NULL;
113}
114
115void clean_all_torrents( ) {
116 mutex_workqueue_pushtask( 0, TASK_CLEAN );
117}
118
119static pthread_t thread_id;
113void clean_init( void ) { 120void clean_init( void ) {
114 byte_zero( all_torrents_clean, sizeof( all_torrents_clean ) ); 121 pthread_create( &thread_id, NULL, clean_worker, NULL );
115} 122}
116 123
117void clean_deinit( void ) { 124void clean_deinit( void ) {
118 byte_zero( all_torrents_clean, sizeof( all_torrents_clean ) ); 125 pthread_cancel( thread_id );
119} 126}