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
33Int_t TWin32Thread::Run(TThread *th, const int affinity)
34{
35 DWORD dwThreadId;
36 HANDLE hHandle = CreateThread(0, 0,
37 (LPTHREAD_START_ROUTINE)&TThread::Function,
38 th, 0, (DWORD*)&dwThreadId);
39 if (affinity >= 0)
40 Warning("Run", "Affinity setting not yet implemented on Win32");
41 if (th->fDetached) {
42 ::CloseHandle(hHandle);
43 th->fHandle = 0L;
44 } else
45 th->fHandle = (Longptr_t)hHandle;
46
47 th->fId = dwThreadId;
48
49 return hHandle ? 0 : EINVAL;
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// Wait for specified thread execution (if any) to complete
54/// (like pthread_join).
55
57{
58 DWORD R = WaitForSingleObject((HANDLE)th->fHandle, INFINITE);
59
60 if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) ) {
61 //::CloseHandle((HANDLE)th->fHandle);
62 return 0;
63 }
64 if ( R == WAIT_TIMEOUT )
65 return EAGAIN;
66 return EINVAL;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Exit the thread.
71
73{
74 ExitThread(0);
75 return 0;
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// This is a somewhat dangerous function; it's not
80/// suggested to Stop() threads a lot.
81
83{
84 if (TerminateThread((HANDLE)th->fHandle,0)) {
86 return 0;
87 }
88 return EINVAL;
89}
90
91////////////////////////////////////////////////////////////////////////////////
92
93Int_t TWin32Thread::CleanUpPush(void **main, void *free,void *arg)
94{
95 if (!free) fprintf(stderr, "CleanUpPush ***ERROR*** Routine=0\n");
97 return 0;
98}
99
100////////////////////////////////////////////////////////////////////////////////
101
103{
104 if (!*main) return 1;
106 if (!l->fRoutine) fprintf(stderr,"CleanUpPop ***ERROR*** Routine=0\n");
107 if (exe && l->fRoutine) ((void (*)(void*))(l->fRoutine))(l->fArgument);
108 *main = l->fNext; delete l;
109 return 0;
110}
111
112////////////////////////////////////////////////////////////////////////////////
113
115{
116 fprintf(stderr," CleanUp %zx\n",(size_t)*main);
117 while(!CleanUpPop(main,1)) { }
118 return 0;
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Return the current thread's ID.
123
125{
126 return (Long_t)::GetCurrentThreadId();
127}
128
129////////////////////////////////////////////////////////////////////////////////
130
132{
133 if (gDebug)
134 Warning("SetCancelOff", "Not implemented on Win32");
135 return 0;
136}
137
138////////////////////////////////////////////////////////////////////////////////
139
141{
142 if (gDebug)
143 Warning("SetCancelOn", "Not implemented on Win32");
144 return 0;
145}
146
147////////////////////////////////////////////////////////////////////////////////
148
150{
151 if (gDebug)
152 Warning("SetCancelAsynchronous", "Not implemented on Win32");
153 return 0;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157
159{
160 if (gDebug)
161 Warning("SetCancelDeferred", "Not implemented on Win32");
162 return 0;
163}
164
165////////////////////////////////////////////////////////////////////////////////
166
168{
169 if (gDebug)
170 Warning("CancelPoint", "Not implemented on Win32");
171 return 0;
172}
173
174// Clean Up section. PTHREAD implementations of cleanup after cancel are
175// too different and often too bad. Temporary I invent my own bicycle.
176// V.Perev.
177
178////////////////////////////////////////////////////////////////////////////////
179
180TWin32ThreadCleanUp::TWin32ThreadCleanUp(void **main, void *routine, void *arg)
181{
183 fRoutine = routine; fArgument = arg;
184 *main = this;
185}
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
int main()
Definition Prototype.cxx:12
long Longptr_t
Definition RtypesCore.h:82
long Long_t
Definition RtypesCore.h:54
#define ClassImp(name)
Definition Rtypes.h:364
Int_t gDebug
Definition TROOT.cxx:592
#define free
Definition civetweb.c:1539
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:949
EState fState
Definition TThread.h:78
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:802
@ kCanceledState
Definition TThread.h:69
Longptr_t fHandle
Definition TThread.h:81
TWin32ThreadCleanUp(void **main, void *routine, void *arg)
TWin32ThreadCleanUp * fNext
virtual Int_t CancelPoint()
virtual Int_t Run(TThread *th, const int affinity=-1)
Win32 threads – spawn new thread (like pthread_create).
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 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.
auto * l
Definition textangle.C:4