~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-security

« back to all changes in this revision

Viewing changes to src/backend/storage/lmgr/spin.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * spin.c
 
4
 *         Hardware-independent implementation of spinlocks.
 
5
 *
 
6
 *
 
7
 * For machines that have test-and-set (TAS) instructions, s_lock.h/.c
 
8
 * define the spinlock implementation.  This file contains only a stub
 
9
 * implementation for spinlocks using PGSemaphores.  Unless semaphores
 
10
 * are implemented in a way that doesn't involve a kernel call, this
 
11
 * is too slow to be very useful :-(
 
12
 *
 
13
 *
 
14
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
 
15
 * Portions Copyright (c) 1994, Regents of the University of California
 
16
 *
 
17
 *
 
18
 * IDENTIFICATION
 
19
 *        $PostgreSQL$
 
20
 *
 
21
 *-------------------------------------------------------------------------
 
22
 */
 
23
#include "postgres.h"
 
24
 
 
25
#include "miscadmin.h"
 
26
#include "storage/lwlock.h"
 
27
#include "storage/spin.h"
 
28
 
 
29
 
 
30
#ifdef HAVE_SPINLOCKS
 
31
 
 
32
/*
 
33
 * Report number of semaphores needed to support spinlocks.
 
34
 */
 
35
int
 
36
SpinlockSemas(void)
 
37
{
 
38
        return 0;
 
39
}
 
40
#else                                                   /* !HAVE_SPINLOCKS */
 
41
 
 
42
/*
 
43
 * No TAS, so spinlocks are implemented as PGSemaphores.
 
44
 */
 
45
 
 
46
 
 
47
/*
 
48
 * Report number of semaphores needed to support spinlocks.
 
49
 */
 
50
int
 
51
SpinlockSemas(void)
 
52
{
 
53
        /*
 
54
         * It would be cleaner to distribute this logic into the affected modules,
 
55
         * similar to the way shmem space estimation is handled.
 
56
         *
 
57
         * For now, though, we just need a few spinlocks (10 should be plenty)
 
58
         * plus one for each LWLock and one for each buffer header.
 
59
         */
 
60
        return NumLWLocks() + NBuffers + 10;
 
61
}
 
62
 
 
63
/*
 
64
 * s_lock.h hardware-spinlock emulation
 
65
 */
 
66
 
 
67
void
 
68
s_init_lock_sema(volatile slock_t *lock)
 
69
{
 
70
        PGSemaphoreCreate((PGSemaphore) lock);
 
71
}
 
72
 
 
73
void
 
74
s_unlock_sema(volatile slock_t *lock)
 
75
{
 
76
        PGSemaphoreUnlock((PGSemaphore) lock);
 
77
}
 
78
 
 
79
bool
 
80
s_lock_free_sema(volatile slock_t *lock)
 
81
{
 
82
        /* We don't currently use S_LOCK_FREE anyway */
 
83
        elog(ERROR, "spin.c does not support S_LOCK_FREE()");
 
84
        return false;
 
85
}
 
86
 
 
87
int
 
88
tas_sema(volatile slock_t *lock)
 
89
{
 
90
        /* Note that TAS macros return 0 if *success* */
 
91
        return !PGSemaphoreTryLock((PGSemaphore) lock);
 
92
}
 
93
 
 
94
#endif   /* !HAVE_SPINLOCKS */