ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
utilPth.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <assert.h>
25
26#ifdef ABC_USE_PTHREADS
27
28#ifdef _WIN32
29#include "../lib/pthread.h"
30#else
31#include <pthread.h>
32#endif
33
34#ifdef __cplusplus
35#include <atomic>
36using namespace std;
37#else
38#include <stdatomic.h>
39#include <stdbool.h>
40#endif
41
42#endif
43
44#include "misc/vec/vec.h"
45
47
51
55
67
68#ifndef ABC_USE_PTHREADS
69
70void Util_ProcessThreads( int (*pUserFunc)(void *), void * vData, int nProcs, int TimeOut, int fVerbose )
71{
72 void * pData; int i;
73 Vec_PtrForEachEntry( void *, (Vec_Ptr_t *)vData, pData, i )
74 pUserFunc( pData );
75}
76
77#else // pthreads are used
78
79#define PAR_THR_MAX 100
80typedef struct Util_ThData_t_
81{
82 void * pUserData;
83 int (*pUserFunc)(void *);
84 int iThread;
85 int nTimeOut;
86 atomic_bool fWorking;
87} Util_ThData_t;
88
89void * Util_Thread( void * pArg )
90{
91 struct timespec pause_duration;
92 pause_duration.tv_sec = 0;
93 pause_duration.tv_nsec = 10000000L; // 10 milliseconds
94
95 Util_ThData_t * pThData = (Util_ThData_t *)pArg;
96 while ( 1 )
97 {
98 while ( !atomic_load_explicit((atomic_bool *)&pThData->fWorking, memory_order_acquire) )
99 nanosleep(&pause_duration, NULL);
100 if ( pThData->pUserData == NULL )
101 {
102 pthread_exit( NULL );
103 assert( 0 );
104 return NULL;
105 }
106 pThData->pUserFunc( pThData->pUserData );
107 atomic_store_explicit(&pThData->fWorking, false, memory_order_release);
108 }
109 assert( 0 );
110 return NULL;
111}
112
113void Util_ProcessThreads( int (*pUserFunc)(void *), void * vData, int nProcs, int TimeOut, int fVerbose )
114{
115 //abctime clkStart = Abc_Clock();
116 Util_ThData_t ThData[PAR_THR_MAX];
117 pthread_t WorkerThread[PAR_THR_MAX];
118 Vec_Ptr_t * vStack = NULL;
119 int i, status;
120 fflush( stdout );
121 if ( nProcs <= 2 ) {
122 void * pData; int i;
123 Vec_PtrForEachEntry( void *, (Vec_Ptr_t *)vData, pData, i )
124 pUserFunc( pData );
125 return;
126 }
127 // subtract manager thread
128 nProcs--;
129 assert( nProcs >= 1 && nProcs <= PAR_THR_MAX );
130 // start threads
131 for ( i = 0; i < nProcs; i++ )
132 {
133 ThData[i].pUserData = NULL;
134 ThData[i].pUserFunc = pUserFunc;
135 ThData[i].iThread = i;
136 ThData[i].nTimeOut = TimeOut;
137 atomic_store_explicit(&ThData[i].fWorking, false, memory_order_release);
138 status = pthread_create( WorkerThread + i, NULL, Util_Thread, (void *)(ThData + i) ); assert( status == 0 );
139 }
140
141 struct timespec pause_duration;
142 pause_duration.tv_sec = 0;
143 pause_duration.tv_nsec = 10000000L; // 10 milliseconds
144
145 // look at the threads
146 vStack = Vec_PtrDup( (Vec_Ptr_t *)vData );
147 while ( Vec_PtrSize(vStack) > 0 )
148 {
149 for ( i = 0; i < nProcs; i++ )
150 {
151 if ( atomic_load_explicit(&ThData[i].fWorking, memory_order_acquire) )
152 continue;
153 ThData[i].pUserData = Vec_PtrPop( vStack );
154 atomic_store_explicit(&ThData[i].fWorking, true, memory_order_release);
155 break;
156 }
157 }
158 Vec_PtrFree( vStack );
159
160 // wait till threads finish
161 for ( i = 0; i < nProcs; i++ )
162 {
163 if ( atomic_load_explicit(&ThData[i].fWorking, memory_order_acquire) )
164 i = -1; // Start from the beginning again
165 nanosleep(&pause_duration, NULL);
166 }
167
168 // stop threads
169 for ( i = 0; i < nProcs; i++ )
170 {
171 ThData[i].pUserData = NULL;
172 atomic_store_explicit(&ThData[i].fWorking, true, memory_order_release);
173 }
174
175 // Join threads
176 for ( i = 0; i < nProcs; i++ )
177 pthread_join( WorkerThread[i], NULL );
178
179 //if ( fVerbose )
180 // Abc_PrintTime( 1, "Time", Abc_Clock() - clkStart );
181}
182
183#endif // pthreads are used
184
185
189
190
192
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define PAR_THR_MAX
DECLARATIONS ///.
Definition bmcBmcG.c:31
ABC_NAMESPACE_IMPL_START void Util_ProcessThreads(int(*pUserFunc)(void *), void *vData, int nProcs, int TimeOut, int fVerbose)
DECLARATIONS ///.
Definition utilPth.c:70
#define assert(ex)
Definition util_old.h:213
typedefABC_NAMESPACE_HEADER_START struct Vec_Ptr_t_ Vec_Ptr_t
INCLUDES ///.
Definition vecPtr.h:42
#define Vec_PtrForEachEntry(Type, vVec, pEntry, i)
MACRO DEFINITIONS ///.
Definition vecPtr.h:55