newlib/winsup/cygwin/sync.cc

146 lines
3.6 KiB
C++
Raw Normal View History

2000-02-17 20:38:33 +01:00
/* sync.cc: Synchronization functions for cygwin.
This file implements the methods for controlling the "muto" class
which is intended to operate similarly to a mutex but attempts to
avoid making expensive calls to the kernel.
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2010, 2011, 2012
Red Hat, Inc.
2000-02-17 20:38:33 +01:00
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "miscfuncs.h"
#include "sync.h"
#include "thread.h"
#include "cygtls.h"
2000-02-17 20:38:33 +01:00
#undef WaitForSingleObject
Remove unneeded header files from source files throughout. Update copyrights where appropriate. * globals.cc: New file for generic global variables. * mkglobals_h: New file to generate globals.h. * mkstatic: New Script used to build a (currently non-working) static libcygwin_s.a. * Makefile.in: Add unused rule to build a non-working libcygwin_s.a. (DLL_OFILES): Add globals.o. Make all objects rely on globals.h. (globals.h): New target. Generate globals.h. * cygtls.h: Honor new CYGTLS_HANDLE define to control when the HANDLE operator is allowed in _cygtls. * dcrt0.cc: Move most globals to globals.cc. * init.cc: Ditto. * environ.cc (strip_title_path): Remove now-unneeded extern. * fhandler_serial.cc (fhandler_serial::open): Ditto. * pinfo.cc: Ditto. (commune_process): Ditto. * shared.cc: Ditto. * glob.cc: Ditto. * strace.cc: Ditto. * exceptions.cc: Define CYGTLS_HANDLE before including winsup.h. * path.cc (stat_suffixes): Move here. * security.h: Add forward class path_conv declaration. * smallprint.cc (__small_vsprintf): Make a true c++ function. (__small_sprintf): Ditto. (small_printf): Ditto. (console_printf): Ditto. (__small_vswprintf): Ditto. (__small_swprintf): Ditto. * spawn.cc (spawn_guts): Remove _stdcall decoration in favor of regparm. (hExeced): Move to globals.cc * strfuncs.cc (current_codepage): Ditto. (active_codepage): Ditto. * sync.cc (lock_process::locker): Move here from dcrt0.cc. * syscalls.cc (stat_suffixes): Move to path.cc. * tty.cc (tty::create_master): Uncapitalize fatal warning for consistency. * winsup.h: Include globals.h to declare most of the grab bag list of globals which were previously defined here. * mount.h: Move USER_* defines back to shared_info.h. * speclib: Force temporary directory cleanup.
2009-01-03 06:12:22 +01:00
muto NO_COPY lock_process::locker;
void
muto::grab ()
{
tls = &_my_tls;
}
2000-02-17 20:38:33 +01:00
/* Constructor */
muto *
muto::init (const char *s)
2000-02-17 20:38:33 +01:00
{
2013-04-23 11:44:36 +02:00
char *already_exists = (char *) InterlockedExchangePointer ((PVOID *) &name,
(PVOID) s);
if (already_exists)
while (!bruteforce)
yield ();
else
2000-02-17 20:38:33 +01:00
{
waiters = -1;
/* Create event which is used in the fallback case when blocking is necessary */
bruteforce = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
if (!bruteforce)
api_fatal ("couldn't allocate muto '%s', %E", s);
2000-02-17 20:38:33 +01:00
}
return this;
2000-02-17 20:38:33 +01:00
}
#if 0 /* FIXME: Do we need this? mutos aren't destroyed until process exit */
/* Destructor (racy?) */
2000-02-17 20:38:33 +01:00
muto::~muto ()
{
while (visits)
release ();
HANDLE h = bruteforce;
bruteforce = NULL;
2000-02-17 20:38:33 +01:00
/* Just need to close the event handle */
if (h)
CloseHandle (h);
2000-02-17 20:38:33 +01:00
}
#endif
2000-02-17 20:38:33 +01:00
/* Acquire the lock. Argument is the number of milliseconds to wait for
the lock. Multiple visits from the same thread are allowed and should
be handled correctly.
Note: The goal here is to minimize, as much as possible, calls to the
OS. Hence the use of InterlockedIncrement, etc., rather than (much) more
expensive OS mutexes. */
2000-02-17 20:38:33 +01:00
int
muto::acquire (DWORD ms)
{
void *this_tls = &_my_tls;
2000-02-17 20:38:33 +01:00
if (tls != this_tls)
2000-02-17 20:38:33 +01:00
{
/* Increment the waiters part of the class. Need to do this first to
avoid potential races. */
LONG was_waiting = ms ? InterlockedIncrement (&waiters) : 0;
while (was_waiting || InterlockedExchange (&sync, 1) != 0)
switch (WaitForSingleObject (bruteforce, ms))
{
case WAIT_OBJECT_0:
was_waiting = 0;
break;
default:
return 0; /* failed. */
}
/* Have to do it this way to avoid a race */
if (!ms)
InterlockedIncrement (&waiters);
tls = this_tls; /* register this thread. */
2000-02-17 20:38:33 +01:00
}
return ++visits; /* Increment visit count. */
}
bool
muto::acquired ()
{
return tls == &_my_tls;
}
2000-02-17 20:38:33 +01:00
/* Return the muto lock. Needs to be called once per every acquire. */
int
muto::release (_cygtls *this_tls)
2000-02-17 20:38:33 +01:00
{
if (tls != this_tls || !visits)
2000-02-17 20:38:33 +01:00
{
SetLastError (ERROR_NOT_OWNER); /* Didn't have the lock. */
return 0; /* failed. */
}
/* FIXME: Need to check that other thread has not exited, too. */
if (!--visits)
{
tls = 0; /* We were the last unlocker. */
InterlockedExchange (&sync, 0); /* Reset trigger. */
2000-02-17 20:38:33 +01:00
/* This thread had incremented waiters but had never decremented it.
Decrement it now. If it is >= 0 then there are possibly other
threads waiting for the lock, so trigger bruteforce. */
if (InterlockedDecrement (&waiters) >= 0)
SetEvent (bruteforce); /* Wake up one of the waiting threads */
else if (*name == '!')
{
CloseHandle (bruteforce); /* If *name == '!' and there are no
other waiters, then this is the
last time this muto will ever be
used, so close the handle. */
#ifdef DEBUGGING
bruteforce = NULL;
#endif
}
2000-02-17 20:38:33 +01:00
}
return 1; /* success. */
}