Logo ROOT  
Reference Guide
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#include <process.h>
22#include <errno.h>
23
25
26////////////////////////////////////////////////////////////////////////////////
27/// Win32 threads -- spawn new thread (like pthread_create).
28/// Win32 has a thread handle in addition to the thread ID.
29
31{
32 DWORD dwThreadId;
33 HANDLE hHandle = CreateThread(0, 0,
34 (LPTHREAD_START_ROUTINE)&TThread::Function,
35 th, 0, (DWORD*)&dwThreadId);
36 if (th->fDetached) {
37 ::CloseHandle(hHandle);
38 th->fHandle = 0L;
39 } else
40 th->fHandle = (Long_t)hHandle;
41
42 th->fId = dwThreadId;
43
44 return hHandle ? 0 : EINVAL;
45}
46
47////////////////////////////////////////////////////////////////////////////////
48/// Wait for specified thread execution (if any) to complete
49/// (like pthread_join).
50
52{
53 DWORD R = WaitForSingleObject((HANDLE)th->fHandle, INFINITE);
54
55 if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) ) {
56 //::CloseHandle((HANDLE)th->fHandle);
57 return 0;
58 }
59 if ( R == WAIT_TIMEOUT )
60 return EAGAIN;
61 return EINVAL;
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Exit the thread.
66
68{
69 ExitThread(0);
70 return 0;
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// This is a somewhat dangerous function; it's not
75/// suggested to Stop() threads a lot.
76
78{
79 if (TerminateThread((HANDLE)th->fHandle,0)) {
81 return 0;
82 }
83 return EINVAL;
84}
85
86////////////////////////////////////////////////////////////////////////////////
87
88Int_t TWin32Thread::CleanUpPush(void **main, void *free,void *arg)
89{
90 if (!free) fprintf(stderr, "CleanUpPush ***ERROR*** Routine=0\n");
92 return 0;
93}
94
95////////////////////////////////////////////////////////////////////////////////
96
98{
99 if (!*main) return 1;
101 if (!l->fRoutine) fprintf(stderr,"CleanUpPop ***ERROR*** Routine=0\n");
102 if (exe && l->fRoutine) ((void (*)(void*))(l->fRoutine))(l->fArgument);
103 *main = l->fNext; delete l;
104 return 0;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108
110{
111 fprintf(stderr," CleanUp %lx\n",(ULong_t)*main);
112 while(!CleanUpPop(main,1)) { }
113 return 0;
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// Return the current thread's ID.
118
120{
121 return (Long_t)::GetCurrentThreadId();
122}
123
124////////////////////////////////////////////////////////////////////////////////
125
127{
128 if (gDebug)
129 Warning("SetCancelOff", "Not implemented on Win32");
130 return 0;
131}
132
133////////////////////////////////////////////////////////////////////////////////
134
136{
137 if (gDebug)
138 Warning("SetCancelOn", "Not implemented on Win32");
139 return 0;
140}
141
142////////////////////////////////////////////////////////////////////////////////
143
145{
146 if (gDebug)
147 Warning("SetCancelAsynchronous", "Not implemented on Win32");
148 return 0;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152
154{
155 if (gDebug)
156 Warning("SetCancelDeferred", "Not implemented on Win32");
157 return 0;
158}
159
160////////////////////////////////////////////////////////////////////////////////
161
163{
164 if (gDebug)
165 Warning("CancelPoint", "Not implemented on Win32");
166 return 0;
167}
168
169// Clean Up section. PTHREAD implementations of cleanup after cancel are
170// too different and often too bad. Temporary I invent my own bicycle.
171// V.Perev.
172
173////////////////////////////////////////////////////////////////////////////////
174
175TWin32ThreadCleanUp::TWin32ThreadCleanUp(void **main, void *routine, void *arg)
176{
178 fRoutine = routine; fArgument = arg;
179 *main = this;
180}
unsigned long ULong_t
Definition: RtypesCore.h:53
long Long_t
Definition: RtypesCore.h:52
R__EXTERN Int_t gDebug
Definition: RtypesCore.h:117
#define ClassImp(name)
Definition: Rtypes.h:361
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:877
EState fState
Definition: TThread.h:74
Long_t fHandle
Definition: TThread.h:77
Bool_t fDetached
Definition: TThread.h:78
Long_t fId
Definition: TThread.h:76
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:793
@ kCanceledState
Definition: TThread.h:65
TWin32ThreadCleanUp(void **main, void *routine, void *arg)
TWin32ThreadCleanUp * fNext
Definition: TWin32Thread.h:65
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(int argc, char **argv)
static constexpr double L
auto * l
Definition: textangle.C:4