Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TWin32Thread.cxx
Go to the documentation of this file.
1// @(#)root/thread:$Id$
2// Author: Bertrand Bellenot 20/10/2004
3
4/*************************************************************************
5 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//////////////////////////////////////////////////////////////////////////
13// //
14// TWin32Thread //
15// //
16// This class provides an interface to the win32 thread routines. //
17// //
18//////////////////////////////////////////////////////////////////////////
19
20#include "TWin32Thread.h"
21
22#include "TThread.h"
23
24#include <process.h>
25#include <errno.h>
26
28
29////////////////////////////////////////////////////////////////////////////////
30/// Win32 threads -- spawn new thread (like pthread_create).
31/// Win32 has a thread handle in addition to the thread ID.
32
34{
35 DWORD dwThreadId;
36 HANDLE hHandle = CreateThread(0, 0,
37 (LPTHREAD_START_ROUTINE)&TThread::Function,
38 th, 0, (DWORD*)&dwThreadId);
39 if (th->fDetached) {
40 ::CloseHandle(hHandle);
41 th->fHandle = 0L;
42 } else
43 th->fHandle = (Long_t)hHandle;
44
45 th->fId = dwThreadId;
46
47 return hHandle ? 0 : EINVAL;
48}
49
50////////////////////////////////////////////////////////////////////////////////
51/// Wait for specified thread execution (if any) to complete
52/// (like pthread_join).
53
55{
56 DWORD R = WaitForSingleObject((HANDLE)th->fHandle, INFINITE);
57
58 if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) ) {
59 //::CloseHandle((HANDLE)th->fHandle);
60 return 0;
61 }
62 if ( R == WAIT_TIMEOUT )
63 return EAGAIN;
64 return EINVAL;
65}
66
67////////////////////////////////////////////////////////////////////////////////
68/// Exit the thread.
69
71{
72 ExitThread(0);
73 return 0;
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// This is a somewhat dangerous function; it's not
78/// suggested to Stop() threads a lot.
79
81{
82 if (TerminateThread((HANDLE)th->fHandle,0)) {
84 return 0;
85 }
86 return EINVAL;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
91Int_t TWin32Thread::CleanUpPush(void **main, void *free,void *arg)
92{
93 if (!free) fprintf(stderr, "CleanUpPush ***ERROR*** Routine=0\n");
95 return 0;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99
101{
102 if (!*main) return 1;
104 if (!l->fRoutine) fprintf(stderr,"CleanUpPop ***ERROR*** Routine=0\n");
105 if (exe && l->fRoutine) ((void (*)(void*))(l->fRoutine))(l->fArgument);
106 *main = l->fNext; delete l;
107 return 0;
108}
109
110////////////////////////////////////////////////////////////////////////////////
111
113{
114 fprintf(stderr," CleanUp %lx\n",(ULong_t)*main);
115 while(!CleanUpPop(main,1)) { }
116 return 0;
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Return the current thread's ID.
121
123{
124 return (Long_t)::GetCurrentThreadId();
125}
126
127////////////////////////////////////////////////////////////////////////////////
128
130{
131 if (gDebug)
132 Warning("SetCancelOff", "Not implemented on Win32");
133 return 0;
134}
135
136////////////////////////////////////////////////////////////////////////////////
137
139{
140 if (gDebug)
141 Warning("SetCancelOn", "Not implemented on Win32");
142 return 0;
143}
144
145////////////////////////////////////////////////////////////////////////////////
146
148{
149 if (gDebug)
150 Warning("SetCancelAsynchronous", "Not implemented on Win32");
151 return 0;
152}
153
154////////////////////////////////////////////////////////////////////////////////
155
157{
158 if (gDebug)
159 Warning("SetCancelDeferred", "Not implemented on Win32");
160 return 0;
161}
162
163////////////////////////////////////////////////////////////////////////////////
164
166{
167 if (gDebug)
168 Warning("CancelPoint", "Not implemented on Win32");
169 return 0;
170}
171
172// Clean Up section. PTHREAD implementations of cleanup after cancel are
173// too different and often too bad. Temporary I invent my own bicycle.
174// V.Perev.
175
176////////////////////////////////////////////////////////////////////////////////
177
178TWin32ThreadCleanUp::TWin32ThreadCleanUp(void **main, void *routine, void *arg)
179{
181 fRoutine = routine; fArgument = arg;
182 *main = this;
183}
unsigned long ULong_t
Definition RtypesCore.h:55
long Long_t
Definition RtypesCore.h:54
#define ClassImp(name)
Definition Rtypes.h:364
Int_t gDebug
Definition TROOT.cxx:590
typedef void((*Func_t)())
#define free
Definition civetweb.c:1539
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:879
EState fState
Definition TThread.h:78
Long_t fHandle
Definition TThread.h:81
Bool_t fDetached
Definition TThread.h:82
Long_t fId
Definition TThread.h:80
static void * Function(void *ptr)
Static method which is called by the system thread function and which in turn calls the actual user f...
Definition TThread.cxx:799
@ kCanceledState
Definition TThread.h:69
TWin32ThreadCleanUp(void **main, void *routine, void *arg)
TWin32ThreadCleanUp * fNext
virtual Int_t CancelPoint()
virtual Int_t Exit(void *ret)
Exit the thread.
virtual Int_t CleanUpPop(void **main, Int_t exe)
virtual Int_t SetCancelAsynchronous()
virtual Int_t Join(TThread *th, void **ret)
Wait for specified thread execution (if any) to complete (like pthread_join).
virtual Int_t SetCancelOn()
virtual Int_t Run(TThread *th)
Win32 threads – spawn new thread (like pthread_create).
virtual Int_t SetCancelOff()
virtual Long_t SelfId()
Return the current thread's ID.
virtual Int_t CleanUpPush(void **main, void *free, void *arg)
virtual Int_t SetCancelDeferred()
virtual Int_t CleanUp(void **main)
virtual Int_t Kill(TThread *th)
This is a somewhat dangerous function; it's not suggested to Stop() threads a lot.
int main()
auto * l
Definition textangle.C:4