[MUD-Dev] Re: META: who are we?

Petri Virkkula pvirkkul at iki.fi
Wed Sep 23 04:19:35 CEST 1998


>>>>> "JCL" == J C Lawrence <claw at under.engr.sgi.com> writes:

JCL> Ditto.  I would that I had the time right now.  My old question
JCL> concerning lightweight locking supports is still out there
JCL> (http://www.kanga.nu/~petidomo/lists/mud-dev/1998Q3/msg00467.html).
JCL> I haven't had time to get much beyond that.

	I don't see why the virtualized semaphore idea wouldn't work
	if the implementation were slightly modified. See code below
	for detail how I suggest to be changed.

	As I see performance of the code below depends on value of
	MAX_LOCKS and implementation of HASH() and both can be
	modified based after you have studied the locking pattern of
	your code.


	Petri

--- cut here ---
typedef struct {
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} lock_entry_t;

typedef struct {
    unsigned short is_locked;
    unsigned short sleeper_count;
} lock_data_t;


typedef struct {
    ...
    lock_data_t lock_data;
    ...
} object_t;


static lock_entry_t lock_table[MAX_LOCKS];


#define HASH(ptr, size, max) \
    ((((unsigned long)(ptr)) / (size)) % (max))

    
void
lock_object (object_t *ob)
{
    unsigned int ix = HASH(ob, sizeof(object_t), MAX_LOCKS);

    pthread_mutex_lock (&lock_table[ix].mutex);
    while (ob->lock_data.is_locked) {
	ob->lock_data.sleeper_count++;
	pthread_cond_wait (&lock_table[ix].cond, &lock_table[ix].mutex);
	ob->lock_data.sleeper_count--;
    }
    ob->lock_data.is_locked = 1;
    pthread_mutex_unlock (&lock_table[ix].mutex);
}
    

void
unlock_object (object_t *ob)
{
    unsigned int ix = HASH(ob, sizeof(object_t), MAX_LOCKS);

    pthread_mutex_lock (&lock_table[ix].mutex);
    ob->lock_data.is_locked = 0;
    if (ob->lock_data.sleeper_count)
	pthread_cond_signal (&lock_table[ix].cond);
    pthread_mutex_unlock (&lock_table[ix].mutex);
}
--- cut here ---

	




More information about the mud-dev-archive mailing list