I don't see how one can implement blocking locking using a single bit.
Here you go:
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <process.h>
typedef struct {
void *protected;
int loops;
} args;
void lock( void *protected ) {
while( _interlockedbittestandset64( (__int64*)protected, 0 ) ) {
Sleep( 1 );
}
}
void unlock( void *protected ) {
_interlockedbittestandreset64( (__int64*)protected, 0 );
}
void worker( void *arg ) {
args *a = (args*)arg;
int i = 0;
for( i=0; i < a->loops; ++i ) {
lock( a->protected );
*( (int*)a->protected ) += 2;
unlock( a->protected );
}
return;
}
void main( int argc, char **argv ) {
int i = 0, nThreads = 4;
clock_t start, finish;
double elapsed;
uintptr_t threads[32];
int shared = 0;
args a = { (void *)&shared, 1000000 };;
if( argc > 1 ) nThreads = atol( argv[1] );
if( argc > 2 ) a.loops = atol( argv[2] );
printf( "threads:%d loops:%d\n", nThreads, a.loops );
start = clock();
for( i=0; i < nThreads; ++i )
threads[ i ] = _beginthread( &worker, 0, &a );
WaitForMultipleObjects( nThreads, (HANDLE*)&threads, 1, INFINITE )
+;
finish = clock();
elapsed = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "count: %lu time:%.6f\n", shared, elapsed );
}
And a run with 32 threads all contending to add 2 to a shared integer 1 million times each: C:\test\lockfree>bitlock 32 1000000
threads:32 loops:1000000
count: 64000000 time:1.332000
And implemented using the simplest primitive possible -- one that will be available in some form on any modern processor.
The other "overhead" you show is so locking can block, so Perl can have a single interface over multiple implementations of blocking locking on multiple operating systems, so Linux can implement a portable blocking locking interface over the current choice of kernel implementation, so Linux can detect deadlock loops, so Linux users can select different types of "wake order" behavior, so somebody can adjust "spin" to reduce context switches in certain scenarios, etc
And therein lies the rub. Perl implements it own recursive locking in terms of pthreads 0.1 primitives. But those "speced" pthreads primitives have long since been superseded on every modern *nix system by vastly more efficient effective and flexible primitives -- eg. futexes -- which already have recursive capabilities.
And then on other platforms -- ie. windows -- the pthreads 0.1 primitives are clumsily emulated using oldest, least effective OS primitives.
Everyone, everywhere is getting big, slow, clumsy emulations of a defunct standard instead of being able to use the modern, efficient, effective mechanisms that have evolved since the pthreads api was frozen in stone.
And all those "so Linux users can" and "so somebody can" are pie-in-the sky, what-ifs and maybes that can never happen for perl users anywhere. Typical, lowest common denominator stuff.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
|