2010/11/8 Daniel P. Berrange <berrange@xxxxxxxxxx>: > The util/threads.c/h code already has APIs for mutexes, > condition variables and thread locals. This commit adds > in code for actually creating threads. > > * src/libvirt_private.syms: Export new symbols > * src/util/threads.h: Define APIs virThreadCreate, virThreadSelf, > ÂvirThreadIsSelf and virThreadJoin > * src/util/threads-win32.c, src/util/threads-win32.h: Win32 > Âimpl of threads > * src/util/threads-pthread.c, src/util/threads-pthread.h: POSIX > Âimpl of threads > Âstruct virThreadLocalData { > @@ -33,7 +35,7 @@ typedef virThreadLocalData *virThreadLocalDataPtr; > ÂvirMutex virThreadLocalLock; > Âunsigned int virThreadLocalCount = 0; > ÂvirThreadLocalDataPtr virThreadLocalList = NULL; > - > +DWORD selfkey; > > ÂvirThreadLocal virCondEvent; Not introduced by this patch, but why are this local variables not marked as static? > + > +static unsigned int __stdcall virThreadHelperDaemon(void *data) > +{ > + Â Âstruct virThreadArgs *args = data; > + Â ÂvirThread self; > + Â ÂHANDLE handle = GetCurrentThread(); > + Â ÂHANDLE process = GetCurrentProcess(); > + > + Â Âself.joinable = true; > + Â ÂDuplicateHandle(process, handle, process, > + Â Â Â Â Â Â Â Â Â Â&self.thread, 0, FALSE, > + Â Â Â Â Â Â Â Â Â ÂDUPLICATE_SAME_ACCESS); > + Â ÂTlsSetValue(selfkey, &self); > + > + Â Âargs->func(args->opaque); > + > + Â ÂTlsSetValue(selfkey, NULL); > + Â ÂCloseHandle(self.thread); > + Â Âreturn 0; > +} IMHO virThreadHelperJoinable would be a better name for this function. > +int virThreadCreate(virThreadPtr thread, > + Â Â Â Â Â Â Â Â Â Âbool joinable, > + Â Â Â Â Â Â Â Â Â ÂvirThreadFunc func, > + Â Â Â Â Â Â Â Â Â Âvoid *opaque) > +{ > + Â Âstruct virThreadArgs args = { func, opaque }; > + Â Âthread->joinable = joinable; > + Â Âif (joinable) { > + Â Â Â Âthread->thread = (HANDLE)_beginthreadex(NULL, 0, > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂvirThreadHelperDaemon, > + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â&args, 0, NULL); > + Â Â Â Âif (thread->thread == 0) > + Â Â Â Â Â Âreturn -1; > + Â Â} else { > + Â Â Â Âthread->thread = (HANDLE)_beginthread(virThreadHelper, 0, &args); > + Â Â Â Âif (thread->thread == (HANDLE)-1L) > + Â Â Â Â Â Âreturn -1; > + Â Â} > + Â Âreturn 0; > +} > + > +void virThreadSelf(virThreadPtr thread) > +{ > + Â ÂvirThreadPtr self = TlsGetValue(selfkey); > + Â Âthread->thread = self->thread; > + Â Âthread->joinable = self->joinable; > +} > + > +bool virThreadIsSelf(virThreadPtr thread) > +{ > + Â ÂvirThread self; > + Â ÂvirThreadSelf(&self); > + Â Âreturn self.thread == thread->thread ? true : false; > +} > + > +void virThreadJoin(virThreadPtr thread) > +{ > + Â Âif (thread->joinable) { > + Â Â Â ÂWaitForSingleObject(thread->thread, INFINITE); > + Â Â Â ÂCloseHandle(thread->thread); > + Â Â Â Âthread->thread = 0; > + Â Â Â Âthread->joinable = false; > + Â Â} > +} > + ACK. Matthias -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list