[racket] FFI question: 0 microseconds returned from gettimeofday call

From: keydana at gmx.de (keydana at gmx.de)
Date: Thu Jul 14 15:48:52 EDT 2011


I am kind of stuck trying to get the current microsecond from gettimeofday... and I wonder how best to systematically approach the question of what racket datatype to choose when defining a FFI function.

>From the man page I see that timeval is defined as

struct timeval {
             time_t       tv_sec;   /* seconds since Jan. 1, 1970 */
             suseconds_t  tv_usec;  /* and microseconds */

but the search for suseconds_t - in case of time_t a first attempt at just using _int in racket worked fine - left me searching through a bunch of header files, following one typedef after the other...

In fact I'd assume that using _int for suseconds_t should work fine, too, given that man even tells us that

"The values in timeval are opaque types whose length may vary on different machines; depending on
     them to have any given length may lead to errors."

So, I wonder if there is a different kind of error in my code:

(define libsys (ffi-lib "libsystem"))
(define-cstruct _timeval ((secs _int) (μsecs _int)))
(define-cstruct _timezone ((minswest _int) (dsttime _int)))
(define gettimeofday (get-ffi-obj "gettimeofday" libsys (_fun (tm : (_ptr o _timeval)) (tz : (_ptr o _timezone)) -> _int -> (values (timeval-secs tm) (timeval-μsecs tm) (timezone-minswest tz) (timezone-dsttime tz)))))

leading to the output


with a 0 in the microsecond field...? I'd be grateful for any hints!
Also, I am confused by the coexistence of _xxx.h and xxx.h header files (like sys/types.h and _types.h), and I wonder what is the signification of single and double underscores in variables defined in the header files - could anyone perhaps point me to a resource documenting these conventions?

Many thanks in advance!

Posted on the users mailing list.