C-Thread-Pool icon indicating copy to clipboard operation
C-Thread-Pool copied to clipboard

Mac Os Warning: implicit declaration of function 'pthread_setname_np'

Open dvirsky opened this issue 7 years ago • 6 comments

Compiling thpool on Mac Os with lldb 370.0.42 triggers the above message.

cc -Wall -D_GNU_SOURCE -std=gnu99 -O2 -g  -c -o dep/thpool/thpool.o dep/thpool/thpool.c
dep/thpool/thpool.c:298:3: warning: implicit declaration of function 'pthread_setname_np' is invalid in C99
      [-Wimplicit-function-declaration]
  pthread_setname_np(thread_name);
  ^

Removing

#define _POSIX_C_SOURCE 200809L

which was added in a3916021c837e4892667e02e1681437ddc362725 gets rid of the warning, and passes on Linux as well.

Any hint on how to fix this warning? Is it safe to just comment out the _POSIX_C_SOURCE line?

dvirsky avatar Jun 22 '17 22:06 dvirsky

I can't really recall exactly the reasoning behind it (my commit should probably be more informative).

However my gut feeling is that the macro is overriding existing information in your case. Do you think you can try the below on a Mac and see if it solves it?

#if ! defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200809L
#endif

Pithikos avatar Jun 23 '17 11:06 Pithikos

@Pithikos nope, same results.

cc: @mnunberg

dvirsky avatar Jun 23 '17 14:06 dvirsky

the way these macros normally work is that they make your compiler run in a specific 'mode'. According to the POSIX standard (http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_02.html):

Additional symbols not required or explicitly permitted by IEEE Std 1003.1-2001 to be in that header shall not be made visible, except when enabled by another feature test macro.

It might be the case that you need to include an os-x specific header (maybe *mach*.h) to get the _np functionality.

mnunberg avatar Jun 23 '17 14:06 mnunberg

FWIW, this is what's on my system. All the _np decls are guarded by this. It seems you might have luck with _DARWIN_C_SOURCE.

#if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE)

/* returns non-zero if pthread_create or cthread_fork have been called */
__OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0)
int pthread_is_threaded_np(void);

mnunberg avatar Jun 23 '17 14:06 mnunberg

On top use that instead of forcing the _POSIX_C_SOURCE define :

#	if defined(__APPLE__)
#		include <AvailabilityMacros.h>
#	else
#		ifndef _POSIX_C_SOURCE
#			define _POSIX_C_SOURCE 200809L
#		endif
#	endif

c99-pedantic.patch (Tested : compile without any warnings or errors (BTW you can't dereference func_ptr to void* : nada verboten): on Debian 7, iOS (min-version 6.0) and MacOS (min-version 10.7), Android (min-version lollipop) gcc, gcc-clang and clang toolchains)

--- thpool_original.c	2018-03-21 13:46:04.000000000 -0700
+++ thpool_pedantic.c	2018-03-21 13:45:05.000000000 -0700
@@ -8,7 +8,17 @@
  *
  ********************************/
 
-#define _POSIX_C_SOURCE 200809L
+
+#	if defined(__APPLE__)
+#		include <AvailabilityMacros.h>
+#	else
+#		ifndef _POSIX_C_SOURCE
+#			define _POSIX_C_SOURCE 200809L
+#		elif _POSIX_C_SOURCE < 200809L
+#			error "Valid _POSIX_C_SOURCE version required."
+#		endif
+#	endif
+
 #include <unistd.h>
 #include <signal.h>
 #include <stdio.h>
@@ -94,7 +104,7 @@
 
 
 static int  thread_init(thpool_* thpool_p, struct thread** thread_p, int id);
-static void* thread_do(struct thread* thread_p);
+static void* thread_do(void * arg);
 static void  thread_hold(int sig_id);
 static void  thread_destroy(struct thread* thread_p);
 
@@ -123,8 +133,8 @@
 	threads_on_hold   = 0;
 	threads_keepalive = 1;
 
-	if (num_threads < 0){
-		num_threads = 0;
+	if (num_threads < 1){
+		num_threads = 1;
 	}
 
 	/* Make new thread pool */
@@ -290,7 +300,7 @@
 	(*thread_p)->thpool_p = thpool_p;
 	(*thread_p)->id       = id;
 
-	pthread_create(&(*thread_p)->pthread, NULL, (void *)thread_do, (*thread_p));
+	pthread_create(&(*thread_p)->pthread, NULL, thread_do, (*thread_p));
 	pthread_detach((*thread_p)->pthread);
 	return 0;
 }
@@ -314,8 +324,13 @@
 * @param  thread        thread that will run this function
 * @return nothing
 */
-static void* thread_do(struct thread* thread_p){
+static void* thread_do(void * arg){
 
+	struct thread * thread_p = (struct thread *)arg;
+	if (thread_p == NULL){
+		err("thread_do(): Could not access cookie\n");
+		return NULL;
+	}
 	/* Set thread name for profiling and debuging */
 	char thread_name[128] = {0};
 	sprintf(thread_name, "thread-pool-%d", thread_p->id);

mu578 avatar Mar 16 '18 17:03 mu578

Hi ! I'm having the same warning

nil0x42 avatar Sep 29 '20 16:09 nil0x42