# include <sys/syscall.h>
#endif
+#include "memory.h"
+
/* Nothing special required for pthreads */
int virThreadInitialize(void)
{
struct virThreadArgs *args = data;
args->func(args->opaque);
+ VIR_FREE(args);
return NULL;
}
virThreadFunc func,
void *opaque)
{
- struct virThreadArgs args = { func, opaque };
+ struct virThreadArgs *args;
pthread_attr_t attr;
pthread_attr_init(&attr);
+ if (VIR_ALLOC(args) < 0)
+ return -1;
+
+ args->func = func;
+ args->opaque = opaque;
+
if (!joinable)
pthread_attr_setdetachstate(&attr, 1);
- int ret = pthread_create(&thread->thread, &attr, virThreadHelper, &args);
+ int ret = pthread_create(&thread->thread, &attr, virThreadHelper, args);
if (ret != 0) {
+ VIR_FREE(args);
errno = ret;
return -1;
}
+ /* New thread owns 'args' in success case, so don't free */
return 0;
}
TlsSetValue(selfkey, NULL);
CloseHandle(self.thread);
+
+ VIR_FREE(args);
}
static unsigned int __stdcall virThreadHelperJoinable(void *data)
TlsSetValue(selfkey, NULL);
CloseHandle(self.thread);
+
+ VIR_FREE(args);
return 0;
}
virThreadFunc func,
void *opaque)
{
- struct virThreadArgs args = { func, opaque };
+ struct virThreadArgs *args;
+
+ if (VIR_ALLOC(args) < 0)
+ return -1;
+
+ args->func = func;
+ args->opaque = opaque;
+
thread->joinable = joinable;
if (joinable) {
thread->thread = (HANDLE)_beginthreadex(NULL, 0,
virThreadHelperJoinable,
- &args, 0, NULL);
+ args, 0, NULL);
if (thread->thread == 0)
return -1;
} else {
thread->thread = (HANDLE)_beginthread(virThreadHelperDaemon,
- 0, &args);
+ 0, args);
if (thread->thread == (HANDLE)-1L)
return -1;
}