| 1 | // -*- C++ -*- |
| 2 | |
| 3 | #ifndef HL_FRED_H |
| 4 | #define HL_FRED_H |
| 5 | |
| 6 | /// A thread-wrapper of childlike simplicity :). |
| 7 | |
| 8 | #if defined(_WIN32) |
| 9 | |
| 10 | #include <windows.h> |
| 11 | #include <process.h> |
| 12 | |
| 13 | #elif defined(__SVR4) |
| 14 | |
| 15 | #include <thread.h> |
| 16 | #include <pthread.h> |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #else |
| 20 | |
| 21 | #include <pthread.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #endif |
| 25 | |
| 26 | extern "C" typedef void * (*ThreadFunctionType) (void *); |
| 27 | |
| 28 | namespace HL { |
| 29 | |
| 30 | class Fred { |
| 31 | public: |
| 32 | |
| 33 | Fred() { |
| 34 | #if !defined(_WIN32) |
| 35 | pthread_attr_init (&attr); |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | ~Fred() { |
| 40 | #if !defined(_WIN32) |
| 41 | pthread_attr_destroy (&attr); |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | void create (ThreadFunctionType function, void * arg) { |
| 46 | #if defined(_WIN32) |
| 47 | t = CreateThread (0, 0, (LPTHREAD_START_ROUTINE) *function, (LPVOID) arg, 0, 0); |
| 48 | #else |
| 49 | pthread_create (&t, &attr, function, arg); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | void join() { |
| 54 | #if defined(_WIN32) |
| 55 | WaitForSingleObject (t, INFINITE); |
| 56 | #else |
| 57 | pthread_join (t, NULL); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | static void yield() { |
| 62 | #if defined(_WIN32) |
| 63 | Sleep (0); |
| 64 | #elif defined(__SVR4) |
| 65 | thr_yield(); |
| 66 | #else |
| 67 | sched_yield(); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static void setConcurrency (int n) { |
| 73 | #if defined(_WIN32) |
| 74 | #elif defined(__SVR4) |
| 75 | thr_setconcurrency (n); |
| 76 | #elif !defined(__NetBSD__) |
| 77 | pthread_setconcurrency (n); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | |
| 82 | private: |
| 83 | |
| 84 | Fred (const Fred&); |
| 85 | Fred& operator=(const Fred&); |
| 86 | |
| 87 | #if defined(_WIN32) |
| 88 | typedef HANDLE FredType; |
| 89 | #else |
| 90 | typedef pthread_t FredType; |
| 91 | pthread_attr_t attr; |
| 92 | #endif |
| 93 | |
| 94 | FredType t; |
| 95 | }; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | |
| 100 | #endif |