Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tsan] Add support for OS X OSAtomic* functions
OS X provides atomic functions in libkern/OSAtomic.h. These provide atomic guarantees and they have alternatives which have barrier semantics. This patch adds proper TSan support for the functions from libkern/OSAtomic.h. Differential Revision: http://reviews.llvm.org/D18500 llvm-svn: 265665
- Loading branch information
1 parent
7231c33
commit 74f7f39
Showing
3 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// RUN: %clang_tsan %s -o %t -framework Foundation -std=c++11 | ||
// RUN: %run %t 2>&1 | FileCheck %s | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <libkern/OSAtomic.h> | ||
|
||
#include <thread> | ||
|
||
volatile int64_t retainCount = 0; | ||
|
||
long g = 0; | ||
|
||
void dealloc() { | ||
g = 42; | ||
} | ||
|
||
void release() { | ||
if (OSAtomicAdd64Barrier(-1, &retainCount) == 0) { | ||
dealloc(); | ||
} | ||
} | ||
|
||
void retain() { | ||
OSAtomicAdd64Barrier(1, &retainCount); | ||
} | ||
|
||
int main(int argc, const char * argv[]) { | ||
fprintf(stderr, "start\n"); | ||
retain(); | ||
retain(); | ||
|
||
std::thread t([]{ | ||
release(); | ||
}); | ||
|
||
g = 47; | ||
|
||
release(); | ||
t.join(); | ||
|
||
fprintf(stderr, "end, g = %ld\n", g); | ||
|
||
return 0; | ||
} | ||
|
||
// CHECK: start | ||
// CHECK: end, g = 42 | ||
// CHECK-NOT: WARNING: ThreadSanitizer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: %clang_tsan %s -o %t -framework Foundation -std=c++11 | ||
// RUN: %run %t 2>&1 | FileCheck %s | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <libkern/OSAtomic.h> | ||
|
||
#include <thread> | ||
|
||
#include "../test.h" | ||
|
||
typedef struct { | ||
void *next; | ||
long data; | ||
} ListItem; | ||
|
||
OSQueueHead q; | ||
|
||
int main(int argc, const char *argv[]) { | ||
barrier_init(&barrier, 2); | ||
|
||
std::thread t1([] { | ||
ListItem *li = new ListItem{nullptr, 42}; | ||
OSAtomicEnqueue(&q, li, 0); | ||
barrier_wait(&barrier); | ||
}); | ||
|
||
std::thread t2([] { | ||
barrier_wait(&barrier); | ||
ListItem *li = (ListItem *)OSAtomicDequeue(&q, 0); | ||
fprintf(stderr, "data = %ld\n", li->data); | ||
}); | ||
|
||
t1.join(); | ||
t2.join(); | ||
|
||
fprintf(stderr, "done\n"); | ||
|
||
return 0; | ||
} | ||
|
||
// CHECK: data = 42 | ||
// CHECK: done | ||
// CHECK-NOT: WARNING: ThreadSanitizer |