aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkThreadUtils_win.cpp
blob: 208ffded1bff4eb7ffb8b04356af403c8ced79ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkTypes.h"

#include "SkThreadUtils.h"
#include "SkThreadUtils_win.h"

SkThread_WinData::SkThread_WinData(SkThread::entryPointProc entryPoint, void* data)
    : fHandle(NULL)
    , fParam(data)
    , fThreadId(0)
    , fEntryPoint(entryPoint)
    , fStarted(false)
{
    fCancelEvent = CreateEvent(
        NULL,  // default security attributes
        false, //auto reset
        false, //not signaled
        NULL); //no name
}

SkThread_WinData::~SkThread_WinData() {
    CloseHandle(fCancelEvent);
}

static DWORD WINAPI thread_start(LPVOID data) {
    SkThread_WinData* winData = static_cast<SkThread_WinData*>(data);

    //See if this thread was canceled before starting.
    if (WaitForSingleObject(winData->fCancelEvent, 0) == WAIT_OBJECT_0) {
        return 0;
    }

    winData->fEntryPoint(winData->fParam);
    return 0;
}

SkThread::SkThread(entryPointProc entryPoint, void* data) {
    SkThread_WinData* winData = new SkThread_WinData(entryPoint, data);
    fData = winData;

    if (NULL == winData->fCancelEvent) {
        return;
    }

    winData->fHandle = CreateThread(
        NULL,                   // default security attributes
        0,                      // use default stack size
        thread_start,           // thread function name (proxy)
        winData,                // argument to thread function (proxy args)
        CREATE_SUSPENDED,       // create suspended so affinity can be set
        &winData->fThreadId);   // returns the thread identifier
}

SkThread::~SkThread() {
    if (fData != NULL) {
        SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData);
        // If created thread but start was never called, kill the thread.
        if (winData->fHandle != NULL && !winData->fStarted) {
            if (SetEvent(winData->fCancelEvent) != 0) {
                if (this->start()) {
                    this->join();
                }
            } else {
                //kill with prejudice
                TerminateThread(winData->fHandle, -1);
            }
        }
        delete winData;
    }
}

bool SkThread::start() {
    SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData);
    if (NULL == winData->fHandle) {
        return false;
    }

    if (winData->fStarted) {
        return false;
    }
    winData->fStarted = -1 != ResumeThread(winData->fHandle);
    return winData->fStarted;
}

void SkThread::join() {
    SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData);
    if (NULL == winData->fHandle || !winData->fStarted) {
        return;
    }

    WaitForSingleObject(winData->fHandle, INFINITE);
}

static unsigned int num_bits_set(DWORD_PTR mask) {
    unsigned int count;
    for (count = 0; mask; ++count) {
        mask &= mask - 1;
    }
    return count;
}

static unsigned int nth_set_bit(unsigned int n, DWORD_PTR mask) {
    n %= num_bits_set(mask);
    for (unsigned int setBitsSeen = 0, currentBit = 0; true; ++currentBit) {
        if (mask & (1 << currentBit)) {
            ++setBitsSeen;
            if (setBitsSeen > n) {
                return currentBit;
            }
        }
    }
}

bool SkThread::setProcessorAffinity(unsigned int processor) {
    SkThread_WinData* winData = static_cast<SkThread_WinData*>(fData);
    if (NULL == winData->fHandle) {
        return false;
    }

    DWORD_PTR processAffinityMask;
    DWORD_PTR systemAffinityMask;
    if (0 == GetProcessAffinityMask(GetCurrentProcess(),
                                    &processAffinityMask,
                                    &systemAffinityMask)) {
        return false;
    }

    DWORD_PTR threadAffinityMask = 1 << nth_set_bit(processor, processAffinityMask);
    return 0 != SetThreadAffinityMask(winData->fHandle, threadAffinityMask);
}