~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

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