summaryrefslogtreecommitdiff
path: root/src/pal/src/include/pal/synchobjects.hpp
blob: d33ac5b777e5aa14aab2f832211b6ac47bbd72f2 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*++



Module Name:

    include/pal/synchobjects.hpp

Abstract:
    Header file for synchronization manager and controllers



--*/

#ifndef _SINCHOBJECTS_HPP_
#define _SINCHOBJECTS_HPP_

#include "corunix.hpp"
#include "threadinfo.hpp"
#include "shm.hpp"
#include "list.h"

#include <pthread.h>

#define SharedID SHMPTR
#define SharedPoolId ULONG_PTR
#define DefaultSharedPool ((ULONG_PTR)0)
#define NULLSharedID ((SHMPTR)NULL)
#define SharedIDToPointer(shID) SHMPTR_TO_TYPED_PTR(PVOID, shID)
#define SharedIDToTypePointer(TYPE,shID) SHMPTR_TO_TYPED_PTR(TYPE, shID)
#define RawSharedObjectAlloc(szSize, shPoolId) SHMalloc(szSize)
#define RawSharedObjectFree(shID) SHMfree(shID)
    
namespace CorUnix
{   
    DWORD InternalWaitForMultipleObjectsEx(
        CPalThread * pthrCurrent,
        DWORD nCount,
        CONST HANDLE *lpHandles,
        BOOL bWaitAll,
        DWORD dwMilliseconds,
        BOOL bAlertable);
    
    PAL_ERROR InternalSleepEx(
        CPalThread * pthrCurrent,
        DWORD dwMilliseconds,
        BOOL bAlertable);
        
    enum THREAD_STATE
    {
        TS_IDLE,
        TS_STARTING,
        TS_RUNNING,
        TS_FAILED,
        TS_DONE,
    };

    // forward declarations
    struct _ThreadWaitInfo;
    struct _WaitingThreadsListNode;
    class CSynchData;
    
    typedef struct _WaitingThreadsListNode * PWaitingThreadsListNode;
    typedef struct _OwnedObjectsListNode * POwnedObjectsListNode;
    typedef struct _ThreadApcInfoNode * PThreadApcInfoNode;
        
    typedef struct _ThreadWaitInfo
    {   
        WaitType wtWaitType;
        WaitDomain wdWaitDomain;
        LONG lObjCount;
        LONG lSharedObjCount;
        CPalThread * pthrOwner;        
        PWaitingThreadsListNode rgpWTLNodes[MAXIMUM_WAIT_OBJECTS];
        
        _ThreadWaitInfo() : wtWaitType(SingleObject), wdWaitDomain(LocalWait), 
                            lObjCount(0), lSharedObjCount(0), 
                            pthrOwner(NULL) {}
    } ThreadWaitInfo;

    typedef struct _ThreadNativeWaitData 
    {
#if !SYNCHMGR_PIPE_BASED_THREAD_BLOCKING
        pthread_mutex_t     mutex;
        pthread_cond_t      cond;  
        int                 iPred;
#else // SYNCHMGR_PIPE_BASED_THREAD_BLOCKING
        int                 iPipeRd;
        int                 iPipeWr;
#endif // SYNCHMGR_PIPE_BASED_THREAD_BLOCKING

        DWORD               dwObjectIndex;
        ThreadWakeupReason  twrWakeupReason;
        bool                fInitialized;

        _ThreadNativeWaitData() : 
#if !SYNCHMGR_PIPE_BASED_THREAD_BLOCKING
            iPred(0), 
#else // SYNCHMGR_PIPE_BASED_THREAD_BLOCKING
            iPipeRd(-1),
            iPipeWr(-1),
#endif // SYNCHMGR_PIPE_BASED_THREAD_BLOCKING
            dwObjectIndex(0), 
            twrWakeupReason(WaitSucceeded), 
            fInitialized(false)
        {
        }

        ~_ThreadNativeWaitData();
    } ThreadNativeWaitData;

    class CThreadSynchronizationInfo : public CThreadInfoInitializer
    {
        friend class CPalSynchronizationManager;
        friend class CSynchWaitController;

        THREAD_STATE          m_tsThreadState; 
        SharedID              m_shridWaitAwakened;
        Volatile<LONG>        m_lLocalSynchLockCount;
        Volatile<LONG>        m_lSharedSynchLockCount;
        LIST_ENTRY            m_leOwnedObjsList;

        ThreadNativeWaitData  m_tnwdNativeData;
        ThreadWaitInfo        m_twiWaitInfo;

#ifdef SYNCHMGR_SUSPENSION_SAFE_CONDITION_SIGNALING
        static const int      PendingSignalingsArraySize = 10;
        LONG                  m_lPendingSignalingCount;
        CPalThread *          m_rgpthrPendingSignalings[PendingSignalingsArraySize];
        LIST_ENTRY            m_lePendingSignalingsOverflowList;
#endif // SYNCHMGR_SUSPENSION_SAFE_CONDITION_SIGNALING

    public:

        CThreadSynchronizationInfo();
        virtual ~CThreadSynchronizationInfo();

        //
        // CThreadInfoInitializer methods
        //
        virtual PAL_ERROR InitializePreCreate(void);
        
        virtual PAL_ERROR InitializePostCreate(
            CPalThread *pthrCurrent,
            SIZE_T threadId,
            DWORD dwLwpId
            );

        THREAD_STATE GetThreadState(void)
        {
            return m_tsThreadState;
        };

        void SetThreadState(THREAD_STATE tsThreadState)
        {
            m_tsThreadState = tsThreadState;
        };

        ThreadNativeWaitData * GetNativeData()
        {
            return &m_tnwdNativeData;
        }

#if SYNCHMGR_SUSPENSION_SAFE_CONDITION_SIGNALING && !SYNCHMGR_PIPE_BASED_THREAD_BLOCKING
        PAL_ERROR RunDeferredThreadConditionSignalings();
#endif // SYNCHMGR_SUSPENSION_SAFE_CONDITION_SIGNALING && !SYNCHMGR_PIPE_BASED_THREAD_BLOCKING
    
        // NOTE: the following methods provide non-synchronized access to 
        //       the list of owned objects for this thread. Any thread 
        //       accessing this list MUST own the appropriate 
        //       synchronization lock(s).
        void AddObjectToOwnedList(POwnedObjectsListNode pooln);
        void RemoveObjectFromOwnedList(POwnedObjectsListNode pooln);
        POwnedObjectsListNode RemoveFirstObjectFromOwnedList(void);

        // The following methods provide access to the native wait lock for 
        // those implementations that need a lock to protect the support for 
        // native thread blocking (e.g.: pthread conditions)
        void AcquireNativeWaitLock(void);
        void ReleaseNativeWaitLock(void);
        bool TryAcquireNativeWaitLock(void);
    };

    class CThreadApcInfo : public CThreadInfoInitializer
    {
        friend class CPalSynchronizationManager;

        PThreadApcInfoNode m_ptainHead;
        PThreadApcInfoNode m_ptainTail;
        
    public:
        CThreadApcInfo() :
            m_ptainHead(NULL),
            m_ptainTail(NULL)
        {
        }
    };
        
    class CPalSynchMgrController
    {
    public:
        static IPalSynchronizationManager * CreatePalSynchronizationManager();

        static PAL_ERROR StartWorker(CPalThread * pthrCurrent);

        static PAL_ERROR PrepareForShutdown(void);
        
        static PAL_ERROR Shutdown(CPalThread *pthrCurrent, bool fFullCleanup);
    };
}

#endif // _SINCHOBJECTS_HPP_