int initialize_rtp_globals() { int i; int st; srand(time(0)); /* Initialize globals */ n_workers = 0; workers_hash = 0; sockfd = -1; main_pid = getpid(); /* Intialize workers array */ for (i = 0; i < MAX_RTP_WORKERS; ++i) workers[i]->used = 0; signal(SIGINT, rtp_server_stop); signal(SIGUSR1, rtp_worker_stop_eos); /* Initialize message queue */ msg_queue = msgget(MSG_IDENTIFIER/*TODO: Don't hardcode this */, IPC_CREAT /*| IPC_EXCL */| 0700); if (msg_queue == -1) return(0); /* Initialize hash table */ workers_hash = newhashtable(longhash, longequal, MAX_RTP_WORKERS * 2, 1); if (!workers_hash) { msgctl(msg_queue, IPC_RMID, 0); return(0); } /* Initialize hash table mutex */ if (pthread_mutex_init(&workers_mutex, 0)) { msgctl(msg_queue, IPC_RMID, 0); clearhashtable(&workers_hash); freehashtable(&workers_hash); return(0); } if (pthread_mutex_init(&play_state_mutex, 0)) { pthread_mutex_destroy(&workers_mutex); msgctl(msg_queue, IPC_RMID, 0); clearhashtable(&workers_hash); freehashtable(&workers_hash); return(0); } /* Create thread that checks idle workers */ st = pthread_create(&worker_comm, 0, worker_comm_fun, 0); if (st) kill(getpid(), SIGINT); return(1); }
void rtp_server_stop(int sig) { RTP_WORKER *worker; int i; /* Close socket */ if (sockfd != -1) { fprintf(stderr, "\n\nRTP - Closing main socket "); close(sockfd); fprintf(stderr, "- closed\n"); } /* Kill all workers */ fprintf(stderr, "RTP - Starting killing workers "); pthread_mutex_lock(&workers_mutex); for (i = 0; i < MAX_RTP_WORKERS; ++i) { if (workers[i]->used) { workers[i]->used = 0; /* kill worker */ kill(workers[i]->pid, SIGINT); waitpid(workers[i]->pid, 0, 0); worker = gethashtable(&workers_hash, &workers[i]->ssrc); if (worker) { delhashtable(&workers_hash, &workers[i]->ssrc); } } fprintf(stderr, "."); } pthread_mutex_unlock(&workers_mutex); fprintf(stderr, "- killed\n"); /* Kill thread that checks idle workers */ fprintf(stderr, "RTP - Killing threads "); pthread_cancel(worker_comm); pthread_join(worker_comm, 0); /* Free workers hash. */ if (workers_hash) { fprintf(stderr, "RTP - Deleting workers hash "); clearhashtable(&workers_hash); freehashtable(&workers_hash); fprintf(stderr, "- deleted\n"); } /* Destroy workers mutex */ fprintf(stderr, "RTP - Destroying mutex "); pthread_mutex_destroy(&workers_mutex); pthread_mutex_destroy(&play_state_mutex); fprintf(stderr, "- destroyed\n"); /* Free message queue */ fprintf(stderr, "RTP - Destroying message queue "); msgctl(msg_queue, IPC_RMID, 0); fprintf(stderr, "destroyed\n"); /* Die */ fprintf(stderr, "RTP - Finished\n"); exit(0); }
int main(int argc, char *argv[]) { Hashtable* ht = newStrHashTable(); char buff[100]; int i; char str[10] = "thisis0"; char object[90] = "this is a shared object"; size_t sizeobject = sizeof(char) * 90; for (i = 0; i < 18; i++) { str[6] = '0'+(i%10); strhh_insert(ht, str, (void**)&object, &sizeobject); } printf("All\n"); printAll(ht); for (i = 0; i < 18; i++) { str[6] = '0'+(i%10); strhh_remove(ht, str, (void**)&object, &sizeobject); } printf("none\n"); printAll(ht); strhh_insert(ht, "Test", (void**)&object, &sizeobject); strhh_insert(ht, "TesT", (void**)&object, &sizeobject); strhh_insert(ht, "TeSt", (void**)&object, &sizeobject); strhh_insert(ht, "TEst", (void**)&object, &sizeobject); strhh_insert(ht, "test", (void**)&object, &sizeobject); assert(strhh_contains(ht, "Test", (void**)&object, &sizeobject)); assert(strhh_contains(ht, "Test", (void**)&object, &sizeobject)); assert(strhh_contains(ht, "TEst", (void**)&object, &sizeobject)); assert(strhh_contains(ht, "TesT", (void**)&object, &sizeobject)); assert(strhh_contains(ht, "TeSt", (void**)&object, &sizeobject)); assert(!strhh_contains(ht, "TEST", (void**)&object, &sizeobject)); assert(!strhh_contains(ht, "T778ST", (void**)&object, &sizeobject)); printf("Test\n"); printAll(ht); for (i = 0; i < 10000; i++) { gen_random(buff, 90); strhh_insert(ht, buff, (void**)&object, &sizeobject); } strhh_remove(ht, "|||blubla##", (void**)&object, &sizeobject); freehashtable(ht); freepool(); return 0; }