How do I lock an object for one person at a time?

Submitted by javelin on Sun, 2012-02-12 23:16

How do I lock an object for one person at a time?

The following prevents a second user coming along and being built a room by a vendor whilst still processing a previous user request:
&INUSE Room Digger<rd>=0
In the Room Digger object\'s dig command:
@switch v(INUSE)=<1,{v(INUSE)=1;{build the room};v(INUSE)=0},{@pemit %#=Sorry but the clerk is busy dealing with another client. Wait a few seconds.}


2008-May-09 3:51pm eric.twose

Actually, that code\'s wrong in several ways. v(INUSE)=.. should probably be &INUSE me=..., because commands are expected there, not function. But the more serious problem is that there\'s a race condition. @switch queues its action lists, so you\'ll find that you can very quickly trigger that command twice. For real protection, you could use a semaphore, but the easiest modern approach involves @break:

$build: @break [v(INUSE)]=@pemit%#=Sorry but the clerk is busy; 
   &INUSE me=1; 
   @@ Build the room;
   &INUSE me=0

This assumes your room-building commands don\'t use the queue (that is, don\'t involve @wait, @force, @trigger, @switch). If they do, semaphores are your friend:

&build vendor=
    $build: @wait me={ @@ Room building commands; 
        @notify me }
@drain vendor
@notify vendor

2012-Feb-10 javelin