[openbeos-midi] Re: BMidiEndPoint

  • From: "Matthijs Hollemans" <matthijs@xxxxxxxxxxxxxxxxxxx>
  • To: <openbeos-midi@xxxxxxxxxxxxx>
  • Date: Thu, 10 Oct 2002 13:34:18 +0100

> >From the Be Newsletter Volume 4 Issue 3 (one of the few places
where
> >Midi Kit 2is documented):
>
> I just find this document what are the few other places?

There is another newslettter, volume 3, issue 47. Later this week I
plan to look at what other documentation I have on midi2, but it
isn't much, I'm afraid.

> > This 2 functions just count up and down? or they make something
else?
>
> I guess BMidiEndpoint::Release() may, when reach a 0 count,
close/free/delete
> some internal stuffs, like closing the /dev/midi/xxxx device it
was using,
> before deleting itself.
> But it just a bet.

That would be my bet too. Basically, you would implement it
something like this, in pseudo code:

class BMidiEndpoint
{
private:
    uint32 counter;
    sem_id sem;
}

BMidiEndpoint::BMidiEndpoint()
{
    counter = 0;
    sem = create_sem();
}

BMidiEndpoint::~BMidiEndpoint()
{
       // clean up;
      release_sem(sem);
      delete_sem(sem);
}

BMidiEndpoint::Acquire()
{
    if (acquire_sem(sem) == B_NO_ERROR)
    {
        counter++;
        release_sem(sem);
    }
}

BMidiEndpoint::Release()
{
    if (acquire_sem(sem) == B_NO_ERROR)
    {
        counter--;

        if (counter == 0)
        {
            delete this;
        }
        else
        {
            release_sem(sem);
        }
    }
}

Like I said, this is pseudo code and you may want to think about the
way you use the semaphores (the example above probably isn't
entirely correct). Maybe semaphores are a bit overkill for this,
actually, and an atomic_add() is sufficient. In any case, you should
protect the counter from concurrent access by multiple threads.
--
Matthijs Hollemans
All Your Software
www.allyoursoftware.com



Other related posts: