caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* pthread_getcpuclockid et al
@ 2006-05-24 15:51 Sam Steingold
  0 siblings, 0 replies; only message in thread
From: Sam Steingold @ 2006-05-24 15:51 UTC (permalink / raw)
  To: caml-list

How do I time an individual Ocaml thread?

There appears to be no interface to pthread_getcpuclockid() and
clock_gettime() and it is not obvious from
ocaml/otherlibs/systhreads/posix.c how to get the pthread_t of a given
Ocaml Thread.t object (caml_thread_descr does not point to a
caml_thread_struct).

here is what I have so far:
=========================== pth_clock.mli
type clock
external getcpuclock: Thread.t -> clock = "getcpuclock"
=========================== pth_clock.mli
=========================== pth_clock.ml
external getcpuclock : Thread.t -> clock = "getcpuclock"
=========================== pth_clock.ml
=========================== pth_clock.c
#include <pthread.h>
#include <time.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/alloc.h>
#include <caml/custom.h>

/* see /usr/local/home/godi/godi/lib/godi/saved-src/ocaml.tar.gz
   ocaml/otherlibs/systhreads/posix.c */
extern pthread_t tid_to_pth (value tid);/* how do I define this??? */

/* Encapsulation of clockid_t as Caml custom blocks. */
#define clock_val(v) (*((clockid_t **) Data_custom_val(v)))

void clock_finalize (value v) { free(clock_val(v)); }

static struct custom_operations clockid_ops = {
  "caml.clockid_t",
  &clock_finalize,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

/* Allocating a Caml custom block to hold the given clockid_t * */
static value alloc_clock (clockid_t * c) {
  value v = alloc_custom(&clockid_ops, sizeof(clockid_t *), 0, 1);
  clock_val(v) = c;
  return v;
}

CAMLprim value getcpuclock (value tid) {
  CAMLparam1(tid);
  clockid_t *c = malloc(sizeof(clockid_t));
  if (pthread_getcpuclockid(tid_to_pth(tid),c) == 0)
    CAMLreturn(alloc_clock(c));
  else {
    free(c);
    caml_failwith("pthread_getcpuclockid");
  }
}
=========================== pth_clock.c

Help?

Thanks.

-- 
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://pmw.org.il http://openvotingconsortium.org http://dhimmi.com
http://thereligionofpeace.com http://palestinefacts.org http://ffii.org
Why do we want intelligent terminals when there are so many stupid users?


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2006-05-24 15:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-24 15:51 pthread_getcpuclockid et al Sam Steingold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).