home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / sharewar / quake106 / utils / common / threads.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-12  |  3.9 KB  |  240 lines

  1.  
  2. #include "cmdlib.h"
  3. #include "threads.h"
  4.  
  5. #define    MAX_THREADS    64
  6.  
  7. int        dispatch;
  8. int        workcount;
  9. int        oldf;
  10. qboolean        pacifier;
  11.  
  12. /*
  13. =============
  14. GetThreadWork
  15.  
  16. =============
  17. */
  18. int    GetThreadWork (void)
  19. {
  20.     int    r;
  21.     int    f;
  22.  
  23.     ThreadLock ();
  24.  
  25.     if (dispatch == workcount)
  26.     {
  27.         ThreadUnlock ();
  28.         return -1;
  29.     }
  30.  
  31.     f = 10*dispatch / workcount;
  32.     if (f != oldf)
  33.     {
  34.         oldf = f;
  35.         if (pacifier)
  36.             printf ("%i...", f);
  37.     }
  38.  
  39.     r = dispatch;
  40.     dispatch++;
  41.     ThreadUnlock ();
  42.  
  43.     return r;
  44. }
  45.  
  46.  
  47.  
  48. /*
  49. ===================================================================
  50.  
  51. WIN32
  52.  
  53. ===================================================================
  54. */
  55. #ifdef WIN32
  56.  
  57. #define    USED
  58.  
  59. #include <windows.h>
  60.  
  61. int        numthreads = 1;
  62. CRITICAL_SECTION        crit;
  63.  
  64. void ThreadLock (void)
  65. {
  66.     EnterCriticalSection (&crit);
  67. }
  68.  
  69. void ThreadUnlock (void)
  70. {
  71.     LeaveCriticalSection (&crit);
  72. }
  73.  
  74. /*
  75. =============
  76. RunThreadsOn
  77. =============
  78. */
  79. void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
  80. {
  81.     int        threadid[MAX_THREADS];
  82.     HANDLE    threadhandle[MAX_THREADS];
  83.     int        i;
  84.  
  85.     dispatch = 0;
  86.     workcount = workcnt;
  87.     oldf = -1;
  88.     pacifier = showpacifier;
  89.  
  90.     //
  91.     // run threads in parallel
  92.     //
  93.     InitializeCriticalSection (&crit);
  94.     for (i=0 ; i<numthreads ; i++)
  95.     {
  96.         threadhandle[i] = CreateThread(
  97.            NULL,    // LPSECURITY_ATTRIBUTES lpsa,
  98.            0,        // DWORD cbStack,
  99.            (LPTHREAD_START_ROUTINE)func,    // LPTHREAD_START_ROUTINE lpStartAddr,
  100.            (LPVOID)i,    // LPVOID lpvThreadParm,
  101.            0,            //   DWORD fdwCreate,
  102.            &threadid[i]);
  103.     }
  104.  
  105.     for (i=0 ; i<numthreads ; i++)
  106.         WaitForSingleObject (threadhandle[i], INFINITE);
  107.     DeleteCriticalSection (&crit);
  108.     if (pacifier)
  109.         printf ("\n");
  110. }
  111.  
  112.  
  113. #endif
  114.  
  115. /*
  116. ===================================================================
  117.  
  118. OSF1
  119.  
  120. ===================================================================
  121. */
  122.  
  123. #ifdef __osf__
  124. #define    USED
  125.  
  126. int        numthreads = 4;
  127.  
  128. #include <pthread.h>
  129.  
  130. pthread_mutex_t    *my_mutex;
  131.  
  132. void ThreadLock (void)
  133. {
  134.     if (my_mutex)
  135.         pthread_mutex_lock (my_mutex);
  136. }
  137.  
  138. void ThreadUnlock (void)
  139. {
  140.     if (my_mutex)
  141.         pthread_mutex_unlock (my_mutex);
  142. }
  143.  
  144.  
  145. /*
  146. =============
  147. RunThreadsOn
  148. =============
  149. */
  150. void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
  151. {
  152.     int        i;
  153.     pthread_t    work_threads[MAX_THREADS];
  154.     pthread_addr_t    status;
  155.     pthread_attr_t    attrib;
  156.     pthread_mutexattr_t    mattrib;
  157.  
  158.     dispatch = 0;
  159.     workcount = workcnt;
  160.     oldf = -1;
  161.     pacifier = showpacifier;
  162.  
  163.     if (!my_mutex)
  164.     {
  165.         my_mutex = malloc (sizeof(*my_mutex));
  166.         if (pthread_mutexattr_create (&mattrib) == -1)
  167.             Error ("pthread_mutex_attr_create failed");
  168.         if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
  169.             Error ("pthread_mutexattr_setkind_np failed");
  170.         if (pthread_mutex_init (my_mutex, mattrib) == -1)
  171.             Error ("pthread_mutex_init failed");
  172.     }
  173.  
  174.     if (pthread_attr_create (&attrib) == -1)
  175.         Error ("pthread_attr_create failed");
  176.     if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
  177.         Error ("pthread_attr_setstacksize failed");
  178.     
  179.     for (i=0 ; i<numthreads ; i++)
  180.     {
  181.           if (pthread_create(&work_threads[i], attrib
  182.         , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
  183.             Error ("pthread_create failed");
  184.     }
  185.         
  186.     for (i=0 ; i<numthreads ; i++)
  187.     {
  188.         if (pthread_join (work_threads[i], &status) == -1)
  189.             Error ("pthread_join failed");
  190.     }
  191.  
  192.     if (pacifier)
  193.         printf ("\n");
  194. }
  195.  
  196.  
  197. #endif
  198.  
  199. /*
  200. =======================================================================
  201.  
  202.   SINGLE THREAD
  203.  
  204. =======================================================================
  205. */
  206.  
  207. #ifndef USED
  208.  
  209. int        numthreads = 1;
  210.  
  211. void ThreadLock (void)
  212. {
  213. }
  214.  
  215. void ThreadUnlock (void)
  216. {
  217. }
  218.  
  219. /*
  220. =============
  221. RunThreadsOn
  222. =============
  223. */
  224. void RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int))
  225. {
  226.     int        i;
  227.  
  228.     dispatch = 0;
  229.     workcount = workcnt;
  230.     oldf = -1;
  231.     pacifier = showpacifier;
  232.  
  233.     func(0);
  234.  
  235.     if (pacifier)
  236.         printf ("\n");
  237. }
  238.  
  239. #endif
  240.