I m trying to implement mutexes inside my standard library but I m not getting anywhere with them. I know that it is a bad idea to try to implement something that you do not necessarily understand, but I have to, since compiling an existing threading library (such as uClibc) is impossible for this platform. Are there any "explain it to me like I m 5" things for mutexes? Or are there any "simple to follow" implementations? All of the pthread implementations I ve seen so far are impossible to follow.
我履行锁定职能的情况如下。 我确信,这确实存在严重错误,因为我不知道我做什么。
int pthread_mutex_lock(pthread_mutex_t *pmutex)
{
OSMutex* mutex = GetOSMutex(pmutex);
/* Decrement the mutex counter */
OSAtomicDecrement32(&(mutex->count));
if (mutex->count < -1) {
/*
Contended, wait for the mutex to be released.
*/
lnkLog("mutex %p already held (n=%d), waiting for stuff to change", mutex, mutex->count);
futex$LINUX(&(mutex->data),
FUTEX_WAIT,
MUTEX_MAGIC,
NULL,
NULL,
0);
lnkLog("mutex %p was released", mutex);
return 0;
}
else {
/*
Not contended. Acquire the mutex.
*/
lnkLog("locking %p", mutex);
mutex->data = MUTEX_MAGIC;
return 0;
}
}
P.S. If you re wondering about the futexes, I m using the Linux kernel.