________________________________________________
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.
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.
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.
> 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 ?
Its a habit of mine to reserve the last lock in the array. Some of my code
uses that last lock for special purposes.
should be:
struct per_thread {
pthread_rwlock_t rwlck[100];
};
[...]
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.