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