diff options
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/userlock/README.user_locks | 14 | ||||
-rw-r--r-- | contrib/userlock/user_locks.c | 30 | ||||
-rw-r--r-- | contrib/userlock/user_locks.h | 23 |
3 files changed, 26 insertions, 41 deletions
diff --git a/contrib/userlock/README.user_locks b/contrib/userlock/README.user_locks index 48acdd1d5fe..5e61d4eea04 100644 --- a/contrib/userlock/README.user_locks +++ b/contrib/userlock/README.user_locks @@ -35,11 +35,11 @@ It could also be done with a begin/end block but in this case the entire table would be locked by Postgres and it is not acceptable to do this for a long period because other transactions would block completely. -The generic user locks use two values, group and id, to identify a lock, -which correspond to ip_posid and ip_blkid of an ItemPointerData. -Group is a 16 bit value while id is a 32 bit integer which could also be -an oid. The oid user lock functions, which take only an oid as argument, -use a group equal to 0. +The generic user locks use two values, group and id, to identify a lock. +Each of these are 32-bit integers. + +The oid user lock functions, which take only an OID as argument, store the +OID as "id" with a group equal to 0. The meaning of group and id is defined by the application. The user lock code just takes two numbers and tells you if the corresponding @@ -47,7 +47,9 @@ entity has been successfully locked. What this means is up to you. My suggestion is that you use the group to identify an area of your application and the id to identify an object in this area. -Or you can just lock the oid of the tuples which are by definition unique. + +In all cases, user locks are local to individual databases within an +installation. Note also that a process can acquire more than one lock on the same entity and it must release the lock the corresponding number of times. This can diff --git a/contrib/userlock/user_locks.c b/contrib/userlock/user_locks.c index 3dee92ea316..207610084b1 100644 --- a/contrib/userlock/user_locks.c +++ b/contrib/userlock/user_locks.c @@ -18,16 +18,20 @@ #include "user_locks.h" +#define SET_LOCKTAG_USERLOCK(locktag,id1,id2) \ + ((locktag).locktag_field1 = (id1), \ + (locktag).locktag_field2 = (id2), \ + (locktag).locktag_field3 = MyDatabaseId, \ + (locktag).locktag_field4 = 0, \ + (locktag).locktag_type = LOCKTAG_USERLOCK) + + int user_lock(uint32 id1, uint32 id2, LOCKMODE lockmode) { LOCKTAG tag; - memset(&tag, 0, sizeof(LOCKTAG)); - tag.dbId = MyDatabaseId; - tag.relId = 0; - tag.objId.blkno = (BlockNumber) id2; - tag.offnum = (OffsetNumber) (id1 & 0xffff); + SET_LOCKTAG_USERLOCK(tag, id1, id2); return LockAcquire(USER_LOCKMETHOD, &tag, InvalidTransactionId, lockmode, true); @@ -38,11 +42,7 @@ user_unlock(uint32 id1, uint32 id2, LOCKMODE lockmode) { LOCKTAG tag; - memset(&tag, 0, sizeof(LOCKTAG)); - tag.dbId = MyDatabaseId; - tag.relId = 0; - tag.objId.blkno = (BlockNumber) id2; - tag.offnum = (OffsetNumber) (id1 & 0xffff); + SET_LOCKTAG_USERLOCK(tag, id1, id2); return LockRelease(USER_LOCKMETHOD, &tag, InvalidTransactionId, lockmode); } @@ -77,13 +77,3 @@ user_unlock_all(void) { return LockReleaseAll(USER_LOCKMETHOD, true); } - -/* end of file */ - -/* - * Local Variables: - * tab-width: 4 - * c-indent-level: 4 - * c-basic-offset: 4 - * End: - */ diff --git a/contrib/userlock/user_locks.h b/contrib/userlock/user_locks.h index 526e8646615..e00735d693d 100644 --- a/contrib/userlock/user_locks.h +++ b/contrib/userlock/user_locks.h @@ -1,19 +1,12 @@ #ifndef USER_LOCKS_H #define USER_LOCKS_H -int user_lock(unsigned int id1, unsigned int id2, LOCKMODE lockmode); -int user_unlock(unsigned int id1, unsigned int id2, LOCKMODE lockmode); -int user_write_lock(unsigned int id1, unsigned int id2); -int user_write_unlock(unsigned int id1, unsigned int id2); -int user_write_lock_oid(Oid oid); -int user_write_unlock_oid(Oid oid); -int user_unlock_all(void); -#endif +extern int user_lock(uint32 id1, uint32 id2, LOCKMODE lockmode); +extern int user_unlock(uint32 id1, uint32 id2, LOCKMODE lockmode); +extern int user_write_lock(uint32 id1, uint32 id2); +extern int user_write_unlock(uint32 id1, uint32 id2); +extern int user_write_lock_oid(Oid oid); +extern int user_write_unlock_oid(Oid oid); +extern int user_unlock_all(void); -/* - * Local Variables: - * tab-width: 4 - * c-indent-level: 4 - * c-basic-offset: 4 - * End: - */ +#endif |