11
11
#include <unistd.h>
12
12
#include <sched.h>
13
13
14
+ #include "lemon.h"
15
+
14
16
static int nprocs = 0 ;
15
17
static pthread_t * threads = NULL ;
16
18
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER ;
@@ -84,23 +86,23 @@ static void* thread_function(void *arg) {
84
86
* @param n: Number of threads to join.
85
87
*/
86
88
static int join_n_cpu_stealers (int n ) {
87
- int ret = 0 ;
88
- if (!threads ) return 0 ;
89
+ if (!threads ) return -1 ;
89
90
90
91
/* 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 ;
93
95
}
94
96
95
97
/* 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 ;
99
101
}
100
102
101
103
free (threads );
102
104
threads = NULL ;
103
- return ret ;
105
+ return 0 ;
104
106
}
105
107
106
108
/*
@@ -121,7 +123,7 @@ static int launch_cpu_stealers(const int priority) {
121
123
nprocs = get_nprocs ();
122
124
threads = (pthread_t * )malloc ((nprocs ) * sizeof (pthread_t ));
123
125
if (!threads ) {
124
- perror ("Fail to allocate pthread_t structs" );
126
+ perror ("Failed to allocate pthread_t structs" );
125
127
return errno ;
126
128
}
127
129
@@ -179,7 +181,7 @@ int increase_priority_and_launch_stealers() {
179
181
/*
180
182
* @brief Join all CPU stealer threads.
181
183
*
182
- * Wrapper around join_n_cpu_stealers() using the global nprocs value .
184
+ * Wrapper around join_n_cpu_stealers() that unlocks all of them .
183
185
*/
184
186
int join_cpu_stealers () {
185
187
return join_n_cpu_stealers (nprocs );
0 commit comments