1 /*
2     Copyright (C) 1998  Dennis Roddeman
3     email: dennis.roddeman@feat.nl
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software Foundation
17     59 Temple Place, Suite 330, Boston, MA, 02111-1307, USA
18 */
19 
20 #include "tochnog.h"
21 #include "pthread.h"
22 
23 pthread_mutex_t mtx;
24 long int inext=0;
25 pthread_t threads[MTHREAD];
26 
parallel_sys_initialize(void)27 void parallel_sys_initialize( void )
28 
29 {
30   pthread_mutex_init( &mtx, NULL );
31 }
32 
parallel_sys_lock(void)33 void parallel_sys_lock( void )
34 
35 {
36   pthread_mutex_lock( &mtx );
37 }
38 
parallel_sys_next_of_loop(long int next_of_loop[],long int max_loop,long int & nloop,long int & ithread)39 void parallel_sys_next_of_loop( long int next_of_loop[], long int max_loop,
40   long int &nloop, long int &ithread )
41 
42 {
43   long int i=0, nthread=0, ldum=0;
44   double ddum[1];
45 
46   db( OPTIONS_PROCESSORS, 0, &nthread, ddum, ldum, VERSION_NORMAL, GET );
47 
48   parallel_sys_lock();
49 
50   inext++;
51 
52   nloop = 1 + max_loop/nthread;
53   for ( i=0; i<nloop; i++ ) {
54     assert( i<=max_loop );
55     next_of_loop[i] = inext*nloop + i;
56   }
57 
58   ithread = inext;
59 
60   parallel_sys_unlock();
61 
62 }
63 
parallel_sys_routine(void (* routine)(void))64 void parallel_sys_routine( void (*routine)(void) )
65 
66 {
67   long int ithread=0, nthread=0, icheck=0, ldum=0;
68   double ddum[1];
69 
70   parallel_active = 1;
71   inext = -1;
72 
73   db( OPTIONS_PROCESSORS, 0, &nthread, ddum, ldum, VERSION_NORMAL, GET );
74 
75   for ( ithread=0; ithread<nthread; ithread++ ) {
76     icheck = pthread_create( &threads[ithread], NULL,
77       (void * (*)(void *)) routine, 0 );
78     if ( icheck!=0 ) {
79       cout << "Error in creating threads.\n";
80       exit(TN_EXIT_STATUS);
81     }
82   }
83 
84   for ( ithread=0; ithread<nthread; ithread++ ) {
85     icheck = pthread_join( threads[ithread], NULL );
86     if ( icheck!=0 ) {
87       cout << "Error in joining threads.\n";
88       exit(TN_EXIT_STATUS);
89     }
90   }
91 
92   parallel_active = 0;
93 
94 }
95 
parallel_sys_unlock(void)96 void parallel_sys_unlock( void )
97 
98 {
99   pthread_mutex_unlock( &mtx );
100 }
101