wine/scheduler/semaphore.c
Alexandre Julliard 03468f7d4b Release 980215
Sun Feb 15 12:02:59 1998  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [graphics/x11drv/*.c] [objects/*.c]
	A few X11 critical section optimizations, mostly with XGet/PutPixel.

	* [scheduler/sysdeps.c] [misc/main.c]
	Make sure X11 critical section is available before any Xlib call.

	* [if1632/relay.c] [tools/build.c]
	Yet another attempt at fixing Catch/Throw.

	* [loader/pe_image.c]
	Fixed broken PE DLL loading.

	* [include/winnt.h] [scheduler/handle.c] [scheduler/*.c]
	Implemented handle access rights.
	Added Get/SetHandleInformation.

Sun Feb 15 09:45:23 1997  Andreas Mohr <100.30936@germany.net>

	* [misc/winsock.c]
	Fixed bug in WSACleanup which lead to crashes in WINSOCK_HandleIO.

	* [graphics/fontengine.c] [include/font.h]
	Minor improvements.

	* [memory/global.c]
	Implemented GlobalEntryHandle.

	* [misc/toolhelp.c]
	Fixed a memory bug in Notify*register.

	* [misc/w32scomb.c]
	Improved Get16DLLAddress.

	* [objects/gdiobj.c]
	Implemented GdiSeeGdiDo.


Sat Feb 14 14:57:39 1998  John Richardson <jrichard@zko.dec.com>

	* [win32/console.c]
	Added the console implementation, AllocConsole, FreeConsole,
	CONSOLE_InheritConsole.

	* [documentation/console]
	Some documentation on the console.

	* [include/winerror.h]
	Added some error defines.

	* [scheduler/k32obj.c]
	Registered the scheduler ops.

Fri Feb 13 19:35:35 1998  James Moody  <013263m@dragon.acadiau.ca>

	* [ole/ole2nls.c]
	Some English language fixes for missing values.

	* [controls/listbox.c]
	Fix to allow an empty listbox to deselect all items.

	* [relay32/user32.spec] [windows/keyboard.c]
	CreateAcceleratorTableA stub method.

	* [windows/sysmetrics.c]
	Added missing SM_CXCURSOR & SM_CYCURSOR initializers.

	* [windows/message.c]
	PostThreadMessage32A stub method.

Fri Feb 13 17:12:24 1998  Jim Peterson <jspeter@roanoke.infi.net>

	* [libtest/hello3res.rc] [libtest/hello3.c] [libtest/Makefile.in]
	Updated the 'hello3' test so that it functions properly again.

Fri Feb 13 14:08:07 1998  Martin Boehme  <boehme@informatik.mu-luebeck.de>
	
	* [graphics/mapping.c]
	Fixed the embarrassing bugs I introduced into DPtoLP and
	LPtoDP.

	* [windows/scroll.c]
	Prevent ScrollWindow32 from sending WM_ERASEBKGND.

Thu Feb 12 22:46:53 1998  Huw D M Davies <h.davies1@physics.oxford.ac.uk>

	* [objects/metafile] [include/ldt.h]
	Fix to cope with records longer than 64K.

	* [windows/clipboard.c]
	Clean up bitmaps and metapicts properly.

Mon Feb  3 21:52:18 1998  Karl Backström <karl_b@geocities.com>

	* [programs/winhelp/Sw.rc] [resources/sysres_Sw.rc]
	Minor update of Swedish language support.
1998-02-15 19:40:49 +00:00

217 lines
5.9 KiB
C

/*
* Win32 semaphores
*
* Copyright 1998 Alexandre Julliard
*/
#include <assert.h>
#include "windows.h"
#include "winerror.h"
#include "k32obj.h"
#include "process.h"
#include "thread.h"
#include "heap.h"
typedef struct
{
K32OBJ header;
THREAD_QUEUE wait_queue;
LONG count;
LONG max;
} SEMAPHORE;
static BOOL32 SEMAPHORE_Signaled( K32OBJ *obj, DWORD thread_id );
static BOOL32 SEMAPHORE_Satisfied( K32OBJ *obj, DWORD thread_id );
static void SEMAPHORE_AddWait( K32OBJ *obj, DWORD thread_id );
static void SEMAPHORE_RemoveWait( K32OBJ *obj, DWORD thread_id );
static void SEMAPHORE_Destroy( K32OBJ *obj );
const K32OBJ_OPS SEMAPHORE_Ops =
{
SEMAPHORE_Signaled, /* signaled */
SEMAPHORE_Satisfied, /* satisfied */
SEMAPHORE_AddWait, /* add_wait */
SEMAPHORE_RemoveWait, /* remove_wait */
SEMAPHORE_Destroy /* destroy */
};
/***********************************************************************
* CreateSemaphore32A (KERNEL32.174)
*/
HANDLE32 WINAPI CreateSemaphore32A( SECURITY_ATTRIBUTES *sa, LONG initial,
LONG max, LPCSTR name )
{
HANDLE32 handle;
SEMAPHORE *sem;
/* Check parameters */
if ((max <= 0) || (initial < 0) || (initial > max))
{
SetLastError( ERROR_INVALID_PARAMETER );
return INVALID_HANDLE_VALUE32;
}
SYSTEM_LOCK();
sem = (SEMAPHORE *)K32OBJ_Create( K32OBJ_SEMAPHORE, sizeof(*sem),
name, SEMAPHORE_ALL_ACCESS, &handle );
if (sem)
{
/* Finish initializing it */
sem->wait_queue = NULL;
sem->count = initial;
sem->max = max;
K32OBJ_DecCount( &sem->header );
}
SYSTEM_UNLOCK();
return handle;
}
/***********************************************************************
* CreateSemaphore32W (KERNEL32.175)
*/
HANDLE32 WINAPI CreateSemaphore32W( SECURITY_ATTRIBUTES *sa, LONG initial,
LONG max, LPCWSTR name )
{
LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
HANDLE32 ret = CreateSemaphore32A( sa, initial, max, nameA );
if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
return ret;
}
/***********************************************************************
* OpenSemaphore32A (KERNEL32.545)
*/
HANDLE32 WINAPI OpenSemaphore32A( DWORD access, BOOL32 inherit, LPCSTR name )
{
HANDLE32 handle = 0;
K32OBJ *obj;
SYSTEM_LOCK();
if ((obj = K32OBJ_FindNameType( name, K32OBJ_SEMAPHORE )) != NULL)
{
handle = HANDLE_Alloc( obj, access, inherit );
K32OBJ_DecCount( obj );
}
SYSTEM_UNLOCK();
return handle;
}
/***********************************************************************
* OpenSemaphore32W (KERNEL32.546)
*/
HANDLE32 WINAPI OpenSemaphore32W( DWORD access, BOOL32 inherit, LPCWSTR name )
{
LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
HANDLE32 ret = OpenSemaphore32A( access, inherit, nameA );
if (nameA) HeapFree( GetProcessHeap(), 0, nameA );
return ret;
}
/***********************************************************************
* ReleaseSemaphore (KERNEL32.583)
*/
BOOL32 WINAPI ReleaseSemaphore( HANDLE32 handle, LONG count, LONG *previous )
{
SEMAPHORE *sem;
SYSTEM_LOCK();
if (!(sem = (SEMAPHORE *)HANDLE_GetObjPtr( handle, K32OBJ_SEMAPHORE,
SEMAPHORE_MODIFY_STATE )))
{
SYSTEM_UNLOCK();
return FALSE;
}
if (previous) *previous = sem->count;
if (sem->count + count > sem->max)
{
SYSTEM_UNLOCK();
SetLastError( ERROR_TOO_MANY_POSTS );
return FALSE;
}
if (sem->count > 0)
{
/* There cannot be any thread waiting if the count is > 0 */
assert( sem->wait_queue == NULL );
sem->count += count;
}
else
{
sem->count = count;
SYNC_WakeUp( &sem->wait_queue, count );
}
K32OBJ_DecCount( &sem->header );
SYSTEM_UNLOCK();
return TRUE;
}
/***********************************************************************
* SEMAPHORE_Signaled
*/
static BOOL32 SEMAPHORE_Signaled( K32OBJ *obj, DWORD thread_id )
{
SEMAPHORE *sem = (SEMAPHORE *)obj;
assert( obj->type == K32OBJ_SEMAPHORE );
return (sem->count > 0);
}
/***********************************************************************
* SEMAPHORE_Satisfied
*
* Wait on this object has been satisfied.
*/
static BOOL32 SEMAPHORE_Satisfied( K32OBJ *obj, DWORD thread_id )
{
SEMAPHORE *sem = (SEMAPHORE *)obj;
assert( obj->type == K32OBJ_SEMAPHORE );
assert( sem->count > 0 );
sem->count--;
return FALSE; /* Not abandoned */
}
/***********************************************************************
* SEMAPHORE_AddWait
*
* Add current thread to object wait queue.
*/
static void SEMAPHORE_AddWait( K32OBJ *obj, DWORD thread_id )
{
SEMAPHORE *sem = (SEMAPHORE *)obj;
assert( obj->type == K32OBJ_SEMAPHORE );
THREAD_AddQueue( &sem->wait_queue, THREAD_ID_TO_THDB(thread_id) );
}
/***********************************************************************
* SEMAPHORE_RemoveWait
*
* Remove thread from object wait queue.
*/
static void SEMAPHORE_RemoveWait( K32OBJ *obj, DWORD thread_id )
{
SEMAPHORE *sem = (SEMAPHORE *)obj;
assert( obj->type == K32OBJ_SEMAPHORE );
THREAD_RemoveQueue( &sem->wait_queue, THREAD_ID_TO_THDB(thread_id) );
}
/***********************************************************************
* SEMAPHORE_Destroy
*/
static void SEMAPHORE_Destroy( K32OBJ *obj )
{
SEMAPHORE *sem = (SEMAPHORE *)obj;
assert( obj->type == K32OBJ_SEMAPHORE );
/* There cannot be any thread on the list since the ref count is 0 */
assert( sem->wait_queue == NULL );
obj->type = K32OBJ_UNKNOWN;
HeapFree( SystemHeap, 0, sem );
}