简体   繁体   English

如何检查从哪个文件调用此函数?

[英]How can i check this function is called from which file?

see i have one library which has two api lets call 看到我有一个具有两个api let调用的库

api1() 

and

 api2()

Now internally api2 calls api1 also. 现在内部api2也调用api1。

so in one situation i want to do is that 所以在一种情况下,我想做的是

  • when some other application is calling api1() then do some special work 当其他应用程序正在调用api1()时,请执行一些特殊的工作
  • but when api2() calls api1() then do'nt do that special work. 但是,当api2()调用api1()时,则不要执行该特殊工作。

how can i do that ? 我怎样才能做到这一点 ?

Is there any way so i can know that api1() is calling from library itself not application? 有什么办法让我知道api1()是从库本身而不是应用程序中调用的吗?

Edit : 编辑:

api1()
{
sem_wait();  // this create deadlock 

// do some task 

sem_post();
}

now api2() is like this 现在api2()就像这样

  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 ... 看到我两个函数...当应用程序调用api1()时,我需要在sem_wait和sem_post中工作,但是当api2()调用api1()时,我不想再次使用sem_wait,因为它使死锁...

i need some mechanism so api1() checks if it is being called from api2() then dont use sem_wait and sem_post 我需要某种机制,因此api1()检查是否从api2()调用了它,然后不要使用sem_wait和sem_post

That's not easy to achieve by introspection. 通过内省很难做到这一点。 You could investigate the call stack but that is very non-portable and really not to be recommended. 您可以调查调用堆栈,但这是非常不可移植的,因此不建议这样做。

One way to deal with this is to pass a parameter to control the variation of behaviour. 解决此问题的一种方法是传递参数来控制行为的变化。 This has the addition benefit of making the function's behaviour more transparent and explicit. 这具有使功能的行为更加透明和明确的附加好处。 As @blueshift points out, this can place a burden on external callers. 正如@blueshift指出的那样,这可能会给外部呼叫者带来负担。 Splitting the function into two versions, one for internal use and one for external use relieves that burden. 将功能分为两个版本,一个用于内部使用,一个用于外部使用可以减轻这种负担。


In light of your edit, I think the design is a little off. 根据您的编辑,我认为设计有些不完善。 The synchronization responsibility should either be internal to the function, or external. 同步责任应该在功能内部或外部。 It's very risky to make that responsibility sometimes be internal and sometimes be external. 使责任有时是内部的,有时是外部的,这是非常冒险的。 That would be one way round the problem. 那将是解决问题的一种方法。 Another way around it would be to use recursive locks. 解决它的另一种方法是使用递归锁。

You'll need two different function calls (function name or parameter) for the two different jobs (+special work and -special work). 对于两个不同的作业(+特殊工作和-特殊工作),您将需要两个不同的函数调用(函数名称或参数)。 Context-sensitive functions are not only not supported by C, but absolutely and totally evil because they add a whole Chomsky of complexity. C不仅不支持上下文相关的函数,而且绝对有害,因为它们增加了整个Chomsky的复杂性。

Often done something like this: 经常这样做:

// internal function
static int api1_internal(void) {
  do stuff assuming the lock is held;
}

// function for external callers, doing extra locking
int api1(void) {
  int ret;
  lock();
  ret = api1_internal();
  unlock();
  return ret;
}

// some internal function using internal api1
void internalfunc(void) {
  lock();
  do some things();
  api1_internal();
  unlock();
}

Note api1_internal is declared static to stop anything outside that file using it. 注意api1_internal声明为static,以停止使用该文件的文件之外的任何文件。

If you really want to confuse yourself you could put the definition of api1() first, then have something like 如果您真的想弄混自己,可以先输入api1()的定义,然后输入类似

#define api1 api1_internal

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 对于内部的非锁定版本的功能,通常使用前导下划线,因此

static int _api1(void)

You can build your library so that api2() tells api1() that it is the caller. 您可以构建您的库,以便api2()告诉api1()它是调用方。 When called from outside, api1() sees an unknown caller. 从外部调用时, api1()看到未知的调用者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查某个函数是否可以被另一个函数间接调用? - How can I check if a certain function could be indirectly called by another certain function? 从.cpp文件中调用的函数,该文件在.c文件中定义 - function called from .cpp file which is defined in in .c file 如何检查以3秒为间隔的函数被调用多少次? - How can I check how many times a function was called with a 3 seconds interval? 如果我的函数来自另一个 .c,我能知道从哪里调用我的函数的文件和函数名称吗? - Can I know the file and function names from where my function is called if it's from another .c? 如何增加被调用函数中的值? - How can I increment the value in the called function? 如何从整数之前的文件中读取字符串 - How can i read a string from a file which is before a integer 如何从其他函数中调用的函数中获取返回值? - How can i get the return value from a function called in other function? 如何获取从命令行参数调用的文件的目录? - How to get directory of a file which is called from command line argument? 如何解决“被调用的对象不是函数或函数指针”错误? - How can I resolve a "called object is not a function or function pointer" error? 如何从这个递归函数中获取由 fork() 调用的子进程总数? - How can I get the total number of child processes called by fork() from this recursive function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM