xthread.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 //---------------------------------------------------------------------------
15 
16 #include "xthread.h"
17 
18 //---------------------------------------------------------------------------
19 
20 #ifdef USE_QTHREAD
21 
22 XMutex::XMutex() {}
23 XMutex::~XMutex() {}
24 
25 void XMutex::lock() {m_mutex.lock();}
26 
27 void XMutex::unlock() {m_mutex.unlock();}
28 
29 bool XMutex::trylock() {return m_mutex.tryLock();}
30 
31 XCondition::XCondition() : XMutex() {
32 }
33 XCondition::~XCondition() {
34 }
35 
36 int XCondition::wait(int usec) {
37  bool ret;
38  if(usec)
39  ret = m_cond.wait( &m_mutex, usec / 1000);
40  else
41  ret = m_cond.wait( &m_mutex);
42  return ret ? 0 : 1;
43 }
44 
46  m_cond.wakeOne();
47 }
48 
50  m_cond.wakeAll();
51 }
52 
53 
54 #else
55  #ifdef USE_PTHREAD
56 
57  #include <assert.h>
58  #include <errno.h>
59  #include <algorithm>
60  #include <sys/time.h>
61 
62  XMutex::XMutex() {
63  pthread_mutexattr_t attr;
64  int ret;
65  ret = pthread_mutexattr_init( &attr);
66  if(DEBUG_XTHREAD) assert( !ret);
67 
68  ret = pthread_mutex_init( &m_mutex, &attr);
69  if(DEBUG_XTHREAD) assert( !ret);
70 
71  ret = pthread_mutexattr_destroy( &attr);
72  if(DEBUG_XTHREAD) assert( !ret);
73  }
74 
75  XMutex::~XMutex() {
76  int ret = pthread_mutex_destroy( &m_mutex);
77  if(DEBUG_XTHREAD) assert( !ret);
78  }
79  void
80  XMutex::lock() {
81  int ret = pthread_mutex_lock( &m_mutex);
82  if(DEBUG_XTHREAD) assert( !ret);
83  }
84  bool
85  XMutex::trylock() {
86  int ret = pthread_mutex_trylock(&m_mutex);
87  if(DEBUG_XTHREAD) assert(ret != EINVAL);
88  return (ret == 0);
89  }
90  void
91  XMutex::unlock() {
92  int ret = pthread_mutex_unlock( &m_mutex);
93  if(DEBUG_XTHREAD) assert( !ret);
94  }
95 
96  XCondition::XCondition() : XMutex() {
97  int ret = pthread_cond_init( &m_cond, NULL);
98  if(DEBUG_XTHREAD) assert( !ret);
99  }
100  XCondition::~XCondition() {
101  int ret = pthread_cond_destroy( &m_cond);
102  if(DEBUG_XTHREAD) assert( !ret);
103  }
104  int
105  XCondition::wait(int usec) {
106  int ret;
107  if(usec > 0) {
108  struct timespec abstime;
109  timeval tv;
110  long nsec;
111  gettimeofday(&tv, NULL);
112  abstime.tv_sec = tv.tv_sec;
113  nsec = (tv.tv_usec + usec) * 1000;
114  if(nsec >= 1000000000) {
115  nsec -= 1000000000; abstime.tv_sec++;
116  }
117  abstime.tv_nsec = nsec;
118  ret = pthread_cond_timedwait(&m_cond, &m_mutex, &abstime);
119  }
120  else {
121  ret = pthread_cond_wait(&m_cond, &m_mutex);
122  }
123  return ret;
124  }
125  void
127  int ret = pthread_cond_signal( &m_cond);
128  if(DEBUG_XTHREAD) assert( !ret);
129  }
130  void
132  int ret = pthread_cond_broadcast( &m_cond);
133  if(DEBUG_XTHREAD) assert( !ret);
134  }
135  #endif // USE_PTHREAD
136 #endif // USE_QTHREAD

Generated for KAME4 by  doxygen 1.8.3