Re: [PATCH] MSVC: Windows-native implementation for subset of Pthreads API
- From
Nicolas Pitre <nico@fluxnic.net>
- Date
- Nov 5, 2009, 19:25 UTC
- Message-ID
- <alpine.LFD.2.00.0911051422590.10340@xanadu.home>
- In-Reply-To
- <16cee31f0911050100v76316dacye7edd8718a893f01@mail.gmail.com>
On Thu, 5 Nov 2009, Andrzej K. Haczewski wrote:
Show 33 quoted lines
> 2009/11/5 Nicolas Pitre <nico@fluxnic.net>:
> > 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...Why not? At least gcc is quite happy with such a construct. It probably makes a copy of the stack before passing it though.
Show 11 quoted lines
> 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 APIRight.
Nicolas