thread: Correct thread count error message.
[Ale.git] / thread.h
blob125fb3475ad992128b2ea409a9deb7eecd9aff0a
1 // Copyright 2006 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
3 // <dhilvert@gmail.com>
5 /* This file is part of the Anti-Lamenessing Engine.
7 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the Anti-Lamenessing Engine; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * thread.h: threading details.
26 #ifndef __thread_h__
27 #define __thread_h__
29 #include <assert.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
34 #define THREAD_PER_CPU_DEFAULT 1
35 #define THREAD_COUNT_DEFAULT 4
37 class thread {
38 static unsigned int _count;
39 static unsigned int _cpu_count;
41 static void try_linux() {
42 assert (_cpu_count == 0);
44 FILE *cpuinfo;
45 char buffer[100];
47 cpuinfo = fopen("/proc/cpuinfo", "r");
49 if (!cpuinfo)
50 return;
52 while (!feof(cpuinfo)) {
53 fgets(buffer, 100, cpuinfo);
54 if (strncmp("processor", buffer, strlen("processor")))
55 continue;
56 _cpu_count++;
60 public:
61 static void init() {
62 if (_cpu_count == 0) {
63 try_linux();
66 if (_cpu_count > 0) {
67 _count = THREAD_PER_CPU_DEFAULT * _cpu_count;
68 } else {
69 fprintf(stderr, "\n\n");
70 fprintf(stderr, "Warning: Unable to determine CPU count.\n");
71 fprintf(stderr, "\n\n");
72 _count = THREAD_COUNT_DEFAULT;
75 assert (_count > 0);
78 static void set_per_cpu(unsigned int new_per_cpu) {
79 if (_cpu_count == 0) {
80 fprintf(stderr, "\n\n");
81 fprintf(stderr, "Error: per-cpu thread count specified, but CPU count is unknown.\n");
82 fprintf(stderr, " Try setting the thread count explicitly.\n");
84 exit(1);
86 if (new_per_cpu == 0) {
87 fprintf(stderr, "\n\n");
88 fprintf(stderr, "Error: per-cpu thread count specified is zero.\n");
89 fprintf(stderr, " Try setting a positive thread count.\n");
91 exit(1);
94 _count = _cpu_count * new_per_cpu;
95 assert (_count > 0);
98 static void set_count(unsigned int new_count) {
99 if (new_count == 0) {
100 fprintf(stderr, "\n\n");
101 fprintf(stderr, "Error: thread count specified is zero.\n");
102 fprintf(stderr, " Try setting a positive thread count.\n");
104 exit(1);
107 _count = new_count;
108 assert (_count > 0);
111 static int count() {
112 assert (_count > 0);
113 return _count;
117 #endif