Version: 8.3.0
ThreadPT.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 
20 #include "ThreadPT.hxx"
21 #include "Exception.hxx"
22 #ifdef WIN32
23 #include <windows.h>
24 #define usleep(A) Sleep(A/1000)
25 #else
26 #include <unistd.h>
27 #endif
28 
29 using namespace YACS::BASES;
30 
32 {
33 }
34 
35 ThreadPT::ThreadPT(ThreadJob funcPtr, void *stack, size_t stackSize)
36 {
37  go(funcPtr,stack,stackSize);
38 }
39 
40 void ThreadPT::go(ThreadJob funcPtr, void *stack, size_t stackSize)
41 {
42  int err;
43  void **stackT=(void **) stack;
44  pthread_attr_t attr;
45  pthread_attr_init(&attr);
46  if (stackSize > 0)
47  {
48  err = pthread_attr_setstacksize(&attr, stackSize);
49  if (err != 0) throw Exception("Error when setting thread stack size");
50  }
51  err = pthread_create(&_threadId, &attr, funcPtr, stackT);
52  pthread_attr_destroy(&attr);
53  if(err!=0)throw Exception("Error in thread creation");
54 }
55 
56 bool ThreadPT::operator==(const ThreadPT& other)
57 {
58  return pthread_equal(_threadId, other._threadId) != 0;
59 }
60 
63 {
64  pthread_detach(pthread_self());
65 }
66 
67 void ThreadPT::exit(void *what)
68 {
69  pthread_exit(what);
70 }
71 
73 {
74  void *ret;
75  pthread_join(_threadId, &ret);
76 }
77 
78 void ThreadPT::sleep(unsigned long usec)
79 {
80  usleep(usec);
81 }