Skip to content

Commit

Permalink
cond var
Browse files Browse the repository at this point in the history
  • Loading branch information
uecker committed Jul 8, 2024
1 parent 33f3143 commit dbffab5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
52 changes: 52 additions & 0 deletions src/misc/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,55 @@ void bart_lock_destroy(bart_lock_t* lock)
xfree(lock);
}


struct bart_cond {

unsigned long counter;
};

bart_cond_t* bart_cond_create(void)
{
bart_cond_t* cond = xmalloc(sizeof *cond);

cond->counter = 0;
return cond;
}

void bart_cond_wait(bart_cond_t* cond, bart_lock_t* lock)
{
unsigned long counter;

#pragma omp atomic read
counter = cond->counter;

#ifdef _OPENMP
bart_unlock(lock);

unsigned long cnt2 = counter;

while (cnt2 == counter) {

#pragma omp atomic read
cnt2 = cond->counter;

#pragma omp taskyield
}

bart_lock(lock);
#endif
}

void bart_cond_notify_all(bart_cond_t* cond)
{
#pragma omp atomic
cond->counter++;
}

void bart_cond_destroy(bart_cond_t* cond)
{
xfree(cond);
}




19 changes: 15 additions & 4 deletions src/misc/lock.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@

typedef struct bart_lock bart_lock_t;

void bart_lock(bart_lock_t* lock);
void bart_unlock(bart_lock_t* lock);
void bart_lock_destroy(bart_lock_t* x);
bart_lock_t* bart_lock_create(void);
extern void bart_lock(bart_lock_t* lock);
extern void bart_unlock(bart_lock_t* lock);
extern void bart_lock_destroy(bart_lock_t* x);
extern bart_lock_t* bart_lock_create(void);


typedef struct bart_cond bart_cond_t;

extern void bart_cond_wait(bart_cond_t* cond, bart_lock_t* lock);
extern void bart_cond_notify_all(bart_cond_t* cond);
extern void bart_cond_destroy(bart_cond_t* x);
extern bart_cond_t* bart_cond_create(void);




0 comments on commit dbffab5

Please sign in to comment.