Improve spinlock selftest to make it able to detect misdeclaration of
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 30 Aug 2004 23:47:20 +0000 (23:47 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 30 Aug 2004 23:47:20 +0000 (23:47 +0000)
the slock_t datatype (ie, declared type smaller than what the hardware
TAS instruction needs).

src/backend/storage/lmgr/s_lock.c

index b81a7e67e58fb436e0846467eb88fe8dccae222f..20f40bef9414c0a47a81afe14c0e402617d70ce2 100644 (file)
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.31 2004/08/30 02:54:38 momjian Exp $
+ *   $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.32 2004/08/30 23:47:20 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -249,40 +249,73 @@ tas_dummy()                       /* really means: extern int tas(slock_t
  * test program for verifying a port's spinlock support.
  */
 
-volatile slock_t test_lock;
+struct test_lock_struct
+{
+   char        pad1;
+   slock_t     lock;
+   char        pad2;
+};
+
+volatile struct test_lock_struct test_lock;
 
 int
 main()
 {
    srandom((unsigned int) time(NULL));
 
-   S_INIT_LOCK(&test_lock);
+   test_lock.pad1 = test_lock.pad2 = 0x44;
+
+   S_INIT_LOCK(&test_lock.lock);
+
+   if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
+   {
+       printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+       return 1;
+   }
 
-   if (!S_LOCK_FREE(&test_lock))
+   if (!S_LOCK_FREE(&test_lock.lock))
    {
        printf("S_LOCK_TEST: failed, lock not initialized\n");
        return 1;
    }
 
-   S_LOCK(&test_lock);
+   S_LOCK(&test_lock.lock);
 
-   if (S_LOCK_FREE(&test_lock))
+   if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
+   {
+       printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+       return 1;
+   }
+
+   if (S_LOCK_FREE(&test_lock.lock))
    {
        printf("S_LOCK_TEST: failed, lock not locked\n");
        return 1;
    }
 
-   S_UNLOCK(&test_lock);
+   S_UNLOCK(&test_lock.lock);
+
+   if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
+   {
+       printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+       return 1;
+   }
 
-   if (!S_LOCK_FREE(&test_lock))
+   if (!S_LOCK_FREE(&test_lock.lock))
    {
        printf("S_LOCK_TEST: failed, lock not unlocked\n");
        return 1;
    }
 
-   S_LOCK(&test_lock);
+   S_LOCK(&test_lock.lock);
+
+   if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
+   {
+       printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+       return 1;
+   }
 
-   if (S_LOCK_FREE(&test_lock))
+   if (S_LOCK_FREE(&test_lock.lock))
    {
        printf("S_LOCK_TEST: failed, lock not re-locked\n");
        return 1;
@@ -293,7 +326,7 @@ main()
    printf("             if S_LOCK() and TAS() are working.\n");
    fflush(stdout);
 
-   s_lock(&test_lock, __FILE__, __LINE__);
+   s_lock(&test_lock.lock, __FILE__, __LINE__);
 
    printf("S_LOCK_TEST: failed, lock not locked\n");
    return 1;