Example #1
0
static bool init_aio_threadpool(struct vfs_handle_struct *handle)
{
	struct fd_event *sock_event = NULL;
	int ret = 0;
	int num_threads;

	if (pool) {
		return true;
	}

	num_threads = aio_get_num_threads(handle);
	ret = pthreadpool_init(num_threads, &pool);
	if (ret) {
		errno = ret;
		return false;
	}
	sock_event = tevent_add_fd(server_event_context(),
				NULL,
				pthreadpool_signal_fd(pool),
				TEVENT_FD_READ,
				aio_pthread_handle_completion,
				NULL);
	if (sock_event == NULL) {
		pthreadpool_destroy(pool);
		pool = NULL;
		return false;
	}

	DEBUG(10,("init_aio_threadpool: initialized with up to %d threads\n",
			num_threads));

	return true;
}
Example #2
0
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; i++) {
		ret = pthreadpool_add_job(p, i, test_sleep, &timeout);
		if (ret != 0) {
			fprintf(stderr, "pthreadpool_add_job failed: %s\n",
				strerror(ret));
			return -1;
		}
	}

	for (i=0; i<num_jobs; i++) {
		int jobid = -1;
		ret = pthreadpool_finished_job(p, &jobid);
		if ((ret != 0) || (jobid >= num_jobs)) {
			fprintf(stderr, "invalid job number %d\n", jobid);
			return -1;
		}
		finished[jobid] += 1;
	}

	for (i=0; i<num_jobs; i++) {
		if (finished[i] != 1) {
			fprintf(stderr, "finished[%d] = %d\n",
				i, finished[i]);
			return -1;
		}
	}

	ret = pthreadpool_destroy(p);
	if (ret != 0) {
		fprintf(stderr, "pthreadpool_destroy failed: %s\n",
			strerror(ret));
		return -1;
	}

	free(finished);
	return 0;
}
Example #3
0
int asys_context_init(struct asys_context **pctx, unsigned max_parallel)
{
	struct asys_context *ctx;
	int ret;

	ctx = calloc(1, sizeof(struct asys_context));
	if (ctx == NULL) {
		return ENOMEM;
	}
	ret = pthreadpool_init(max_parallel, &ctx->pool);
	if (ret != 0) {
		free(ctx);
		return ret;
	}
	ctx->pthreadpool_fd = pthreadpool_signal_fd(ctx->pool);

	*pctx = ctx;
	return 0;
}
Example #4
0
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;
}
Example #5
0
static int test_fork(void)
{
	struct pthreadpool *p;
	pid_t child, waited;
	int status, 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_destroy failed: %s\n",
			strerror(ret));
		return -1;
	}

	child = fork();
	if (child < 0) {
		perror("fork failed");
		return -1;
	}
	if (child == 0) {
		exit(0);
	}
	waited = wait(&status);
	if (waited == -1) {
		perror("wait failed");
		return -1;
	}
	if (waited != child) {
		fprintf(stderr, "expected child %d, got %d\n",
			(int)child, (int)waited);
		return -1;
	}
	return 0;
}
Example #6
0
static int test_busydestroy(void)
{
	struct pthreadpool *p;
	int timeout = 50;
	struct pollfd pfd;
	int ret;

	ret = pthreadpool_init(1, &p);
	if (ret != 0) {
		fprintf(stderr, "pthreadpool_init failed: %s\n",
			strerror(ret));
		return -1;
	}
	ret = pthreadpool_add_job(p, 1, test_sleep, &timeout);
	if (ret != 0) {
		fprintf(stderr, "pthreadpool_add_job failed: %s\n",
			strerror(ret));
		return -1;
	}
	ret = pthreadpool_destroy(p);
	if (ret != EBUSY) {
		fprintf(stderr, "Could destroy a busy pool\n");
		return -1;
	}

	pfd.fd = pthreadpool_signal_fd(p);
	pfd.events = POLLIN|POLLERR;

	poll(&pfd, 1, -1);

	ret = pthreadpool_destroy(p);
	if (ret != 0) {
		fprintf(stderr, "pthreadpool_destroy failed: %s\n",
			strerror(ret));
		return -1;
	}
	return 0;
}
Example #7
0
static bool init_aio_threadpool(struct event_context *ev_ctx,
				struct pthreadpool **pp_pool,
				void (*completion_fn)(struct event_context *,
						struct fd_event *,
						uint16,
						void *))
{
	struct fd_event *sock_event = NULL;
	int ret = 0;

	if (*pp_pool) {
		return true;
	}

	ret = pthreadpool_init(aio_pending_size, pp_pool);
	if (ret) {
		errno = ret;
		return false;
	}
	sock_event = tevent_add_fd(ev_ctx,
				NULL,
				pthreadpool_signal_fd(*pp_pool),
				TEVENT_FD_READ,
				completion_fn,
				NULL);
	if (sock_event == NULL) {
		pthreadpool_destroy(*pp_pool);
		*pp_pool = NULL;
		return false;
	}

	DEBUG(10,("init_aio_threadpool: initialized with up to %d threads\n",
		  aio_pending_size));

	return true;
}
Example #8
0
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; i<num_pools; i++) {
		ret = pthreadpool_init(poolsize, &pools[i]);
		if (ret != 0) {
			fprintf(stderr, "pthreadpool_init failed: %s\n",
				strerror(ret));
			return -1;
		}
		pfds[i].fd = pthreadpool_signal_fd(pools[i]);
		pfds[i].events = POLLIN|POLLHUP;
	}

	poolnum = 0;

	for (i=0; i<num_threads; i++) {
		state = &states[i];

		state->p = 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_pools; i++) {
			ret = pthreadpool_destroy(pools[i]);
			if (ret != 0) {
				fprintf(stderr, "pthreadpool_destroy failed: "
					"%s\n", strerror(ret));
				exit(1);
			}
		}
		/* child */
		exit(0);
	}

	for (i=0; i<num_threads; i++) {
		ret = pthread_join(states[i].tid, NULL);
		if (ret != 0) {
			fprintf(stderr, "pthread_join(%d) failed: %s\n",
				i, strerror(ret));
			return -1;
		}
	}

	received = 0;

	while (received < num_threads*num_jobs) {
		int j;

		ret = poll(pfds, num_pools, 1000);
		if (ret == -1) {
			fprintf(stderr, "poll failed: %s\n",
				strerror(errno));
			return -1;
		}
		if (ret == 0) {
			fprintf(stderr, "\npoll timed out\n");
			break;
		}

		for (j=0; j<num_pools; j++) {
			int jobid = -1;

			if ((pfds[j].revents & (POLLIN|POLLHUP)) == 0) {
				continue;
			}

			ret = pthreadpool_finished_job(pools[j], &jobid);
			if ((ret != 0) || (jobid >= num_jobs * num_threads)) {
				fprintf(stderr, "invalid job number %d\n",
					jobid);
				return -1;
			}
			finished[jobid] += 1;
			received += 1;
		}
	}

	for (i=0; i<num_threads*num_jobs; i++) {
		if (finished[i] != 1) {
			fprintf(stderr, "finished[%d] = %d\n",
				i, finished[i]);
			return -1;
		}
	}

	for (i=0; i<num_pools; i++) {
		ret = pthreadpool_destroy(pools[i]);
		if (ret != 0) {
			fprintf(stderr, "pthreadpool_destroy failed: %s\n",
				strerror(ret));
			return -1;
		}
	}

	free(pfds);
	free(pools);
	free(states);
	free(finished);

	return 0;
}