Skip to content

Commit edf7c9c

Browse files
committed
fix
1 parent 040f4eb commit edf7c9c

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

cpu_stealer.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <unistd.h>
1212
#include <sched.h>
1313

14+
#include "lemon.h"
15+
1416
static int nprocs = 0;
1517
static pthread_t *threads = NULL;
1618
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
@@ -84,23 +86,23 @@ static void* thread_function(void *arg) {
8486
* @param n: Number of threads to join.
8587
*/
8688
static int join_n_cpu_stealers(int n) {
87-
int ret = 0;
88-
if(!threads) return 0;
89+
if(!threads) return -1;
8990

9091
/* Unlock the global mutex */
91-
if((ret = pthread_mutex_unlock(&mut))) {
92-
fprintf(stderr, "Fail to unlock mutex\n");
92+
if(pthread_mutex_unlock(&mut)) {
93+
WARN("Fail to unlock mutex\n");
94+
return -1;
9395
}
9496

9597
/* Join all the processes */
96-
for (int i = 0; i < n - 1; i++) {
97-
ret = pthread_join(threads[i], NULL);
98-
if (ret) continue;
98+
for (int i = 0; i < n; i++) {
99+
const int ret = pthread_join(threads[i], NULL);
100+
if (ret) return -1;
99101
}
100102

101103
free(threads);
102104
threads = NULL;
103-
return ret;
105+
return 0;
104106
}
105107

106108
/*
@@ -121,7 +123,7 @@ static int launch_cpu_stealers(const int priority) {
121123
nprocs = get_nprocs();
122124
threads = (pthread_t *)malloc((nprocs) * sizeof(pthread_t));
123125
if(!threads) {
124-
perror("Fail to allocate pthread_t structs");
126+
perror("Failed to allocate pthread_t structs");
125127
return errno;
126128
}
127129

@@ -179,7 +181,7 @@ int increase_priority_and_launch_stealers() {
179181
/*
180182
* @brief Join all CPU stealer threads.
181183
*
182-
* Wrapper around join_n_cpu_stealers() using the global nprocs value.
184+
* Wrapper around join_n_cpu_stealers() that unlocks all of them.
183185
*/
184186
int join_cpu_stealers() {
185187
return join_n_cpu_stealers(nprocs);

lemon.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef LEMON_H
2+
#define LEMON_H
3+
14
#include <stdbool.h>
25
#include <errno.h>
36

@@ -50,4 +53,6 @@ typedef struct __attribute__((packed)) {
5053
struct read_mem_result {
5154
int ret_code;
5255
unsigned char buf[HUGE_PAGE_SIZE];
53-
};
56+
};
57+
58+
#endif /* LEMON_H */

0 commit comments

Comments
 (0)