#include #include #include #include #include #include #include #include "pthreadpool.h" static int test_init(void) { struct pthreadpool *p; int ret; ret = pthreadpool_init(1, &p); if (ret != 0) { fprintf(stderr, "pthreadpool_init failed: %s\n", strerror(ret)); return -1; } ret = pthreadpool_destroy(p); if (ret != 0) { fprintf(stderr, "pthreadpool_init failed: %s\n", strerror(ret)); return -1; } return 0; } static void test_sleep(void *ptr) { int *ptimeout = (int *)ptr; int ret; ret = poll(NULL, 0, *ptimeout); if (ret != 0) { fprintf(stderr, "poll returned %d (%s)\n", ret, strerror(errno)); } } static int test_jobs(int num_threads, int num_jobs) { char *finished; struct pthreadpool *p; int timeout = 1; int i, ret; finished = (char *)calloc(1, num_jobs); if (finished == NULL) { fprintf(stderr, "calloc failed\n"); return -1; } ret = pthreadpool_init(num_threads, &p); if (ret != 0) { fprintf(stderr, "pthreadpool_init failed: %s\n", strerror(ret)); return -1; } for (i=0; i= num_jobs)) { fprintf(stderr, "invalid job number %d\n", jobid); return -1; } finished[jobid] += 1; } for (i=0; inum_jobs; i++) { int ret = pthreadpool_add_job(state->p, state->start_job + i, test_sleep, &state->timeout); if (ret != 0) { fprintf(stderr, "pthreadpool_add_job failed: %s\n", strerror(ret)); return NULL; } } return NULL; } static int test_threaded_addjob(int num_pools, int num_threads, int poolsize, int num_jobs) { struct pthreadpool **pools; struct threaded_state *states; struct threaded_state *state; struct pollfd *pfds; char *finished; pid_t child; int i, ret, poolnum; int received; states = calloc(num_threads, sizeof(struct threaded_state)); if (states == NULL) { fprintf(stderr, "calloc failed\n"); return -1; } finished = calloc(num_threads * num_jobs, 1); if (finished == NULL) { fprintf(stderr, "calloc failed\n"); return -1; } pools = calloc(num_pools, sizeof(struct pthreadpool *)); if (pools == NULL) { fprintf(stderr, "calloc failed\n"); return -1; } pfds = calloc(num_pools, sizeof(struct pollfd)); if (pfds == NULL) { fprintf(stderr, "calloc failed\n"); return -1; } for (i=0; ip = pools[poolnum]; poolnum = (poolnum + 1) % num_pools; state->num_jobs = num_jobs; state->timeout = 1; state->start_job = i * num_jobs; ret = pthread_create(&state->tid, NULL, test_threaded_worker, state); if (ret != 0) { fprintf(stderr, "pthread_create failed: %s\n", strerror(ret)); return -1; } } if (random() % 1) { poll(NULL, 0, 1); } child = fork(); if (child < 0) { fprintf(stderr, "fork failed: %s\n", strerror(errno)); return -1; } if (child == 0) { for (i=0; i= num_jobs * num_threads)) { fprintf(stderr, "invalid job number %d\n", jobid); return -1; } finished[jobid] += 1; received += 1; } } for (i=0; i