mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-06 12:46:35 +02:00
Building out ck_array
This commit is contained in:
parent
c45c064663
commit
bd11b9c7fa
2 changed files with 84 additions and 8 deletions
|
@ -10,13 +10,85 @@
|
||||||
|
|
||||||
#include "cyclone/types.h"
|
#include "cyclone/types.h"
|
||||||
#include "cyclone/runtime.h"
|
#include "cyclone/runtime.h"
|
||||||
#include "ck_pr.h"
|
#include "ck-polyfill.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static pthread_mutex_t glock;
|
||||||
|
|
||||||
void ck_polyfill_init()
|
void ck_polyfill_init()
|
||||||
{
|
{
|
||||||
// TODO: init any global locks, etc
|
|
||||||
// will need to call this as soon as possible, perhaps from main()
|
// will need to call this as soon as possible, perhaps from main()
|
||||||
|
if (pthread_mutex_init(&(glock), NULL) != 0) {
|
||||||
|
fprintf(stderr, "Unable to initialize global ck mutex\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CK Array section
|
||||||
|
bool
|
||||||
|
ck_array_init(ck_array_t *array, unsigned int mode,
|
||||||
|
struct ck_malloc *allocator, unsigned int initial_length)
|
||||||
|
{
|
||||||
|
array = malloc(sizeof(ck_array_t));
|
||||||
|
array->hs = hashset_create();
|
||||||
|
if (pthread_mutex_init(&(array->lock), NULL) != 0) {
|
||||||
|
fprintf(stderr, "Unable to initialize ck array mutex\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// DESCRIPTION
|
||||||
|
// The ck_array_put_unique(3) function will attempt to insert the value of
|
||||||
|
// pointer into the array pointed to by array. This function may incur
|
||||||
|
// additional memory allocations if not enough memory has been allocated in
|
||||||
|
// the array for a new entry. The operation is also free to apply the opera-
|
||||||
|
// tion immediately if there is an opportunity for elimination with a pend-
|
||||||
|
// ing (uncommitted) remove operation. The function will not make any modi-
|
||||||
|
// fications if the pointer already exists in the array.
|
||||||
|
//
|
||||||
|
// RETURN VALUES
|
||||||
|
// This function returns 1 if the pointer already exists in the array. It
|
||||||
|
// returns 0 if the put operation succeeded. It returns -1 on error due to
|
||||||
|
// internal memory allocation failures.
|
||||||
|
int
|
||||||
|
ck_array_put_unique(ck_array_t *array, void *pointer)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&(array->lock));
|
||||||
|
hashset_add(array->hs, pointer);
|
||||||
|
pthread_mutex_unlock(&(array->lock));
|
||||||
|
}
|
||||||
|
|
||||||
|
// DESCRIPTION
|
||||||
|
// The ck_array_remove(3) function will attempt to remove the value of
|
||||||
|
// pointer into the array pointed to by array. The operation is also free to
|
||||||
|
// apply the operation immediately if there is an opportunity for elimina-
|
||||||
|
// tion with a pending (uncommitted) put operation. If no elimination was
|
||||||
|
// possible, the function may require to allocate more memory.
|
||||||
|
//
|
||||||
|
// RETURN VALUES
|
||||||
|
// This function returns true if the remove operation succeeded. It will
|
||||||
|
// return false otherwise due to internal allocation failures or because the
|
||||||
|
// value did not exist.
|
||||||
|
bool
|
||||||
|
ck_array_remove(ck_array_t *array, void *pointer){
|
||||||
|
pthread_mutex_lock(&(array->lock));
|
||||||
|
hashset_remove(array->hs, pointer);
|
||||||
|
pthread_mutex_unlock(&(array->lock));
|
||||||
|
}
|
||||||
|
|
||||||
|
// DESCRIPTION
|
||||||
|
// The ck_array_commit(3) function will commit any pending put or remove
|
||||||
|
// operations associated with the array. The function may end up requesting
|
||||||
|
// the safe reclamation of memory actively being iterated upon by other
|
||||||
|
// threads.
|
||||||
|
//
|
||||||
|
// RETURN VALUES
|
||||||
|
// This function returns true if the commit operation succeeded. It will
|
||||||
|
// return false otherwise, and pending operations will not be applied.
|
||||||
|
bool ck_array_commit(ck_array_t *array) {
|
||||||
|
// Nothing to do in this polyfill
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -94,12 +94,16 @@ ck_array_commit(ck_array_t *array);
|
||||||
|
|
||||||
// Can we safely lock the array, make a copy, and interate over that????
|
// Can we safely lock the array, make a copy, and interate over that????
|
||||||
#define CK_ARRAY_FOREACH(a, i, b) \
|
#define CK_ARRAY_FOREACH(a, i, b) \
|
||||||
(i)->snapshot = ck_pr_load_ptr(&(a)->active); \
|
TODO:
|
||||||
ck_pr_fence_load(); \
|
pthread_mutex_lock(&(array->lock));
|
||||||
for (unsigned int _ck_i = 0; \
|
hashset_remove(array->hs, pointer);
|
||||||
_ck_i < (a)->active->n_committed && \
|
pthread_mutex_unlock(&(array->lock));
|
||||||
((*b) = (a)->active->values[_ck_i], 1); \
|
// (i)->snapshot = ck_pr_load_ptr(&(a)->active);
|
||||||
_ck_i++)
|
// ck_pr_fence_load();
|
||||||
|
// for (unsigned int _ck_i = 0;
|
||||||
|
// _ck_i < (a)->active->n_committed &&
|
||||||
|
// ((*b) = (a)->active->values[_ck_i], 1);
|
||||||
|
// _ck_i++)
|
||||||
|
|
||||||
// CAS section
|
// CAS section
|
||||||
bool
|
bool
|
||||||
|
|
Loading…
Add table
Reference in a new issue