OSDN Git Service

first
[psychlops/cpp.git] / psychlops / platform / osx / psychlops_app_thread_osx.cpp
1 /*
2  *  psychlops_app_init_Win32.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2005/12/19 by Kenchi HOSOKAWA
6  *  (C) 2005 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9 #include "../../core/ApplicationInterfaces/psychlops_app_thread.h"
10 #include <ApplicationServices/ApplicationServices.h>
11 #include <CoreServices/CoreServices.h>
12
13 namespace Psychlops {
14
15 namespace Prototype {
16
17
18         void* threadIDPtr(char *t) { return t; }
19         void* threadFuncPtr(char *t) { return (t+8); }
20         OSStatus beginThread(void *param) {
21                 void (*func)() = (void(*)())param;
22                 func();
23                 return 0;
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                 MPCreateTask((TaskProc)&beginThread, (void*)func, 4096, NULL, NULL, NULL, 0, (MPTaskID*)threadIDPtr(memory));
38         }
39
40         const unsigned int thread_state[5] = {
41                 kRunningThreadState,
42                 kRunningThreadState,
43                 kRunningThreadState,
44                 kReadyThreadState,
45                 kStoppedThreadState
46         };
47         void Thread::sleep(int microsec) {
48                 MPSemaphoreID delay;
49                 MPCreateSemaphore(0, 0, &delay);
50                 MPWaitOnSemaphore(delay, kDurationMicrosecond*microsec);
51                 MPDeleteSemaphore(delay);
52         }
53         void Thread::yield() {
54                 MPYield();
55         }
56         void Thread::priority( Priority priority ) {
57                 SetThreadState(kCurrentThreadID, thread_state[priority], kNoThreadID);
58         }
59         Thread::Priority Thread::priority() {
60                 ThreadState threadState;
61                 GetThreadState(kCurrentThreadID, &threadState);
62                 switch(threadState) {
63                         case kStoppedThreadState:
64                                 return Thread::SLEEP;
65                         case kReadyThreadState:
66                                 return Thread::IDLE;
67                         default:
68                         case kRunningThreadState:
69                                 return Thread::NORMAL;
70                 }
71         }
72 }
73
74 }
75