~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NThreadGNU.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
 
 
3
 
 
4
NAMESPACE_BEGIN
 
5
 
 
6
#ifdef _WIN32
 
7
    #define INL_ATOMOP_ITERLOCKED_INCREMENT      InterlockedIncrement
 
8
    #define INL_ATOMOP_ITERLOCKED_DECREMENT      InterlockedDecrement
 
9
    #define INL_ATOMOP_ITERLOCKED_EXCHANGED      InterlockedExchange
 
10
    #define INL_ATOMOP_ITERLOCKED_VALUE
 
11
#elif _WIN64
 
12
    #define INL_ATOMOP_ITERLOCKED_INCREMENT      InterlockedIncrement64
 
13
    #define INL_ATOMOP_ITERLOCKED_DECREMENT      InterlockedDecrement64
 
14
    #define INL_ATOMOP_ITERLOCKED_EXCHANGED      InterlockedExchange64
 
15
    #define INL_ATOMOP_ITERLOCKED_VALUE
 
16
#endif
 
17
 
 
18
t_int NThreadSafeCounter::Increment()
 
19
{
 
20
    return __sync_add_and_fetch(&m_Counter, 1);
 
21
}
 
22
 
 
23
t_int NThreadSafeCounter::Decrement()
 
24
{
 
25
    return __sync_add_and_fetch(&m_Counter, -1);
 
26
}
 
27
 
 
28
t_int NThreadSafeCounter::Set(t_int i)
 
29
{
 
30
    m_Counter = i;
 
31
    return m_Counter;
 
32
}
 
33
 
 
34
t_int NThreadSafeCounter::GetValue() const
 
35
{
 
36
    return m_Counter;
 
37
}
 
38
 
 
39
t_int NThreadSafeCounter::operator ++ ()
 
40
{
 
41
    return Increment();
 
42
}
 
43
 
 
44
t_int NThreadSafeCounter::operator -- ()
 
45
{
 
46
    return Decrement();
 
47
}
 
48
 
 
49
t_bool NThreadSafeCounter::operator == (t_int i)
 
50
{
 
51
    return (m_Counter == i);
 
52
}
 
53
 
 
54
__thread void* NThreadLocalStorage::m_TLSIndex[NThreadLocalStorage::NbTLS];    
 
55
BOOL NThreadLocalStorage::m_TLSUsed[NThreadLocalStorage::NbTLS];
 
56
NThreadLocalStorage::TLS_ShutdownCallback  NThreadLocalStorage::m_TLSCallbacks[NThreadLocalStorage::NbTLS];
 
57
 
 
58
BOOL NThreadLocalStorage::RegisterTLS(t_u32 index, NThreadLocalStorage::TLS_ShutdownCallback shutdownCallback)
 
59
{
 
60
    nuxAssert(!m_TLSUsed[index]);
 
61
 
 
62
    if (!m_TLSUsed[index])
 
63
    {
 
64
//         m_TLSIndex[index] = TlsAlloc();
 
65
//         if(m_TLSIndex[index] == TLS_OUT_OF_INDEXES)
 
66
//         {
 
67
//             nuxAssertMsg(0, TEXT("[NThreadLocalStorage::RegisterTLS] Out of TLS index."));
 
68
//         }
 
69
        m_TLSUsed[index]  = TRUE;       
 
70
        m_TLSCallbacks[index] =  shutdownCallback;
 
71
        return TRUE;
 
72
    }
 
73
    else
 
74
    {
 
75
        return FALSE;
 
76
    }
 
77
}
 
78
 
 
79
void NThreadLocalStorage::Initialize()
 
80
{
 
81
    Memset(m_TLSUsed, 0, sizeof(m_TLSUsed));
 
82
 
 
83
    for (t_u32 i = 0; i < NThreadLocalStorage::NbTLS; i++)
 
84
    {
 
85
        // Fill the array with invalid values
 
86
        m_TLSIndex[i] = 0;
 
87
    }
 
88
}
 
89
 
 
90
void NThreadLocalStorage::Shutdown()
 
91
{
 
92
    ThreadShutdown();
 
93
}
 
94
 
 
95
void NThreadLocalStorage::ThreadInit()
 
96
{
 
97
}
 
98
 
 
99
void NThreadLocalStorage::ThreadShutdown()
 
100
{
 
101
    TLS_ShutdownCallback *callback = m_TLSCallbacks;
 
102
    for (t_u32 i = 0; i < NThreadLocalStorage::NbTLS; ++i, ++callback)
 
103
    {
 
104
        if (*callback)
 
105
        {
 
106
            (**callback)();
 
107
        }
 
108
    }
 
109
}
 
110
 
 
111
IMPLEMENT_ROOT_OBJECT_TYPE(NThread);
 
112
 
 
113
NThread::NThread()
 
114
:   m_ThreadState(THREADINIT)
 
115
 
116
    m_pThreadFunc = NThread::EntryPoint; // Can call Detach() also.
 
117
}
 
118
 
 
119
NThread::NThread(ThreadRoutineFunc lpExternalRoutine)
 
120
{
 
121
    Attach(lpExternalRoutine);
 
122
}
 
123
 
 
124
NThread::~NThread()
 
125
{
 
126
    if(m_ThreadCtx.m_dwTID)
 
127
        pthread_detach(m_ThreadCtx.m_dwTID);
 
128
}
 
129
 
 
130
ThreadState NThread::Start( void* arg )
 
131
{
 
132
    m_ThreadCtx.m_pUserData = arg;
 
133
    int ret = pthread_create(&m_ThreadCtx.m_dwTID,
 
134
        NULL,
 
135
        m_pThreadFunc,
 
136
        this);
 
137
 
 
138
    if(ret != 0)
 
139
    {
 
140
        nuxDebugMsg(TEXT("[NThread::Start] Cannot start thread."));
 
141
        m_ThreadState = THREAD_START_ERROR;
 
142
        return m_ThreadState;
 
143
    }
 
144
    return m_ThreadState;
 
145
}
 
146
 
 
147
ThreadState NThread::Stop( bool bForceKill )
 
148
{
 
149
    int ret = pthread_detach(m_ThreadCtx.m_dwTID);
 
150
    if(ret != 0)
 
151
    {
 
152
        nuxDebugMsg(TEXT("[NThread::Stop] Cannot detach thread."));
 
153
        m_ThreadState = THREAD_STOP_ERROR;
 
154
        return m_ThreadState;
 
155
    }
 
156
    m_ThreadState = THREADSTOP;
 
157
    return m_ThreadState;
 
158
}
 
159
 
 
160
ThreadState NThread::Suspend()
 
161
{
 
162
    m_ThreadState = THREADSUSPENDED;
 
163
    return m_ThreadState;
 
164
}
 
165
 
 
166
ThreadState NThread::Resume()
 
167
{
 
168
    return m_ThreadState;
 
169
}
 
170
 
 
171
// go from suspended to thread start
 
172
ThreadState NThread::ResumeStart()
 
173
{
 
174
    m_ThreadState = THREADINIT;
 
175
    return m_ThreadState;
 
176
}
 
177
 
 
178
// go from suspended to thread exit
 
179
ThreadState NThread::ResumeExit()
 
180
{
 
181
    m_ThreadState = THREADSTOP;
 
182
    return m_ThreadState;
 
183
}
 
184
 
 
185
void* NThread::EntryPoint(void* pArg)
 
186
{
 
187
    NThread *pParent = reinterpret_cast<NThread*>(pArg);
 
188
    if(pParent == 0)
 
189
    {
 
190
        nuxDebugMsg(TEXT("[NThread::EntryPoint] Invalid pointer. The thread will exit."));
 
191
        return 0;
 
192
    }
 
193
 
 
194
    if(!pParent->ThreadCtor())
 
195
    {
 
196
        // return another message saying the thread could not execute due to error in ThreadCtor;
 
197
    }
 
198
 
 
199
    pParent->Run( pParent->m_ThreadCtx.m_pUserData );
 
200
    
 
201
    pParent->ThreadDtor();
 
202
    return 0;
 
203
}
 
204
 
 
205
t_u32 NThread::GetExitCode() const 
 
206
 
207
    return m_ThreadCtx.m_dwExitCode;
 
208
}
 
209
 
 
210
t_u32 NThread::GetThreadId()
 
211
{
 
212
    return (t_u32)m_ThreadCtx.m_dwTID;
 
213
}
 
214
 
 
215
ThreadState NThread::GetThreadState() const
 
216
{
 
217
    return m_ThreadState;
 
218
}
 
219
 
 
220
void NThread::SetThreadState(ThreadState state)
 
221
{
 
222
    m_ThreadState = state;
 
223
}
 
224
 
 
225
NAMESPACE_END