1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/* Ping Test the locking interface */
#ifdef __linux__
#include <pthread.h>
#include <errno.h>
#include <sys/types.h>
#include <netdb.h>
#include <libdlm.h>
static char our_lvb[DLM_LVB_LEN];
static struct dlm_lksb our_lksb = {.sb_lvbptr = our_lvb};
static int *lvb_int = (int *)our_lvb;
#define SUCCESS 0
#endif
#ifdef VMS
#include starlet
#include psldef
#include ssdef
#include errno
#include descrip
#include rsdmdef
#include lckdef
#include stdio
#include stdlib
#include string
struct lksb
{
int sb_status;
int sb_lkid;
char sb_lvb[16];
};
static struct lksb our_lksb;
static int *lvb_int = (int *)&our_lksb.sb_lvb;
#define LKM_NLMODE LCK$K_NLMODE
#define LKM_CRMODE LCK$K_CRMODE
#define LKM_CWMODE LCK$K_CWMODE
#define LKM_PRMODE LCK$K_PRMODE
#define LKM_PWMODE LCK$K_PWMODE
#define LKM_EXMODE LCK$K_EXMODE
#define EDEADLOCK SS$_DEADLOCK
/* This is wrong...VMS does not deliver ASTs
for $DEQ but we'll never get this code, honest.
*/
#define EUNLOCK 65535
#define SUCCESS SS$_NORMAL
#endif
static char *lockname="ping";
static int us = 1;
static int maxnode = 2;
static int cur_mode;
static int granted = 0;
static void compast_routine(void *arg);
static void blockast_routine(void *arg);
#ifdef __linux__
static int convert_lock(int mode)
{
int old_mode = cur_mode;
int status;
printf("pinglock: convert to %d starting\n", mode);
status = dlm_lock( mode,
&our_lksb,
LKF_VALBLK | LKF_CONVERT,
NULL,
0,
0,
compast_routine,
&our_lksb,
blockast_routine,
NULL);
if (status != 0)
{
perror("pinglock: convert failed");
}
else
{
cur_mode = mode;
printf("pinglock: convert to %d started\n", mode);
}
return status;
}
static void unlock()
{
int status;
status = dlm_unlock( our_lksb.sb_lkid,
0,
&our_lksb,
0);
if (status != 0)
perror("pinglock: unlock failed");
}
static void start_lock()
{
int status;
cur_mode = LKM_EXMODE;
*lvb_int = us-1;
printf("pinglock: starting\n");
status = dlm_lock( cur_mode,
&our_lksb,
LKF_VALBLK,
lockname,
strlen(lockname)+1, /* include trailing NUL for ease of following */
0, /* Parent */
compast_routine,
&our_lksb,
blockast_routine,
NULL);
if (status != 0)
perror("pinglock: lock failed");
}
#endif /* __linux__ */
#ifdef VMS
static void start_lock()
{
struct dsc$descriptor_s name_s;
int status;
char *name = "PINGLOCK";
cur_mode = LCK$K_EXMODE;
*lvb_int = us-1;
/* Make a descriptor of the name */
memset(&name_s, 0, sizeof(struct dsc$descriptor_s));
name_s.dsc$w_length = strlen(name);
name_s.dsc$a_pointer = name;
/* Lock it */
status = sys$enqw(0,
cur_mode,
&our_lksb,
0, /* flags */
&name_s,
0, /* parent */
compast_routine,
0, /* astp */
blockast_routine,
PSL$C_USER,
RSDM$K_PROCESS_RSDM_ID,
0);
if (status != SS$_NORMAL)
{
printf("lock enq failed : %s\n", strerror(EVMSERR, status));
sys$exit(status);
}
printf("Lock ID is %x\n", our_lksb.sb_lkid);
return;
}
static int convert_lock(int mode)
{
int status;
int old_mode = cur_mode;
printf("converting to %d\n", mode);
/* Lock it */
status = sys$enq(0,
mode,
&our_lksb,
LCK$M_CONVERT | LCK$M_VALBLK,
NULL,
0, /* parent */
compast_routine,
0, /* astp */
blockast_routine,
PSL$C_USER,
RSDM$K_PROCESS_RSDM_ID,
0);
if (status != SS$_NORMAL)
{
printf("convert enq failed : %s\n", strerror(EVMSERR, status));
sys$wake();
}
else
{
cur_mode = mode;
status = 0; /* simulate Unix retcodes */
}
return status;
}
static void unlock()
{
int status;
status = sys$deq(our_lksb.sb_lkid, NULL,0,0,0);
if (status != SS$_NORMAL)
{
printf("denq failed : %s\n", strerror(EVMSERR, status));
sys$wake();
}
}
#endif
static void compast_routine(void *arg)
{
#ifdef __linux__
if (our_lksb.sb_flags & DLM_SBF_VALNOTVALID)
#endif
#ifdef VMS
if (our_lksb.sb_status == SS$_VALNOTVALID)
#endif
{
printf(" valblk not valid. current value is %d\n", *lvb_int);
unlock();
exit(10);
}
if (our_lksb.sb_status == EDEADLOCK)
{
printf("pinglock: deadlocked\n");
unlock();
exit(11);
}
if (our_lksb.sb_status == EUNLOCK)
{
return;
}
if (our_lksb.sb_status == SUCCESS)
{
granted = 1;
switch (cur_mode)
{
case LKM_NLMODE:
printf("pinglock. compast, (valblk = %d) now at NL\n", *lvb_int);
if (convert_lock(LKM_CRMODE) == 0)
granted = 0;
break;
case LKM_CRMODE:
printf("pinglock. compast, (valblk = %d) now at CR\n", *lvb_int);
if (*lvb_int == us-1)
{
if (convert_lock(LKM_EXMODE) == 0)
granted = 0;
}
break;
case LKM_EXMODE:
printf("pinglock. compast. (valblk = %d) now at EX\n", *lvb_int);
if (*lvb_int == us-1)
{
(*lvb_int)++;
*lvb_int %= maxnode;
printf("pinglock. compast. incrementing valblk to %d\n", *lvb_int);
}
if (convert_lock(LKM_CRMODE) == 0)
granted = 0;
break;
}
}
else
{
printf("pinglock: lock failed (compast): %d\n", our_lksb.sb_status);
unlock();
}
}
static void blockast_routine(void *arg)
{
printf("pinglock. blkast, granted = %d\n", granted);
if (!granted)
{
return;
}
printf("pinglock. blkast, demoting lock to NL\n");
if (convert_lock(LKM_NLMODE) == 0)
granted = 0;
}
#ifdef __linux__
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("usage: %s <maxnodes> <us>\n", argv[0]);
return 2;
}
maxnode = atoi(argv[1]);
us = atoi(argv[2]);
dlm_pthread_init();
start_lock();
while(1)
sleep(100000);
return 0;
}
#endif
#ifdef VMS
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("usage: %s <maxnodes> <us>\n", argv[0]);
return 2;
}
maxnode = atoi(argv[1]);
us = atoi(argv[2]);
start_lock();
while(1)
sys$hiber();
return SS$_NORMAL;
}
#endif
|