Initialise thread pool */
| 109 | |
| 110 | /* Initialise thread pool */ |
| 111 | struct thpool_ *thpool_init(int num_threads, const char *name) { |
| 112 | |
| 113 | threads_on_hold = 0; |
| 114 | threads_keepalive = 1; |
| 115 | |
| 116 | if(num_threads < 0) { |
| 117 | num_threads = 0; |
| 118 | } |
| 119 | |
| 120 | /* Make new thread pool */ |
| 121 | thpool_* thpool_p; |
| 122 | thpool_p = (struct thpool_*)rm_calloc(1, sizeof(struct thpool_)); |
| 123 | if(thpool_p == NULL) { |
| 124 | err("thpool_init(): Could not allocate memory for thread pool\n"); |
| 125 | return NULL; |
| 126 | } |
| 127 | if(name == NULL) { |
| 128 | err("thpool_init(): missing thread pool name\n"); |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | thpool_p->name = name; |
| 133 | thpool_p->num_threads_alive = 0; |
| 134 | thpool_p->num_threads_working = 0; |
| 135 | |
| 136 | /* Initialise the job queue */ |
| 137 | if(jobqueue_init(&thpool_p->jobqueue) == -1) { |
| 138 | err("thpool_init(): Could not allocate memory for job queue\n"); |
| 139 | rm_free(thpool_p); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | /* Make threads in pool */ |
| 144 | thpool_p->threads = (struct thread **)rm_calloc(num_threads, sizeof(struct thread *)); |
| 145 | if(thpool_p->threads == NULL) { |
| 146 | err("thpool_init(): Could not allocate memory for threads\n"); |
| 147 | jobqueue_destroy(&thpool_p->jobqueue); |
| 148 | rm_free(thpool_p); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | pthread_mutex_init(&(thpool_p->thcount_lock), NULL); |
| 153 | pthread_cond_init(&thpool_p->threads_all_idle, NULL); |
| 154 | |
| 155 | /* Thread init */ |
| 156 | int n; |
| 157 | for(n = 0; n < num_threads; n++) { |
| 158 | thread_init(thpool_p, &thpool_p->threads[n], n); |
| 159 | #if THPOOL_DEBUG |
| 160 | printf("THPOOL_DEBUG: Created thread %d in pool \n", n); |
| 161 | #endif |
| 162 | } |
| 163 | |
| 164 | /* Wait for threads to initialize */ |
| 165 | while(thpool_p->num_threads_alive != num_threads) { |
| 166 | } |
| 167 | |
| 168 | return thpool_p; |
no test coverage detected