Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add spinlocks, add tryjoin, add libev submodule. sleeping is working …
…using the libev backend
- Loading branch information
Brian Watling
committed
Dec 3, 2011
1 parent
29ace63
commit 8fd903a
Showing
43 changed files
with
55,031 additions
and
26 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef _FIBER_SPINLOCK_H_ | ||
#define _FIBER_SPINLOCK_H_ | ||
|
||
/* | ||
Author: Brian Watling | ||
Email: brianwatling@hotmail.com | ||
Website: https://github.com/brianwatling | ||
Description: A spin lock for fibers. This is meant to be used in places | ||
where a fiber does not want to or cannot perform a context | ||
switch. A fiber will spin on this lock without yielding to | ||
other fibers, but will yield to other system threads after | ||
spinning for a while. | ||
*/ | ||
|
||
#include <stddef.h> | ||
|
||
typedef struct fiber_spinlock | ||
{ | ||
volatile int state; | ||
} fiber_spinlock_t; | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
extern int fiber_spinlock_init(fiber_spinlock_t* spinlock); | ||
|
||
extern int fiber_spinlock_destroy(fiber_spinlock_t* spinlock); | ||
|
||
extern int fiber_spinlock_lock(fiber_spinlock_t* spinlock); | ||
|
||
extern int fiber_spinlock_trylock(fiber_spinlock_t* spinlock, size_t tries); | ||
|
||
extern int fiber_spinlock_unlock(fiber_spinlock_t* spinlock); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "fiber_spinlock.h" | ||
#include "fiber.h" | ||
#include "sched.h" | ||
|
||
int fiber_spinlock_init(fiber_spinlock_t* spinlock) | ||
{ | ||
assert(spinlock); | ||
spinlock->state = 0; | ||
return FIBER_SUCCESS; | ||
} | ||
|
||
int fiber_spinlock_destroy(fiber_spinlock_t* spinlock) | ||
{ | ||
assert(spinlock); | ||
return FIBER_SUCCESS; | ||
} | ||
|
||
int fiber_spinlock_lock(fiber_spinlock_t* spinlock) | ||
{ | ||
assert(spinlock); | ||
unsigned int counter = 0; | ||
while(!__sync_bool_compare_and_swap(&spinlock->state, 0, 1)) { | ||
while(spinlock->state) { | ||
++counter; | ||
if(counter > 128) { | ||
sched_yield(); | ||
counter = 0; | ||
} | ||
} | ||
} | ||
load_load_barrier(); | ||
return FIBER_SUCCESS; | ||
} | ||
|
||
int fiber_spinlock_trylock(fiber_spinlock_t* spinlock, size_t tries) | ||
{ | ||
assert(spinlock); | ||
while(!__sync_bool_compare_and_swap(&spinlock->state, 0, 1)) { | ||
if(tries == 0) { | ||
return FIBER_ERROR; | ||
} | ||
--tries; | ||
while(spinlock->state && tries > 0) { | ||
--tries; | ||
} | ||
} | ||
load_load_barrier(); | ||
return FIBER_SUCCESS; | ||
} | ||
|
||
int fiber_spinlock_unlock(fiber_spinlock_t* spinlock) | ||
{ | ||
assert(spinlock); | ||
store_load_barrier();//flush this fiber's writes | ||
spinlock->state = 0; | ||
} | ||
|
Oops, something went wrong.