Rev | Line | |
---|
[176] | 1 | // -*- C++ -*- |
---|
| 2 | |
---|
| 3 | #ifndef _FRED_H_ |
---|
| 4 | #define _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 | namespace HL { |
---|
| 27 | |
---|
| 28 | extern "C" typedef void * (*ThreadFunctionType) (void *); |
---|
| 29 | |
---|
| 30 | class Fred { |
---|
| 31 | public: |
---|
| 32 | |
---|
| 33 | Fred (void) { |
---|
| 34 | #if !defined(_WIN32) |
---|
| 35 | pthread_attr_init (&attr); |
---|
| 36 | #endif |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | ~Fred (void) { |
---|
| 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 (void) { |
---|
| 54 | #if defined(_WIN32) |
---|
| 55 | WaitForSingleObject (t, INFINITE); |
---|
| 56 | #else |
---|
| 57 | pthread_join (t, NULL); |
---|
| 58 | #endif |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | static void yield (void) { |
---|
| 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 | #else |
---|
| 77 | pthread_setconcurrency (n); |
---|
| 78 | #endif |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | private: |
---|
| 83 | #if defined(_WIN32) |
---|
| 84 | typedef HANDLE FredType; |
---|
| 85 | #else |
---|
| 86 | typedef pthread_t FredType; |
---|
| 87 | pthread_attr_t attr; |
---|
| 88 | #endif |
---|
| 89 | |
---|
| 90 | FredType t; |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.