6
* $Id: nxtval.shm.c,v 1.9 2005-02-21 21:51:40 manoj Exp $
14
long nxtval_counter=0;
15
long *nxtval_shmem = &nxtval_counter;
18
#define INCR 1 /* increment for NXTVAL */
19
#define BUSY -1L /* indicates somebody else updating counter*/
22
#if (defined(__i386__) || defined(__x86_64__)) && defined(__GNUC__)
23
# define TESTANDSET testandset
25
static inline int testandset(int *spinlock)
28
__asm__ __volatile__("xchgl %0, %1"
29
: "=r"(ret), "=m"(*spinlock)
30
: "0"(1), "m"(*spinlock));
35
#elif defined(__APPLE__) && defined(__GNUC__)
36
# define TESTANDSET(x) ( krspin_lock((long int *)(x)))
37
static int krspin_lock(long int *p)
61
# define LOCK if(nproc>1)acquire_spinlock((int*)(nxtval_shmem+1))
62
# define UNLOCK if(nproc>1)release_spinlock((int*)(nxtval_shmem+1))
64
static void acquire_spinlock(int *mutex)
66
int loop=0, maxloop =10;
67
while (TESTANDSET(mutex)){
69
if(loop==maxloop){ usleep(1); loop=0; }
73
static void release_spinlock(int *mutex)
78
#endif /* TESTANDSET */
82
# define LOCK if(nproc>1)Error("nxtval: sequential version with silly mproc ", (long) *mproc);
88
* Get next value of shared counter.
90
* mproc > 0 ... returns requested value
91
* mproc < 0 ... server blocks until abs(mproc) processes are queued
93
* mproc = 0 ... indicates to server that I am about to terminate
95
long NXTVAL_(long *mproc)
99
long sync_type= INTERNAL_SYNC_TYPE;
100
long nproc= NNODES_();
104
(void) printf("%2ld: nxtval: mproc=%ld\n",(long)NODEID_(),(long)*mproc);
105
(void) fflush(stdout);
110
/* reset the counter value to zero */
111
if( NODEID_() == server) *nxtval_shmem = 0;
116
local = *nxtval_shmem;
117
*nxtval_shmem += INCR;