| 217 | } |
| 218 | |
| 219 | struct thdpool *thdpool_create(const char *name, size_t per_thread_data_sz) |
| 220 | { |
| 221 | struct thdpool *pool; |
| 222 | |
| 223 | pool = calloc(1, sizeof(struct thdpool)); |
| 224 | if (!pool) { |
| 225 | logmsg(LOGMSG_ERROR, "%s: out of memory\n", __func__); |
| 226 | return NULL; |
| 227 | } |
| 228 | pool->name = strdup(name); |
| 229 | if (!pool->name) { |
| 230 | logmsg(LOGMSG_ERROR, "%s: strdup out of memory\n", __func__); |
| 231 | free(pool); |
| 232 | return NULL; |
| 233 | } |
| 234 | pool->pool = pool_init(sizeof(struct workitem), 0); |
| 235 | if (!pool->pool) { |
| 236 | logmsg(LOGMSG_ERROR, "%s: pool_init failed\n", __func__); |
| 237 | free(pool->name); |
| 238 | free(pool); |
| 239 | return NULL; |
| 240 | } |
| 241 | #ifdef MONITOR_STACK |
| 242 | pool->stack_alloc = |
| 243 | comdb2ma_create_with_scope(0, 0, "stack", pool->name, 1); |
| 244 | if (pool->stack_alloc == NULL) { |
| 245 | logmsg(LOGMSG_ERROR, "%s: comdb2ma_create failed\n", __func__); |
| 246 | free(pool->name); |
| 247 | free(pool); |
| 248 | return NULL; |
| 249 | } |
| 250 | #endif |
| 251 | listc_init(&pool->thdlist, offsetof(struct thd, thdlist_linkv)); |
| 252 | listc_init(&pool->freelist, offsetof(struct thd, freelist_linkv)); |
| 253 | listc_init(&pool->queue, offsetof(struct workitem, linkv)); |
| 254 | |
| 255 | Pthread_mutex_init(&pool->mutex, NULL); |
| 256 | Pthread_attr_init(&pool->attrs); |
| 257 | Pthread_attr_setstacksize(&pool->attrs, DEFAULT_THD_STACKSZ); |
| 258 | Pthread_attr_setdetachstate(&pool->attrs, PTHREAD_CREATE_DETACHED); |
| 259 | |
| 260 | /* 1 meg default mem per thread for thread_malloc() */ |
| 261 | pool->mem_sz = 1 * 1024 * 1024; |
| 262 | |
| 263 | pool->per_thread_data_sz = per_thread_data_sz; |
| 264 | pool->longwaitms = 500; |
| 265 | pool->wait = 0; |
| 266 | pool->exit_on_create_fail = 1; |
| 267 | pool->dump_on_full = 0; |
| 268 | pool->stack_sz = DEFAULT_THD_STACKSZ; |
| 269 | |
| 270 | Pthread_cond_init(&pool->wait_for_thread, NULL); |
| 271 | |
| 272 | Pthread_mutex_lock(&pool_list_lk); |
| 273 | pthread_once(&init_pool_list_once, init_pool_list); |
| 274 | listc_abl(&threadpools, pool); |
| 275 | Pthread_mutex_unlock(&pool_list_lk); |
| 276 | |