19 #include <semaphore.h>
24 #include <sys/types.h>
29 std::string SharedMemoryRegion::Path(
const char *
Name) {
33 std::string SharedMemoryRegion::SemName(
const char *Name,
int Idx) {
34 std::string Res(Name);
35 return Res + (
char)(
'0' + Idx);
38 bool SharedMemoryRegion::Map(
int fd) {
40 (uint8_t *)mmap(0, kShmemSize, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
41 if (Data == (uint8_t*)-1)
47 int fd = open(Path(Name).
c_str(), O_CREAT | O_RDWR, 0777);
48 if (fd < 0)
return false;
49 if (ftruncate(fd, kShmemSize) < 0)
return false;
52 for (
int i = 0; i < 2; i++) {
53 sem_unlink(SemName(Name, i).
c_str());
54 Semaphore[i] = sem_open(SemName(Name, i).
c_str(), O_CREAT, 0644, 0);
55 if (Semaphore[i] == (
void *)-1)
63 int fd = open(Path(Name).
c_str(), O_RDWR);
64 if (fd < 0)
return false;
66 if (0 != fstat(fd, &stat_res))
68 assert(stat_res.st_size == kShmemSize);
71 for (
int i = 0; i < 2; i++) {
72 Semaphore[i] = sem_open(SemName(Name, i).
c_str(), 0);
73 if (Semaphore[i] == (
void *)-1)
81 return 0 == unlink(Path(Name).
c_str());
84 void SharedMemoryRegion::Post(
int Idx) {
85 assert(Idx == 0 || Idx == 1);
86 sem_post((sem_t*)Semaphore[Idx]);
90 assert(Idx == 0 || Idx == 1);
91 for (
int i = 0; i < 10 && sem_wait((sem_t*)Semaphore[Idx]); i++) {
95 Printf(
"%s: sem_wait[%d] failed %s\n", i < 9 ?
"WARNING" :
"ERROR", i,
103 #endif // LIBFUZZER_POSIX
bool Create(const char *Name)
bool Open(const char *Name)
std::string DirPlusFile(const std::string &DirPath, const std::string &FileName)
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
bool Destroy(const char *Name)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ProcessInfo Wait(const ProcessInfo &PI, unsigned SecondsToWait, bool WaitUntilTerminates, std::string *ErrMsg=nullptr)
This function waits for the process specified by PI to finish.
Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i])