File: ThreadWin32.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (108 lines) | stat: -rw-r--r-- 2,416 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Modification History
 *
 * 2001-January-27		Jason Rohrer
 * Created.
 *
 * 2001-March-4		Jason Rohrer
 * Replaced include of <winbase.h> and <windef.h> with <windows.h> 
 * to fix compile bugs encountered with newer windows compilers.   
 *
 * 2004-March-31   Jason Rohrer
 * Added missing call to CloseHandle in destructor.
 * Added support for detatched mode.
 *
 * 2004-April-1   Jason Rohrer
 * Fixed a bug in CloseHandle call pointed out by Mycroftxxx.
 *
 * 2005-January-22   Jason Rohrer
 * Added a static sleep function.
 */
 
#include "minorGems/system/Thread.h"

#include <windows.h>


/**
 * Win32-specific implementation of the Thread class member functions.
 *
 */


// prototype
/**
 * A wrapper for the run method, since windows thread (perhaps) won't take
 * a class's member function.  Takes a pointer to the Thread to run,
 * cast as a void*;
 */
DWORD WINAPI win32ThreadFunction( void * );



Thread::Thread() {
	// allocate a handle on the heap
	mNativeObjectPointer = (void *)( new HANDLE[1] );
	}



Thread::~Thread() {
    // get a pointer to the allocated handle
	HANDLE *threadPointer = (HANDLE *)mNativeObjectPointer;

    // close the handle to ensure that the thread resources are freed
    CloseHandle( threadPointer[0] );
    
    // de-allocate the thread handle from the heap
	delete [] threadPointer;
	}



void Thread::start( char inDetach ) {

    mIsDetached = inDetach;
    
	// get a pointer to the allocated handle
	HANDLE *threadPointer = (HANDLE *)mNativeObjectPointer;
	
	DWORD threadID;
	
	threadPointer[0] = CreateThread( 
						(LPSECURITY_ATTRIBUTES)NULL,	// no attributes 
						(DWORD)0, 		// default stack size
						win32ThreadFunction,	// function
						(LPVOID)this,			// function arg
						(DWORD)0,		// no creation flags (start thread immediately)
						&threadID );					 
	}    



void Thread::join() {
	
	HANDLE *threadPointer = (HANDLE *)mNativeObjectPointer;
	
	WaitForSingleObject( threadPointer[0], INFINITE );
	}


void Thread::staticSleep( unsigned long inTimeInMilliseconds ) {
	Sleep( inTimeInMilliseconds );
	}


// takes a pointer to a Thread object as the data value
DWORD WINAPI win32ThreadFunction( void *inPtrToThread ) {
	Thread *threadToRun = (Thread *)inPtrToThread;
	threadToRun->run();

    
    if( threadToRun->isDetatched() ) {
        // thread detached, so we must destroy it
        delete threadToRun;
        }

	return 0;
	}