Created
January 23, 2015 10:40
-
-
Save hotzenklotz/6f93e974cd2e69a3385a to your computer and use it in GitHub Desktop.
ConcurrencyKit RW-Cohort-Lock
This file contains hidden or 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
#include <ck_pr.h> | |
#include <ck_spinlock.h> | |
#include <ck_cohort.h> | |
#include <ck_rwcohort.h> | |
/* Create cohort methods with signatures that match the required signature */ | |
static void ck_spinlock_lock_with_context(ck_spinlock_t *lock, void *context){ | |
(void)context; | |
ck_spinlock_lock(lock); | |
return; | |
} | |
static void ck_spinlock_unlock_with_context(ck_spinlock_t *lock, void *context){ | |
(void)context; | |
ck_spinlock_unlock(lock); | |
return; | |
} | |
static bool ck_spinlock_locked_with_context(ck_spinlock_t *lock, void *context){ | |
(void)context; | |
return ck_spinlock_locked(lock); | |
} | |
/* | |
* define a cohort type named "test_cohort" that will use | |
* the above methods for both its global and local locks | |
*/ | |
CK_COHORT_PROTOTYPE(test_cohort, | |
ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context, | |
ck_spinlock_lock_with_context, ck_spinlock_unlock_with_context, ck_spinlock_locked_with_context) | |
/* define a reader-writer type using the same cohort type */ | |
CK_RWCOHORT_WP_PROTOTYPE(test_cohort) | |
static ck_spinlock_t global_lock = CK_SPINLOCK_INITIALIZER; | |
static CK_COHORT_INSTANCE(test_cohort) *cohorts; | |
static CK_RWCOHORT_WP_INSTANCE(test_cohort) rw_cohort_lock = CK_RWCOHORT_WP_INITIALIZER; | |
/* main */ | |
int main(int argc, char *argv[]) | |
{ | |
/* allocate 2 cohorts of the defined type */ | |
CK_COHORT_INSTANCE(test_cohort) *cohorts = calloc(NUMA_NODES, sizeof(CK_COHORT_INSTANCE(test_cohort))); | |
ck_spinlock_t *local_locks = calloc(NUMA_NODES, sizeof(ck_spinlock_t)); | |
/* initialize each of the cohorts before using them */ | |
for (int i = 0 ; i < NUMA_NODES ; ++i) { | |
CK_COHORT_INIT(test_cohort, cohorts + i, &global_lock, local_locks + i, | |
CK_COHORT_DEFAULT_LOCAL_PASS_LIMIT); | |
} | |
/* initialize RW Cohort Lock */ | |
int waitTime = 100; | |
CK_RWCOHORT_WP_INIT(test_cohort, &rw_cohort_lock, waitTime); | |
/* Spawn some Threads with critical sections | |
pthread_create(someFuntion, rw_cohort_lock, cohort) ... | |
*/ | |
} | |
void someFunction(void *arguments) { | |
CK_COHORT_INSTANCE(test_cohort) *cohort = arguments.cohort | |
CK_RWCOHORT_WP_INSTANCE(test_cohort) *rw_cohort_lock = arguments.rw_cohort_lock | |
CK_RWCOHORT_WP_READ_LOCK(test_cohort, _cohort_lock, cohort, NULL, NULL); | |
// critical section | |
CK__cohort_lockCOHORT_WP_READ_UNLOCK(test_cohort, _cohort_lock, cohort, NULL, NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment