Hi all,
Correction of 3 of 8) Add direct_util_monotonic_pthread_cond_init and
direct_util_get_monotonic_pthread_timeout interface
So pthread_cond_timedwait will not
affected by system date/time changes.
Previous one is wrong, Sequence of pthread_condattr_init in
direct_util_monotonic_pthread_cond_init is wrong for obvious reason.
Sorry for the noise.
Regards,
diff --git a/lib/direct/util.c b/lib/direct/util.c
index fd245b9..3401948 100644
--- a/lib/direct/util.c
+++ b/lib/direct/util.c
@@ -37,6 +37,11 @@
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/util.h>
+#include <direct/clock.h>
+
+#include <sys/syscall.h>
+
+static int no_monotonic_pthread_clock;
/*
* translates errno to DirectResult
@@ -243,6 +248,60 @@ direct_util_recursive_pthread_mutex_init(
pthread_mutex_t *mutex )
}
/*
+ * Utility function to initialize monotonic condition.
+ */
+int
+direct_util_monotonic_pthread_cond_init( pthread_cond_t *cond )
+{
+ int ret;
+ pthread_condattr_t attr;
+
+#ifdef CLOCK_MONOTONIC
+ struct timespec dummy;
+ pthread_condattr_init( &attr );
+
+ if(!no_monotonic_pthread_clock) {
+ if((syscall( __NR_clock_getres, CLOCK_MONOTONIC, &dummy ) ==
0) &&
+ (pthread_condattr_setclock( &attr, CLOCK_MONOTONIC ) == 0))
+ ;
+ else
+ no_monotonic_pthread_clock = 1;
+ }
+#else
+ pthread_condattr_init( &attr );
+#endif
+ ret = pthread_cond_init( cond, &attr );
+ if (ret)
+ D_PERROR( "Direct/Lock: Could not initialize monotonic
condition!\n" );
+
+ pthread_condattr_destroy( &attr );
+
+ return ret;
+}
+
+/*
+ * Utility function to calibrate timeout for monotonic condition.
+ */
+void
+direct_util_get_monotonic_pthread_timeout( struct timespec *timeout,
+ int seconds,
+ int nano_seconds )
+{
+ struct timeval now;
+
+ if(no_monotonic_pthread_clock)
+ gettimeofday( &now, NULL );
+ else
+ direct_monotonic_gettimeofday( &now );
+
+ timeout->tv_sec = now.tv_sec + seconds;
+ timeout->tv_nsec = (now.tv_usec * 1000) + nano_seconds;
+
+ timeout->tv_sec += timeout->tv_nsec / 1000000000;
+ timeout->tv_nsec %= 1000000000;
+}
+
+/*
* Encode/Decode Base-64.
*/
char*
diff --git a/lib/direct/util.h b/lib/direct/util.h
index f8b1f00..521763d 100644
--- a/lib/direct/util.h
+++ b/lib/direct/util.h
@@ -225,6 +225,17 @@ direct_util_align( int value,
*/
int direct_util_recursive_pthread_mutex_init( pthread_mutex_t *mutex );
+/*
+ * Utility function to calibrate timeout for monotonic condition.
+ */
+void direct_util_get_monotonic_pthread_timeout( struct timespec *timeout,
+ int seconds,
+ int nano_seconds );
+
+/*
+ * Utility function to initialize monotonic condition.
+ */
+int direct_util_monotonic_pthread_cond_init( pthread_cond_t *cond );
/* floor and ceil implementation to get rid of libm */
_______________________________________________
directfb-dev mailing list
[email protected]
http://mail.directfb.org/cgi-bin/mailman/listinfo/directfb-dev