/* Clean up all peers in current bucket, remove timedout pools and torrents */ static void * clean_worker( void * args ) { (void) args; while( 1 ) { int bucket = OT_BUCKET_COUNT; while( bucket-- ) { ot_vector *torrents_list = mutex_bucket_lock( bucket ); size_t toffs; int delta_torrentcount = 0; for( toffs=0; toffs<torrents_list->size; ++toffs ) { ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + toffs; if( clean_single_torrent( torrent ) ) { vector_remove_torrent( torrents_list, torrent ); --delta_torrentcount; --toffs; } } mutex_bucket_unlock( bucket, delta_torrentcount ); if( !g_opentracker_running ) return NULL; usleep( OT_CLEAN_SLEEP ); } stats_cleanup(); } return NULL; }
/** * Clean up zone. * */ void zone_cleanup(zone_type* zone) { allocator_type* allocator; lock_basic_type zone_lock; lock_basic_type xfr_lock; if (!zone) { return; } allocator = zone->allocator; zone_lock = zone->zone_lock; xfr_lock = zone->xfr_lock; ldns_rdf_deep_free(zone->apex); adapter_cleanup(zone->adinbound); adapter_cleanup(zone->adoutbound); namedb_cleanup(zone->db); ixfr_cleanup(zone->ixfr); xfrd_cleanup(zone->xfrd); notify_cleanup(zone->notify); signconf_cleanup(zone->signconf); stats_cleanup(zone->stats); allocator_deallocate(allocator, (void*) zone->notify_command); allocator_deallocate(allocator, (void*) zone->notify_args); allocator_deallocate(allocator, (void*) zone->policy_name); allocator_deallocate(allocator, (void*) zone->signconf_filename); allocator_deallocate(allocator, (void*) zone->name); allocator_deallocate(allocator, (void*) zone); allocator_cleanup(allocator); lock_basic_destroy(&xfr_lock); lock_basic_destroy(&zone_lock); return; }
/* Clean up all peers in current bucket, remove timedout pools and torrents */ static void * clean_worker( void * args ) { #ifdef _DEBUG ts_log_debug("ot_clean::clean_worker: start"); #endif (void) args; while( 1 ) { int bucket = OT_BUCKET_COUNT; while( bucket-- ) { ot_vector *torrents_list = mutex_bucket_lock( bucket ); size_t toffs; int delta_torrentcount = 0; for( toffs=0; toffs<torrents_list->size; ++toffs ) { ot_torrent *torrent = ((ot_torrent*)(torrents_list->data)) + toffs; if( clean_single_torrent( torrent ) ) { /* terasaur -- begin mod */ /* torrent->peer_list->peer_count = 0; torrent->peer_list->seed_count = 0; #ifdef _DEBUG ts_log_debug("ot_clean::clean_worker: calling ts_update_torrent_stats"); #endif ts_update_torrent_stats(torrent, 0); #ifdef _DEBUG ts_log_debug("ot_clean::clean_worker: after ts_update_torrent_stats"); #endif */ /* terasaur -- end mod */ vector_remove_torrent( torrents_list, torrent ); #ifdef _DEBUG ts_log_debug("ot_clean::clean_worker: after vector_remove_torrent"); #endif --delta_torrentcount; --toffs; } #ifdef _DEBUG ts_log_debug("ot_clean::clean_worker: after if clean_single_torrent block"); #endif } mutex_bucket_unlock( bucket, delta_torrentcount ); if( !g_opentracker_running ) return NULL; usleep( OT_CLEAN_SLEEP ); } #ifdef _DEBUG ts_log_debug("ot_clean::clean_worker: calling stats_cleanup"); #endif stats_cleanup(); } #ifdef _DEBUG ts_log_debug("ot_clean::clean_worker: returning"); #endif return NULL; }
static void stats_process_write(TSCont contp, TSEvent event, stats_state * my_state) { if (event == TS_EVENT_VCONN_WRITE_READY) { if (my_state->body_written == 0) { TSDebug("istats", "plugin adding response body"); my_state->body_written = 1; json_out_stats(my_state); TSVIONBytesSet(my_state->write_vio, my_state->output_bytes); } TSVIOReenable(my_state->write_vio); } else if (TS_EVENT_VCONN_WRITE_COMPLETE) { stats_cleanup(contp, my_state); } else if (event == TS_EVENT_ERROR) { TSError("stats_process_write: Received TS_EVENT_ERROR\n"); } else { TSReleaseAssert(!"Unexpected Event"); } }
// 1. Extract arguments // 2. Initialize modules // 3. Launch candy-factory threads // 4. Launch kid threads // 5. Wait for requested time // 6. Stop candy-factory threads // 7. Wait until no more candy // 8. Stop kid threads // 9. Print statistics // 10. Cleanup any allocated memory int main(int argc, char* argv[]) { //1. Extract Arguments int factories = 0, kids = 0, seconds = 0; int* array[3] = {&factories, &kids, &seconds}; if(invalid_args(argc, argv)) { printf(INVALID_ARG_ERR); exit(1); } for(int i=1; i<=ARG_COUNT; i++){ sscanf(argv[i], "%d", array[i-1]); } //2. Initialize Modules srand(time(NULL)); bbuff_init(); stats_init(factories); //3. Launch candy-factory threads pthread_t* factory_thread_IDs = malloc(factories *(sizeof(pthread_t))); launch_threads(factories, factory_thread_IDs, (void*)factory_thread); //4. Launch kid threads pthread_t* kid_thread_IDs = malloc(kids *(sizeof(pthread_t))); launch_threads(kids, kid_thread_IDs, (void*)kid_thread); //5. Wait for requested time for(int i=0; i<seconds; i++) { sleep(1); printf("Time: %ds\n", i+1); } //6. Stop candy-factory threads stop_thread = true; for(int i=0; i<factories; i++) { pthread_join(factory_thread_IDs[i], NULL); } //7. Wait until no more candy while(!bbuff_is_empty()) { sleep(1); } //8. Stop kid threads for(int i=0; i<kids; i++) { pthread_cancel(kid_thread_IDs[i]); pthread_join(kid_thread_IDs[i], NULL); } //9. Print statistics stats_display(); //10. Cleanup any allocated memory stats_cleanup(); free(factory_thread_IDs); free(kid_thread_IDs); printf("Exiting program!\n"); return 0; }
int main(int argc, char *argv[]) { bbuff_init(); stats_init(atoi(argv[1])); int factory=0; int kid=0; int sec=0; //-----1. Extract arguments----------------- if (argc == 0){ printf("Error: Invalid input\n"); exit(1); } if (argc > 0 && argc <= 4){ factory = atoi(argv[1]); kid = atoi(argv[2]); sec = atoi(argv[3]); } if (factory <= 0 || kid <= 0 || sec <= 0){ printf("Error: Invalid input\n"); exit(1); } //-----2. Initialize modules------------------- //------3. Launch candy-factory threads----------- pthread_t factory_thread[factory]; //to hold factory threads int factory_num[factory]; //to pass factory number to factoryFunc for (int k=0; k<factory; k++){ factory_num[k] = k; } for (int i = 0; i < factory; i++) { pthread_attr_t attr; pthread_attr_init(&attr); if(pthread_create(&factory_thread[i],&attr, factoryFunc, &factory_num[i])) { printf ("Create pthread error!\n"); exit (1); } } pthread_t kid_thread[kid]; //------4. Launch kid threads------------------- for (int i = 0; i < kid; i++) { pthread_attr_t attr; pthread_attr_init(&attr); if(pthread_create(&kid_thread[i], &attr, kidFunc, NULL)) { printf ("Create pthread error!\n"); exit (1); } } //------5. Wait for requested time------------ for (int p=1; p<sec; p++){ sleep(1); printf("Time %ds\n", p); } //------6. Stop candy-factory threads----------- stop_thread = true; for (int x=0; x<factory; x++){ pthread_join(factory_thread[x], NULL); } //------7. Wait until no more candy-------------- while (bbuff_is_empty() == false){ printf("Waiting for all candy to be consumed\n"); sleep(1); } //------8. Stop kid threads---------------------- for (int i = 0; i < kid; i++) { pthread_cancel(kid_thread[i]); pthread_join(kid_thread[i], NULL); } //------9. Print statistics----------------- stats_display(); //------10. Cleanup any allocated memory--------- stats_cleanup(); return 0; }