summaryrefslogtreecommitdiff
path: root/src/inc/apithreadstress.h
blob: 515e5885bda28b8c6668138883a27fdea2c03397 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// ---------------------------------------------------------------------------
// APIThreadStress.h  (API thread stresser)
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// This class provides a simple base to wrap "thread stress" logic around an API,
// which will (in thread stress mode) cause an API to "fork" onto many threads
// executing the same operation simulatenously.  This can help to expose race
// conditions.
//
// Usage:
//
// First, subtype APIThreadStress and override Invoke to implement the operation.
// You will likely need to add data members for the arguments.
//
// Next, inside the API, write code like this:
//
// void MyRoutine(int a1, void *a2)
// {
//      class stress : APIThreadStress
//      {
//          int a1;
//          void *a2;
//          stress(int a1, void *a2) : a1(a1), a2(a2) 
//               { DoThreadStress(); }
//          void Invoke() { MyRoutine(a1, a2); }
//      } ts (a1, a2);
//
//      // implementation
//
//      // perhaps we have a common sub-point in the routine where we want the threads to 
//      // queue up and race again
//
//      ts.SyncThreadStress();
//
//      // more implementation    
//  }
// ---------------------------------------------------------------------------


#ifndef _APITHREADSTRESS_H_
#define _APITHREADSTRESS_H_

#include "utilcode.h"

#ifdef STRESS_THREAD

class APIThreadStress
{
 public:
    APIThreadStress();
    ~APIThreadStress();

    BOOL DoThreadStress();
    static void SyncThreadStress();

    static void SetThreadStressCount(int count);

 protected:
    virtual void Invoke() {LIMITED_METHOD_CONTRACT;};

 private:
    static DWORD WINAPI StartThread(void *arg);

    static int s_threadStressCount;     

    int       m_threadCount;
    HANDLE    *m_hThreadArray;
    BOOL      m_setupOK;
    LONG      m_runCount;
    HANDLE    m_syncEvent;

};

#else // STRESS_THREAD

class APIThreadStress
{
 public:
    BOOL DoThreadStress() { return FALSE; }
    static void SyncThreadStress() { }
    static void SetThreadStressCount(int count) { }
};

#endif // STRESS_THREAD

#endif  // _APITHREADSTRESS_H_