Last active
August 29, 2015 14:19
-
-
Save andyrudoff/4b13da00bce9e857150e to your computer and use it in GitHub Desktop.
libpmemobj tutorial examples
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
struct node { | |
struct node *nextp; | |
int data; | |
}; | |
struct root { | |
struct node *headp; | |
}; | |
void | |
addnode(struct root *rootp, int data) | |
{ | |
struct node *newnodep; | |
if ((newnodep = calloc(1, sizeof(struct node))) == NULL) | |
fatal("out of memory"); | |
newnodep->data = data; | |
newnodep->nextp = rootp->headp; | |
rootp->headp = newnodep; | |
} |
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
struct node { | |
struct node *nextp; | |
int data; | |
}; | |
struct root { | |
pthread_mutex_t listlock; /* protects entire list */ | |
struct node *headp; | |
}; | |
void | |
addnode(struct root *rootp, int data) | |
{ | |
struct node *newnodep; | |
if ((newnodep = calloc(1, sizeof(struct node))) == NULL) | |
fatal("out of memory"); | |
newnodep->data = data; | |
/* lock the critical section */ | |
pthread_mutex_lock(&rootp->listlock); | |
newnode->nextp = rootp->headp; | |
rootp->headp = newnodep; | |
pthread_mutex_unlock(&rootp->listlock); | |
} |
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
struct node { | |
PMEMoid nextoid; /* OID of next node */ | |
int data; | |
}; | |
struct root { | |
PMEMmutex listlock; /* protects entire list */ | |
PMEMoid headoid; /* OID of head node */ | |
}; | |
void | |
addnode(PMEMoid rootoid, int data) | |
{ | |
struct root *rootp = pmemobj_direct(rootoid); | |
pmemobj_tx_begin(Pop, NULL, | |
TX_LOCK_MUTEX, &rootp->listlock, TX_LOCK_NONE); | |
PMEMoid newnodeoid = pmemobj_tx_zalloc(sizeof(struct node), 0); | |
struct node *newnodep = pmemobj_direct(newnodeoid); | |
newnodep->data = data; | |
newnodep->nextoid = rootp->headoid; | |
pmemobj_tx_add(rootoid); | |
rootp->headoid = newnodeoid; | |
pmemobj_tx_commit(); | |
pmemobj_tx_end(); | |
} |
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
struct node { | |
PMEMoid nextoid; /* OID of next node */ | |
int data; | |
}; | |
struct root { | |
PMEMmutex listlock; /* protects entire list */ | |
PMEMoid headoid; /* OID of head node */ | |
}; | |
void | |
addnode(PMEMoid rootoid, int data) | |
{ | |
struct root *rootp = pmemobj_direct(rootoid); | |
if (pmemobj_tx_begin(Pop, NULL, | |
TX_LOCK_MUTEX, &rootp->listlock, TX_LOCK_NONE) < 0) { | |
perror("tx_begin"); | |
goto err; | |
} | |
PMEMoid newnodeoid = pmemobj_tx_zalloc(sizeof(struct node), 0); | |
if (OID_IS_NULL(newnodeoid)) { | |
perror("tx_zalloc"); | |
goto err; | |
} | |
struct node *newnodep = pmemobj_direct(newnodeoid); | |
newnodep->data = data; | |
newnodep->nextoid = rootp->headoid; | |
if (pmemobj_tx_add(rootoid) < 0) { | |
perror("tx_add"); | |
goto err; | |
} | |
rootp->headoid = newnodeoid; | |
if (pmemobj_tx_commit() < 0) { | |
perror("tx_commit"); | |
goto err; /* good coding style, eh? */ | |
} | |
err: | |
pmemobj_tx_end(); | |
} |
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
struct node { | |
PMEMoid nextoid; /* OID of next node */ | |
int data; | |
}; | |
struct root { | |
PMEMmutex listlock; /* protects entire list */ | |
PMEMoid headoid; /* OID of head node */ | |
}; | |
void | |
addnode(PMEMoid rootoid, int data) | |
{ | |
struct root *rootp = pmemobj_direct(rootoid); | |
jmp_buf env; | |
if (setjmp(env)) { | |
perror("transaction aborted"); | |
pmemobj_tx_end(); | |
return; | |
} | |
pmemobj_tx_begin(Pop, env, TX_LOCK_MUTEX, | |
&rootp->listlock, TX_LOCK_NONE); | |
PMEMoid newnodeoid = pmemobj_tx_zalloc(sizeof(struct node), 0); | |
struct node *newnodep = pmemobj_direct(newnodeoid); | |
newnodep->data = data; | |
newnodep->nextoid = rootp->headoid; | |
pmemobj_tx_add(rootoid); | |
rootp->headoid = newnodeoid; | |
pmemobj_tx_commit(); | |
pmemobj_tx_end(); | |
} |
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
struct node { | |
PMEMoid nextoid; /* OID of next node */ | |
int data; | |
}; | |
struct root { | |
PMEMmutex listlock; /* protects entire list */ | |
PMEMoid headoid; /* OID of head node */ | |
}; | |
void | |
addnode(PMEMoid rootoid, int data) | |
{ | |
struct root *rootp = pmemobj_direct(rootoid); | |
TX_BEGIN_LOCK(Pop, TX_LOCK_MUTEX, &rootp->listlock) { | |
PMEMoid newnodeoid = pmemobj_tx_zalloc(sizeof(struct node), 0); | |
struct node *newnodep = pmemobj_direct(newnodeoid); | |
newnodep->data = data; | |
newnodep->nextoid = rootp->headoid; | |
pmemobj_tx_add(rootoid); | |
rootp->headoid = newnodeoid; | |
} TX_END | |
} |
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
struct node { | |
PMEMoid nextoid; /* OID of next node */ | |
int data; | |
}; | |
struct root { | |
PMEMmutex listlock; /* protects entire list */ | |
PMEMoid headoid; /* OID of head node */ | |
}; | |
void | |
addnode(PMEMoid rootoid, int data) | |
{ | |
struct root *rootp = pmemobj_direct(rootoid); | |
TX_BEGIN_LOCK(Pop, TX_LOCK_MUTEX, &rootp->listlock) { | |
PMEMoid newnodeoid = pmemobj_tx_zalloc(sizeof(struct node), 0); | |
struct node *newnodep = pmemobj_direct(newnodeoid); | |
newnodep->data = data; | |
newnodep->nextoid = rootp->headoid; | |
pmemobj_tx_add(rootoid); | |
rootp->headoid = newnodeoid; | |
} TX_ONABORT { | |
perror("transaction failed"); | |
/* ... */ | |
} TX_END | |
} |
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
struct node { | |
OID_TYPE(struct node) nextoid; /* OID of next node */ | |
int data; | |
}; | |
struct root { | |
PMEMmutex listlock; /* protects entire list */ | |
OID_TYPE(struct node) headoid; /* OID of head node */ | |
}; | |
void | |
addnode(OID_TYPE(struct root) rootoid, int data) | |
{ | |
TX_BEGIN_LOCK(Pop, TX_LOCK_MUTEX, &D_RW(rootoid)->listlock) { | |
OID_TYPE(struct node) newnodeoid = TX_ZALLOC(struct node, 0); | |
D_RW(newnodeoid)->data = data; | |
D_RW(newnodeoid)->nextoid = D_RO(rootoid)->headoid; | |
TX_ADD(rootoid); | |
D_RW(rootoid)->headoid = newnodeoid; | |
} TX_ONABORT { | |
perror("transaction failed"); | |
/* ... */ | |
} TX_END | |
} |
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
Here are a sequence of examples used to introduce libpmemobj. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment