#include <freerdp/utils/stream.h>
#include <freerdp/utils/list.h>
#include <freerdp/utils/thread.h>
-#include <freerdp/utils/mutex.h>
#include <freerdp/utils/event.h>
#include <freerdp/utils/sleep.h>
#include <freerdp/plugins/tsmf.h>
+#include <winpr/synch.h>
+
#include "drdynvc_types.h"
#include "tsmf_constants.h"
#include "tsmf_types.h"
uint64 audio_start_time;
uint64 audio_end_time;
- /* The stream list could be accessed by differnt threads and need to be protected. */
- freerdp_mutex mutex;
+ /* The stream list could be accessed by different threads and need to be protected. */
+ HANDLE mutex;
LIST* stream_list;
};
static LIST* presentation_list = NULL;
static uint64 last_played_audio_time = 0;
-static pthread_mutex_t tsmf_mutex = PTHREAD_MUTEX_INITIALIZER;
+static HANDLE tsmf_mutex = NULL;
static int TERMINATING = 0;
static uint64 get_current_time(void)
/* Check if some other stream has earlier sample that needs to be played first */
if (stream->last_end_time > AUDIO_TOLERANCE)
{
- freerdp_mutex_lock(presentation->mutex);
+ WaitForSingleObject(presentation->mutex, INFINITE);
+
for (item = presentation->stream_list->head; item; item = item->next)
{
s = (TSMF_STREAM*) item->data;
+
if (s != stream && !s->eos && s->last_end_time &&
s->last_end_time < stream->last_end_time - AUDIO_TOLERANCE)
{
break;
}
}
- freerdp_mutex_unlock(presentation->mutex);
+
+ ReleaseMutex(presentation->mutex);
}
}
else
memcpy(presentation->presentation_id, guid, GUID_SIZE);
presentation->channel_callback = pChannelCallback;
- presentation->mutex = freerdp_mutex_new();
+ presentation->mutex = CreateMutex(NULL, FALSE, NULL);
presentation->stream_list = list_new();
list_enqueue(presentation_list, presentation);
TSMF_STREAM* stream;
tsmf_presentation_stop(presentation);
- freerdp_mutex_lock(presentation->mutex);
+ WaitForSingleObject(presentation->mutex, INFINITE);
list_remove(presentation_list, presentation);
- freerdp_mutex_unlock(presentation->mutex);
+ ReleaseMutex(presentation->mutex);
while (list_size(presentation->stream_list) > 0)
{
}
list_free(presentation->stream_list);
- freerdp_mutex_free(presentation->mutex);
+ CloseHandle(presentation->mutex);
xfree(presentation);
}
stream->sample_list = list_new();
stream->sample_ack_list = list_new();
- freerdp_mutex_lock(presentation->mutex);
+ WaitForSingleObject(presentation->mutex, INFINITE);
list_enqueue(presentation->stream_list, stream);
- freerdp_mutex_unlock(presentation->mutex);
+ ReleaseMutex(presentation->mutex);
return stream;
}
tsmf_stream_stop(stream);
tsmf_stream_flush(stream);
- freerdp_mutex_lock(presentation->mutex);
+ WaitForSingleObject(presentation->mutex, INFINITE);
list_remove(presentation->stream_list, stream);
- freerdp_mutex_unlock(presentation->mutex);
+ ReleaseMutex(presentation->mutex);
list_free(stream->sample_list);
list_free(stream->sample_ack_list);
{
TSMF_SAMPLE* sample;
- pthread_mutex_lock(&tsmf_mutex);
+ WaitForSingleObject(tsmf_mutex, INFINITE);
if (TERMINATING)
{
- pthread_mutex_unlock(&tsmf_mutex);
+ ReleaseMutex(tsmf_mutex);
return;
}
- pthread_mutex_unlock(&tsmf_mutex);
+ ReleaseMutex(tsmf_mutex);
sample = xnew(TSMF_SAMPLE);
static void tsmf_signal_handler(int s)
{
- pthread_mutex_lock(&tsmf_mutex);
- TERMINATING = 1;
- pthread_mutex_unlock(&tsmf_mutex);
LIST_ITEM* p_item;
TSMF_PRESENTATION* presentation;
LIST_ITEM* s_item;
TSMF_STREAM* _stream;
+
+ WaitForSingleObject(tsmf_mutex, INFINITE);
+ TERMINATING = 1;
+ ReleaseMutex(tsmf_mutex);
+
if (presentation_list)
{
for (p_item = presentation_list->head; p_item; p_item = p_item->next)
sigaction(SIGUSR1, &sigtrap, 0);
#endif
+ tsmf_mutex = CreateMutex(NULL, FALSE, NULL);
+
if (presentation_list == NULL)
presentation_list = list_new();
}
#include <freerdp/utils/list.h>
#include <freerdp/utils/thread.h>
#include <freerdp/utils/svc_plugin.h>
-#include <freerdp/utils/mutex.h>
-#include <freerdp/utils/debug.h>
+#include <freerdp/utils/debug.h>
#include "rdpdr_types.h"
#include "rdpdr_constants.h"
stream_set_pos(irp->output, pos);
/* Begin TS Client defect workaround. */
- freerdp_mutex_lock(scard->CompletionIdsMutex);
+ WaitForSingleObject(scard->CompletionIdsMutex, INFINITE);
/* Remove from the list the item identified by the CompletionID.
* The function returns whether or not it was a duplicate CompletionID.
*/
duplicate = scard_check_for_duplicate_id(scard, irp->CompletionId);
- freerdp_mutex_unlock(scard->CompletionIdsMutex);
+ ReleaseMutex(scard->CompletionIdsMutex);
if (false == duplicate)
{
CompletionIdInfo->ID = irp->CompletionId;/* "duplicate" member is set
* to false by "xnew()"
*/
- freerdp_mutex_lock(scard->CompletionIdsMutex);
+ WaitForSingleObject(scard->CompletionIdsMutex, INFINITE);
scard_mark_duplicate_id(scard, irp->CompletionId);
list_enqueue(scard->CompletionIds, CompletionIdInfo);
- freerdp_mutex_unlock(scard->CompletionIdsMutex);
+ ReleaseMutex(scard->CompletionIdsMutex);
irp->Complete = scard_irp_complete; /* Overwrite the previous
* assignment made in
freerdp_thread_signal(scard->thread);
}
-int
-DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
+int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
{
- SCARD_DEVICE* scard;
char* name;
char* path;
int i, length;
+ SCARD_DEVICE* scard;
- name = (char *)pEntryPoints->plugin_data->data[1];
- path = (char *)pEntryPoints->plugin_data->data[2];
+ name = (char*) pEntryPoints->plugin_data->data[1];
+ path = (char*) pEntryPoints->plugin_data->data[2];
if (name)
{
scard->thread = freerdp_thread_new();
scard->CompletionIds = list_new();
- scard->CompletionIdsMutex = freerdp_mutex_new();
+ scard->CompletionIdsMutex = CreateMutex(NULL, FALSE, NULL);
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE *)scard);
#include "devman.h"
#include "rdpdr_types.h"
-#include <freerdp/utils/mutex.h> /* For CompletionIdsMutex */
+
#include <freerdp/utils/list.h>
#include <freerdp/utils/debug.h>
+#include <winpr/synch.h>
+
/*
* When using Windows Server 2008 R2 as the Terminal Services (TS)
* server, and with a smart card reader connected to the TS client machine
freerdp_thread* thread;
LIST* CompletionIds;
- freerdp_mutex CompletionIdsMutex; /* Protect the LIST from
+ HANDLE CompletionIdsMutex; /* Protect the LIST from
* multiple thread writers.
*/
};
#include <termios.h>
#include <unistd.h>
#include <freerdp/freerdp.h>
-#include <freerdp/utils/mutex.h>
#include <freerdp/utils/semaphore.h>
#include <freerdp/utils/load_plugin.h>
#include <freerdp/utils/wait_obj.h>
{
add_test_suite(utils);
- add_test_function(mutex);
add_test_function(semaphore);
add_test_function(load_plugin);
add_test_function(wait_obj);
return 0;
}
-void test_mutex(void)
-{
- freerdp_mutex mutex;
-
- mutex = freerdp_mutex_new();
- freerdp_mutex_lock(mutex);
- freerdp_mutex_unlock(mutex);
- freerdp_mutex_free(mutex);
-}
-
void test_semaphore(void)
{
freerdp_sem sem;
int clean_utils_suite(void);
int add_utils_suite(void);
-void test_mutex(void);
void test_semaphore(void);
void test_load_plugin(void);
void test_wait_obj(void);
+++ /dev/null
-/**
- * FreeRDP: A Remote Desktop Protocol Client
- * Mutex 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.
- */
-
-#ifndef __MUTEX_UTILS_H
-#define __MUTEX_UTILS_H
-
-#include <freerdp/api.h>
-
-typedef void* freerdp_mutex;
-
-FREERDP_API freerdp_mutex freerdp_mutex_new(void);
-FREERDP_API void freerdp_mutex_free(freerdp_mutex mutex);
-FREERDP_API void freerdp_mutex_lock(freerdp_mutex mutex);
-FREERDP_API void freerdp_mutex_unlock(freerdp_mutex mutex);
-
-#endif /* __MUTEX_UTILS_H */
#include <freerdp/api.h>
#include <freerdp/types.h>
-#include <freerdp/utils/mutex.h>
#include <freerdp/utils/wait_obj.h>
#ifndef _WIN32
#include <pthread.h>
#endif
+#include <winpr/synch.h>
+
typedef struct _freerdp_thread freerdp_thread;
struct _freerdp_thread
{
- freerdp_mutex* mutex;
+ HANDLE mutex;
struct wait_obj* signals[5];
int num_signals;
wait_obj_clear(_t->signals[0]); } while (0)
#define freerdp_thread_signal(_t) wait_obj_set(_t->signals[1])
#define freerdp_thread_reset(_t) wait_obj_clear(_t->signals[1])
-#define freerdp_thread_lock(_t) freerdp_mutex_lock(_t->mutex)
-#define freerdp_thread_unlock(_t) freerdp_mutex_unlock(_t->mutex)
+#define freerdp_thread_lock(_t) WaitForSingleObject(_t->mutex, INFINITE)
+#define freerdp_thread_unlock(_t) ReleaseMutex(_t->mutex)
#endif /* __THREAD_UTILS_H */
set(FREERDP_CHANNELS_LIBS)
if(WITH_MONOLITHIC_BUILD)
+ set(FREERDP_CHANNELS_LIBS ${FREERDP_CHANNELS_LIBS} winpr)
set(FREERDP_LIBS ${FREERDP_LIBS} ${FREERDP_CHANNELS_LIBS} PARENT_SCOPE)
else()
set(FREERDP_CHANNELS_LIBS ${FREERDP_CHANNELS_LIBS}
freerdp-utils)
+
+ set(FREERDP_CHANNELS_LIBS ${FREERDP_CHANNELS_LIBS}
+ winpr-synch)
target_link_libraries(freerdp-channels ${FREERDP_CHANNELS_LIBS})
install(TARGETS freerdp-channels DESTINATION ${CMAKE_INSTALL_LIBDIR})
#include <freerdp/utils/memory.h>
#include <freerdp/utils/list.h>
#include <freerdp/utils/semaphore.h>
-#include <freerdp/utils/mutex.h>
#include <freerdp/utils/wait_obj.h>
#include <freerdp/utils/load_plugin.h>
#include <freerdp/utils/event.h>
+#include <winpr/synch.h>
+
#include "libchannels.h"
#define CHANNEL_MAX_COUNT 30
struct wait_obj* signal;
/* used for sync write */
- freerdp_mutex sync_data_mutex;
+ HANDLE sync_data_mutex;
LIST* sync_data_list;
/* used for sync event */
static int g_open_handle_sequence;
/* For locking the global resources */
-static freerdp_mutex g_mutex_init;
-static freerdp_mutex g_mutex_list;
+static HANDLE g_mutex_init;
+static HANDLE g_mutex_list;
/* returns the channels for the open handle passed in */
static rdpChannels* freerdp_channels_find_by_open_handle(int open_handle, int* pindex)
rdpChannels* channels;
rdpChannelsList* channels_list;
- freerdp_mutex_lock(g_mutex_list);
+ WaitForSingleObject(g_mutex_list, INFINITE);
for (channels_list = g_channels_list; channels_list; channels_list = channels_list->next)
{
{
if (channels->channels_data[lindex].open_handle == open_handle)
{
- freerdp_mutex_unlock(g_mutex_list);
+ ReleaseMutex(g_mutex_list);
*pindex = lindex;
return channels;
}
}
}
- freerdp_mutex_unlock(g_mutex_list);
+ ReleaseMutex(g_mutex_list);
return NULL;
}
rdpChannels* channels;
rdpChannelsList* channels_list;
- freerdp_mutex_lock(g_mutex_list);
+ WaitForSingleObject(g_mutex_list, INFINITE);
for (channels_list = g_channels_list; channels_list; channels_list = channels_list->next)
{
channels = channels_list->channels;
if (channels->instance == instance)
{
- freerdp_mutex_unlock(g_mutex_list);
+ ReleaseMutex(g_mutex_list);
return channels;
}
}
- freerdp_mutex_unlock(g_mutex_list);
+ ReleaseMutex(g_mutex_list);
return NULL;
}
lchannel_def = pChannel + index;
lchannel_data = channels->channels_data + channels->num_channels_data;
- freerdp_mutex_lock(g_mutex_list);
+ WaitForSingleObject(g_mutex_list, INFINITE);
lchannel_data->open_handle = g_open_handle_sequence++;
- freerdp_mutex_unlock(g_mutex_list);
+ ReleaseMutex(g_mutex_list);
lchannel_data->flags = 1; /* init */
strncpy(lchannel_data->name, lchannel_def->name, CHANNEL_NAME_LEN);
return CHANNEL_RC_NOT_OPEN;
}
- freerdp_mutex_lock(channels->sync_data_mutex); /* lock channels->sync* vars */
+ WaitForSingleObject(channels->sync_data_mutex, INFINITE); /* lock channels->sync* vars */
if (!channels->is_connected)
{
- freerdp_mutex_unlock(channels->sync_data_mutex);
+ ReleaseMutex(channels->sync_data_mutex);
DEBUG_CHANNELS("error not connected");
return CHANNEL_RC_NOT_CONNECTED;
}
item->user_data = pUserData;
item->index = index;
list_enqueue(channels->sync_data_list, item);
- freerdp_mutex_unlock(channels->sync_data_mutex);
+ ReleaseMutex(channels->sync_data_mutex);
/* set the event */
wait_obj_set(channels->signal);
g_init_channels = NULL;
g_channels_list = NULL;
g_open_handle_sequence = 1;
- g_mutex_init = freerdp_mutex_new();
- g_mutex_list = freerdp_mutex_new();
+ g_mutex_init = CreateMutex(NULL, FALSE, NULL);
+ g_mutex_list = CreateMutex(NULL, FALSE, NULL);
return 0;
}
while (g_channels_list)
freerdp_channels_free(g_channels_list->channels);
- freerdp_mutex_free(g_mutex_init);
- freerdp_mutex_free(g_mutex_list);
+ CloseHandle(g_mutex_init);
+ CloseHandle(g_mutex_list);
return 0;
}
channels = xnew(rdpChannels);
- channels->sync_data_mutex = freerdp_mutex_new();
+ channels->sync_data_mutex = CreateMutex(NULL, FALSE, NULL);
channels->sync_data_list = list_new();
channels->event_sem = freerdp_sem_new(1);
channels_list = xnew(rdpChannelsList);
channels_list->channels = channels;
- freerdp_mutex_lock(g_mutex_list);
+ WaitForSingleObject(g_mutex_list, INFINITE);
channels_list->next = g_channels_list;
g_channels_list = channels_list;
- freerdp_mutex_unlock(g_mutex_list);
+ ReleaseMutex(g_mutex_list);
return channels;
}
rdpChannelsList* list;
rdpChannelsList* prev;
- freerdp_mutex_free(channels->sync_data_mutex);
+ CloseHandle(channels->sync_data_mutex);
list_free(channels->sync_data_list);
freerdp_sem_free(channels->event_sem);
/* Remove from global list */
- freerdp_mutex_lock(g_mutex_list);
+ WaitForSingleObject(g_mutex_list, INFINITE);
for (prev = NULL, list = g_channels_list; list; prev = list, list = list->next)
{
xfree(list);
}
- freerdp_mutex_unlock(g_mutex_list);
+ ReleaseMutex(g_mutex_list);
xfree(channels);
}
channels->can_call_init = 1;
channels->settings = settings;
- freerdp_mutex_lock(g_mutex_init);
+ WaitForSingleObject(g_mutex_init, INFINITE);
g_init_channels = channels;
ok = lib->entry((PCHANNEL_ENTRY_POINTS) &ep);
g_init_channels = NULL;
- freerdp_mutex_unlock(g_mutex_init);
+ ReleaseMutex(g_mutex_init);
/* disable MyVirtualChannelInit */
channels->settings = 0;
strcpy(lchannel_def.name, "rdpdr");
channels->can_call_init = 1;
channels->settings = instance->settings;
- freerdp_mutex_lock(g_mutex_init);
+ WaitForSingleObject(g_mutex_init, INFINITE);
g_init_channels = channels;
MyVirtualChannelInit(&dummy, &lchannel_def, 1,
VIRTUAL_CHANNEL_VERSION_WIN2000, 0);
g_init_channels = NULL;
- freerdp_mutex_unlock(g_mutex_init);
+ ReleaseMutex(g_mutex_init);
channels->can_call_init = 0;
channels->settings = 0;
DEBUG_CHANNELS("registered fake rdpdr for rdpsnd.");
while (list_size(channels->sync_data_list) > 0)
{
- freerdp_mutex_lock(channels->sync_data_mutex);
+ WaitForSingleObject(channels->sync_data_mutex, INFINITE);
item = (struct sync_data*)list_dequeue(channels->sync_data_list);
- freerdp_mutex_unlock(channels->sync_data_mutex);
+ ReleaseMutex(channels->sync_data_mutex);
if (!item)
break ;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+
#include <freerdp/constants.h>
#include <freerdp/utils/memory.h>
+#include <winpr/synch.h>
+
#include "wtsvc.h"
#define CREATE_REQUEST_PDU 0x01
for (item = vcm->dvc_channel_list->head; item; item = item->next)
{
channel = (rdpPeerChannel*) item->data;
+
if (channel->channel_id == ChannelId)
break;
}
item->buffer = xmalloc(length);
memcpy(item->buffer, buffer, length);
- freerdp_mutex_lock(channel->mutex);
+ WaitForSingleObject(channel->mutex, INFINITE);
list_enqueue(channel->receive_queue, item);
- freerdp_mutex_unlock(channel->mutex);
+ ReleaseMutex(channel->mutex);
wait_obj_set(channel->receive_event);
}
item->channel_id = channel->channel_id;
- freerdp_mutex_lock(vcm->mutex);
+ WaitForSingleObject(vcm->mutex, INFINITE);
list_enqueue(vcm->send_queue, item);
- freerdp_mutex_unlock(vcm->mutex);
+ ReleaseMutex(vcm->mutex);
wait_obj_set(vcm->send_event);
}
if (length < 3)
return;
+
stream_seek_uint8(channel->receive_data); /* Pad (1 byte) */
stream_read_uint16(channel->receive_data, Version);
WTSVirtualChannelManager* vcm;
vcm = xnew(WTSVirtualChannelManager);
+
if (vcm != NULL)
{
vcm->client = client;
vcm->send_event = wait_obj_new();
vcm->send_queue = list_new();
- vcm->mutex = freerdp_mutex_new();
+ vcm->mutex = CreateMutex(NULL, FALSE, NULL);
vcm->dvc_channel_id_seq = 1;
vcm->dvc_channel_list = list_new();
wts_data_item_free(item);
}
list_free(vcm->send_queue);
- freerdp_mutex_free(vcm->mutex);
+ CloseHandle(vcm->mutex);
xfree(vcm);
}
}
wait_obj_clear(vcm->send_event);
- freerdp_mutex_lock(vcm->mutex);
+ WaitForSingleObject(vcm->mutex, INFINITE);
+
while ((item = (wts_data_item*) list_dequeue(vcm->send_queue)) != NULL)
{
if (vcm->client->SendChannelData(vcm->client, item->channel_id, item->buffer, item->length) == false)
{
result = false;
}
+
wts_data_item_free(item);
+
if (result == false)
break;
}
- freerdp_mutex_unlock(vcm->mutex);
+
+ ReleaseMutex(vcm->mutex);
return result;
}
channel->receive_data = stream_new(client->settings->vc_chunk_size);
channel->receive_event = wait_obj_new();
channel->receive_queue = list_new();
- channel->mutex = freerdp_mutex_new();
+ channel->mutex = CreateMutex(NULL, FALSE, NULL);
- freerdp_mutex_lock(vcm->mutex);
+ WaitForSingleObject(vcm->mutex, INFINITE);
channel->channel_id = vcm->dvc_channel_id_seq++;
list_enqueue(vcm->dvc_channel_list, channel);
- freerdp_mutex_unlock(vcm->mutex);
+ ReleaseMutex(vcm->mutex);
s = stream_new(64);
wts_write_drdynvc_create_request(s, channel->channel_id, pVirtualName);
else
{
len = strlen(pVirtualName);
+
if (len > 8)
return NULL;
break;
}
}
+
if (i >= client->settings->num_channels)
return NULL;
channel = (rdpPeerChannel*) client->settings->channels[i].handle;
+
if (channel == NULL)
{
channel = xnew(rdpPeerChannel);
channel->receive_data = stream_new(client->settings->vc_chunk_size);
channel->receive_event = wait_obj_new();
channel->receive_queue = list_new();
- channel->mutex = freerdp_mutex_new();
+ channel->mutex = CreateMutex(NULL, FALSE, NULL);
client->settings->channels[i].handle = channel;
}
rdpPeerChannel* channel = (rdpPeerChannel*) hChannelHandle;
item = (wts_data_item*) list_peek(channel->receive_queue);
+
if (item == NULL)
{
wait_obj_clear(channel->receive_event);
*pBytesRead = 0;
return true;
}
+
*pBytesRead = item->length;
+
if (item->length > BufferSize)
return false;
/* remove the first element (same as what we just peek) */
- freerdp_mutex_lock(channel->mutex);
+ WaitForSingleObject(channel->mutex, INFINITE);
list_dequeue(channel->receive_queue);
+
if (list_size(channel->receive_queue) == 0)
wait_obj_clear(channel->receive_event);
- freerdp_mutex_unlock(channel->mutex);
+
+ ReleaseMutex(channel->mutex);
memcpy(Buffer, item->buffer, item->length);
wts_data_item_free(item) ;
}
else
{
- freerdp_mutex_lock(vcm->mutex);
+ WaitForSingleObject(vcm->mutex, INFINITE);
list_remove(vcm->dvc_channel_list, channel);
- freerdp_mutex_unlock(vcm->mutex);
+ ReleaseMutex(vcm->mutex);
if (channel->dvc_open_state == DVC_OPEN_STATE_SUCCEEDED)
{
list_free(channel->receive_queue);
}
if (channel->mutex)
- freerdp_mutex_free(channel->mutex);
+ CloseHandle(channel->mutex);
xfree(channel);
}
return true;
#include <freerdp/freerdp.h>
#include <freerdp/utils/stream.h>
#include <freerdp/utils/list.h>
-#include <freerdp/utils/mutex.h>
#include <freerdp/utils/debug.h>
#include <freerdp/utils/wait_obj.h>
#include <freerdp/channels/wtsvc.h>
+#include <winpr/synch.h>
+
#ifdef WITH_DEBUG_DVC
#define DEBUG_DVC(fmt, ...) DEBUG_CLASS(DVC, fmt, ## __VA_ARGS__)
#else
STREAM* receive_data;
struct wait_obj* receive_event;
LIST* receive_queue;
- freerdp_mutex mutex;
+ HANDLE mutex;
uint8 dvc_open_state;
uint32 dvc_total_length;
freerdp_peer* client;
struct wait_obj* send_event;
LIST* send_queue;
- freerdp_mutex mutex;
+ HANDLE mutex;
rdpPeerChannel* drdynvc_channel;
uint8 drdynvc_state;
file.c
load_plugin.c
memory.c
- mutex.c
passphrase.c
pcap.c
profiler.c
endif()
if(WITH_MONOLITHIC_BUILD)
+ set(FREERDP_UTILS_LIBS ${FREERDP_UTILS_LIBS} winpr)
set(FREERDP_LIBS ${FREERDP_LIBS} ${FREERDP_UTILS_LIBS} PARENT_SCOPE)
else()
+ set(FREERDP_UTILS_LIBS ${FREERDP_UTILS_LIBS} winpr-synch)
target_link_libraries(freerdp-utils ${FREERDP_UTILS_LIBS})
install(TARGETS freerdp-utils DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
+++ /dev/null
-/*
- * FreeRDP: A Remote Desktop Protocol Client
- * Mutex 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.
- */
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <winpr/windows.h>
-#include <freerdp/utils/memory.h>
-#include <freerdp/utils/mutex.h>
-
-#ifdef _WIN32
-#define freerdp_mutex_t HANDLE
-#else
-#include <pthread.h>
-#define freerdp_mutex_t pthread_mutex_t
-#endif
-
-/**
- * Mutex are used to prevent concurrent accesses to specific portions of code.
- * This funciton and the other freerdp_mutex_*() function are defining a portable API
- * to get mutex for both Windows and Linux, where the lower level implementation is very different.
- *
- * This function creates a new freerdp_mutex object.
- * Use freerdp_mutex_lock() to get exclusive ownership of the mutex, and lock a given (protected) portion of code.
- * Use freerdp_mutex_unlock() to release your ownership on a mutex.
- * Use freerdp_mutex_free() to release the resources associated with the mutex when it is no longer needed.
- *
- * @return a freerdp_mutex pointer. This should be considered opaque, as the implementation is platform dependent.
- * NULL is returned if an allocation or initialization error occurred.
- *
- * @see pthread documentation for Linux implementation of mutexes
- * @see MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/ms682411%28v=vs.85%29.aspx for Windows implementation
- */
-freerdp_mutex freerdp_mutex_new(void)
-{
-#ifdef _WIN32
- freerdp_mutex_t mutex;
- mutex = CreateMutex(NULL, FALSE, NULL);
- return (freerdp_mutex) mutex;
-#else
- freerdp_mutex_t* mutex;
- mutex = xnew(freerdp_mutex_t);
- if (mutex)
- pthread_mutex_init(mutex, 0);
- return mutex;
-#endif
-}
-
-/**
- * This function is used to deallocate all resources associated with a freerdp_mutex object.
- *
- * @param mutex [in] - Pointer to the mutex that needs to be deallocated.
- * On return, this object is not valid anymore.
- */
-void freerdp_mutex_free(freerdp_mutex mutex)
-{
-#ifdef _WIN32
- CloseHandle((freerdp_mutex_t) mutex);
-#else
- pthread_mutex_destroy((freerdp_mutex_t*) mutex);
- xfree(mutex);
-#endif
-}
-
-/**
- * Use this function to get exclusive ownership of the mutex.
- * This should be called before entering a portion of code that needs to be protected against concurrent accesses.
- * Use the freerdp_mutex_unlock() call to release ownership when you leave this protected code.
- *
- * @param mutex [in] - An initialized freerdp_mutex object, as returned from a call to freerdp_mutex_new().
- * This function will suspend the running thread when called, until the mutex is available.
- * Only one thread at a time will be allowed to continue execution.
- */
-void freerdp_mutex_lock(freerdp_mutex mutex)
-{
-#ifdef _WIN32
- WaitForSingleObject((freerdp_mutex_t) mutex, INFINITE);
-#else
- pthread_mutex_lock(mutex);
-#endif
-}
-
-/**
- * Use this function to release your ownership on a mutex.
- * This should be called when leaving a portion of code that needs to be protected against concurrent accesses.
- * DO NOT use this call on a mutex that you do not own. See freerdp_mutex_lock() for getting ownership on a mutex.
- *
- * @param mutex [in] - Pointer to a mutex that was locked by a call to freerdp_mutex_lock().
- */
-void freerdp_mutex_unlock(freerdp_mutex mutex)
-{
-#ifdef _WIN32
- ReleaseMutex((freerdp_mutex_t) mutex);
-#else
- pthread_mutex_unlock(mutex);
-#endif
-}
#include <freerdp/constants.h>
#include <freerdp/utils/memory.h>
-#include <freerdp/utils/mutex.h>
#include <freerdp/utils/debug.h>
#include <freerdp/utils/stream.h>
#include <freerdp/utils/list.h>
static rdpSvcPluginList* g_svc_plugin_list = NULL;
/* For locking the global resources */
-static freerdp_mutex g_mutex = NULL;
+static HANDLE g_mutex = NULL;
/* Queue for receiving packets */
struct _svc_data_in_item
static rdpSvcPlugin* svc_plugin_find_by_init_handle(void* init_handle)
{
- rdpSvcPluginList * list;
- rdpSvcPlugin * plugin;
+ rdpSvcPluginList* list;
+ rdpSvcPlugin* plugin;
+
+ WaitForSingleObject(g_mutex, INFINITE);
- freerdp_mutex_lock(g_mutex);
for (list = g_svc_plugin_list; list; list = list->next)
{
plugin = list->plugin;
+
if (plugin->priv->init_handle == init_handle)
{
- freerdp_mutex_unlock(g_mutex);
+ ReleaseMutex(g_mutex);
return plugin;
}
}
- freerdp_mutex_unlock(g_mutex);
+
+ ReleaseMutex(g_mutex);
+
return NULL;
}
rdpSvcPluginList * list;
rdpSvcPlugin * plugin;
- freerdp_mutex_lock(g_mutex);
+ WaitForSingleObject(g_mutex, INFINITE);
for (list = g_svc_plugin_list; list; list = list->next)
{
plugin = list->plugin;
if (plugin->priv->open_handle == open_handle)
{
- freerdp_mutex_unlock(g_mutex);
+ ReleaseMutex(g_mutex);
return plugin;
}
}
- freerdp_mutex_unlock(g_mutex);
+ ReleaseMutex(g_mutex);
return NULL;
}
rdpSvcPluginList* prev;
/* Remove from global list */
- freerdp_mutex_lock(g_mutex);
+ WaitForSingleObject(g_mutex, INFINITE);
for (prev = NULL, list = g_svc_plugin_list; list; prev = list, list = list->next)
{
if (list->plugin == plugin)
g_svc_plugin_list = list->next;
xfree(list);
}
- freerdp_mutex_unlock(g_mutex);
+ ReleaseMutex(g_mutex);
}
static void svc_plugin_process_received(rdpSvcPlugin* plugin, void* pData, uint32 dataLength,
* VirtualChannelInit at a time. So this should be safe.
*/
if (g_mutex == NULL)
- g_mutex = freerdp_mutex_new();
+ g_mutex = CreateMutex(NULL, FALSE, NULL);
memcpy(&plugin->channel_entry_points, pEntryPoints, pEntryPoints->cbSize);
list = xnew(rdpSvcPluginList);
list->plugin = plugin;
- freerdp_mutex_lock(g_mutex);
+ WaitForSingleObject(g_mutex, INFINITE);
list->next = g_svc_plugin_list;
g_svc_plugin_list = list;
- freerdp_mutex_unlock(g_mutex);
+ ReleaseMutex(g_mutex);
plugin->channel_entry_points.pVirtualChannelInit(&plugin->priv->init_handle,
&plugin->channel_def, 1, VIRTUAL_CHANNEL_VERSION_WIN2000, svc_plugin_init_event);
freerdp_thread* thread;
thread = xnew(freerdp_thread);
- thread->mutex = freerdp_mutex_new();
+ thread->mutex = CreateMutex(NULL, FALSE, NULL);
thread->signals[0] = wait_obj_new();
thread->signals[1] = wait_obj_new();
thread->num_signals = 2;
for (i = 0; i < thread->num_signals; i++)
wait_obj_free(thread->signals[i]);
+
thread->num_signals = 0;
- freerdp_mutex_free(thread->mutex);
+ CloseHandle(thread->mutex);
thread->mutex = NULL;
xfree(thread);
WINPR_API ULONG winpr_Handle_GetType(HANDLE handle);
WINPR_API PVOID winpr_Handle_GetObject(HANDLE handle);
+WINPR_API BOOL winpr_Handle_GetInfo(HANDLE handle, ULONG* pType, PVOID* pObject);
#ifndef _WIN32
WINPR_API BOOL ReleaseSemaphore(HANDLE hSemaphore, LONG lReleaseCount, LPLONG lpPreviousCount);
-#define WAIT_OBJECT_0 0x00000000L
-#define WAIT_ABANDONED 0x00000080L
-#define WAIT_TIMEOUT 0x00000102L
-#define WAIT_FAILED ((DWORD) 0xFFFFFFFF)
-
/* Event */
WINPR_API HANDLE CreateEventA(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCSTR lpName);
/* Wait */
+#define INFINITE 0xFFFFFFFF
+
+#define WAIT_OBJECT_0 0x00000000L
+#define WAIT_ABANDONED 0x00000080L
+#define WAIT_TIMEOUT 0x00000102L
+#define WAIT_FAILED ((DWORD) 0xFFFFFFFF)
+
WINPR_API DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
+WINPR_API DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable);
WINPR_API DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds);
WINPR_API DWORD WaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL bWaitAll, DWORD dwMilliseconds, BOOL bAlertable);
#ifndef _WIN32
-#if defined __APPLE__
-#include <pthread.h>
-#include <semaphore.h>
-#include <mach/mach.h>
-#include <mach/semaphore.h>
-#include <mach/task.h>
-#define winpr_sem_t semaphore_t
-#else
-#include <pthread.h>
-#include <semaphore.h>
-#define winpr_sem_t sem_t
-#endif
+#include "../synch/synch.h"
BOOL CloseHandle(HANDLE hObject)
{
-#if defined __APPLE__
- semaphore_destroy(mach_task_self(), *((winpr_sem_t*) hObject));
-#else
- sem_destroy((winpr_sem_t*) hObject);
-#endif
+ ULONG Type;
+ PVOID Object;
+
+ if (!winpr_Handle_GetInfo(hObject, &Type, &Object))
+ return FALSE;
- free(hObject);
+ if (Type == HANDLE_TYPE_MUTEX)
+ {
+ pthread_mutex_destroy((pthread_mutex_t*) Object);
+ free(Object);
+ return TRUE;
+ }
- return 1;
+ return FALSE;
}
BOOL DuplicateHandle(HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle,
LPHANDLE lpTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions)
{
- return 1;
+ return TRUE;
}
BOOL GetHandleInformation(HANDLE hObject, LPDWORD lpdwFlags)
{
- return 1;
+ return TRUE;
}
BOOL SetHandleInformation(HANDLE hObject, DWORD dwMask, DWORD dwFlags)
{
- return 1;
+ return TRUE;
}
#endif
{
LONG Count;
LONG MaxCount;
- PHANDLE_TABLE_ENTRY* Entries;
+ PHANDLE_TABLE_ENTRY Entries;
} HANDLE_TABLE, *PHANDLE_TABLE;
static HANDLE_TABLE HandleTable = { 0, 0, NULL };
size = sizeof(HANDLE_TABLE_ENTRY) * HandleTable.MaxCount;
- HandleTable.Entries = (PHANDLE_TABLE_ENTRY*) malloc(size);
+ HandleTable.Entries = (PHANDLE_TABLE_ENTRY) malloc(size);
ZeroMemory(HandleTable.Entries, size);
}
size = sizeof(HANDLE_TABLE_ENTRY) * HandleTable.MaxCount;
- HandleTable.Entries = (PHANDLE_TABLE_ENTRY*) realloc(HandleTable.Entries, size);
+ HandleTable.Entries = (PHANDLE_TABLE_ENTRY) realloc(HandleTable.Entries, size);
ZeroMemory((void*) &HandleTable.Entries[HandleTable.MaxCount / 2], size / 2);
}
for (index = 0; index < (int) HandleTable.MaxCount; index++)
{
- if (HandleTable.Entries[index]->Object == NULL)
+ if (HandleTable.Entries[index].Object == NULL)
{
HandleTable.Count++;
- HandleTable.Entries[index]->Type = Type;
- HandleTable.Entries[index]->Object = Object;
+ HandleTable.Entries[index].Type = Type;
+ HandleTable.Entries[index].Object = Object;
return Object;
}
for (index = 0; index < (int) HandleTable.MaxCount; index++)
{
- if (HandleTable.Entries[index]->Object == handle)
+ if (HandleTable.Entries[index].Object == handle)
{
- HandleTable.Entries[index]->Type = HANDLE_TYPE_NONE;
- HandleTable.Entries[index]->Object = NULL;
+ HandleTable.Entries[index].Type = HANDLE_TYPE_NONE;
+ HandleTable.Entries[index].Object = NULL;
HandleTable.Count--;
return TRUE;
for (index = 0; index < (int) HandleTable.MaxCount; index++)
{
- if (HandleTable.Entries[index]->Object == handle)
- return HandleTable.Entries[index]->Type;
+ if (HandleTable.Entries[index].Object == handle)
+ return HandleTable.Entries[index].Type;
}
return HANDLE_TYPE_NONE;
return handle;
}
+BOOL winpr_Handle_GetInfo(HANDLE handle, ULONG* pType, PVOID* pObject)
+{
+ int index;
+
+ HandleTable_GetInstance();
+
+ for (index = 0; index < (int) HandleTable.MaxCount; index++)
+ {
+ if (HandleTable.Entries[index].Object == handle)
+ {
+ *pType = HandleTable.Entries[index].Type;
+ *pObject = HandleTable.Entries[index].Object;
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
#endif
#include <winpr/synch.h>
+#include "synch.h"
+
/**
* CreateMutexA
* CreateMutexW
* ReleaseMutex
*/
-HANDLE CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCSTR lpName)
+#ifndef _WIN32
+
+HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName)
{
- return NULL;
+ HANDLE handle;
+ pthread_mutex_t* pMutex;
+
+ pMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
+
+ if (pMutex)
+ pthread_mutex_init(pMutex, 0);
+
+ handle = winpr_Handle_Insert(HANDLE_TYPE_MUTEX, pMutex);
+
+ return handle;
}
-HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCWSTR lpName)
+HANDLE CreateMutexA(LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOL bInitialOwner, LPCSTR lpName)
{
- return NULL;
+ return CreateMutexW(lpMutexAttributes, bInitialOwner, NULL);
}
HANDLE CreateMutexExA(LPSECURITY_ATTRIBUTES lpMutexAttributes, LPCTSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
{
- return NULL;
+ return CreateMutexW(lpMutexAttributes, FALSE, NULL);
}
HANDLE CreateMutexExW(LPSECURITY_ATTRIBUTES lpMutexAttributes, LPCWSTR lpName, DWORD dwFlags, DWORD dwDesiredAccess)
{
- return NULL;
+ return CreateMutexW(lpMutexAttributes, FALSE, NULL);
}
HANDLE OpenMutexA(DWORD dwDesiredAccess, BOOL bInheritHandle,LPCSTR lpName)
BOOL ReleaseMutex(HANDLE hMutex)
{
- return TRUE;
+ ULONG Type;
+ PVOID Object;
+
+ if (!winpr_Handle_GetInfo(hMutex, &Type, &Object))
+ return FALSE;
+
+ if (Type == HANDLE_TYPE_MUTEX)
+ {
+ pthread_mutex_unlock((pthread_mutex_t*) Object);
+ return TRUE;
+ }
+
+ return FALSE;
}
+
+#endif
#include <winpr/synch.h>
+#ifndef _WIN32
+
#if defined __APPLE__
#include <pthread.h>
#include <semaphore.h>
#define winpr_sem_t sem_t
#endif
+#endif
+
#endif /* WINPR_SYNCH_PRIVATE_H */
* SignalObjectAndWait
*/
+#ifndef _WIN32
+
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
{
-#if defined __APPLE__
- semaphore_wait(*((winpr_sem_t*) hHandle));
-#else
- sem_wait((winpr_sem_t*) hHandle);
-#endif
+ ULONG Type;
+ PVOID Object;
+
+ if (!winpr_Handle_GetInfo(hHandle, &Type, &Object))
+ return WAIT_FAILED;
+
+ if (Type == HANDLE_TYPE_MUTEX)
+ {
+ pthread_mutex_lock((pthread_mutex_t*) Object);
+ }
+
+ return WAIT_OBJECT_0;
+}
+DWORD WaitForSingleObjectEx(HANDLE hHandle, DWORD dwMilliseconds, BOOL bAlertable)
+{
return WAIT_OBJECT_0;
}
{
return 0;
}
+
+#endif