mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-03-14 10:50:05 +01:00
[SPCA] Finish multicore
This commit is contained in:
28
semester3/spca/code-examples/03_hw/02_mcs.c
Normal file
28
semester3/spca/code-examples/03_hw/02_mcs.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
struct qnode {
|
||||
struct qnode *next;
|
||||
int locked;
|
||||
};
|
||||
typedef struct qnode *lock_t;
|
||||
|
||||
void acquire( lock_t *lock, struct qnode *local ) {
|
||||
local->next = NULL;
|
||||
struct qnode *prev = XCHG( lock, local );
|
||||
if ( prev ) { // queue was non-empty
|
||||
local->locked = 1;
|
||||
prev->next = local;
|
||||
while ( local->locked )
|
||||
; // spin
|
||||
}
|
||||
}
|
||||
|
||||
void release( lock_t *lock, struct qnode *local ) {
|
||||
if ( local->next == NULL ) {
|
||||
if ( CAS( lock, local, NULL ) )
|
||||
return;
|
||||
while ( local->next == NULL )
|
||||
; // spin
|
||||
}
|
||||
local->next->locked = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user