1997-11-06 00:38:25 +03:00
|
|
|
/*
|
|
|
|
* user_locks.c --
|
|
|
|
*
|
2001-06-22 04:04:59 +04:00
|
|
|
* This loadable module provides support for user-level long-term
|
|
|
|
* cooperative locks.
|
1997-11-06 00:38:25 +03:00
|
|
|
*
|
1999-09-28 00:04:14 +04:00
|
|
|
* Copyright (C) 1999, Massimo Dal Zotto <dz@cs.unitn.it>
|
1998-08-30 23:37:51 +04:00
|
|
|
*
|
1999-09-28 00:04:14 +04:00
|
|
|
* This software is distributed under the GNU General Public License
|
1998-08-30 23:37:51 +04:00
|
|
|
* either version 2, or (at your option) any later version.
|
1997-11-06 00:38:25 +03:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2001-06-22 04:04:59 +04:00
|
|
|
|
1997-11-06 00:38:25 +03:00
|
|
|
#include "miscadmin.h"
|
1999-06-01 13:35:39 +04:00
|
|
|
#include "storage/lmgr.h"
|
1997-11-06 00:38:25 +03:00
|
|
|
#include "storage/proc.h"
|
|
|
|
|
|
|
|
#include "user_locks.h"
|
|
|
|
|
2001-06-22 04:04:59 +04:00
|
|
|
|
2005-04-30 02:28:24 +04:00
|
|
|
#define SET_LOCKTAG_USERLOCK(locktag,id1,id2) \
|
2005-05-18 01:46:11 +04:00
|
|
|
((locktag).locktag_field1 = MyDatabaseId, \
|
|
|
|
(locktag).locktag_field2 = (id1), \
|
|
|
|
(locktag).locktag_field3 = (id2), \
|
2005-04-30 02:28:24 +04:00
|
|
|
(locktag).locktag_field4 = 0, \
|
2005-12-09 04:22:04 +03:00
|
|
|
(locktag).locktag_type = LOCKTAG_USERLOCK, \
|
|
|
|
(locktag).locktag_lockmethodid = USER_LOCKMETHOD)
|
2005-04-30 02:28:24 +04:00
|
|
|
|
|
|
|
|
1997-11-06 00:38:25 +03:00
|
|
|
int
|
1999-06-01 13:35:39 +04:00
|
|
|
user_lock(uint32 id1, uint32 id2, LOCKMODE lockmode)
|
1997-11-06 00:38:25 +03:00
|
|
|
{
|
1998-02-26 07:46:47 +03:00
|
|
|
LOCKTAG tag;
|
1997-11-06 00:38:25 +03:00
|
|
|
|
2005-04-30 02:28:24 +04:00
|
|
|
SET_LOCKTAG_USERLOCK(tag, id1, id2);
|
1997-11-06 00:38:25 +03:00
|
|
|
|
2005-12-09 04:22:04 +03:00
|
|
|
return (LockAcquire(&tag, false,
|
2005-06-15 02:15:33 +04:00
|
|
|
lockmode, true, true) != LOCKACQUIRE_NOT_AVAIL);
|
1997-11-06 00:38:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-01 13:35:39 +04:00
|
|
|
user_unlock(uint32 id1, uint32 id2, LOCKMODE lockmode)
|
1997-11-06 00:38:25 +03:00
|
|
|
{
|
1998-02-26 07:46:47 +03:00
|
|
|
LOCKTAG tag;
|
|
|
|
|
2005-04-30 02:28:24 +04:00
|
|
|
SET_LOCKTAG_USERLOCK(tag, id1, id2);
|
1998-02-26 07:46:47 +03:00
|
|
|
|
2005-12-09 04:22:04 +03:00
|
|
|
return LockRelease(&tag, lockmode, true);
|
1997-11-06 00:38:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-06-01 13:35:39 +04:00
|
|
|
user_write_lock(uint32 id1, uint32 id2)
|
1997-11-06 00:38:25 +03:00
|
|
|
{
|
1999-06-01 13:35:39 +04:00
|
|
|
return user_lock(id1, id2, ExclusiveLock);
|
1997-11-06 00:38:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1999-06-01 13:35:39 +04:00
|
|
|
user_write_unlock(uint32 id1, uint32 id2)
|
1997-11-06 00:38:25 +03:00
|
|
|
{
|
1999-06-01 13:35:39 +04:00
|
|
|
return user_unlock(id1, id2, ExclusiveLock);
|
1997-11-06 00:38:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
user_write_lock_oid(Oid oid)
|
|
|
|
{
|
1999-06-01 13:35:39 +04:00
|
|
|
return user_lock(0, oid, ExclusiveLock);
|
1997-11-06 00:38:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
user_write_unlock_oid(Oid oid)
|
|
|
|
{
|
1999-06-01 13:35:39 +04:00
|
|
|
return user_unlock(0, oid, ExclusiveLock);
|
1997-11-06 00:38:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-07 04:27:30 +04:00
|
|
|
user_unlock_all(void)
|
1997-11-06 00:38:25 +03:00
|
|
|
{
|
2005-05-20 03:30:18 +04:00
|
|
|
LockReleaseAll(USER_LOCKMETHOD, true);
|
|
|
|
|
|
|
|
return true;
|
1997-11-06 00:38:25 +03:00
|
|
|
}
|