Permalink
Browse files

Dynamically determine the number of CPUs--now we're actually useful

  • Loading branch information...
1 parent 67541dc commit 9b0c2bd95b58bb41125f830b4cedd7c0df40377d @vasi committed Jan 17, 2010
Showing with 15 additions and 10 deletions.
  1. +1 −1 Makefile
  2. +5 −0 cpu.c
  3. +1 −0 pixz.h
  4. +8 −9 write.c
View
@@ -22,7 +22,7 @@ all: $(PROGS)
list: list.o common.o endian.o
$(LD) $@ $^ -llzma
-write: write.o common.o endian.o
+write: write.o common.o endian.o cpu.o
$(LD) $@ $^ -larchive -llzma
read: read.o common.o endian.o
View
@@ -0,0 +1,5 @@
+#include <unistd.h>
+
+size_t num_threads(void) {
+ return sysconf(_SC_NPROCESSORS_ONLN);
+}
View
@@ -61,6 +61,7 @@ char *xstrdup(const char *s);
uint64_t xle64dec(const uint8_t *d);
void xle64enc(uint8_t *d, uint64_t n);
+size_t num_threads(void);
void decode_index(void);
void *decode_block_start(off_t block_seek);
View
@@ -4,11 +4,6 @@
#include <archive_entry.h>
-#pragma mark DEFINES
-
-#define ENCODE_THREADS 2
-
-
#pragma mark TYPES
typedef enum {
@@ -29,7 +24,8 @@ struct io_block_t {
#pragma mark GLOBALS
-static pthread_t gEncodeThreads[ENCODE_THREADS];
+static size_t gNumEncodeThreads = 0;
+static pthread_t *gEncodeThreads = NULL;
static pthread_t gReadThread;
static queue_t *gEncodeQ, *gWriteQ;
static size_t gBlockInSize = 0, gBlockOutSize = 0;
@@ -92,11 +88,13 @@ int main(int argc, char **argv) {
gBlockOutSize = lzma_block_buffer_bound(gBlockInSize);
// thread setup
+ gNumEncodeThreads = num_threads();
+ gEncodeThreads = malloc(gNumEncodeThreads * sizeof(pthread_t));
gEncodeQ = queue_new();
gWriteQ = queue_new();
if (pthread_create(&gReadThread, NULL, &read_thread, NULL))
die("Error creating read thread");
- for (int i = 0; i < ENCODE_THREADS; ++i) {
+ for (int i = 0; i < gNumEncodeThreads; ++i) {
if (pthread_create(&gEncodeThreads[i], NULL, &encode_thread, NULL))
die("Error creating encode thread");
}
@@ -135,6 +133,7 @@ int main(int argc, char **argv) {
die("Error joining read thread");
queue_free(gEncodeQ);
queue_free(gWriteQ);
+ free(gEncodeThreads);
return 0;
}
@@ -178,10 +177,10 @@ static void *read_thread(void *data) {
}
// stop the other threads
- for (int i = 0; i < ENCODE_THREADS; ++i) {
+ for (int i = 0; i < gNumEncodeThreads; ++i) {
queue_push(gEncodeQ, MSG_STOP, NULL);
}
- for (int i = 0; i < ENCODE_THREADS; ++i) {
+ for (int i = 0; i < gNumEncodeThreads; ++i) {
if (pthread_join(gEncodeThreads[i], NULL))
die("Error joining encode thread");
}

0 comments on commit 9b0c2bd

Please sign in to comment.