int wthreads_init() { // create queues for working threads int err; if(err = rqueue_init(&unprot_queue_g, unprot_queue_data_g, MAX_QUEUE_SIZE)) return err; if(err = rqueue_init(&sync_queue_g, sync_queue_data_g, MAX_QUEUE_SIZE)) return err; // start working threads if(semaph_init(&init_sem_g, 0)) return -1; pthread_t dummy_pthread; if(pthread_create(&dummy_pthread, 0, unprot_thread, 0)) { fprintf(stderr, "wthread_init: can\'t start unprot thread\n"); return -1; } if(pthread_create(&dummy_pthread, 0, sync_thread, 0)) { fprintf(stderr, "wthread_init: can\'t start sync thread\n"); unprot_quit(); return -1; } // set exit handlers if(semaph_wait(&init_sem_g) || semaph_wait(&init_sem_g) || atexit(unprot_quit) || atexit(sync_quit)) { fprintf(stderr, "wthread_init: can\'t finish initialization\n"); unprot_quit(); sync_quit(); } // add to immute threads if(immune_nthreads_g + 2 > MAX_NTHREADS) { fprintf(stderr, "wthread_init: too many immune threads\n"); unprot_quit(); sync_quit(); } immune_threads_g[immune_nthreads_g++] = unprot_thread_g; immune_threads_g[immune_nthreads_g++] = sync_thread_g; // destroy initialization semaphore semaph_destroy(&init_sem_g); return 0; } // wthread_init
struct rqueue* rqueue_new(size_t initial_capacity) { struct rqueue *tmp = (struct rqueue *) malloc(sizeof(struct rqueue)); if(rqueue_init(tmp, initial_capacity) != 0) { goto cleanup; } return tmp; cleanup: free(tmp); return NULL; }