Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

simple distributed POSIX rw-mutex...

4 views
Skip to first unread message

Chris Thomasson

unread,
Oct 26, 2007, 3:55:01 AM10/26/07
to
Here is the code:

________________________________________________
struct per_thread {
pthread_mutex_t rwlck[100];
};


#define PER_THREAD_RWHASH(mp_ptr) \
(((size_t)(mp_ptr)) % 99)


int read_lock(
per_thread* const _this,
void const* ptr
) {
return pthread_rwlock_rdlock(
&_this->rwlck[PER_THREAD_RWHASH(ptr)]
);
}

int read_unlock(
per_thread* const _this,
void const* ptr
) {
return pthread_rwlock_unlock(
&_this->rwlck[PER_THREAD_RWHASH(ptr)]
);
}


int write_lock(
void const* ptr
) {
for each per_thread as _this {
return pthread_rwlock_wrlock(
&_this->rwlck[PER_THREAD_RWHASH(ptr)]
);
}
}


int write_unlock(
void const* ptr
) {
for each per_thread as _this {
return pthread_rwlock_wunlock(
&_this->rwlck[PER_THREAD_RWHASH(ptr)]
);
}
}

________________________________________________

That scales better than using a single rw-mutex... Might be useful in some
scenarios. I think linux/us kernel used this scheme as well.

Chris Thomasson

unread,
Oct 26, 2007, 3:58:18 AM10/26/07
to

"Chris Thomasson" <cri...@comcast.net> wrote in message
news:Gt6dnUN4hfO5Abza...@comcast.com...

> Here is the code:
>
> ________________________________________________
>
> int write_lock(
> void const* ptr
> ) {
> for each per_thread as _this {
> return pthread_rwlock_wrlock(
> &_this->rwlck[PER_THREAD_RWHASH(ptr)]
> );
> }
> }
>
>
> int write_unlock(
> void const* ptr
> ) {
> for each per_thread as _this {
> return pthread_rwlock_wunlock(
> &_this->rwlck[PER_THREAD_RWHASH(ptr)]
> );
> }
> }

That should be:


int write_lock(
void const* ptr
) {
for each per_thread as _this {

pthread_rwlock_wrlock(
&_this->rwlck[PER_THREAD_RWHASH(ptr)]
);
}

return [...];
}

int write_unlock(
void const* ptr
) {
for each per_thread as _this {

pthread_rwlock_unlock(
&_this->rwlck[PER_THREAD_RWHASH(ptr)]
);
}

return [...];
}


Of course!


Sorry for any confusion.


Joe Seigh

unread,
Oct 26, 2007, 6:11:01 AM10/26/07
to


http://groups.google.com/group/comp.programming.threads/msg/0373939b42517498

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.

Arnold Hendriks

unread,
Oct 26, 2007, 5:30:20 PM10/26/07
to
"Chris Thomasson" <cri...@comcast.net> wrote in message
news:Gt6dnUN4hfO5Abza...@comcast.com...

> struct per_thread {


> pthread_mutex_t rwlck[100];
> };
>
> #define PER_THREAD_RWHASH(mp_ptr) \
> (((size_t)(mp_ptr)) % 99)

Any reason for not using % 100 ?


Chris Thomasson

unread,
Oct 26, 2007, 8:50:55 PM10/26/07
to
"Arnold Hendriks" <a.hen...@b-lex.nl> wrote in message
news:fftm9e$l0f$1...@news1.zwoll1.ov.home.nl...

Its a habit of mine to reserve the last lock in the array. Some of my code
uses that last lock for special purposes.

Chris Thomasson

unread,
Oct 26, 2007, 8:56:53 PM10/26/07
to

"Chris Thomasson" <cri...@comcast.net> wrote in message
news:Gt6dnUN4hfO5Abza...@comcast.com...
> Here is the code:
>
> ________________________________________________
> struct per_thread {
> pthread_mutex_t rwlck[100];
> };
^^^^^^^^^^^^^^^^^

should be:

struct per_thread {
pthread_rwlock_t rwlck[100];
};

[...]


Chris Thomasson

unread,
Oct 26, 2007, 9:03:35 PM10/26/07
to

"Joe Seigh" <jsei...@xemaps.com> wrote in message
news:V2jUi.36732$DX.12642@trnddc06...

> Chris Thomasson wrote:
>> Here is the code:
[...]

>> That scales better than using a single rw-mutex... Might be useful in
>> some scenarios. I think linux/us kernel used this scheme as well.
>
>
> http://groups.google.com/group/comp.programming.threads/msg/0373939b42517498

Right. I just transformed the locking array from a global entity into an
array per-thread. You can still use it to access global structures.

0 new messages