-
Notifications
You must be signed in to change notification settings - Fork 559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dissable threads at compile time with NOTHREADS option #359
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The usage of the compile time flag is inconsistent. Either you use a macro to skip compiling phread_cross
and the usage of its symbols in the final library, i.e. use it as an optional library, or you use it as an alternative implementation of the pthread
functions.
The latter can further be automated by checking which system you are on. If you are on anything UNIX-like that has the pthread.h
, you use that as the native implementation of the pthread_
functions. If you are on Windows, you use our custom wrappers. Else, you use dummy noop functions that implement the interface but don't do anything.
I.e. either you do not expose the pthread
symbols or you expose them but with a noop implementation.
Also, can you rebase your PR and squash the relevant commits? The summed-up diff changes the entire thread files due to the CRLF changes from the already merged commit.
Yes, I agree the way I was treating |
I think I personally would have preferred to have a noop- How does this now deal with Also, how do you test this? Is this something that can be reproduced on the CI to make sure this compiles and does not break in the future? |
The problem with the noop- Ultimately, the issue is that we want NOTHREADS to change the meaning of We could choose to get around this by creating our own IMO the I have been testing these changes by compiling with and without the NOTHREADS cmake option, then running the simple detection code (in original PR comment) and inspecting the output of If we wanted to make automated tests for this, I think it would be sufficient to check that |
You do not need to On top of that, we can just test with macros on which platform we are on and define the types and functions appropriately. There is no need for a special flag, unless we also want to provide that non-threading option on UNIX and Windows. |
Here is my best attempt at guessing weather pthread_* types and or symbols are already defined by looking at Posix macros. Not sure if this is what you had in mind. I have left |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the three options (win32, pthread, no pthread) I would restructure this differently.
In the header, if _POSIX_THREADS
is defined, reuse the definitions from the pthread.h
. Else, re-define types and functions. That's it. I don't think we have to check for _WIN32
in the header, unless we want to define Windows-specific types.
For the implementation in the source, I would also first check if _POSIX_THREADS
is defined, is so "do nothing". else-if _WIN32
defined, add the Windows wrapper, else
(no pthread, no windows) add the noop implementations.
IMHO, we shouldn't need changes in any other files, such as the workerpool
.
@@ -246,11 +246,33 @@ unsigned int pcthread_get_num_procs() | |||
return sysinfo.dwNumberOfProcessors; | |||
} | |||
|
|||
#else | |||
#elif !defined(_POSIX_THREADS) /* Not _WIN32. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the Not _WIN32.
comment misleading, since this is an elif
. So this section is not only defined when _WIN32
is not defined, but also it needs _POSIX_THREADS
to not be defined.
@@ -65,8 +67,50 @@ int sched_yield(void); | |||
#ifdef __cplusplus | |||
} | |||
#endif | |||
|
|||
#elif !defined(_POSIX_THREADS) /* _WIN32 not defined */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, I find the comment _WIN32 not defined
to be misleading.
int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); | ||
int pthread_join(pthread_t thread, void **value_ptr); | ||
int pthread_detach(pthread_t thread); | ||
|
||
int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr); | ||
int pthread_mutex_destroy(pthread_mutex_t *mutex); | ||
int pthread_mutex_lock(pthread_mutex_t *mutex); | ||
int pthread_mutex_unlock(pthread_mutex_t *mutex); | ||
|
||
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr); | ||
int pthread_cond_destroy(pthread_cond_t *cond); | ||
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); | ||
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); | ||
int pthread_cond_signal(pthread_cond_t *cond); | ||
int pthread_cond_broadcast(pthread_cond_t *cond); | ||
|
||
int sched_yield(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this duplicating the WIN32 section above?
#ifdef NOTHREADS | ||
nthreads = 1; | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is NOTHREADS
defined? Shouldn't this be conditioned on _POSIX_THREADS
too?
@Chris-F5 Are you still interested in this PR? |
Not really. If nobody else is interested we can close. |
I think having the option to compile the library without threading support is still useful. If this gets implemented, there should be one common API definition in the header, probably replicating the POSIX thread API, and the different implementations, including noop, in the source file. But if someone is interested in implementing this, it also needs a check in the CI to make sure that this actually compiles and works in an embedded setting without threading support. I am going to close this now. Anyone interested in this can pickup from here. |
Pull request for #355.
My initial idea was to define empty
pthread_*
functions inpthreads_cross.{c,h}
so that when other code uses them the symbols would get resolved. Then I would enforceworker_pool->nthreads==1
so that these vacuouspthread_*
functions didnt get used.Problem is that these functions need pthread types in their signatures and we cant just typedef our own because its hard to predict if the standard libs will already define them. For example, I'm trying to cross compile to an embedded arm device for which
#import <time.h>
importssys/types.h
which importssys/pthread_types.h
which defines pthread types even thoughpthread.h
does not declarepthread_*
functions!So the way it works now is if the user defines
NOTHREADS
then all#include <pthread.h>
and all uses ofpthread
types or functions are dissabled using#ifdef
s. Then inworkerpool.c
we just ignore thenthreads
argument toworkerpool_create
.Also
workerpool.c:workerpool_get_nprocs
andcommon/pthreads_cross.c:pcthread_get_num_procs
were re-implementing the same function. So I madeworkerpool_get_nprocs
just callpcthread_get_num_procs
. IfNOTHREADS
is defined thenpcthread_get_num_procs
returns1
.I've tested this quickly by running the following code with and without NOTHREADS.
Note that these changes have been made on top of my Remove CRLF line ends commit. So diffing the branch makes it look like all the lines in
pthreads_cross.{c,h}
have changed even though only some have.