Changeset 408 for trunk/src/sh_unix.c


Ignore:
Timestamp:
Aug 23, 2012, 8:52:20 PM (12 years ago)
Author:
katerina
Message:

Fix for ticket #311 (Thread safety of --enable-ptrace).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_unix.c

    r402 r408  
    54015401
    54025402#ifdef HAVE_GETTIMEOFDAY
    5403 struct timeval  save_tv;
     5403
     5404#if defined(HAVE_PTHREAD)
     5405
     5406static pthread_key_t  gSaveTv_key;
     5407static pthread_once_t gSaveTv_key_once = PTHREAD_ONCE_INIT;
     5408
     5409static inline void make_gSaveTv_key()
     5410{
     5411    (void) pthread_key_create(&gSaveTv_key, free);
     5412}
     5413
     5414static inline struct timeval * sh_get_save_tv()
     5415{
     5416  void * ptr;
     5417  struct timeval * save_tv;
     5418
     5419  (void) pthread_once(&gSaveTv_key_once, make_gSaveTv_key);
     5420 
     5421  if ((ptr = pthread_getspecific(gSaveTv_key)) == NULL)
     5422    {
     5423      ptr = malloc(sizeof(struct timeval));
     5424      if (ptr)
     5425        {
     5426          save_tv  = (struct timeval*) ptr;
     5427          (void) pthread_setspecific(gSaveTv_key, ptr);
     5428        }
     5429      else
     5430        {
     5431          return NULL;
     5432        }
     5433    }
     5434  else
     5435    {
     5436      save_tv  = (struct timeval*) ptr;
     5437    }
     5438  return save_tv;
     5439}
     5440
     5441void sh_set_save_tv()
     5442{
     5443  struct timeval * save_tv = sh_get_save_tv();
     5444  if (save_tv)
     5445    gettimeofday(save_tv, NULL);
     5446  return;
     5447}
     5448
     5449/* !defined(HAVE_PTHREAD) */
     5450#else
     5451
     5452static struct timeval * sSaveTv = NULL;
     5453
     5454static inline struct timeval * sh_get_save_tv()
     5455{
     5456  return sSaveTv;
     5457}
     5458void sh_set_save_tv()
     5459{
     5460  gettimeofday(sSaveTv, NULL);
     5461}
     5462#endif
     5463
     5464/* #ifdef HAVE_GETTIMEOFDAY */
    54045465#endif
    54055466
     
    54095470  struct timeval  tv;
    54105471  long   difftv;
     5472  struct timeval * save_tv = sh_get_save_tv();
     5473
     5474  gettimeofday(&tv, NULL);
    54115475 
    5412   gettimeofday(&tv, NULL);
    5413   difftv = (tv.tv_sec - save_tv.tv_sec) * 1000000 +
    5414     (tv.tv_usec - save_tv.tv_usec);
     5476  difftv = (tv.tv_sec - save_tv->tv_sec) * 1000000 +
     5477    (tv.tv_usec - save_tv->tv_usec);
    54155478  if (difftv > 500000)
    54165479    raise(SIGKILL);
Note: See TracChangeset for help on using the changeset viewer.