virthreadwin32.c 9.1 KB
Newer Older
1
/*
2
 * virthreadwin32.c: basic thread synchronization primitives
3
 *
4
 * Copyright (C) 2009-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23
 *
 */

#include <config.h>

24 25
#include <process.h>

26
#include "viralloc.h"
27 28 29 30 31 32 33 34 35 36 37

struct virThreadLocalData {
    DWORD key;
    virThreadLocalCleanup cleanup;
};
typedef struct virThreadLocalData virThreadLocalData;
typedef virThreadLocalData *virThreadLocalDataPtr;

virMutex virThreadLocalLock;
unsigned int virThreadLocalCount = 0;
virThreadLocalDataPtr virThreadLocalList = NULL;
38
DWORD selfkey;
39 40 41 42 43 44 45

virThreadLocal virCondEvent;

void virCondEventCleanup(void *data);

int virThreadInitialize(void)
{
46 47 48 49
    if (virMutexInit(&virThreadLocalLock) < 0)
        return -1;
    if (virThreadLocalInit(&virCondEvent, virCondEventCleanup) < 0)
        return -1;
50 51
    if ((selfkey = TlsAlloc()) == TLS_OUT_OF_INDEXES)
        return -1;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    return 0;
}

void virThreadOnExit(void)
{
    unsigned int i;
    virMutexLock(&virThreadLocalLock);
    for (i = 0 ; i < virThreadLocalCount ; i++) {
        if (virThreadLocalList[i].cleanup) {
            void *data = TlsGetValue(virThreadLocalList[i].key);
            if (data) {
                TlsSetValue(virThreadLocalList[i].key, NULL);

                (virThreadLocalList[i].cleanup)(data);
            }
        }
    }
    virMutexUnlock(&virThreadLocalLock);
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
int virOnce(virOnceControlPtr once, virOnceFunc func)
{
    if (!once->complete) {
        if (InterlockedIncrement(&once->init) == 1) {
            /* We're the first thread. */
            func();
            once->complete = 1;
        } else {
            /* We're a later thread.  Decrement the init counter back
             * to avoid overflow, then yield until the first thread
             * marks that the function is complete.  It is rare that
             * multiple threads will be waiting here, and since each
             * thread is yielding except the first, we should get out
             * soon enough.  */
            InterlockedDecrement(&once->init);
            while (!once->complete)
                Sleep(0);
        }
    }
    return 0;
}
93 94

int virMutexInit(virMutexPtr m)
S
Stefan Berger 已提交
95
{
96
    return virMutexInitRecursive(m);
S
Stefan Berger 已提交
97 98 99
}

int virMutexInitRecursive(virMutexPtr m)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{
    if (!(m->lock = CreateMutex(NULL, FALSE, NULL))) {
        errno = ESRCH;
        return -1;
    }
    return 0;
}

void virMutexDestroy(virMutexPtr m)
{
    CloseHandle(m->lock);
}

void virMutexLock(virMutexPtr m)
{
    WaitForSingleObject(m->lock, INFINITE);
}

void virMutexUnlock(virMutexPtr m)
{
    ReleaseMutex(m->lock);
}



int virCondInit(virCondPtr c)
{
    c->waiters = NULL;
    if (virMutexInit(&c->lock) < 0)
        return -1;
    return 0;
}

int virCondDestroy(virCondPtr c)
{
    if (c->waiters) {
        errno = EINVAL;
        return -1;
    }
    virMutexDestroy(&c->lock);
    return 0;
}

void virCondEventCleanup(void *data)
{
    HANDLE event = data;
    CloseHandle(event);
}

int virCondWait(virCondPtr c, virMutexPtr m)
{
    HANDLE event = virThreadLocalGet(&virCondEvent);

    if (!event) {
        event = CreateEvent(0, FALSE, FALSE, NULL);
        if (!event) {
            return -1;
        }
158 159 160 161
        if (virThreadLocalSet(&virCondEvent, event) < 0) {
            CloseHandle(event);
            return -1;
        }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    }

    virMutexLock(&c->lock);

    if (VIR_REALLOC_N(c->waiters, c->nwaiters + 1) < 0) {
        virMutexUnlock(&c->lock);
        return -1;
    }
    c->waiters[c->nwaiters] = event;
    c->nwaiters++;

    virMutexUnlock(&c->lock);

    virMutexUnlock(m);

    if (WaitForSingleObject(event, INFINITE) == WAIT_FAILED) {
        virMutexLock(m);
        errno = EINVAL;
        return -1;
    }

    virMutexLock(m);
    return 0;
}

187 188 189 190 191 192 193 194 195 196
int virCondWaitUntil(virCondPtr c ATTRIBUTE_UNUSED,
                     virMutexPtr m ATTRIBUTE_UNUSED,
                     unsigned long long whenms ATTRIBUTE_UNUSED)
{
    /* FIXME: this function is currently only used by the QEMU driver that
     *        is not compiled on Windows, so it's okay for now to just
     *        miss an implementation */
    return -1;
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
void virCondSignal(virCondPtr c)
{
    virMutexLock(&c->lock);

    if (c->nwaiters) {
        HANDLE event = c->waiters[0];
        if (c->nwaiters > 1)
            memmove(c->waiters,
                    c->waiters + 1,
                    sizeof(c->waiters[0]) * (c->nwaiters-1));
        if (VIR_REALLOC_N(c->waiters, c->nwaiters - 1) < 0) {
            ;
        }
        c->nwaiters--;
        SetEvent(event);
    }

    virMutexUnlock(&c->lock);
}

void virCondBroadcast(virCondPtr c)
{
    virMutexLock(&c->lock);

    if (c->nwaiters) {
        unsigned int i;
        for (i = 0 ; i < c->nwaiters ; i++) {
            HANDLE event = c->waiters[i];
            SetEvent(event);
        }
        VIR_FREE(c->waiters);
        c->nwaiters = 0;
    }

    virMutexUnlock(&c->lock);
}


235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
struct virThreadArgs {
    virThreadFunc func;
    void *opaque;
};

static void virThreadHelperDaemon(void *data)
{
    struct virThreadArgs *args = data;
    virThread self;
    HANDLE handle = GetCurrentThread();
    HANDLE process = GetCurrentProcess();

    self.joinable = false;
    DuplicateHandle(process, handle, process,
                    &self.thread, 0, FALSE,
                    DUPLICATE_SAME_ACCESS);
    TlsSetValue(selfkey, &self);

    args->func(args->opaque);

    TlsSetValue(selfkey, NULL);
    CloseHandle(self.thread);
257 258

    VIR_FREE(args);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
}

static unsigned int __stdcall virThreadHelperJoinable(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);
278 279

    VIR_FREE(args);
280 281 282 283 284 285 286 287
    return 0;
}

int virThreadCreate(virThreadPtr thread,
                    bool joinable,
                    virThreadFunc func,
                    void *opaque)
{
288
    struct virThreadArgs *args;
289
    uintptr_t ret;
290 291 292 293 294 295 296

    if (VIR_ALLOC(args) < 0)
        return -1;

    args->func = func;
    args->opaque = opaque;

297 298
    thread->joinable = joinable;
    if (joinable) {
299 300 301 302
        ret = _beginthreadex(NULL, 0,
                             virThreadHelperJoinable,
                             args, 0, NULL);
        if (ret == 0)
303 304
            return -1;
    } else {
305 306 307
        ret = _beginthread(virThreadHelperDaemon,
                           0, args);
        if (ret == -1L)
308 309
            return -1;
    }
310 311 312

    thread->thread = (HANDLE)ret;

313 314 315 316 317 318
    return 0;
}

void virThreadSelf(virThreadPtr thread)
{
    virThreadPtr self = TlsGetValue(selfkey);
319 320 321 322 323 324 325 326 327

    if (self == NULL) {
        /* called on a thread not created by virThreadCreate, e.g. the main thread */
        thread->thread = 0;
        thread->joinable = false;
    } else {
        thread->thread = self->thread;
        thread->joinable = self->joinable;
    }
328 329 330 331 332 333 334 335 336
}

bool virThreadIsSelf(virThreadPtr thread)
{
    virThread self;
    virThreadSelf(&self);
    return self.thread == thread->thread ? true : false;
}

337
/* For debugging use only; see comments in threads-pthread.c.  */
338 339 340 341 342
int virThreadSelfID(void)
{
    return (int)GetCurrentThreadId();
}

343 344 345
/* For debugging use only; see comments in threads-pthread.c.  */
int virThreadID(virThreadPtr thread)
{
346
    return (intptr_t)thread->thread;
347 348
}

349

350 351 352 353 354 355 356 357 358 359
void virThreadJoin(virThreadPtr thread)
{
    if (thread->joinable) {
        WaitForSingleObject(thread->thread, INFINITE);
        CloseHandle(thread->thread);
        thread->thread = 0;
        thread->joinable = false;
    }
}

360 361
void virThreadCancel(virThreadPtr thread ATTRIBUTE_UNUSED)
{}
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

int virThreadLocalInit(virThreadLocalPtr l,
                       virThreadLocalCleanup c)
{
    if ((l->key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
        errno = ESRCH;
        return -1;
    }
    TlsSetValue(l->key, NULL);

    if (c) {
        virMutexLock(&virThreadLocalLock);
        if (VIR_REALLOC_N(virThreadLocalList,
                          virThreadLocalCount + 1) < 0)
            return -1;
        virThreadLocalList[virThreadLocalCount].key = l->key;
        virThreadLocalList[virThreadLocalCount].cleanup = c;
        virThreadLocalCount++;
        virMutexUnlock(&virThreadLocalLock);
    }

    return 0;
}

void *virThreadLocalGet(virThreadLocalPtr l)
{
    return TlsGetValue(l->key);
}

391
int virThreadLocalSet(virThreadLocalPtr l, void *val)
392
{
393
    return TlsSetValue(l->key, val) == 0 ? -1 : 0;
394
}