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

  • Home
  • SEARCH
  • 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 8676639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:14:53+00:00 2026-06-12T20:14:53+00:00

So I think I understand the source code for a signal and a wait

  • 0

So I think I understand the source code for a signal and a wait (the wait being a lock) but I am not sure how to implement a try lock.
Here is my code for a wait:

//if s->type is zero it is a binary semaphore type
if (s->type == 0)
    {
            // binary semaphore
            // if state is zero, then block task

            if (s->state == 0)
            {
                             // block task


                    // ?? move task from ready queue to blocked queue

                              //reschedule the tasks
                    return 1;
            }
            // state is non-zero (semaphore already signaled)
            s->state = 0;                // reset state, and don't block
            return 0;
    }
    else
    {
            // counting semaphore
            s->state--;
            // ?? implement counting semaphore
            if (s->state < 0)
            {


            }
    }

This is what I have for a try lock so far:

if (s->type == 0)
{
            // binary semaphore
            // if state is zero, then block task

            if (s->state == 0)
            {
                    tcb[curTask].event = s;         // block task
                    tcb[curTask].state = S_BLOCKED;

                    removeNode(tcb[curTask].priority, READY_QUEUE, curTask);
                    enqueue(tcb[curTask].priority, curTask, BLOCKED_QUEUE);
                    return 1;
            }
            // state is non-zero (semaphore already signaled)
            s->state = 1;                                           // reset state, and don't block
            return 0;
}
else
{
        s->state--;
        if (s->state >= 0)
        {
            s->state++;
        }
        else
        {
            tcb[curTask].event = s;
            tcb[curTask].state = S_BLOCKED;
            removeNode(tcb[curTask].priority, READY_QUEUE, curTask);
            enqueue(tcb[curTask].priority, curTask, BLOCKED_QUEUE);
        }
}
  • 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-12T20:14:54+00:00Added an answer on June 12, 2026 at 8:14 pm

    I was looking for a non-spin lock trylock.
    I have figured out what to do. If it is a counting semaphore, then I decrement if the count is positive and I consume the resource. If it is zero or less, I do nothing but return an error code. I do not decrement the count or consume the resource. The program is then able to continue past that point. If it is a binary semaphore, I consume it if the resource is available. I then change the value of the binary semaphore to consumed. If it isn’t available, then I return an error code.

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

Sidebar

Related Questions

I am not quite sure how to implement my the code after generating the
I think I understand the basic principals of T4 but I'm having a hard
I think I understand strong typing , but every time I look for examples
I understand the regular fixed-point type combinator and I think I understand the higher-order
So I (think I) understand the difference between Float, Double, and Decimal , but
I have never seen this being done anywhere in all the source code i've
Quite often I see source code where language's keyword are replaced with full type
I am reading STL source code right now. Though I understand the meat in
I am trying to understand the Linux driver source code associated with Wi-Fi cards
I got an open source code, about 15 mb. I want to understand the

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.