summaryrefslogtreecommitdiff
path: root/src/inc/apithreadstress.cpp
blob: 36cf6a98c0d5e5f0117cf1f7b1bf15938fb86c49 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

// ---------------------------------------------------------------------------
// APIThreadStress.cpp  (API thread stresser)
// ---------------------------------------------------------------------------

#include "stdafx.h"

#ifdef _DEBUG
#define LOGGING 1
#endif

#include "apithreadstress.h"
#include "clrhost.h"
#include "ex.h"
#include "log.h"



// For now, thread stress is incompatible with hosting.  We need a host CreateThread
// to fix this.
#undef SetEvent
#undef ResetEvent

int APIThreadStress::s_threadStressCount = 0;

APIThreadStress::APIThreadStress()
{
    m_threadCount = 0;

    // Don't "fork" stress threads
    if (ClrFlsGetValue(TlsIdx_StressThread) == NULL)
        m_threadCount = s_threadStressCount;

    if (m_threadCount != 0)
    {
        m_setupOK = TRUE;

        m_hThreadArray = new (nothrow) HANDLE [ m_threadCount ];
        if (m_hThreadArray == NULL)
            m_setupOK = FALSE;
        else
        {
            HANDLE *p = m_hThreadArray;
            HANDLE *pEnd = p + m_threadCount;
            
            while (p < pEnd)
            {
                DWORD id;
                *p = ::CreateThread(NULL, 0, StartThread, this, 0, &id);
                if (*p == NULL)
                    m_setupOK = FALSE;
                p++;
            }
        }

        m_syncEvent = ClrCreateManualEvent(FALSE);
        if (m_syncEvent == INVALID_HANDLE_VALUE)
            m_setupOK = FALSE;
    }
}

APIThreadStress::~APIThreadStress()
{
    if (m_threadCount > 0)
    {
        HANDLE *p = m_hThreadArray;
        HANDLE *pEnd = p + m_threadCount;

        if (p != NULL)
        {
            while (p < pEnd)
            {
                if (*p != NULL)
                {
                    if (m_threadCount > 0 && m_setupOK)
                        WaitForSingleObjectEx(*p, INFINITE, FALSE);

                    ::CloseHandle(*p);
                }
                p++;
            }
            delete [] m_hThreadArray;
        }

        if (m_syncEvent != INVALID_HANDLE_VALUE)
            CloseHandle(m_syncEvent);
    }
}

void APIThreadStress::SetThreadStressCount(int threadCount)
{
    s_threadStressCount = threadCount;
}


DWORD WINAPI APIThreadStress::StartThread(void *arg)
{
    APIThreadStress *pThis = (APIThreadStress *) arg;

    ClrFlsSetValue(TlsIdx_StressThread, pThis);

    EX_TRY
    {
        // Perform initial synchronization
        WaitForSingleObjectEx(pThis->m_syncEvent, INFINITE, FALSE);
        InterlockedIncrement(&pThis->m_runCount);

        LOG((LF_SYNC, LL_INFO100, "Stressing operation on thread %d\n", GetCurrentThreadId()));
        ((APIThreadStress *)arg)->Invoke();
        LOG((LF_SYNC, LL_INFO100, "End stress operation on thread %d\n", GetCurrentThreadId()));

        if (InterlockedDecrement(&pThis->m_runCount) == 0)
            ::SetEvent(pThis->m_syncEvent);
    }
    EX_CATCH
    {
        LOG((LF_SYNC, LL_ERROR, "Exception during stress operation on thread %d\n", GetCurrentThreadId()));
    }
    EX_END_CATCH(SwallowAllExceptions);

    return 0;
}

BOOL APIThreadStress::DoThreadStress()
{
    if (m_threadCount > 0 && m_setupOK)
    {
        HANDLE *p = m_hThreadArray;
        HANDLE *pEnd = p + m_threadCount;

        while (p < pEnd)
        {
            ::ResumeThread(*p);
            p++;
        }

        // Start the threads at the same time
        ::SetEvent(m_syncEvent);

        return TRUE;
    }
    else
    {
        SyncThreadStress();
        return FALSE;
    }
}

void APIThreadStress::SyncThreadStress()
{
    APIThreadStress *pThis = (APIThreadStress *) ClrFlsGetValue(TlsIdx_StressThread);

    if (pThis != NULL)
    {
        LOG((LF_SYNC, LL_INFO1000, "Syncing stress operation on thread %d\n", GetCurrentThreadId()));

        ::ResetEvent(pThis->m_syncEvent);

        if (InterlockedDecrement(&pThis->m_runCount) == 0)
            ::SetEvent(pThis->m_syncEvent);
        else
            WaitForSingleObjectEx(pThis->m_syncEvent, INFINITE, FALSE);
        InterlockedIncrement(&pThis->m_runCount);

        LOG((LF_SYNC, LL_INFO1000, "Resuming stress operation on thread %d\n", GetCurrentThreadId()));
    }
}