Plan 9 from Bell Labs’s /usr/web/sources/contrib/quanstro/locktest/semlock2.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include "semlock.h"

extern int xadd(long*, int);		/* wrong signature */

void
lock(Lock *l)
{
	if(xadd(&l->key, 1) == 0)
		return;	/* changed from 0 -> 1: we hold lock */
	/* otherwise wait in kernel */
	while(semacquire(&l->sem, 1) < 0){
		/* interrupted; try again */
	}
}

void
unlock(Lock *l)
{
	if(xadd(&l->key, -1) == 1)
		return;	/* changed from 1 -> 0: no contention */
	semrelease(&l->sem, 1);
}

int
canlock(Lock *l)
{
	if(xadd(&l->key, 1) == 0)
		return 1;	/* changed from 0 -> 1: success */
	/* Undo increment (but don't miss wakeup) */
	if(xadd(&l->key, -1) == 1)
		return 0;	/* changed from 1 -> 0: no contention */
	semrelease(&l->sem, 1);
	return 0;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].