我正在尝试使用TL2在Java中实现STM api。我已经编写了诸如ReadTx和WriteTx的较低级别的函数,但是无法使用诸如atomic的关键字转换为较高级别的结构。例如,我当前的实现适用于第一段代码,但由于麻烦,我想转换为第二段代码。
void PushLeft(DQueue *q, int val) {
QNode *qn = malloc(sizeof(QNode));
qn->val = val;
do {
StartTx();
QNode *leftSentinel = ReadTx(&(q->left));
QNode *oldLeftNode = ReadTx(&(leftSentinel->right));
WriteTx(&(qn->left), leftSentinel);
WriteTx(&(qn->right), oldLeftNode);
WriteTx(&(leftSentinel->right), qn);
WriteTx(&(oldLeftNode->left), qn);
} while (!CommitTx());
}
void PushLeft(DQueue *q, int val) {
QNode *qn = malloc(sizeof(QNode));
qn->val = val;
atomic {
QNode *leftSentinel = q->left;
QNode *oldLeftNode = leftSentinel->right;
qn->left = leftSentinel;
qn->right = oldLeftNode;
leftSentinel->right = qn;
oldLeftNode->left = qn;
} }
如何在不更改语言语法的情况下做到这一点?