SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RakThread.cpp
Go to the documentation of this file.
1 /*
2  * Original work: Copyright (c) 2014, Oculus VR, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
16 #include "slikenet/thread.h"
17 #include "slikenet/assert.h"
18 #include "slikenet/defines.h"
19 #include "slikenet/sleep.h"
21 
22 using namespace SLNet;
23 
24 
25 
26 
27 #if defined(_WIN32)
29  #include <stdio.h>
30  #if !defined(_WIN32_WCE)
31  #include <process.h>
32  #endif
33 
34 
35 
36 
37 #else
38 #include <pthread.h>
39 #endif
40 
41 #if defined(_WIN32_WCE) || defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
42 int RakThread::Create( LPTHREAD_START_ROUTINE start_address, void *arglist, int priority)
43 #elif defined(_WIN32)
44 int RakThread::Create( unsigned __stdcall start_address( void* ), void *arglist, int priority)
45 
46 
47 
48 #else
49 int RakThread::Create( void* start_address( void* ), void *arglist, int priority)
50 #endif
51 {
52 #ifdef _WIN32
53  HANDLE threadHandle;
54  unsigned threadID = 0;
55 
56 
57 #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
58  threadHandle = CreateThread(NULL,0,start_address,arglist,CREATE_SUSPENDED, 0);
59 #elif defined _WIN32_WCE
60  threadHandle = CreateThread(NULL,MAX_ALLOCA_STACK_ALLOCATION*2,start_address,arglist,0,(DWORD*)&threadID);
61 #else
62  threadHandle = (HANDLE) _beginthreadex( NULL, MAX_ALLOCA_STACK_ALLOCATION*2, start_address, arglist, 0, &threadID );
63 #endif
64 
65  SetThreadPriority(threadHandle, priority);
66 
67 #if defined(WINDOWS_PHONE_8) || defined(WINDOWS_STORE_RT)
68  ResumeThread(threadHandle);
69 #endif
70 
71  if (threadHandle==0)
72  {
73  return 1;
74  }
75  else
76  {
77  CloseHandle( threadHandle );
78  return 0;
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 #else
120  pthread_t threadHandle;
121  // Create thread linux
122  pthread_attr_t attr;
123  sched_param param;
124  param.sched_priority=priority;
125  pthread_attr_init( &attr );
126  pthread_attr_setschedparam(&attr, &param);
127 
128 
129 
130 
131 
132  pthread_attr_setstacksize(&attr, MAX_ALLOCA_STACK_ALLOCATION*2);
133 
134  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
135  int res = pthread_create( &threadHandle, &attr, start_address, arglist );
136  RakAssert(res==0 && "pthread_create in RakThread.cpp failed.")
137  return res;
138 #endif
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