root/trunk/dep/src/zthread/win32/AtomicFastLock.h @ 2

Revision 2, 3.4 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (c) 2005, Eric Crahen
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is furnished
9 * to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22
23#ifndef __ZTFASTLOCK_H__
24#define __ZTFASTLOCK_H__
25
26#include "zthread/NonCopyable.h"
27#include <windows.h>
28#include <assert.h>
29
30namespace ZThread {
31
32
33/**
34 * @class FastLock
35 *
36 * @author Eric Crahen <http://www.code-foo.com>
37 * @date <2003-07-16T23:32:20-0400>
38 * @version 2.1.6
39 *
40 * This is the smallest and fastest synchronization object in the library.
41 * It is an implementation of fast mutex, an all or nothing exclusive
42 * lock. It should be used only where you need speed and are willing
43 * to sacrifice all the state & safety checking provided by the framework
44 * for speed.
45 *
46 * The current Platform SDK defines:
47 *
48 *   LONG InterlockedExchange(LPLONG, LONG)
49 *   LONG InterlockedCompareExchange(LPLONG, LONG, LONG, LONG)
50 *
51 * If your compiler complains about LPLONG not being implicitly casted to
52 * a PVOID, then you should get the SDK update from microsoft or use the
53 * WIN9X implementation of this class.
54 *
55 * ----
56 * Because Windows 95 and earlier can run on processors prior to the 486, they
57 * don't all support the CMPXCHG function, and so Windows 95 an earlier dont support
58 * InterlockedCompareExchange. For this, you should use the win9x implementation
59 * of this class
60 */ 
61class FastLock : private NonCopyable {
62
63#pragma pack(push, 8)
64  LONG volatile _lock;
65#pragma pack(pop)
66 
67  public:
68 
69  /**
70   * Create a new FastLock
71   */
72  inline FastLock() : _lock(0) { }
73
74 
75  /**
76   * Destroy FastLock
77   */
78  inline ~FastLock() { assert(_lock == 0); }
79
80  /**
81   * Lock the fast Lock, no error check.
82   *
83   * @exception None
84   */ 
85  inline void acquire() {
86
87    while (::InterlockedCompareExchange(const_cast<LPLONG>(&_lock), 1, 0) != 0)
88      ::Sleep(0);
89
90  }
91
92 
93  /**
94   * Release the fast Lock, no error check.
95   *
96   * @exception None
97   */ 
98  inline void release() {
99
100    ::InterlockedExchange(const_cast<LPLONG>(&_lock), (LONG)0);
101
102  }
103   
104  /**
105   * Try to acquire an exclusive lock. No safety or state checks are performed.
106   * This function returns immediately regardless of the value of the timeout
107   *
108   * @param timeout Unused
109   * @return bool
110   * @exception Synchronization_Exception - not thrown
111   */
112 inline bool tryAcquire(unsigned long timeout=0) {
113 
114   return ::InterlockedCompareExchange(const_cast<LPLONG>(&_lock), 1, 0) == 0;
115
116 }
117 
118}; /* FastLock */
119
120
121};
122#endif
Note: See TracBrowser for help on using the browser.