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
31Int_t TPosixThread::Run(TThread *th, const int affinity)
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 if (affinity >= 0) {
40 #ifdef __GLIBC__
41 cpu_set_t cpuset;
42 CPU_ZERO(&cpuset);
43 CPU_SET(affinity, &cpuset);
44 pthread_attr_setaffinity_np(attr, sizeof(cpu_set_t), &cpuset);
45 #else
46 Warning("Run", "Affinity setting not yet implemented on this platform");
47 #endif
48 }
49
50 // Set detach state
51 det = (th->fDetached) ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE;
52
53 pthread_attr_setdetachstate(attr, det);
54
55 // See e.g. https://developer.apple.com/library/mac/#qa/qa1419/_index.html
56 // MacOS has only 512k of stack per thread; Linux has 2MB.
57 const size_t requiredStackSize = 1024*1024*2;
58 size_t stackSize = 0;
59 if (!pthread_attr_getstacksize(attr, &stackSize)
60 && stackSize < requiredStackSize) {
61 pthread_attr_setstacksize(attr, requiredStackSize);
62 }
63 int ierr = pthread_create(&id, attr, &TThread::Function, th);
64 if (!ierr) th->fId = (Long_t) id;
65
66 pthread_attr_destroy(attr);
67 delete attr;
68
69 return ierr;
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// Join suspends the execution of the calling thread until the
74/// thread identified by th terminates, either by calling pthread_exit
75/// or by being cancelled. Returns 0 on success, otherwise an error number will
76/// be returned.
77
79{
80 return pthread_join((pthread_t) th->fId, ret);
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Terminates the execution of the calling thread. Return 0.
85
87{
88 pthread_exit(ret);
89 return 0;
90}
91
92////////////////////////////////////////////////////////////////////////////////
93/// Cancellation is the mechanism by which a thread can terminate the
94/// execution of another thread. Returns 0 on success, otherwise an error
95/// number will be returned.
96
98{
99 return pthread_cancel((pthread_t) th->fId);
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Turn off the cancellation state of the calling thread. Returns 0 on
104/// success, otherwise an error number will be returned.
105
107{
108 return pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Turn on the cancellation state of the calling thread. Returns 0 on
113/// success, otherwise an error number will be returned.
114
116{
117 return pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Set the cancellation response type of the calling thread to
122/// asynchronous, i.e. cancel as soon as the cancellation request
123/// is received.
124
126{
127 return pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Set the cancellation response type of the calling thread to
132/// deferred, i.e. cancel only at next cancellation point.
133/// Returns 0 on success, otherwise an error number will be returned.
134
136{
137 return pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, 0);
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Introduce an explicit cancellation point. Returns 0.
142
144{
145 int istate;
146 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &istate);
147 pthread_testcancel();
148 pthread_setcancelstate(istate, 0);
149
150 return 0;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Add thread cleanup function.
155
156Int_t TPosixThread::CleanUpPush(void **main, void *free, void *arg)
157{
158 // pthread_cleanup_push(free, arg);
159 if (!free) Error("CleanUpPush", "cleanup rountine = 0");
160 new TPosixThreadCleanUp(main, free, arg);
161 return 0;
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Pop thread cleanup function from stack.
166
168{
169 // pthread_cleanup_pop(exe); // happy pthread future
170
171 if (!main || !*main) return 1;
173 if (!l->fRoutine) Error("CleanUpPop", "cleanup routine = 0");
174 if (exe && l->fRoutine) ((void (*)(void*))(l->fRoutine))(l->fArgument);
175 *main = l->fNext; delete l;
176 return 0;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Default thread cleanup routine.
181
183{
184 if (gDebug > 0)
185 Info("Cleanup", "cleanup 0x%lx", (Long_t)*main);
186 while (!CleanUpPop(main, 1)) { }
187 return 0;
188}
189
190////////////////////////////////////////////////////////////////////////////////
191/// Return the thread identifier for the calling thread.
192
194{
195 return (Long_t) pthread_self();
196}
197
198// Clean Up section. PTHREAD implementations of cleanup after cancel are
199// too different and often too bad. Temporary I invent my own bicycle.
200// V.Perev.
201
202////////////////////////////////////////////////////////////////////////////////
203///cleanup function
204
205TPosixThreadCleanUp::TPosixThreadCleanUp(void **main, void *routine, void *arg)
206{
208 fRoutine = routine; fArgument = arg;
209 *main = this;
210}
int main()
Definition Prototype.cxx:12
long Long_t
Definition RtypesCore.h:54
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
Int_t gDebug
Definition TROOT.cxx:597
#define free
Definition civetweb.c:1539
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:979
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:993
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:967
TPosixThreadCleanUp(void **main, void *routine, void *arg)
cleanup function
TPosixThreadCleanUp * fNext
Int_t SetCancelOn() override
Turn on the cancellation state of the calling thread.
Int_t SetCancelOff() override
Turn off the cancellation state of the calling thread.
Int_t SetCancelAsynchronous() override
Set the cancellation response type of the calling thread to asynchronous, i.e.
Int_t CleanUpPop(void **main, Int_t exe) override
Pop thread cleanup function from stack.
Int_t Kill(TThread *th) override
Cancellation is the mechanism by which a thread can terminate the execution of another thread.
Int_t CleanUpPush(void **main, void *free, void *arg) override
Add thread cleanup function.
Int_t CleanUp(void **main) override
Default thread cleanup routine.
Int_t Exit(void *ret) override
Terminates the execution of the calling thread. Return 0.
Int_t SetCancelDeferred() override
Set the cancellation response type of the calling thread to deferred, i.e.
Int_t Run(TThread *th, const int affinity=-1) override
Create a pthread.
Long_t SelfId() override
Return the thread identifier for the calling thread.
Int_t Join(TThread *th, void **ret) override
Join suspends the execution of the calling thread until the thread identified by th terminates,...
Int_t CancelPoint() override
Introduce an explicit cancellation point. Returns 0.
<div class="legacybox"><h2>Legacy Code</h2> TThread is a legacy interface: there will be no bug fixes...
Definition TThread.h:40
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:797
TLine l
Definition textangle.C:4