Summary: Stack-allocated variables cannot be raced on in cpp as every thread has its own stack. At the beginning of the analysis we add ownership to the local variables. Reviewed By: jberdine Differential Revision: D6020506 fbshipit-source-id: 0a90a97master
parent
aa33cc7eac
commit
3001cb6323
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017 - present Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace locals {
|
||||||
|
|
||||||
|
struct X {
|
||||||
|
int f;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Ownership {
|
||||||
|
public:
|
||||||
|
Ownership() {}
|
||||||
|
|
||||||
|
int test0_ok() {
|
||||||
|
X x;
|
||||||
|
mutex_.lock();
|
||||||
|
x.f = 7;
|
||||||
|
mutex_.unlock();
|
||||||
|
return x.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test1_ok() {
|
||||||
|
X* x = new X();
|
||||||
|
mutex_.lock();
|
||||||
|
x->f = 7;
|
||||||
|
mutex_.unlock();
|
||||||
|
return x->f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test2_ok() {
|
||||||
|
X x = current; // copy constructor
|
||||||
|
mutex_.lock();
|
||||||
|
x.f = 7;
|
||||||
|
mutex_.unlock();
|
||||||
|
return x.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test2_bad() {
|
||||||
|
X* x = ¤t;
|
||||||
|
mutex_.lock();
|
||||||
|
x->f = 7;
|
||||||
|
mutex_.unlock();
|
||||||
|
return x->f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test3_ok(X xformal) {
|
||||||
|
X x = xformal; // copy constructor
|
||||||
|
mutex_.lock();
|
||||||
|
x.f = 7;
|
||||||
|
mutex_.unlock();
|
||||||
|
return x.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test3_bad(X* xformal) {
|
||||||
|
X* x = xformal;
|
||||||
|
mutex_.lock();
|
||||||
|
x->f = 7;
|
||||||
|
mutex_.unlock();
|
||||||
|
return x->f;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
X current;
|
||||||
|
std::mutex mutex_;
|
||||||
|
};
|
||||||
|
} // namespace locals
|
Loading…
Reference in new issue