Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7838909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:15:27+00:00 2026-06-02T15:15:27+00:00

I’m trying to implement mutexes inside my standard library but I’m not getting anywhere

  • 0

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.

My implementation of the locking function is provided below. I’m pretty sure there is something seriously wrong with it, since I have no idea what I’m doing.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:15:28+00:00Added an answer on June 2, 2026 at 3:15 pm

    http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html

    Look into __sync_lock_test_and_set
    You have to have hardware support for doing atomics. GCC builtins are a good start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.