aboutsummaryrefslogtreecommitdiff
path: root/xbmc/linux/XThreadUtils.cpp
blob: 86ee000ada55078b234a71c5ae5f958c4e2e28bd (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 *      Copyright (C) 2005-2009 Team XBMC
 *      http://www.xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "PlatformDefs.h"
#include "XHandle.h"
#include "XThreadUtils.h"
#include "XTimeUtils.h"
#include "XEventUtils.h"
#include "system.h"
#include "log.h"
#include "GraphicContext.h"

#ifdef _LINUX
#include <signal.h>
#include <pthread.h>
#include <limits.h>

// a variable which is defined __thread will be defined in thread local storage
// which means it will be different for each thread that accesses it.
#define TLS_INDEXES 16
#define TLS_OUT_OF_INDEXES (DWORD)0xFFFFFFFF

#ifdef __APPLE__
// FIXME, this needs to be converted to use pthread_once.
static LPVOID tls[TLS_INDEXES] = { NULL };
#else
static LPVOID __thread tls[TLS_INDEXES] = { NULL };
#endif

static BOOL tls_used[TLS_INDEXES];

HANDLE WINAPI CreateThread(
      LPSECURITY_ATTRIBUTES lpThreadAttributes,
        SIZE_T dwStackSize,
          LPTHREAD_START_ROUTINE lpStartAddress,
            LPVOID lpParameter,
        DWORD dwCreationFlags,
          LPDWORD lpThreadId
    ) {

  // a thread handle would actually contain an event
  // the event would mark if the thread is running or not. it will be used in the Wait functions.
  HANDLE h = CreateEvent(NULL, TRUE, FALSE, NULL);
  h->ChangeType(CXHandle::HND_THREAD);
  h->m_nRefCount++;
#ifdef __APPLE__
  h->m_machThreadPort = MACH_PORT_NULL;
#endif
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  if (dwStackSize > PTHREAD_STACK_MIN)
    pthread_attr_setstacksize(&attr, dwStackSize);
  if (pthread_create(&(h->m_hThread), &attr, (void*(*)(void*))lpStartAddress, lpParameter) == 0)
    h->m_threadValid = true;
  else
  {
    CloseHandle(h);
    h = NULL;
  }
  pthread_attr_destroy(&attr);
  if (h && lpThreadId)
    // WARNING: This can truncate thread IDs on x86_64.
    *lpThreadId = (DWORD)h->m_hThread;
  return h;
}


#if 0 // Deprecated, use CThread::GetCurrentThreadId() instead
DWORD WINAPI GetCurrentThreadId(void) {
  // WARNING:  This can truncate thread IDs on x86_64.
  return (DWORD)pthread_self();
}
#endif

HANDLE WINAPI GetCurrentThread(void) {
  return (HANDLE)-1; // -1 a special value - pseudo handle
}

HANDLE WINAPI GetCurrentProcess(void) {
  return (HANDLE)-1; // -1 a special value - pseudo handle
}

HANDLE _beginthreadex(
   void *security,
   unsigned stack_size,
   int ( *start_address )( void * ),
   void *arglist,
   unsigned initflag,
   unsigned *thrdaddr
) {

  HANDLE h = CreateThread(NULL, stack_size, start_address, arglist, initflag, (LPDWORD)thrdaddr);
  return h;

}

uintptr_t _beginthread(
    void( *start_address )( void * ),
    unsigned stack_size,
    void *arglist
) {
  HANDLE h = CreateThread(NULL, stack_size, (LPTHREAD_START_ROUTINE)start_address, arglist, 0, NULL);
  return (uintptr_t)h;
}

BOOL WINAPI GetThreadTimes (
  HANDLE hThread,
  LPFILETIME lpCreationTime,
  LPFILETIME lpExitTime,
  LPFILETIME lpKernelTime,
  LPFILETIME lpUserTime
) {
  if (!hThread)
    return false;
  if (!hThread->m_threadValid)
    return false;

  if (hThread == (HANDLE)-1) {
    if (lpCreationTime)
      TimeTToFileTime(0,lpCreationTime);
    if (lpExitTime)
      TimeTToFileTime(time(NULL),lpExitTime);
    if (lpKernelTime)
      TimeTToFileTime(0,lpKernelTime);
    if (lpUserTime)
      TimeTToFileTime(0,lpUserTime);

    return true;
  }

  if (lpCreationTime)
    TimeTToFileTime(hThread->m_tmCreation,lpCreationTime);
  if (lpExitTime)
    TimeTToFileTime(time(NULL),lpExitTime);
  if (lpKernelTime)
    TimeTToFileTime(0,lpKernelTime);

#ifdef __APPLE__
  thread_info_data_t     threadInfo;
  mach_msg_type_number_t threadInfoCount = THREAD_INFO_MAX;
  
  if (hThread->m_machThreadPort == MACH_PORT_NULL)
    hThread->m_machThreadPort = pthread_mach_thread_np(hThread->m_hThread);
    
  kern_return_t ret = thread_info(hThread->m_machThreadPort, THREAD_BASIC_INFO, (thread_info_t)threadInfo, &threadInfoCount);
  if (ret == KERN_SUCCESS)
  {
    thread_basic_info_t threadBasicInfo = (thread_basic_info_t)threadInfo;

    if (lpUserTime)
    {
      // User time.
      unsigned long long time = ((__int64)threadBasicInfo->user_time.seconds * 10000000L) + threadBasicInfo->user_time.microseconds*10L;
      lpUserTime->dwLowDateTime = (time & 0xFFFFFFFF);
      lpUserTime->dwHighDateTime = (time >> 32);
    }

    if (lpKernelTime)
    {
      // System time.
      unsigned long long time = ((__int64)threadBasicInfo->system_time.seconds * 10000000L) + threadBasicInfo->system_time.microseconds*10L;
      lpKernelTime->dwLowDateTime = (time & 0xFFFFFFFF);
      lpKernelTime->dwHighDateTime = (time >> 32);
    }
  }
  else
  {
    if (lpUserTime)
      lpUserTime->dwLowDateTime = lpUserTime->dwHighDateTime = 0;

    if (lpKernelTime)
      lpKernelTime->dwLowDateTime = lpKernelTime->dwHighDateTime = 0;
  }
#elif _POSIX_THREAD_CPUTIME != -1
    if(lpUserTime)
    {
      lpUserTime->dwLowDateTime = 0;
      lpUserTime->dwHighDateTime = 0;
      clockid_t clock;
      if (pthread_getcpuclockid(hThread->m_hThread, &clock) == 0)
      {
        struct timespec tp = {};
        clock_gettime(clock, &tp);
        unsigned long long time = (unsigned long long)tp.tv_sec * 10000000 + (unsigned long long)tp.tv_nsec/100;
        lpUserTime->dwLowDateTime = (time & 0xFFFFFFFF);
        lpUserTime->dwHighDateTime = (time >> 32);
      }
    }
#else
  if (lpUserTime)
    TimeTToFileTime(0,lpUserTime);
#endif
  return true;
}

BOOL WINAPI SetThreadPriority(HANDLE hThread, int nPriority)
{
#if defined(__APPLE__)
  struct sched_param sched;
  pthread_t currentThread = pthread_self();
  int rtn, policy;
  
  rtn = pthread_getschedparam(currentThread, &policy, &sched);
  int min = sched_get_priority_min(policy);
  int max = sched_get_priority_max(policy);

  if(nPriority < min)
    sched.sched_priority = min;
  if(nPriority > max)
    sched.sched_priority = max;

  rtn = pthread_setschedparam(currentThread, policy, &sched);
  
  return true;
#else
  return true;
#endif
}

int GetThreadPriority(HANDLE hThread)
{
#if defined(__APPLE__)
  struct sched_param sched;
  int rtn, policy;
  
  rtn = pthread_getschedparam(pthread_self(), &policy, &sched);
  
  return(sched.sched_priority);
#else
  return 0;
#endif
}

// thread local storage -
// we use different method than in windows. TlsAlloc has no meaning since
// we always take the __thread variable "tls".
// so we return static answer in TlsAlloc and do nothing in TlsFree.
LPVOID WINAPI TlsGetValue(DWORD dwTlsIndex) {
   return tls[dwTlsIndex];
}

BOOL WINAPI TlsSetValue(int dwTlsIndex, LPVOID lpTlsValue) {
   tls[dwTlsIndex]=lpTlsValue;
   return true;
}

BOOL WINAPI TlsFree(DWORD dwTlsIndex) {
   tls_used[dwTlsIndex] = false;
   return true;
}

DWORD WINAPI TlsAlloc() {
  for (int i = 0; i < TLS_INDEXES; i++)
  {
    if (!tls_used[i])
    {
      tls_used[i] = TRUE;
      return i;
    }
  }

  return TLS_OUT_OF_INDEXES;
}


#endif