OSDN Git Service

first
[psychlops/cpp.git] / psychlops / platform / win32 / psychlops_app_thread_Win32.cpp
1 /*\r
2  *  psychlops_app_init_Win32.h\r
3  *  Psychlops Standard Library (Universal)\r
4  *\r
5  *  Last Modified 2005/12/19 by Kenchi HOSOKAWA\r
6  *  (C) 2005 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO\r
7  */\r
8 \r
9 #include "../../core/ApplicationInterfaces/psychlops_app_thread.h"
10 #include <windows.h>\r
11 \r
12 namespace Psychlops {
13
14 namespace Prototype {
15
16
17         void* threadIDPtr(char *t) { return t; }
18         void* threadFuncPtr(char *t) { return (t+8); }
19         unsigned long __stdcall beginThread(void *param) {
20                 void (*func)() = (void(*)())param;
21                 func();
22                 return 0;
23         }
24
25
26         Thread::Thread() {
27                 for(int i=0; i<32; i++) memory[i] = 0;
28         }
29         Thread::Thread(void (*func)()) {
30                 for(int i=0; i<32; i++) memory[i] = 0;
31                 create(func);
32         }
33         Thread::~Thread() {
34                 //TerminateThread(()threadIDPtr(memory))
35         }
36         void Thread::create(void (*func)()) {
37                 CreateThread(NULL, 0, &beginThread, (void*)func, 0, (LPDWORD)threadIDPtr(memory));
38         }
39
40         void Thread::sleep(int microsec) {
41                 Sleep(microsec/1000);
42         }
43         void Thread::yield() {
44                 Sleep(0);
45         }
46         int thread_priority_[5] =  {
47                 THREAD_PRIORITY_HIGHEST,
48                 THREAD_PRIORITY_NORMAL,
49                 THREAD_PRIORITY_LOWEST,
50                 THREAD_PRIORITY_IDLE,
51                 THREAD_PRIORITY_IDLE
52         };
53         void Thread::priority( Priority priority ) {
54                 SetThreadPriority(GetCurrentThread(), thread_priority_[priority]);
55         }
56         Thread::Priority Thread::priority() {
57                 int priority = GetThreadPriority(GetCurrentThread());
58                 switch(priority) {
59                         case THREAD_PRIORITY_HIGHEST:
60                                 return Thread::HIGH;
61                         case THREAD_PRIORITY_NORMAL:
62                                 return Thread::NORMAL;
63                         case THREAD_PRIORITY_LOWEST:
64                                 return Thread::LOW;
65                         default:
66                         case THREAD_PRIORITY_IDLE:
67                                 return Thread::IDLE;
68                 }
69         }
70
71 }
72 \r
73 }\r
74 \r