Thread safe with CAS (atomic_comapre_and_set) in embedded system












0















I am trying to use CAS to perform thread safe in an embedded system.
Unfortunatly, it fails to work properly.



Say there is a global array and several threads will access it.



typedef struct{
int in_use;
data_type data;
} item_type;
item_type global_array[N]


The idea is: threads could use available item in the global_array which indicated by "in_use" flag. the implementation as below:



item_type* get_available_item_pointer(void){
int available_index = N;
for (int i=0; i<N; i++){
if (atomic_compare_and_set(&(global_array[index].in_use), FALSE, TRUE))
{
available_index = i;
break;
}
}
return &(global_array[index]);
}
void free_item_pointer(item_type* item_ptr){
if (item_ptr->in_use){
memset(&item_ptr->data, 0x0, sizeof(data_type));
item_ptr->in_use = FALSE; //should I use atomic function for this line?
}
}


In bad case, I find 2 threads happen to access same item and have data corruption, I have no idea why it is possible?



thread A is calling get_available_item_pointer() //return index 0
thread B is calling free_item_pointer() //freeing index 0


BTW, the atomic_compare_and_set implementation is like:



static inline int atomic_compare_and_set(
unsigned int* target,
unsigned int old_val,
unsigned int new_val)
{
unsigned int current_val;
__asm__ __volatile__(
......
return current_val == old_val;
}









share|improve this question

























  • Without knowing the memory visibility rules for your platform, I'm not sure how we could know what you need to do differently. You're not using anything portable (for memory visibility).

    – David Schwartz
    Nov 26 '18 at 4:35


















0















I am trying to use CAS to perform thread safe in an embedded system.
Unfortunatly, it fails to work properly.



Say there is a global array and several threads will access it.



typedef struct{
int in_use;
data_type data;
} item_type;
item_type global_array[N]


The idea is: threads could use available item in the global_array which indicated by "in_use" flag. the implementation as below:



item_type* get_available_item_pointer(void){
int available_index = N;
for (int i=0; i<N; i++){
if (atomic_compare_and_set(&(global_array[index].in_use), FALSE, TRUE))
{
available_index = i;
break;
}
}
return &(global_array[index]);
}
void free_item_pointer(item_type* item_ptr){
if (item_ptr->in_use){
memset(&item_ptr->data, 0x0, sizeof(data_type));
item_ptr->in_use = FALSE; //should I use atomic function for this line?
}
}


In bad case, I find 2 threads happen to access same item and have data corruption, I have no idea why it is possible?



thread A is calling get_available_item_pointer() //return index 0
thread B is calling free_item_pointer() //freeing index 0


BTW, the atomic_compare_and_set implementation is like:



static inline int atomic_compare_and_set(
unsigned int* target,
unsigned int old_val,
unsigned int new_val)
{
unsigned int current_val;
__asm__ __volatile__(
......
return current_val == old_val;
}









share|improve this question

























  • Without knowing the memory visibility rules for your platform, I'm not sure how we could know what you need to do differently. You're not using anything portable (for memory visibility).

    – David Schwartz
    Nov 26 '18 at 4:35
















0












0








0








I am trying to use CAS to perform thread safe in an embedded system.
Unfortunatly, it fails to work properly.



Say there is a global array and several threads will access it.



typedef struct{
int in_use;
data_type data;
} item_type;
item_type global_array[N]


The idea is: threads could use available item in the global_array which indicated by "in_use" flag. the implementation as below:



item_type* get_available_item_pointer(void){
int available_index = N;
for (int i=0; i<N; i++){
if (atomic_compare_and_set(&(global_array[index].in_use), FALSE, TRUE))
{
available_index = i;
break;
}
}
return &(global_array[index]);
}
void free_item_pointer(item_type* item_ptr){
if (item_ptr->in_use){
memset(&item_ptr->data, 0x0, sizeof(data_type));
item_ptr->in_use = FALSE; //should I use atomic function for this line?
}
}


In bad case, I find 2 threads happen to access same item and have data corruption, I have no idea why it is possible?



thread A is calling get_available_item_pointer() //return index 0
thread B is calling free_item_pointer() //freeing index 0


BTW, the atomic_compare_and_set implementation is like:



static inline int atomic_compare_and_set(
unsigned int* target,
unsigned int old_val,
unsigned int new_val)
{
unsigned int current_val;
__asm__ __volatile__(
......
return current_val == old_val;
}









share|improve this question
















I am trying to use CAS to perform thread safe in an embedded system.
Unfortunatly, it fails to work properly.



Say there is a global array and several threads will access it.



typedef struct{
int in_use;
data_type data;
} item_type;
item_type global_array[N]


The idea is: threads could use available item in the global_array which indicated by "in_use" flag. the implementation as below:



item_type* get_available_item_pointer(void){
int available_index = N;
for (int i=0; i<N; i++){
if (atomic_compare_and_set(&(global_array[index].in_use), FALSE, TRUE))
{
available_index = i;
break;
}
}
return &(global_array[index]);
}
void free_item_pointer(item_type* item_ptr){
if (item_ptr->in_use){
memset(&item_ptr->data, 0x0, sizeof(data_type));
item_ptr->in_use = FALSE; //should I use atomic function for this line?
}
}


In bad case, I find 2 threads happen to access same item and have data corruption, I have no idea why it is possible?



thread A is calling get_available_item_pointer() //return index 0
thread B is calling free_item_pointer() //freeing index 0


BTW, the atomic_compare_and_set implementation is like:



static inline int atomic_compare_and_set(
unsigned int* target,
unsigned int old_val,
unsigned int new_val)
{
unsigned int current_val;
__asm__ __volatile__(
......
return current_val == old_val;
}






multithreading thread-safety cas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 4:26







云海帆

















asked Nov 26 '18 at 3:23









云海帆云海帆

11




11













  • Without knowing the memory visibility rules for your platform, I'm not sure how we could know what you need to do differently. You're not using anything portable (for memory visibility).

    – David Schwartz
    Nov 26 '18 at 4:35





















  • Without knowing the memory visibility rules for your platform, I'm not sure how we could know what you need to do differently. You're not using anything portable (for memory visibility).

    – David Schwartz
    Nov 26 '18 at 4:35



















Without knowing the memory visibility rules for your platform, I'm not sure how we could know what you need to do differently. You're not using anything portable (for memory visibility).

– David Schwartz
Nov 26 '18 at 4:35







Without knowing the memory visibility rules for your platform, I'm not sure how we could know what you need to do differently. You're not using anything portable (for memory visibility).

– David Schwartz
Nov 26 '18 at 4:35














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474398%2fthread-safe-with-cas-atomic-comapre-and-set-in-embedded-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474398%2fthread-safe-with-cas-atomic-comapre-and-set-in-embedded-system%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)