root/juggler/tags/1.0.7/Threads/vjThreadWin32.h

Revision 8789, 11.0 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34
35 #ifndef _THREAD_WIN32_H
36 #define _THREAD_WIN32_H
37
38 // NOTE: This file(vjThreadWin32.h) MUST be included by vjThread.h.
39 // Not the other way around
40
41 #include <vjConfig.h>
42
43 #include <process.h>
44
45 #include <Threads/vjThreadFunctor.h>
46 #include <Threads/vjBaseThread.h>
47
48 //typedef int cancel_state_t;
49 //typedef int pid_t;
50 //typedef int sigset_t;
51 //-----------------------------------------------
52 //: Wrapper for Win32 thread handling functions.
53 //-----------------------------------------------
54 //!PUBLIC_API:
55 class vjThreadWin32 : public vjBaseThread
56 {
57 public:
58     // -----------------------------------------------------------------------
59     //: Constructor with arguments.  This will start a new thread that will
60     //+ execute the specified function with the given argument.
61     // -----------------------------------------------------------------------
62     vjThreadWin32(vj_thread_func_t func, void* arg = 0, long flags = 0,
63                   u_int priority = 0, void* stack_addr = NULL,
64                   size_t stack_size = 0);
65
66     // -----------------------------------------------------------------------
67     //: Constructor with arguments (functor version).  This will start a new
68     //+ thread that will execute the specified function.
69     // -----------------------------------------------------------------------
70     vjThreadWin32(vjBaseThreadFunctor* functorPtr, long flags = 0,
71                   u_int priority = 0, void* stack_addr = NULL,
72                   size_t stack_size = 0);
73
74     // -----------------------------------------------------------------------
75     //: Destructor.
76     // -----------------------------------------------------------------------
77     virtual ~vjThreadWin32 (void) {
78         ;
79     }
80
81
82     // -----------------------------------------------------------------------
83     //: Create a new thread that will execute functorPtr.
84     //
85     //! PRE: None.
86     //! POST: A thread (with any specified attributes) is created that begins
87     //+       executing func().  Depending on the scheduler, it may being
88     //+       execution immediately, or it may block for a short time before
89     //+       beginning execution.
90     //
91     //! ARGS: functorPtr - Function to be executed by the thread.
92     //! ARGS: flags - Flags for the thread--not currently used in this
93     //+               implementation (optional).
94     //! ARGS: priority - Priority of created thread (optional).
95     //! ARGS: stack_addr - Alternate address for thread's stack (optional).
96     //! ARGS: stack_size - Size for thread's stack (optional).
97     //
98     //! RETURNS:  non-zero - Successful thread creation
99     //! RETURNS:        -1 - Error
100     // -----------------------------------------------------------------------
101     virtual int
102     spawn (vjBaseThreadFunctor* functorPtr, long flags = 0,
103                 u_int priority= 0, void* stack_addr = NULL,
104                 size_t stack_size = 0);
105
106     // -----------------------------------------------------------------------
107     //: Make the calling thread wait for the termination of this thread.
108     //
109     //! PRE: None.
110     //! POST: The caller blocks until this thread finishes its execution
111     //+       (i.e., calls the exit() method).  This routine may return
112     //+       immediately if this thread has already exited.
113     //
114     //! ARGS: status - Status value of the terminating thread when that
115     //+                thread calls the exit routine (optional).
116     //
117     //! RETURNS:  0 - Successful completion
118     //! RETURNS: -1 - Error
119     // -----------------------------------------------------------------------
120     inline int
121     join (void** status = 0) {
122         WaitForSingleObject(mThreadHandle,INFINITE);
123         return 0;
124     }
125
126
127     // -----------------------------------------------------------------------
128     //: Resume the execution of a thread that was previously suspended using
129     //+ suspend().
130     //
131     //! PRE: This thread was previously suspended using the suspend() member
132     //+      function.
133     //! POST: This thread is sent the SIGCONT signal and is allowed to begin
134     //+       executing again.
135     //
136     //! RETURNS:  0 - Successful completion
137     //! RETURNS: -1 - Error
138     // -----------------------------------------------------------------------
139     virtual int
140     resume (void) {
141         if( 0xFFFFFFFF == ResumeThread(mThreadHandle))
142            return -1;
143         else
144            return 0;
145     }
146
147     // -----------------------------------------------------------------------
148     //: Suspend the execution of this thread.
149     //
150     //! PRE: None.
151     //! POST: This thread is sent the SIGSTOP signal and is thus suspended
152     //+       from execution until the member function resume() is called.
153     //
154     //! RETURNS:  0 - Successful completion
155     //! RETURNS: -1 - Error
156     // -----------------------------------------------------------------------
157     virtual int
158     suspend (void) {
159         if( 0xFFFFFFFF == SuspendThread(mThreadHandle))
160            return -1;
161         else
162            return 0;
163     }
164
165     // -----------------------------------------------------------------------
166     //: Get this thread's priority
167     //
168     //! PRE: None.
169     //! POST: The priority of this thread is returned in the integer pointer
170     //+       variable.
171     //
172     //! ARGS: prio - Pointer to an int variable that will have the thread's
173     //+              priority stored in it.
174     //
175     //! RETURNS:  0 - Successful completion
176     //! RETURNS: -1 - Error
177     // -----------------------------------------------------------------------
178     inline int
179     getprio (int* prio) {
180         std::cerr << "vjThreadWin32::getprio() not implemented yet!\n";
181
182         return -1;
183     }
184
185     // -----------------------------------------------------------------------
186     //: Set this thread's priority.
187     //
188     //! PRE: None.
189     //! POST: This thread has its priority set to the specified value.
190     //
191     //! ARGS: prio - The new priority for this thread.
192     //
193     //! RETURNS:  0 - Successful completion
194     //! RETURNS: -1 - Error
195     // -----------------------------------------------------------------------
196     inline int
197     setprio (int prio) {
198         std::cerr << "vjThreadWin32::setprio() not implemented yet!\n";
199
200         return -1;
201     }
202
203     // -----------------------------------------------------------------------
204     //: Yield execution of the calling thread to allow a different blocked
205     //+ thread to execute.
206     //
207     //! PRE: None.
208     //! POST: The caller yields its execution control to another thread or
209     //+       process.
210     // -----------------------------------------------------------------------
211     virtual void
212     vjThreadWin32::yield (void) {
213        Sleep(0);     // Sleep for 0 ms, this gives up our time-slice
214     }
215
216
217     // -----------------------------------------------------------------------
218     //: Send the specified signal to this thread (not necessarily SIGKILL).
219     //
220     //! PRE: None.
221     //! POST: This thread receives the specified signal.
222     //
223     //! ARGS: signum - The signal to send to the specified thread.
224     //
225     //! RETURNS:  0 - Successful completion
226     //! RETURNS: -1 - Error
227     //
228     //: Make the calling thread wait for the termination of this thread.
229     //! NOTE: For the sake of clarity, it is probably better to use the
230     //+       cancel() routine instead of kill() because a two-argument
231     //+       version of kill() is also used for sending signals to threads.
232     //+       This kill() and cancel() do exactly the same thing.
233     // -----------------------------------------------------------------------
234     virtual int
235     kill (int signum) {
236         std::cerr << "vjThreadWin32::kill() not implemented yet!\n";
237
238         return -1;
239     }
240
241     // -----------------------------------------------------------------------
242     //: Kill (cancel) this thread.
243     //
244     //! PRE: None.
245     //! POST: This thread is cancelled.  Depending on the cancellation
246     //+       attributes of the specified thread, it may terminate
247     //+       immediately, it may wait until a pre-defined cancel point to
248     //+       stop or it may ignore the cancel altogether.  Thus, immediate
249     //+       cancellation is not guaranteed.
250     // -----------------------------------------------------------------------
251     virtual void
252     kill (void) {
253         CloseHandle(mThreadHandle);
254     }
255
256
257    // -----------------------------------------------------------------------
258    //: Output the state of the object.
259    // -----------------------------------------------------------------------
260    virtual std::ostream& outStream(std::ostream& out)
261    {
262       out.setf(std::ios::right);
263       out << std::setw(7) << std::setfill('0') << mThreadTID << "/";
264       out.unsetf(std::ios::right);
265       vjBaseThread::outStream(out);
266       out << std::setfill(' ');
267       return out;
268    }
269
270
271 // Private member variables.
272 private:
273     HANDLE     mThreadHandle;    //: HANDLE* data structure for this thread
274     DWORD      mThreadTID;       //: The win32 thread ID
275
276
277 public:
278     // -----------------------------------------------------------------------
279     //: Get the address of thread we are in
280     //! PRE: None.
281     //! POST: The address of a pointer to a thread data structure that
282     //+       contains the data structure for the calling thread.
283     //
284     //! RETURNS: pointer - A pointer to this vjThreadWin32 object's address.
285     // -----------------------------------------------------------------------
286     static vjBaseThread* self () {
287         DWORD thread_id = GetCurrentThreadId();   // Get our pid or handle
288
289         // Get the entry in the thread table.
290         vjBaseThread* cur_thread = mThreadTable.getThread(thread_id);
291
292         // Return us.
293         return cur_thread;
294     }
295
296 private:
297     static vjThreadTable<DWORD> mThreadTable;
298 };
299
300
301 #endif   /* _THREAD_WIN32_H */
Note: See TracBrowser for help on using the browser.