Changeset 134 for trunk/src/samhain.c


Ignore:
Timestamp:
Oct 26, 2007, 12:20:10 AM (17 years ago)
Author:
rainer
Message:

More thread-safety changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/samhain.c

    r133 r134  
    138138#endif
    139139
     140#ifdef HAVE_PTHREAD
     141typedef struct gt {
     142  size_t g_count;
     143  char * g_glob;
     144};
     145
     146pthread_key_t g_key;
     147
     148int sh_g_thread()
     149{
     150  struct gt * ptr = malloc(sizeof(struct gt));
     151  if (!gt)
     152    return -1;
     153  ptr->g_count    = 0;
     154  ptr->g_glob     = calloc(1, SH_MAX_GLOBS * (GLOB_LEN+1));
     155  if (!(ptr->g_glob))
     156    return -1;
     157  return pthread_setspecific(g_key, ptr);
     158}
     159
     160void sh_g_destroy(void * data)
     161{
     162  struct gt * ptr = (struct gt *) data;
     163  free(ptr->g_glob);
     164  free(ptr);
     165  return;
     166}
     167
     168static void sh_g_init()
     169{
     170  if (0 != pthread_key_create(&g_key, sh_g_destroy))
     171    {
     172      perror("1");
     173      exit(EXIT_FAILURE);
     174    }
     175
     176  if (0 != sh_g_thread())
     177    {
     178      perror("1");
     179      exit(EXIT_FAILURE);
     180    }
     181  return;
     182}
     183#define SH_G_INIT sh_g_init()
     184#else
     185#define SH_G_INIT ((void)0)
     186#endif
     187
    140188char * globber(const char * str)
    141189{
     
    143191  size_t j;
    144192
    145   static   size_t  items = 0;
     193#ifdef HAVE_PTHREAD
     194  struct gt * ptr = pthread_getspecific(g_key);
     195  size_t count = ptr->g_count;
     196  char *  glob = ptr->g_glob;
     197#else
    146198  static   size_t  count = 0;
    147199  static   char glob[SH_MAX_GLOBS * (GLOB_LEN+1)];
     200#
    148201
    149202  if (str == NULL)
     
    152205    j = strlen(str);
    153206
    154   ++items;
    155 
    156207  ASSERT((j <= GLOB_LEN), _("j <= GLOB_LEN"))
    157208
     
    164215    {
    165216      count = 0;
    166       items = 0;
    167217    }
    168218
     
    177227
    178228  i     = count;
     229#ifdef HAVE_PTHREAD
     230  ptr->count = count + j + 1;
     231#else
    179232  count = count + j + 1;
     233#endif
    180234  return &glob[i];
    181235}
     
    11441198  char  * my_argv[32];
    11451199#endif
     1200
     1201  SH_G_INIT; /* Must precede any use of _() */
    11461202
    11471203  SL_ENTER(_("main"));
Note: See TracChangeset for help on using the changeset viewer.