Re: [PATCH] MSVC: Windows-native implementation for subset of Pthreads API
- From
Andrzej K. Haczewski <ahaczewski@gmail.com>
- Date
- Nov 5, 2009, 09:00 UTC
- Message-ID
- <16cee31f0911050100v76316dacye7edd8718a893f01@mail.gmail.com>
- In-Reply-To
- <alpine.LFD.2.00.0911042111270.10340@xanadu.home>
2009/11/5 Nicolas Pitre <nico@fluxnic.net>:
Show 24 quoted lines
> On Wed, 4 Nov 2009, Andrzej K. Haczewski wrote:
>
> What about:
>
> typedef struct {
> HANDLE handle;
> void *(*start_routine)(void *);
> void *arg;
> } pthread_t;
>
> DWORD __stdcall windows_thread_start(LPVOID _self)
> {
> pthread_t *self = _self;
> void *ret = self->start_routine(self->arg);
> return (DWORD)ret;
> }
>
> static inline int pthread_create(pthread_t *thread, const void *unused,
> void *(*start_routine)(void *), void *arg)
> {
> thread->handle = CreateThread(NULL, 0, windows_thread_start,
> thread, 0, NULL);
> [...]
> }The problem I see is not with pthread_init, but pthread_join. Here's how it looks:
int pthread_join(pthread_t thread, void **value_ptr);
If pthread_t would be a struct, then we can't call pthread_join like that... At least that's what I though yesterday, but maybe it can be done like this:
int win32_pthread_join(pthread_t *thread, void **value_ptr)
{
[...]
}#define pthread_join(a, b) win32_pthread_join(&(a), (b))
That way we don't need allocations to simulate pthread init/join API
Show 6 quoted lines
> And thread creation is a relatively rare event compared to e.g. mutex > lock/unlock, so the indirection shouldn't be noticeable. For the same > reason, I also think that you could make pthread_create() and > pthread_join() into a C file instead of being inlined which would reduce > the code footprint at every call site, and allow for only one instance > of windows_thread_start() which could then be made static.
Yeah, I'll factor that out to separate file.
-- Andrzej