Back to index

php5  5.3.10
fpm_clock.c
Go to the documentation of this file.
00001 
00002        /* $Id: fpm_clock.c,v 1.4 2008/09/18 23:19:59 anight Exp $ */
00003        /* (c) 2007,2008 Andrei Nigmatulin */
00004 
00005 #include "fpm_config.h"
00006 
00007 #if defined(HAVE_CLOCK_GETTIME)
00008 #include <time.h> /* for CLOCK_MONOTONIC */
00009 #endif
00010 
00011 #include "fpm_clock.h"
00012 #include "zlog.h"
00013 
00014 
00015 /* posix monotonic clock - preferred source of time */
00016 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
00017 
00018 static int monotonic_works;
00019 
00020 int fpm_clock_init() /* {{{ */
00021 {
00022        struct timespec ts;
00023 
00024        monotonic_works = 0;
00025 
00026        if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) {
00027               monotonic_works = 1;
00028        }
00029 
00030        return 0;
00031 }
00032 /* }}} */
00033 
00034 int fpm_clock_get(struct timeval *tv) /* {{{ */
00035 {
00036        if (monotonic_works) {
00037               struct timespec ts;
00038 
00039               if (0 > clock_gettime(CLOCK_MONOTONIC, &ts)) {
00040                      zlog(ZLOG_SYSERROR, "clock_gettime() failed");
00041                      return -1;
00042               }
00043 
00044               tv->tv_sec = ts.tv_sec;
00045               tv->tv_usec = ts.tv_nsec / 1000;
00046               return 0;
00047        }
00048 
00049        return gettimeofday(tv, 0);
00050 }
00051 /* }}} */
00052 
00053 /* macosx clock */
00054 #elif defined(HAVE_CLOCK_GET_TIME)
00055 
00056 #include <mach/mach.h>
00057 #include <mach/clock.h>
00058 #include <mach/mach_error.h>
00059 
00060 static clock_serv_t mach_clock;
00061 
00062 /* this code borrowed from here: http://lists.apple.com/archives/Darwin-development/2002/Mar/msg00746.html */
00063 /* mach_clock also should be re-initialized in child process after fork */
00064 int fpm_clock_init() /* {{{ */
00065 {
00066        kern_return_t ret;
00067        mach_timespec_t aTime;
00068 
00069        ret = host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &mach_clock);
00070 
00071        if (ret != KERN_SUCCESS) {
00072               zlog(ZLOG_ERROR, "host_get_clock_service() failed: %s", mach_error_string(ret));
00073               return -1;
00074        }
00075 
00076        /* test if it works */
00077        ret = clock_get_time(mach_clock, &aTime);
00078 
00079        if (ret != KERN_SUCCESS) {
00080               zlog(ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
00081               return -1;
00082        }
00083 
00084        return 0;
00085 }
00086 /* }}} */
00087 
00088 int fpm_clock_get(struct timeval *tv) /* {{{ */
00089 {
00090        kern_return_t ret;
00091        mach_timespec_t aTime;
00092 
00093        ret = clock_get_time(mach_clock, &aTime);
00094 
00095        if (ret != KERN_SUCCESS) {
00096               zlog(ZLOG_ERROR, "clock_get_time() failed: %s", mach_error_string(ret));
00097               return -1;
00098        }
00099 
00100        tv->tv_sec = aTime.tv_sec;
00101        tv->tv_usec = aTime.tv_nsec / 1000;
00102 
00103        return 0;
00104 }
00105 /* }}} */
00106 
00107 #else /* no clock */
00108 
00109 int fpm_clock_init() /* {{{ */
00110 {
00111        return 0;
00112 }
00113 /* }}} */
00114 
00115 int fpm_clock_get(struct timeval *tv) /* {{{ */
00116 {
00117        return gettimeofday(tv, 0);
00118 }
00119 /* }}} */
00120 
00121 #endif