fixes race condition that exists between do_loop() and sched_setaffinity() on android. due to synchronization problem, sched_setaffinity() was setting affinity for thr_id 0 randomly.
also, allocate the thr_id's dynamically instead of fixed array size 3.
tested on Linaro Android 14.04.
Signed-off-by: Mohammad Merajul Islam Molla meraj.enigma@gmail.com --- utils/heat_cpu.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-)
diff --git a/utils/heat_cpu.c b/utils/heat_cpu.c index 02e0b6d..716cd56 100644 --- a/utils/heat_cpu.c +++ b/utils/heat_cpu.c @@ -47,17 +47,14 @@ #include <string.h> #include <math.h>
-#define NR_THREAD 3 - int cont = 1; int cpu_index;
long long a = 1, c = 1; float f = M_PI; -pid_t thr_id[NR_THREAD]; +pid_t *thr_id; int moderate_inst;
- #ifdef ANDROID int conditionMet; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; @@ -72,11 +69,11 @@ void *do_loop(void *data) a = 1; c = 1; #ifdef ANDROID + pthread_mutex_lock(&mutex); long int thread = (long int) data; thr_id[thread] = gettid(); - pthread_mutex_lock(&mutex); - while (!conditionMet) - pthread_cond_wait(&cond, &mutex); + conditionMet = 1; + pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mutex); #endif
@@ -112,6 +109,12 @@ int main(int arg_count, char *argv[]) } }
+ thr_id = (pid_t *) calloc(num_cpus, sizeof(pid_t)); + if (thr_id == NULL) { + printf("ERROR: out of memory\n"); + return -1; + } + /* clear out cpus */ CPU_ZERO(&cpuset);
@@ -161,8 +164,7 @@ int main(int arg_count, char *argv[]) return ret; } #endif - CPU_CLR(i, &cpuset); - + CPU_CLR(i, &cpuset); }
for (i = 0; i < num_cpus; i++) { @@ -176,23 +178,24 @@ int main(int arg_count, char *argv[]) CPU_ZERO(&cpuset); CPU_SET(i, &cpuset);
+ pthread_mutex_lock(&mutex); + while (!conditionMet) + pthread_cond_wait(&cond, &mutex); + ret = sched_setaffinity(thr_id[i], sizeof(cpuset), &cpuset); if (ret) { printf("Error setting affinity on pthread th_id\n"); printf("Error: %s\n", strerror(ret)); return ret; } -#endif - }
-#ifdef ANDROID - pthread_mutex_lock(&mutex); - conditionMet = 1; - pthread_cond_broadcast(&cond); - pthread_mutex_unlock(&mutex); + conditionMet = 0; + pthread_mutex_unlock(&mutex); #endif + }
while (1) sleep(1); + return 0; } -- 1.8.1.2