root/trunk/dep/src/zthread/posix/FastLock.h @ 2

Revision 2, 3.2 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/Exceptions.h"
27#include "zthread/NonCopyable.h"
28#include <pthread.h>
29#include <assert.h>
30
31namespace ZThread {
32
33/**
34 * @class FastLock
35 *
36 * @author Eric Crahen <http://www.code-foo.com>
37 * @date <2003-07-16T23:28:07-0400>
38 * @version 2.2.8
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 */ 
46class FastLock : private NonCopyable {
47   
48  pthread_mutex_t _mtx;
49
50 public:
51 
52  /**
53   * Create a new FastLock. No safety or state checks are performed.
54   *
55   * @exception Initialization_Exception - not thrown
56   */
57  inline FastLock() {
58
59    if(pthread_mutex_init(&_mtx, 0) != 0)
60      throw Initialization_Exception();
61
62  }
63 
64  /**
65   * Destroy a FastLock. No safety or state checks are performed.
66   */
67  inline ~FastLock() {
68
69    if(pthread_mutex_destroy(&_mtx) != 0) {
70      assert(0);
71    }
72
73  }
74 
75  /**
76   * Acquire an exclusive lock. No safety or state checks are performed.
77   *
78   * @exception Synchronization_Exception - not thrown
79   */
80  inline void acquire() {
81   
82    if(pthread_mutex_lock(&_mtx) != 0)
83      throw Synchronization_Exception();
84
85  }
86
87  /**
88   * Try to acquire an exclusive lock. No safety or state checks are performed.
89   * This function returns immediately regardless of the value of the timeout
90   *
91   * @param timeout Unused
92   * @return bool
93   * @exception Synchronization_Exception - not thrown
94   */
95  inline bool tryAcquire(unsigned long /*timeout*/=0) {
96
97    return (pthread_mutex_trylock(&_mtx) == 0);
98
99  }
100 
101  /**
102   * Release an exclusive lock. No safety or state checks are performed.
103   * The caller should have already acquired the lock, and release it
104   * only once.
105   *
106   * @exception Synchronization_Exception - not thrown
107   */
108  inline void release() {
109   
110    if(pthread_mutex_unlock(&_mtx) != 0)
111      throw Synchronization_Exception();
112
113  }
114 
115 
116}; /* FastLock */
117
118
119};
120
121#endif
Note: See TracBrowser for help on using the browser.