2000-02-18 03:38:33 +08:00
|
|
|
/* sync.h: Header file for cygwin synchronization primitives.
|
|
|
|
|
2012-03-17 04:20:29 +08:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2011, 2012
|
|
|
|
Red Hat, Inc.
|
2000-02-18 03:38:33 +08: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. */
|
|
|
|
|
2012-03-17 04:20:29 +08:00
|
|
|
#pragma once
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* FIXME: Note that currently this class cannot be allocated via `new' since
|
|
|
|
there are issues with malloc and fork. */
|
|
|
|
class muto
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
public:
|
|
|
|
const char *name;
|
|
|
|
private:
|
2000-02-18 03:38:33 +08:00
|
|
|
LONG sync; /* Used to serialize access to this class. */
|
|
|
|
LONG waiters; /* Number of threads waiting for lock. */
|
|
|
|
HANDLE bruteforce; /* event handle used to control waiting for lock. */
|
|
|
|
public:
|
2004-03-15 23:50:20 +08:00
|
|
|
LONG visits; /* Count of number of times a thread has called acquire. */
|
2004-05-16 12:18:50 +08:00
|
|
|
void *tls; /* Tls of lock owner. */
|
2004-01-14 23:45:37 +08:00
|
|
|
// class muto *next;
|
2001-11-03 11:32:27 +08:00
|
|
|
|
|
|
|
/* The real constructor. */
|
2005-04-05 12:31:00 +08:00
|
|
|
muto *init (const char *) __attribute__ ((regparm (2)));
|
2001-11-03 11:32:27 +08:00
|
|
|
|
2002-02-15 05:20:06 +08:00
|
|
|
#if 0 /* FIXME: See comment in sync.cc */
|
|
|
|
~muto ()
|
|
|
|
#endif
|
2002-08-17 03:41:39 +08:00
|
|
|
int acquire (DWORD ms = INFINITE) __attribute__ ((regparm (2))); /* Acquire the lock. */
|
2012-12-22 05:30:56 +08:00
|
|
|
int release (_cygtls * = &_my_tls) __attribute__ ((regparm (2))); /* Release the lock. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
bool acquired () __attribute__ ((regparm (1)));
|
|
|
|
void upforgrabs () {tls = this;} // just set to an invalid address
|
|
|
|
void grab () __attribute__ ((regparm (1)));
|
2005-04-05 12:31:00 +08:00
|
|
|
operator int () const {return !!name;}
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2005-10-24 07:47:45 +08:00
|
|
|
class lock_process
|
|
|
|
{
|
|
|
|
bool skip_unlock;
|
|
|
|
static muto locker;
|
|
|
|
public:
|
|
|
|
static void init () {locker.init ("lock_process");}
|
2006-07-15 06:15:12 +08:00
|
|
|
void dont_bother () {skip_unlock = true;}
|
2012-12-22 11:16:43 +08:00
|
|
|
lock_process (bool exiting = false)
|
2005-10-24 07:47:45 +08:00
|
|
|
{
|
2012-12-22 11:16:43 +08:00
|
|
|
locker.acquire ();
|
2005-10-24 07:47:45 +08:00
|
|
|
skip_unlock = exiting;
|
|
|
|
}
|
2012-12-29 02:06:17 +08:00
|
|
|
void release ()
|
|
|
|
{
|
|
|
|
locker.release ();
|
|
|
|
skip_unlock = true;
|
|
|
|
}
|
2005-10-24 07:47:45 +08:00
|
|
|
~lock_process ()
|
|
|
|
{
|
|
|
|
if (!skip_unlock)
|
2012-12-29 02:06:17 +08:00
|
|
|
release ();
|
2005-10-24 07:47:45 +08:00
|
|
|
}
|
2013-01-14 14:03:59 +08:00
|
|
|
operator LONG () const {return locker.visits; }
|
2012-12-22 05:30:56 +08:00
|
|
|
static void force_release (_cygtls *tid) {locker.release (tid);}
|
2005-10-24 07:47:45 +08:00
|
|
|
friend class dtable;
|
2006-06-23 08:19:39 +08:00
|
|
|
friend class fhandler_fifo;
|
2005-10-24 07:47:45 +08:00
|
|
|
};
|