Code

85554c803c63e806b1c686b2a2260d0c3ff3f107
[inkscape.git] / thread.cpp
1 /**\r
2  * Phoebe DOM Implementation.\r
3  *\r
4  * This is a C++ approximation of the W3C DOM model, which follows\r
5  * fairly closely the specifications in the various .idl files, copies of\r
6  * which are provided for reference.  Most important is this one:\r
7  *\r
8  * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html\r
9  *\r
10  * Authors:\r
11  *   Bob Jamison\r
12  *\r
13  * Copyright (C) 2006 Bob Jamison\r
14  *\r
15  *  This library is free software; you can redistribute it and/or\r
16  *  modify it under the terms of the GNU Lesser General Public\r
17  *  License as published by the Free Software Foundation; either\r
18  *  version 2.1 of the License, or (at your option) any later version.\r
19  *\r
20  *  This library is distributed in the hope that it will be useful,\r
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
23  *  Lesser General Public License for more details.\r
24  *\r
25  *  You should have received a copy of the GNU Lesser General Public\r
26  *  License along with this library; if not, write to the Free Software\r
27  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
28  */\r
29 \r
30 /**\r
31  * Thread wrapper.  This provides a platform-independent thread\r
32  * class for IO and testing.\r
33  *\r
34  */\r
35 \r
36 #include "thread.h"\r
37 \r
38 namespace org\r
39 {\r
40 namespace w3c\r
41 {\r
42 namespace dom\r
43 {\r
44 namespace util\r
45 {\r
46 \r
47 \r
48 #ifdef __WIN32__\r
49 #include <windows.h>\r
50 \r
51 static DWORD WINAPI WinThreadFunction(LPVOID context)\r
52 {\r
53     Thread *thread = (Thread *)context;\r
54     thread->execute();\r
55     return 0;\r
56 }\r
57 \r
58 \r
59 void Thread::start()\r
60 {\r
61     DWORD dwThreadId;\r
62     HANDLE hThread = CreateThread(NULL, 0, WinThreadFunction,\r
63                (LPVOID)this,  0,  &dwThreadId);\r
64     //Make sure the thread is started before 'this' is deallocated\r
65     while (!started)\r
66         sleep(10);\r
67     CloseHandle(hThread);\r
68 }\r
69 \r
70 void Thread::sleep(unsigned long millis)\r
71 {\r
72     Sleep(millis);\r
73 }\r
74 \r
75 \r
76 \r
77 #else /* UNIX */\r
78 #include <pthread.h>\r
79 \r
80 void *PthreadThreadFunction(void *context)\r
81 {\r
82     Thread *thread = (Thread *)context;\r
83     thread->execute();\r
84     return NULL;\r
85 }\r
86 \r
87 \r
88 void Thread::start()\r
89 {\r
90     pthread_t thread;\r
91 \r
92     int ret = pthread_create(&thread, NULL,\r
93             PthreadThreadFunction, (void *)this);\r
94     if (ret != 0)\r
95         printf("Thread::start: thread creation failed: %s\n", strerror(ret));\r
96 \r
97     //Make sure the thread is started before 'this' is deallocated\r
98     while (!started)\r
99         sleep(10);\r
100 \r
101 }\r
102 \r
103 void Thread::sleep(unsigned long millis)\r
104 {\r
105     timespec requested;\r
106     requested.tv_sec = millis / 1000;\r
107     requested.tv_nsec = (millis % 1000 ) * 1000000L;\r
108     nanosleep(&requested, NULL);\r
109 }\r
110 \r
111 #endif /* __WIN32__ */\r
112 \r
113 }  //namespace util\r
114 }  //namespace dom\r
115 }  //namespace w3c\r
116 }  //namespace org\r
117 \r
118 \r
119 \r
120 //#########################################################################\r
121 //# E N D    O F    F I L E\r
122 //#########################################################################\r
123 \r
124 \r