Salome HOME
7e6133cc4fc4d446f7849af9f0d02c0bf9d4dd51
[modules/yacs.git] / src / bases / ThreadPT.cxx
1 // Copyright (C) 2006-2012  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.
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 WNT
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
31 ThreadPT::ThreadPT(ThreadJob funcPtr, void *stack)
32 {
33   int err;
34   void **stackT=(void **) stack;
35   err=pthread_create(&_threadId,0,funcPtr,stackT);
36   if(err!=0)throw Exception("Error in thread creation");
37 }
38
39 bool ThreadPT::operator==(const ThreadPT& other)
40 {
41   return pthread_equal(_threadId, other._threadId) != 0;
42 }
43
44 //! Detach thread to release resources on exit
45 void ThreadPT::detach()
46 {
47   pthread_detach(pthread_self());
48 }
49
50 void ThreadPT::exit(void *what)
51 {
52   pthread_exit(what);
53 }
54
55 void ThreadPT::join()
56 {
57   void *ret;
58   pthread_join(_threadId, &ret);
59 }
60
61 void ThreadPT::sleep(unsigned long usec)
62 {
63   usleep(usec);
64 }