2011-08-06 12:24:36 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-06 12:24:36 +04:00
|
|
|
* Thread Utils
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-06 12:24:36 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2011-08-17 01:40:29 +04:00
|
|
|
|
2012-05-05 03:36:35 +04:00
|
|
|
#include <winpr/windows.h>
|
2012-03-06 06:04:42 +04:00
|
|
|
|
2011-08-17 01:40:29 +04:00
|
|
|
#ifdef _WIN32
|
2011-12-21 04:50:47 +04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <process.h>
|
|
|
|
#endif
|
2011-08-17 01:40:29 +04:00
|
|
|
#endif
|
|
|
|
|
2011-08-17 00:04:28 +04:00
|
|
|
#include <freerdp/utils/sleep.h>
|
2011-08-06 12:24:36 +04:00
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/utils/thread.h>
|
|
|
|
|
|
|
|
freerdp_thread* freerdp_thread_new(void)
|
|
|
|
{
|
|
|
|
freerdp_thread* thread;
|
|
|
|
|
|
|
|
thread = xnew(freerdp_thread);
|
2012-09-19 01:33:52 +04:00
|
|
|
thread->mutex = CreateMutex(NULL, FALSE, NULL);
|
2011-08-06 12:24:36 +04:00
|
|
|
thread->signals[0] = wait_obj_new();
|
|
|
|
thread->signals[1] = wait_obj_new();
|
|
|
|
thread->num_signals = 2;
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_thread_start(freerdp_thread* thread, void* func, void* arg)
|
|
|
|
{
|
|
|
|
thread->status = 1;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
{
|
2012-09-16 23:30:11 +04:00
|
|
|
CloseHandle(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL));
|
2011-08-06 12:24:36 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
pthread_t th;
|
|
|
|
pthread_create(&th, 0, func, arg);
|
|
|
|
pthread_detach(th);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_thread_stop(freerdp_thread* thread)
|
|
|
|
{
|
2011-08-16 22:41:12 +04:00
|
|
|
int i = 0;
|
2011-08-06 12:24:36 +04:00
|
|
|
|
2011-08-16 22:41:12 +04:00
|
|
|
wait_obj_set(thread->signals[0]);
|
|
|
|
|
2011-08-06 12:24:36 +04:00
|
|
|
while (thread->status > 0 && i < 1000)
|
|
|
|
{
|
|
|
|
i++;
|
2011-10-19 18:10:45 +04:00
|
|
|
freerdp_usleep(100000);
|
2011-08-06 12:24:36 +04:00
|
|
|
}
|
2011-09-19 18:52:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_thread_free(freerdp_thread* thread)
|
|
|
|
{
|
|
|
|
int i;
|
2011-08-06 12:24:36 +04:00
|
|
|
|
|
|
|
for (i = 0; i < thread->num_signals; i++)
|
|
|
|
wait_obj_free(thread->signals[i]);
|
2012-09-19 01:33:52 +04:00
|
|
|
|
2011-08-06 12:24:36 +04:00
|
|
|
thread->num_signals = 0;
|
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
CloseHandle(thread->mutex);
|
2011-08-06 12:24:36 +04:00
|
|
|
thread->mutex = NULL;
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(thread);
|
2011-08-06 12:24:36 +04:00
|
|
|
}
|