~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to winpr/libwinpr/thread/thread.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.1.9) (9.1.17 sid)
  • Revision ID: package-import@ubuntu.com-20141111122050-wyr8hrnwco9fcmum
Tags: 1.1.0~git20140921.1.440916e+dfsg1-2ubuntu1
* Merge with Debian unstable, remaining changes
  - Disable ffmpeg support
* Disable gstreamer support, this relies on gstreamer 0.10 and we don't want
  to add any more deps on that.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * WinPR: Windows Portable Runtime
 
3
 * Process Thread Functions
 
4
 *
 
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 
6
 *
 
7
 * Licensed under the Apache License, Version 2.0 (the "License");
 
8
 * you may not use this file except in compliance with the License.
 
9
 * You may obtain a copy of the License at
 
10
 *
 
11
 *     http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
13
 * Unless required by applicable law or agreed to in writing, software
 
14
 * distributed under the License is distributed on an "AS IS" BASIS,
 
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
16
 * See the License for the specific language governing permissions and
 
17
 * limitations under the License.
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <winpr/handle.h>
 
25
 
 
26
#include <winpr/thread.h>
 
27
 
 
28
/**
 
29
 * api-ms-win-core-processthreads-l1-1-1.dll
 
30
 * 
 
31
 * CreateRemoteThread
 
32
 * CreateRemoteThreadEx
 
33
 * CreateThread
 
34
 * DeleteProcThreadAttributeList
 
35
 * ExitThread
 
36
 * FlushInstructionCache
 
37
 * FlushProcessWriteBuffers
 
38
 * GetCurrentThread
 
39
 * GetCurrentThreadId
 
40
 * GetCurrentThreadStackLimits
 
41
 * GetExitCodeThread
 
42
 * GetPriorityClass
 
43
 * GetStartupInfoW
 
44
 * GetThreadContext
 
45
 * GetThreadId
 
46
 * GetThreadIdealProcessorEx
 
47
 * GetThreadPriority
 
48
 * GetThreadPriorityBoost
 
49
 * GetThreadTimes
 
50
 * InitializeProcThreadAttributeList
 
51
 * OpenThread
 
52
 * OpenThreadToken
 
53
 * QueryProcessAffinityUpdateMode
 
54
 * QueueUserAPC
 
55
 * ResumeThread
 
56
 * SetPriorityClass
 
57
 * SetThreadContext
 
58
 * SetThreadPriority
 
59
 * SetThreadPriorityBoost
 
60
 * SetThreadStackGuarantee
 
61
 * SetThreadToken
 
62
 * SuspendThread
 
63
 * SwitchToThread
 
64
 * TerminateThread
 
65
 * UpdateProcThreadAttribute
 
66
 */
 
67
 
 
68
#ifndef _WIN32
 
69
 
 
70
#include <winpr/crt.h>
 
71
 
 
72
#include "thread.h"
 
73
 
 
74
#include "../handle/handle.h"
 
75
 
 
76
/**
 
77
 * TODO: implement thread suspend/resume using pthreads
 
78
 * http://stackoverflow.com/questions/3140867/suspend-pthreads-without-using-condition
 
79
 */
 
80
 
 
81
void winpr_StartThread(WINPR_THREAD* thread)
 
82
{
 
83
        pthread_attr_t attr;
 
84
 
 
85
        pthread_attr_init(&attr);
 
86
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
87
 
 
88
        if (thread->dwStackSize > 0)
 
89
                pthread_attr_setstacksize(&attr, (size_t) thread->dwStackSize);
 
90
 
 
91
        thread->started = TRUE;
 
92
        pthread_create(&thread->thread, &attr, (pthread_start_routine) thread->lpStartAddress, thread->lpParameter);
 
93
 
 
94
        pthread_attr_destroy(&attr);
 
95
}
 
96
 
 
97
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
 
98
        LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
 
99
{
 
100
        HANDLE handle;
 
101
        WINPR_THREAD* thread;
 
102
 
 
103
        thread = (WINPR_THREAD*) malloc(sizeof(WINPR_THREAD));
 
104
        ZeroMemory(thread, sizeof(WINPR_THREAD));
 
105
 
 
106
        thread->started = FALSE;
 
107
        thread->dwStackSize = dwStackSize;
 
108
        thread->lpParameter = lpParameter;
 
109
        thread->lpStartAddress = lpStartAddress;
 
110
        thread->lpThreadAttributes = lpThreadAttributes;
 
111
 
 
112
        pthread_mutex_init(&thread->mutex, 0);
 
113
 
 
114
        WINPR_HANDLE_SET_TYPE(thread, HANDLE_TYPE_THREAD);
 
115
        handle = (HANDLE) thread;
 
116
 
 
117
        if (!(dwCreationFlags & CREATE_SUSPENDED))
 
118
                winpr_StartThread(thread);
 
119
 
 
120
        return handle;
 
121
}
 
122
 
 
123
HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
 
124
                LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
 
125
{
 
126
        return NULL;
 
127
}
 
128
 
 
129
VOID ExitThread(DWORD dwExitCode)
 
130
{
 
131
        pthread_exit((void*) (size_t) dwExitCode);
 
132
}
 
133
 
 
134
BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
 
135
{
 
136
        ULONG Type;
 
137
        PVOID Object;
 
138
        WINPR_THREAD* thread;
 
139
 
 
140
        if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
 
141
                return FALSE;
 
142
 
 
143
        thread = (WINPR_THREAD*) Object;
 
144
 
 
145
        *lpExitCode = thread->dwExitCode;
 
146
 
 
147
        return TRUE;
 
148
}
 
149
 
 
150
HANDLE _GetCurrentThread(VOID)
 
151
{
 
152
        return NULL;
 
153
}
 
154
 
 
155
DWORD GetCurrentThreadId(VOID)
 
156
{
 
157
        pthread_t tid;
 
158
        tid = pthread_self();
 
159
        return (DWORD) tid;
 
160
}
 
161
 
 
162
DWORD ResumeThread(HANDLE hThread)
 
163
{
 
164
        ULONG Type;
 
165
        PVOID Object;
 
166
        WINPR_THREAD* thread;
 
167
 
 
168
        if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
 
169
                return 0;
 
170
 
 
171
        thread = (WINPR_THREAD*) Object;
 
172
 
 
173
        pthread_mutex_lock(&thread->mutex);
 
174
 
 
175
        if (!thread->started)
 
176
                winpr_StartThread(thread);
 
177
 
 
178
        pthread_mutex_unlock(&thread->mutex);
 
179
 
 
180
        return 0;
 
181
}
 
182
 
 
183
DWORD SuspendThread(HANDLE hThread)
 
184
{
 
185
        return 0;
 
186
}
 
187
 
 
188
BOOL SwitchToThread(VOID)
 
189
{
 
190
        return TRUE;
 
191
}
 
192
 
 
193
BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode)
 
194
{
 
195
        ULONG Type;
 
196
        PVOID Object;
 
197
        WINPR_THREAD* thread;
 
198
 
 
199
        if (!winpr_Handle_GetInfo(hThread, &Type, &Object))
 
200
                return 0;
 
201
 
 
202
        thread = (WINPR_THREAD*) Object;
 
203
 
 
204
        pthread_mutex_lock(&thread->mutex);
 
205
 
 
206
#ifndef ANDROID
 
207
        pthread_cancel(thread->thread);
 
208
#endif
 
209
 
 
210
        pthread_mutex_unlock(&thread->mutex);
 
211
 
 
212
        return TRUE;
 
213
}
 
214
 
 
215
#endif
 
216