Mark function to be used only under lock
up vote
0
down vote
favorite
I'm writing a linux kernel driver and I have a function which MUST be called under lock, and I want to make sure no one will use it without acquiring the lock.
This function is called many times from different functions which acquire the lock prior to calling the function.
I'm wondering if there is a way of telling the compiler to verify that all calls to that function are done under a lock?
something like function attributes or something similar.
thanks.
linux linux-kernel locking spinlock function-attributes
add a comment |
up vote
0
down vote
favorite
I'm writing a linux kernel driver and I have a function which MUST be called under lock, and I want to make sure no one will use it without acquiring the lock.
This function is called many times from different functions which acquire the lock prior to calling the function.
I'm wondering if there is a way of telling the compiler to verify that all calls to that function are done under a lock?
something like function attributes or something similar.
thanks.
linux linux-kernel locking spinlock function-attributes
You don't say what language? Assuming c++, you could insist that one argument is a reference to the RAII object wrapping the lock. If caller don't have that RAII object they can't have locked? If the fn is deeply nested vs the locker, passing it round will be a pain. Another option is to put all the methods that need the lock in an internal class, and only by calling a lock method on the public class can you get back an RAII object which both releases the lock and exposes the inner interface. The public class can also have helper methods that hold the lock and call the inner method one-off.
– Gem Taylor
Nov 21 at 23:31
sorry, it's for linux kernel driver, language is C.
– Idan Emergi
Nov 22 at 9:18
No. There is none of my knowledge. I know the opposite, but it’s still for runtime, and not for compile time. Use comment block for that.
– 0andriy
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm writing a linux kernel driver and I have a function which MUST be called under lock, and I want to make sure no one will use it without acquiring the lock.
This function is called many times from different functions which acquire the lock prior to calling the function.
I'm wondering if there is a way of telling the compiler to verify that all calls to that function are done under a lock?
something like function attributes or something similar.
thanks.
linux linux-kernel locking spinlock function-attributes
I'm writing a linux kernel driver and I have a function which MUST be called under lock, and I want to make sure no one will use it without acquiring the lock.
This function is called many times from different functions which acquire the lock prior to calling the function.
I'm wondering if there is a way of telling the compiler to verify that all calls to that function are done under a lock?
something like function attributes or something similar.
thanks.
linux linux-kernel locking spinlock function-attributes
linux linux-kernel locking spinlock function-attributes
edited Nov 22 at 9:17
asked Nov 21 at 20:15
Idan Emergi
11
11
You don't say what language? Assuming c++, you could insist that one argument is a reference to the RAII object wrapping the lock. If caller don't have that RAII object they can't have locked? If the fn is deeply nested vs the locker, passing it round will be a pain. Another option is to put all the methods that need the lock in an internal class, and only by calling a lock method on the public class can you get back an RAII object which both releases the lock and exposes the inner interface. The public class can also have helper methods that hold the lock and call the inner method one-off.
– Gem Taylor
Nov 21 at 23:31
sorry, it's for linux kernel driver, language is C.
– Idan Emergi
Nov 22 at 9:18
No. There is none of my knowledge. I know the opposite, but it’s still for runtime, and not for compile time. Use comment block for that.
– 0andriy
2 days ago
add a comment |
You don't say what language? Assuming c++, you could insist that one argument is a reference to the RAII object wrapping the lock. If caller don't have that RAII object they can't have locked? If the fn is deeply nested vs the locker, passing it round will be a pain. Another option is to put all the methods that need the lock in an internal class, and only by calling a lock method on the public class can you get back an RAII object which both releases the lock and exposes the inner interface. The public class can also have helper methods that hold the lock and call the inner method one-off.
– Gem Taylor
Nov 21 at 23:31
sorry, it's for linux kernel driver, language is C.
– Idan Emergi
Nov 22 at 9:18
No. There is none of my knowledge. I know the opposite, but it’s still for runtime, and not for compile time. Use comment block for that.
– 0andriy
2 days ago
You don't say what language? Assuming c++, you could insist that one argument is a reference to the RAII object wrapping the lock. If caller don't have that RAII object they can't have locked? If the fn is deeply nested vs the locker, passing it round will be a pain. Another option is to put all the methods that need the lock in an internal class, and only by calling a lock method on the public class can you get back an RAII object which both releases the lock and exposes the inner interface. The public class can also have helper methods that hold the lock and call the inner method one-off.
– Gem Taylor
Nov 21 at 23:31
You don't say what language? Assuming c++, you could insist that one argument is a reference to the RAII object wrapping the lock. If caller don't have that RAII object they can't have locked? If the fn is deeply nested vs the locker, passing it round will be a pain. Another option is to put all the methods that need the lock in an internal class, and only by calling a lock method on the public class can you get back an RAII object which both releases the lock and exposes the inner interface. The public class can also have helper methods that hold the lock and call the inner method one-off.
– Gem Taylor
Nov 21 at 23:31
sorry, it's for linux kernel driver, language is C.
– Idan Emergi
Nov 22 at 9:18
sorry, it's for linux kernel driver, language is C.
– Idan Emergi
Nov 22 at 9:18
No. There is none of my knowledge. I know the opposite, but it’s still for runtime, and not for compile time. Use comment block for that.
– 0andriy
2 days ago
No. There is none of my knowledge. I know the opposite, but it’s still for runtime, and not for compile time. Use comment block for that.
– 0andriy
2 days ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419851%2fmark-function-to-be-used-only-under-lock%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You don't say what language? Assuming c++, you could insist that one argument is a reference to the RAII object wrapping the lock. If caller don't have that RAII object they can't have locked? If the fn is deeply nested vs the locker, passing it round will be a pain. Another option is to put all the methods that need the lock in an internal class, and only by calling a lock method on the public class can you get back an RAII object which both releases the lock and exposes the inner interface. The public class can also have helper methods that hold the lock and call the inner method one-off.
– Gem Taylor
Nov 21 at 23:31
sorry, it's for linux kernel driver, language is C.
– Idan Emergi
Nov 22 at 9:18
No. There is none of my knowledge. I know the opposite, but it’s still for runtime, and not for compile time. Use comment block for that.
– 0andriy
2 days ago