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

pull/2/head
Dave Vasilevsky 15 years ago
parent 67541dc95d
commit 9b0c2bd95b

@ -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

@ -0,0 +1,5 @@
#include <unistd.h>
size_t num_threads(void) {
return sysconf(_SC_NPROCESSORS_ONLN);
}

@ -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);

@ -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");
}

Loading…
Cancel
Save