Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

/ray/src/lib/threads/condition.cc

Go to the documentation of this file.
00001 /*
00002  * lib/threads/condition.cc
00003  * 
00004  * Implementing a thread condition; an object that threads can block 
00005  * on, if they find a certain condition to be false. 
00006  * 
00007  * Copyright (c) 2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 
00008  * 
00009  * This file may be distributed and/or modified under the terms of the 
00010  * GNU General Public License version 2 as published by the Free Software 
00011  * Foundation. (See COPYING.GPL for details.)
00012  * 
00013  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00014  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00015  * 
00016  */
00017 
00018 #include "condition.h"
00019 
00020 #if USE_PRIVATE__PTHREADS
00021 #if TIME_WITH_SYS_TIME
00022 # include <sys/time.h>
00023 # include <time.h>
00024 #else
00025 # if HAVE_SYS_TIME_H
00026 #  include <sys/time.h>
00027 # else
00028 #  include <time.h>
00029 # endif
00030 #endif
00031 #else  /* !USE_PRIVATE__PTHREADS */
00032 # include <glib/gmain.h>   /* for g_get_current_time */
00033 # include <glib/gtimer.h>  /* for g_time_val_add */
00034 #endif  /* USE_PRIVATE__PTHREADS */
00035 
00036 
00037 inline int WaitCondition::wait(ConditionMutex &m,long msec)
00038 {
00039     if(msec<0)  msec=0;
00040     
00041 #if USE_PRIVATE__PTHREADS
00042     // Compute absolute time of wait endtime: 
00043     timeval tv;
00044     gettimeofday(&tv,NULL);
00045     tv.tv_sec+=msec/1000;
00046     tv.tv_usec+=(msec%1000)*1000;
00047     if(tv.tv_usec>1000000)
00048     {  tv.tv_usec-=1000000;  ++tv.tv_sec;  }
00049     
00050     timespec tspec;
00051     tspec.tv_sec=tv.tv_sec;
00052     tspec.tv_nsec=1000*tv.tv_usec;
00053     
00054     // Actually wait: 
00055     switch(pthread_cond_timedwait(&c,&m.m,&tspec))
00056     {
00057         case ETIMEDOUT:  return(1);
00058         case EINTR:      return(2);
00059     }
00060     return(0);
00061 #else
00062     GTimeVal tv;
00063     g_get_current_time(&tv);
00064     g_time_val_add(&tv,1000*msec);
00065     if(g_cond_timed_wait(c,m.m,&tv))
00066     {  /* "woken up in time" */  return(0);  }
00067     return(1);
00068 #endif
00069 }
00070 
00071 
00072 WaitCondition::WaitCondition()
00073 {
00074 #if USE_PRIVATE__PTHREADS
00075     //pthread_condattr_t attr;
00076     //pthread_condattr_init(&attr);
00077     pthread_cond_init(&c,/*&attr*/NULL);  // <-- NULL = "all defaults"
00078     //pthread_condattr_destroy(&attr);
00079 #else
00080     // We must make sure that g_thread_init() has been called before. 
00081     c=g_cond_new();
00082 #endif
00083 }
00084 
00085 WaitCondition::~WaitCondition()
00086 {
00087 #if USE_PRIVATE__PTHREADS
00088     pthread_cond_destroy(&c);
00089 #else
00090     // We must make sure that g_thread_init() has been called before. 
00091     g_cond_free(c);
00092 #endif
00093 }

Generated on Sat Feb 19 22:33:46 2005 for Ray by doxygen 1.3.5