How is the use of a mutex (lock) implemented?

I got a little confused trying to implement a very simple mutex (lock) in C. I understand that a mutex is like a binary semaphore, except that the mutex also applies the restriction that the thread that releases the lock must be the same thread that recently acquired it. Am I confused about how ownership is tracked?

This is what I still have. Keep in mind that it has not been completed yet, and it should be really simple (uniprocessor, without recursion on the mutex, disabling interrupts as a method of mutual exclusion, etc.).

struct mutex {
    char *mutexName;
    volatile int inUse;
};

I believe I should add another member variable, i.e. whoIsOwner, but I'm a little confused about what to store there. I guess this should be something that can uniquely identify a thread trying to cause a lock? It's right?

I have a thread structure in place that has a char * threadName member variable (along with others), but I'm not sure how to access it from a mutex implementation.

Any pointers / tips / ideas would be appreciated.

+3
source share
2 answers

, 0 , , . , , ( ).

, , ; , - . , . , bool .

+3
uint32_t  semOwner;

0, . "", ID/ ( , ).

, .

+2

All Articles