Code

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