see i have one library which has two api lets call
api1()
and
api2()
Now internally api2 calls api1 also.
so in one situation i want to do is that
- when some other application is calling api1() then do some special work
- but when api2() calls api1() then do’nt do that special work.
how can i do that ?
Is there any way so i can know that api1() is calling from library itself not application?
Edit :
api1()
{
sem_wait(); // this create deadlock
// do some task
sem_post();
}
now api2() is like this
api2()
{
sem_wait();
api1();
sem_post();
}
see my both function…when application calls api1() i need to be work in sem_wait and sem_post but when api2() calls api1() then i dont want to sem_wait again because its make dead locks …
i need some mechanism so api1() checks if it is being called from api2() then dont use sem_wait and sem_post
Often done something like this:
Note api1_internal is declared static to stop anything outside that file using it.
If you really want to confuse yourself you could put the definition of api1() first, then have something like
Which would keep your code pretty, but just confuse you later. Don’t do that.
It’s common to use a leading underscore for internal, non-locking versions of functions, so