mt_extracttest1.c

Go to the documentation of this file.
00001 /**
00002  * @file test/mt_extracttest1.c
00003  * @brief test keyword extraction from multiple threads simultaneously
00004  * @author Heikki Lindholm
00005  */
00006 #include "platform.h"
00007 #include "extractor.h"
00008 #include <pthread.h>
00009 
00010 struct TaskData
00011 {
00012   int id;
00013   const char *filename;
00014 };
00015 
00016 static volatile int done = 0;
00017 static volatile int failed = 0;
00018 
00019 pthread_mutex_t reference_lock = PTHREAD_MUTEX_INITIALIZER;
00020 static EXTRACTOR_KeywordList *reference_list;
00021 
00022 static int
00023 compare_keywords_to_ref (EXTRACTOR_KeywordList * list)
00024 {
00025   EXTRACTOR_KeywordList *ptr1, *ptr2;
00026   unsigned int cnt;
00027   int *match;
00028   int i;
00029 
00030   cnt = EXTRACTOR_countKeywords (list);
00031 
00032   pthread_mutex_lock (&reference_lock);
00033 
00034   if (cnt != EXTRACTOR_countKeywords (reference_list))
00035     {
00036       pthread_mutex_unlock (&reference_lock);
00037       return -1;
00038     }
00039 
00040   match = alloca (cnt * sizeof (int));
00041   memset (match, 0x00, cnt * sizeof (int));
00042   ptr1 = list;
00043   while (ptr1 != NULL)
00044     {
00045       int found;
00046       found = 0;
00047       ptr2 = reference_list;
00048       i = 0;
00049       while (ptr2 != NULL)
00050         {
00051           if (ptr2->keywordType == ptr1->keywordType &&
00052               strcmp (ptr2->keyword, ptr1->keyword) == 0 && match[i] == 0)
00053             {
00054               found = 1;
00055               match[i] = 1;
00056               break;
00057             }
00058           i++;
00059           ptr2 = ptr2->next;
00060         }
00061       if (found == 0)
00062         break;
00063       ptr1 = ptr1->next;
00064     }
00065 
00066   pthread_mutex_unlock (&reference_lock);
00067   for (i = 0; i < cnt; i++)
00068     if (match[i] == 0)
00069       return -1;
00070 
00071   return 0;
00072 }
00073 
00074 static EXTRACTOR_KeywordList *
00075 get_keywords_for_file (const char *filename)
00076 {
00077   EXTRACTOR_ExtractorList *el;
00078   EXTRACTOR_KeywordList *list;
00079 
00080   el = EXTRACTOR_loadDefaultLibraries ();
00081   if (el == NULL)
00082     {
00083       printf ("ERROR: failed to load plugins!\n");
00084       return NULL;
00085     }
00086   errno = 0;
00087   list = EXTRACTOR_getKeywords (el, filename);
00088   if (errno != 0) {
00089     printf("ERROR: EXTRACTOR_getKeywords: %s\n", strerror(errno));
00090   }
00091   /*EXTRACTOR_printKeywords (stderr, list); */
00092   EXTRACTOR_removeAll (el);
00093 
00094   return list;
00095 }
00096 
00097 static void *
00098 test_plugins (void *arg)
00099 {
00100   struct TaskData *td = (struct TaskData *)arg;
00101   while (!done)
00102     {
00103       EXTRACTOR_KeywordList *list;
00104 
00105       list = get_keywords_for_file (td->filename);
00106 
00107       if ((list == NULL) || (compare_keywords_to_ref (list) != 0))
00108         {
00109           printf ("ERROR: thread id %d failed keyword comparison!\n", td->id);
00110           failed = 1;
00111         }
00112       if (list != NULL)
00113         EXTRACTOR_freeKeywords (list);
00114     }
00115   return 0;
00116 }
00117 
00118 static const char *filename = TESTDATADIR "/text2.sxw";
00119 
00120 #define TEST_SECS 10
00121 
00122 int
00123 main (int argc, char *argv[])
00124 {
00125   int num_tasks = 10;
00126   pthread_t task_list[num_tasks];
00127   struct TaskData td[num_tasks];
00128   int ret = 0;
00129   int i;
00130 
00131   printf("testing with '%s' for %d seconds\n", filename, TEST_SECS);
00132   reference_list = get_keywords_for_file (filename);
00133 
00134     for (i = 0; i < num_tasks; i++)
00135       {
00136         td[i].id = i;
00137         td[i].filename = filename;
00138         ret = pthread_create (&task_list[i], NULL, test_plugins, &td[i]);
00139         if (ret != 0)
00140           {
00141             printf ("ERROR: pthread_create failed for thread %d\n", i);
00142             num_tasks = i;
00143             done = 1;
00144             break;
00145           }
00146       }
00147     if (!done)
00148       sleep (TEST_SECS);
00149     done = 1;
00150     for (i = 0; i < num_tasks; i++)
00151       {
00152         if (pthread_join (task_list[i], NULL) != 0)
00153           printf ("WARNING: pthread_join failed for thread %d\n", i);
00154       }
00155 
00156     if (reference_list != NULL)
00157       EXTRACTOR_freeKeywords (reference_list);
00158 
00159   return failed;
00160 }

Generated on Thu Nov 20 08:44:48 2008 for libextractor by  doxygen 1.5.1