Branch: Tag:

2002-09-14

2002-09-14 02:58:49 by Martin Stjernholm <mast@lysator.liu.se>

Added fallback to clock() in check_threads if gethrtime doesn't exist, to
avoid very frequent context switches. Put the gethrtime check behind the
divisor check since gethrtime isn't entirely cheap.

Rev: src/threads.c:1.163
Rev: src/threads.h:1.115

1:   #include "global.h" - RCSID("$Id: threads.c,v 1.162 2002/06/17 15:44:13 grubba Exp $"); + RCSID("$Id: threads.c,v 1.163 2002/09/14 02:58:49 mast Exp $");      PMOD_EXPORT int num_threads = 1;   PMOD_EXPORT int threads_disabled = 0;
29:   PMOD_EXPORT COND_T threads_disabled_change;   PMOD_EXPORT size_t thread_stack_size=1024 * 1204;    + #ifdef USE_CLOCK_FOR_SLICES + PMOD_EXPORT clock_t thread_start_clock = 0; + #endif +    #ifndef HAVE_PTHREAD_ATFORK   #include "callback.h"   #define PIKE_USE_OWN_ATFORK
543:      static void check_threads(struct callback *cb, void *arg, void * arg2)   { - #ifdef HAVE_GETHRTIME -  static long long last_; -  if( gethrtime()-last_ < 50000000 ) /* 0.05s slice */ -  return; -  last_ = gethrtime(); - #else +     static int div_;    if(div_++ & 255)    return; -  + #ifdef HAVE_GETHRTIME +  { +  static hrtime_t last_; +  hrtime_t now = gethrtime(); +  if( now-last_ < 50000000 ) /* 0.05s slice */ +  return; +  last_ = now; +  } + #elif defined(USE_CLOCK_FOR_SLICES) +  if (clock() - thread_start_clock < (clock_t) (CLOCKS_PER_SEC / 20)) +  return;   #endif      #ifdef DEBUG
569:   #endif    THREADS_DISALLOW();    + #ifdef USE_CLOCK_FOR_SLICES +  /* Must set the base time for the slice here since clock() returns +  * thread local time. */ +  thread_start_clock = clock(); + #endif +    #ifdef DEBUG    if(thread_for_id(th_self()) != Pike_interpreter.thread_id)    fatal("thread_for_id() (or Pike_interpreter.thread_id) failed!\n")