CLAM-Development  1.4.0
Condition.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "Condition.hxx"
23 #include "Thread.hxx"
24 #include "ErrSystem.hxx"
25 #include "Assert.hxx"
26 #include "xtime.hxx"
27 #include <errno.h>
28 #include <cstdio>
29 #include <sstream>
30 
31 namespace CLAM
32 {
33 
35  {
36  int res = 0;
37 
38  res = pthread_cond_init( &mCondition, 0 );
39 
40  CLAM_ASSERT( res == 0, "pthread_cond_init call failed" );
41  }
42 
44  {
45  int res = 0;
46 
47  res = pthread_cond_destroy( &mCondition );
48 
49  CLAM_ASSERT( res==0, "pthread_cond_destroy call failed" );
50  }
51 
53  {
54  int res = 0;
55 
56  res = pthread_cond_broadcast( &mCondition );
57 
58  CLAM_ASSERT( res==0, "pthread_cond_broadcast call failed!" );
59  }
60 
62  {
63  int res = 0;
64 
65  res = pthread_cond_signal( &mCondition );
66 
67  CLAM_ASSERT( res == 0, "pthread_cond_signal call failed" );
68  }
69 
70  void Condition::DoWait( pthread_mutex_t* pmutex )
71  {
72  int res = 0;
73 
74  res = pthread_cond_wait( &mCondition, pmutex );
75 
76  CLAM_ASSERT( res == 0, "pthread_cond_wait call failed" );
77  }
78 
79  bool Condition::DoTimedWait( const xtime& xt, pthread_mutex_t* pmutex )
80  {
81  timespec ts;
82 
83  to_timespec( xt, ts );
84 
85  int res = 0;
86 
87  res = pthread_cond_timedwait( &mCondition, pmutex, &ts );
88 
89  std::ostringstream os;
90  os << "Something strange has happened. PThread returned from timed wait with result code " << res << std::flush;
91  CLAM_ASSERT( res == 0 || res == ETIMEDOUT , os.str().c_str() );
92 
93  return res != ETIMEDOUT;
94  }
95 
96 } // end of namespace CLAM
97