Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TPosixThread.cxx
Go to the documentation of this file.
1// @(#)root/thread:$Id$
2// Author: Fons Rademakers 02/07/97
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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// TPosixThread //
15// //
16// This class provides an interface to the posix thread routines. //
17// //
18//////////////////////////////////////////////////////////////////////////
19
20#include "TPosixThread.h"
21
22#include "TThread.h"
23
25
26
27////////////////////////////////////////////////////////////////////////////////
28/// Create a pthread. Returns 0 on success, otherwise an error number will
29/// be returned.
30
32{
33 int det;
34 pthread_t id;
35 pthread_attr_t *attr = new pthread_attr_t;
36
37 pthread_attr_init(attr);
38
39 // Set detach state
40 det = (th->fDetached) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
41
42 pthread_attr_setdetachstate(attr, det);
43
44 // See e.g. https://developer.apple.com/library/mac/#qa/qa1419/_index.html
45 // MacOS has only 512k of stack per thread; Linux has 2MB.
46 const size_t requiredStackSize = 1024*1024*2;
47 size_t stackSize = 0;
48 if (!pthread_attr_getstacksize(attr, &stackSize)
49 && stackSize < requiredStackSize) {
50 pthread_attr_setstacksize(attr, requiredStackSize);
51 }
52 int ierr = pthread_create(&id, attr, &TThread::Function, th);
53 if (!ierr) th->fId = (Long_t) id;
54
55 pthread_attr_destroy(attr);
56 delete attr;
57
58 return ierr;
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// Join suspends the execution of the calling thread until the
63/// thread identified by th terminates, either by calling pthread_exit
64/// or by being cancelled. Returns 0 on success, otherwise an error number will
65/// be returned.
66
68{
69 return pthread_join((pthread_t) th->fId, ret);
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// Terminates the execution of the calling thread. Return 0.
74
76{
77 pthread_exit(ret);
78 return 0;
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// Cancellation is the mechanism by which a thread can terminate the
83/// execution of another thread. Returns 0 on success, otherwise an error
84/// number will be returned.
85
87{
88 return pthread_cancel((pthread_t) th->fId);
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// Turn off the cancellation state of the calling thread. Returns 0 on
93/// success, otherwise an error number will be returned.
94
96{
97 return pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Turn on the cancellation state of the calling thread. Returns 0 on
102/// success, otherwise an error number will be returned.
103
105{
106 return pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Set the cancellation response type of the calling thread to
111/// asynchronous, i.e. cancel as soon as the cancellation request
112/// is received.
113
115{
116 return pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Set the cancellation response type of the calling thread to
121/// deferred, i.e. cancel only at next cancellation point.
122/// Returns 0 on success, otherwise an error number will be returned.
123
125{
126 return pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, 0);
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Introduce an explicit cancellation point. Returns 0.
131
133{
134 int istate;
135 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &istate);
136 pthread_testcancel();
137 pthread_setcancelstate(istate, 0);
138
139 return 0;
140}
141
142////////////////////////////////////////////////////////////////////////////////
143/// Add thread cleanup function.
144
145Int_t TPosixThread::CleanUpPush(void **main, void *free, void *arg)
146{
147 // pthread_cleanup_push(free, arg);
148 if (!free) Error("CleanUpPush", "cleanup rountine = 0");
149 new TPosixThreadCleanUp(main, free, arg);
150 return 0;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Pop thread cleanup function from stack.
155
157{
158 // pthread_cleanup_pop(exe); // happy pthread future
159
160 if (!main || !*main) return 1;
162 if (!l->fRoutine) Error("CleanUpPop", "cleanup routine = 0");
163 if (exe && l->fRoutine) ((void (*)(void*))(l->fRoutine))(l->fArgument);
164 *main = l->fNext; delete l;
165 return 0;
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Default thread cleanup routine.
170
172{
173 if (gDebug > 0)
174 Info("Cleanup", "cleanup 0x%lx", (Long_t)*main);
175 while (!CleanUpPop(main, 1)) { }
176 return 0;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Return the thread identifier for the calling thread.
181
183{
184 return (Long_t) pthread_self();
185}
186
187// Clean Up section. PTHREAD implementations of cleanup after cancel are
188// too different and often too bad. Temporary I invent my own bicycle.
189// V.Perev.
190
191////////////////////////////////////////////////////////////////////////////////
192///cleanup function
193
194TPosixThreadCleanUp::TPosixThreadCleanUp(void **main, void *routine, void *arg)
195{
197 fRoutine = routine; fArgument = arg;
198 *main = this;
199}
long Long_t
Definition RtypesCore.h:54
#define ClassImp(name)
Definition Rtypes.h:364
XFontStruct * id
Definition TGX11.cxx:109
Int_t gDebug
Definition TROOT.cxx:590
typedef void((*Func_t)())
#define free
Definition civetweb.c:1539
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:893
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:867
TPosixThreadCleanUp(void **main, void *routine, void *arg)
cleanup function
TPosixThreadCleanUp * fNext
virtual Int_t SetCancelDeferred()
Set the cancellation response type of the calling thread to deferred, i.e.
virtual Int_t SetCancelOff()
Turn off the cancellation state of the calling thread.
virtual Int_t CleanUp(void **main)
Default thread cleanup routine.
virtual Int_t Kill(TThread *th)
Cancellation is the mechanism by which a thread can terminate the execution of another thread.
virtual Int_t SetCancelOn()
Turn on the cancellation state of the calling thread.
virtual Int_t SetCancelAsynchronous()
Set the cancellation response type of the calling thread to asynchronous, i.e.
virtual Int_t CancelPoint()
Introduce an explicit cancellation point. Returns 0.
virtual Int_t Join(TThread *th, void **ret)
Join suspends the execution of the calling thread until the thread identified by th terminates,...
virtual Int_t CleanUpPush(void **main, void *free, void *arg)
Add thread cleanup function.
virtual Int_t Exit(void *ret)
Terminates the execution of the calling thread. Return 0.
virtual Long_t SelfId()
Return the thread identifier for the calling thread.
virtual Int_t Run(TThread *th)
Create a pthread.
virtual Int_t CleanUpPop(void **main, Int_t exe)
Pop thread cleanup function from stack.
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
int main()
auto * l
Definition textangle.C:4