Release 990226.
[wine/gsoc-2012-control.git] / scheduler / mutex.c
blobf809a619321243a1adff0a6b875d663795407f06
1 /*
2 * Win32 mutexes
4 * Copyright 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include "winerror.h"
9 #include "k32obj.h"
10 #include "process.h"
11 #include "thread.h"
12 #include "heap.h"
13 #include "server/request.h"
14 #include "server.h"
16 typedef struct _MUTEX
18 K32OBJ header;
19 } MUTEX;
22 /***********************************************************************
23 * CreateMutex32A (KERNEL32.166)
25 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner,
26 LPCSTR name )
28 struct create_mutex_request req;
29 struct create_mutex_reply reply;
30 int len = name ? strlen(name) + 1 : 0;
31 HANDLE handle;
32 MUTEX *mutex;
34 req.owned = owner;
35 req.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
37 CLIENT_SendRequest( REQ_CREATE_MUTEX, -1, 2, &req, sizeof(req), name, len );
38 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
39 if (reply.handle == -1) return 0;
41 SYSTEM_LOCK();
42 mutex = (MUTEX *)K32OBJ_Create( K32OBJ_MUTEX, sizeof(*mutex),
43 name, reply.handle, MUTEX_ALL_ACCESS,
44 sa, &handle );
45 if (mutex) K32OBJ_DecCount( &mutex->header );
46 if (handle == INVALID_HANDLE_VALUE) handle = 0;
47 SYSTEM_UNLOCK();
48 return handle;
52 /***********************************************************************
53 * CreateMutex32W (KERNEL32.167)
55 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner,
56 LPCWSTR name )
58 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
59 HANDLE ret = CreateMutexA( sa, owner, nameA );
60 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
61 return ret;
65 /***********************************************************************
66 * OpenMutex32A (KERNEL32.541)
68 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
70 HANDLE handle = 0;
71 K32OBJ *obj;
72 struct open_named_obj_request req;
73 struct open_named_obj_reply reply;
74 int len = name ? strlen(name) + 1 : 0;
76 req.type = OPEN_MUTEX;
77 req.access = access;
78 req.inherit = inherit;
79 CLIENT_SendRequest( REQ_OPEN_NAMED_OBJ, -1, 2, &req, sizeof(req), name, len );
80 CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL );
81 if (reply.handle != -1)
83 SYSTEM_LOCK();
84 if ((obj = K32OBJ_FindNameType( name, K32OBJ_MUTEX )) != NULL)
86 handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, reply.handle );
87 K32OBJ_DecCount( obj );
88 if (handle == INVALID_HANDLE_VALUE)
89 handle = 0; /* must return 0 on failure, not -1 */
91 else CLIENT_CloseHandle( reply.handle );
92 SYSTEM_UNLOCK();
94 return handle;
98 /***********************************************************************
99 * OpenMutex32W (KERNEL32.542)
101 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
103 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
104 HANDLE ret = OpenMutexA( access, inherit, nameA );
105 if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
106 return ret;
110 /***********************************************************************
111 * ReleaseMutex (KERNEL32.582)
113 BOOL WINAPI ReleaseMutex( HANDLE handle )
115 struct release_mutex_request req;
117 req.handle = HANDLE_GetServerHandle( PROCESS_Current(), handle,
118 K32OBJ_MUTEX, MUTEX_MODIFY_STATE );
119 if (req.handle == -1) return FALSE;
120 CLIENT_SendRequest( REQ_RELEASE_MUTEX, -1, 1, &req, sizeof(req) );
121 return !CLIENT_WaitReply( NULL, NULL, 0 );