Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Limits the size of the results' FIFO.
Julien Laffaye committed 13 years ago
commit 8617c254cba00b64c71a3b78c0457ecaec16dfe2
parent 74dd177
2 files changed +23 -6
modified libpkg/pkg_repo.c
@@ -622,6 +622,8 @@ pkg_create_repo(char *path, bool force,
		goto cleanup;

	thd_data.root_path = path;
+
	thd_data.max_results = num_workers;
+
	thd_data.num_results = 0;
	thd_data.stop = false;
	thd_data.fts = fts;
	pthread_mutex_init(&thd_data.fts_m, NULL);
@@ -629,6 +631,7 @@ pkg_create_repo(char *path, bool force,
	thd_data.thd_finished = 0;
	pthread_mutex_init(&thd_data.results_m, NULL);
	pthread_cond_init(&thd_data.has_result, NULL);
+
	pthread_cond_init(&thd_data.has_room, NULL);

	/* Launch workers */
	tids = calloc(num_workers, sizeof(pthread_t));
@@ -653,6 +656,8 @@ pkg_create_repo(char *path, bool force,
		}
		if (r != NULL) {
			STAILQ_REMOVE_HEAD(&thd_data.results, next);
+
			thd_data.num_results--;
+
			pthread_cond_signal(&thd_data.has_room);
		}
		pthread_mutex_unlock(&thd_data.results_m);
		if (r == NULL) {
@@ -864,7 +869,7 @@ read_pkg_file(void *data)

		// There is no more jobs, exit the main loop.
		if (fts_ent == NULL)
-
				break;
+
			break;

		/* skip everything that is not a file */
		if (fts_info != FTS_F)
@@ -901,7 +906,11 @@ read_pkg_file(void *data)

		/* Add result to the FIFO and notify */
		pthread_mutex_lock(&d->results_m);
+
		if (d->num_results >= d->max_results) {
+
			pthread_cond_wait(&d->has_room, &d->results_m);
+
		}
		STAILQ_INSERT_TAIL(&d->results, r, next);
+
		d->num_results++;
		pthread_cond_signal(&d->has_result);
		pthread_mutex_unlock(&d->results_m);
	}
modified libpkg/private/thd_repo.h
@@ -36,22 +36,30 @@ struct pkg_result {
	char path[MAXPATHLEN + 1];
	char cksum[SHA256_DIGEST_LENGTH * 2 + 1];
	off_t size;
-
	int retcode; // to pass errors
+
	int retcode; /* to pass errors */
	STAILQ_ENTRY(pkg_result) next;
};

struct thd_data {
	char *root_path;
+
	unsigned int max_results;

+
	/*
+
	 * `fts_m' protects `fts' and `stop'
+
	 */
+
	pthread_mutex_t fts_m;
	FTS *fts;
	bool stop;
-
	pthread_mutex_t fts_m; // protects `fts' and `stop'

-
	/* results is used as a FIFO */
+
	/*
+
	 * `results_m' protects `results', `thd_finished' and `num_results'
+
	 */
+
	pthread_mutex_t results_m;
+
	pthread_cond_t has_result;
+
	pthread_cond_t has_room;
	STAILQ_HEAD(results, pkg_result) results;
+
	unsigned int num_results;
	int thd_finished;
-
	pthread_mutex_t results_m; // protects `results' an `thd_finished'
-
	pthread_cond_t has_result; // signal that there is at least one result
};

void read_pkg_file(void *);