Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / mutex.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Mutex Utils
4  *
5  * Copyright 2011 Vic Lee
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 #include <freerdp/utils/memory.h>
21 #include <freerdp/utils/mutex.h>
22
23 #ifdef _WIN32
24 #include <windows.h>
25 #define freerdp_mutex_t HANDLE
26 #else
27 #include <pthread.h>
28 #define freerdp_mutex_t pthread_mutex_t
29 #endif
30
31 freerdp_mutex freerdp_mutex_new(void)
32 {
33 #ifdef _WIN32
34         freerdp_mutex_t mutex;
35         mutex = CreateMutex(NULL, FALSE, NULL);
36         return (freerdp_mutex) mutex;
37 #else
38         freerdp_mutex_t* mutex;
39         mutex = xnew(freerdp_mutex_t);
40         pthread_mutex_init(mutex, 0);
41         return mutex;
42 #endif
43 }
44
45 void freerdp_mutex_free(freerdp_mutex mutex)
46 {
47 #ifdef _WIN32
48         CloseHandle((freerdp_mutex_t) mutex);
49 #else
50         pthread_mutex_destroy((freerdp_mutex_t*) mutex);
51         xfree(mutex);
52 #endif
53 }
54
55 void freerdp_mutex_lock(freerdp_mutex mutex)
56 {
57 #ifdef _WIN32
58         WaitForSingleObject((freerdp_mutex_t) mutex, INFINITE);
59 #else
60         pthread_mutex_lock(mutex);
61 #endif
62 }
63
64 void freerdp_mutex_unlock(freerdp_mutex mutex)
65 {
66 #ifdef _WIN32
67         ReleaseMutex((freerdp_mutex_t) mutex);
68 #else
69         pthread_mutex_unlock(mutex);
70 #endif
71 }